abc

Share This blog with your friends, so that we can improve more & more . our aim is to easy & simple way of learning.

2/13/2022

send DATA from one activity to other activity using simple technique in android.


 in this we will discuss of how to send data from one activity, using simple xml and java.

  step 1)  go to activity-main.xml and add below texbox and button , when user click on button textbox data pass to second activity. you can pass any data it may Firebase, MySQL , this is just sample to elaborate you how is this working.

 

 <EditText

      android:id = "@+id/edit_text"

      android:layout_width = "match_parent"

      android:layout_height = "wrap_content"

      android:layout_gravity = "center"

      android:hint = "Enter something to pass"

      android:inputType = "text" />

   <Button

      android:id = "@+id/button"

      android:layout_width = "wrap_content"

      android:layout_height = "wrap_content"

      android:layout_gravity = "center"

      android:layout_marginTop = "16dp"

      android:text = "Next" />

  

step 2) in oncreate method of mainactivity.java, add onclick method 

 final EditText editText = findViewById(R.id.edit_text);

      Button button = findViewById(R.id.button);

      button.setOnClickListener(new View.OnClickListener() {

         @Override

         public void onClick(View v) {

            String value = editText.getText().toString().trim();

            Intent intent = new Intent(MainActivity.this, SecondActivity.class);

            intent.putString(“value”,value);

            startActivity(intent);

         }

      });


step 3) view-senddata.xml , add any layout as you wish and paste below code,


 <TextView

      android:id = "@+id/text_view"

      android:layout_width = "match_parent"

      android:layout_height = "wrap_content"

      android:layout_gravity = "center" />


and java file of this view-senddata.xml add below code to receive content from intent.


Bundle bundle = getIntent().getExtras();

      if (bundle ! = null) {

         String value = bundle.getString("value");

         TextView textView = findViewById(R.id.text_view);

         textView.setText(value);

      }   

  

  

step 4. add both activity in manifest file .

 Notes - Intent is very useful part of android, with help of intent we can make runtime binding between the code in different applications , activity. in short this is message that is passed between components. 

it's time to go and check on emulator or real device. please test and comment your message "success", if it works perfect then OK, otherwise comment me if an error occurs.

2/12/2022

How to make expandable list in android.

  in this we will discuss of creation of listview with expandable option, using simple xml and java.

 

 step 1) create activity-main.xml file and add relative layout,

within this relative layout use below code, 

 

 <ExpandableListView

      android:id="@+id/expandList"

      android:layout_width="match_parent"

      android:layout_height="match_parent"

      android:divider="@android:color/background_light"

      android:dividerHeight="0.5dp"/>

 

 step 2) go to Mainactivity.java in oncreate method put below code,

 

    expandableListView = findViewById(R.id.expendableList);

expandableListDetail = ExpandableListmyclass.getData();

expandableListTitle = new ArrayList<>(expandableListDetail.keySet());

expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);

expandableListView.setAdapter(expandableListAdapter);

expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

@Override

public void onGroupExpand(int groupPosition) {

Toast.makeText(getApplicationContext(), expandableListTitle.get(groupPosition)

+ " List Expanded.", Toast.LENGTH_SHORT).show();

}

and simply create setOnGroupCollapseListener and setOnChildClickListener,

expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

      @Override

      public void onGroupCollapse(int groupPosition) {

         Toast.makeText(getApplicationContext(), expandableListTitle.get(groupPosition) + " List Collapsed.",

            Toast.LENGTH_SHORT).show();

      }

});

   expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

      @Override

      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

            Toast.makeText( getApplicationContext(), expandableListTitle.get(groupPosition) + "

               -> " + expandableListDetail.get( expandableListTitle.get(groupPosition)).get( childPosition), Toast.LENGTH_SHORT ).show();

            return false;

         }

    });

   

   create new class ExpandableListmyclass by creating new file ExpandableListmyclass.java

   

   simply add below code in class ,

   static HashMap<String, List<String>> getData() {

      HashMap<String, List<String>> expandableListDetail = new HashMap<>();

      List<String> myFavCricketPlayers = new ArrayList<>();

      myFavCricketPlayers.add("MS.Dhoni");

      myFavCricketPlayers.add("Sehwag");

      myFavCricketPlayers.add("Watson");

      myFavCricketPlayers.add("sachin tendulkar");

      myFavCricketPlayers.add("rahul dravid");

  expandableListDetail.put("CRICKET PLAYERS", myFavCricketPlayers);

}  

 

 

then add row list

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:orientation="vertical" android:layout_width="match_parent"

   android:layout_height="match_parent">

   <TextView

  android:id="@+id/listTitle"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:padding="10dp"

  android:textColor="@android:color/black" />

</LinearLayout>


and manifest file no change.

it's time to go and check on emulator or real device. please comment your message "success", if it works perfect, otherwise comment me if an error occurs.

An Introduction to the Laravel Framework: What It Is and Why You Should Use It

  If you're a PHP developer looking for a modern, efficient, and powerful framework to build web applications, look no further than Lara...