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.

1 comment:

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...