Ayush Singh
Ayush Singh

Reputation: 3

How To Print a String in a TextView, when the value is taken from previous Activity

I m new programmer of Android Studio, I Just wanna know How to Print a string in a textview when the string is taken from previous activity by Bundle. I Want to take value from one activity and show it to another activity through TextView. The Code I Use was:

public void imagebtn10(View view) {
    Intent StartNewActivity = new Intent(this, FinalPage.class);
    String code="Easy";
    Bundle bundle = new Bundle();


    bundle.putString("stuff", code);


    StartNewActivity.putExtras(bundle);


    startActivity(StartNewActivity);
    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}

AND THIS IS MY FINAL ACTIVITY

Bundle bundle = getIntent().getExtras();
String stuff = bundle.getString(“stuff”);

But Now I Want to show the String "stuff" in Textview So, Can Anyone tell How is it Possible??

Upvotes: 0

Views: 841

Answers (3)

Vishal Sharma
Vishal Sharma

Reputation: 1061

This is a simple Example to get data from one activity and display into second activity. Hope this will help you

 --------------------activity1-----------------------------------
    but.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String s=editText1.getText().toString();
                    String s1=editText2.getText().toString();



                    SharedPreferences.Editor editor=sp.edit();

                    editor.putString("user",s);
                    editor.putString("id",s1);
                    editor.commit();
                    gotomainactivity();

                }
            });

            String userId=sp.getString("user",null);
            String password=sp.getString("id",null);
            if(userId!=null && password!=null){
                gotomainactivity();
            }

        }

        private void gotomainactivity() {
            Intent intent=new Intent(MainActivity.this,Sharedatarecive.class);
            startActivity(intent);
            finish();
        }
    ------------------------------------activity2--------------
     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sharedatarecive);
            sep=getApplicationContext().getSharedPreferences("mypref",MODE_PRIVATE);
            TextView textView=(TextView)findViewById(R.id.t1);
            TextView textView1=(TextView)findViewById(R.id.t2);

            String s3=sep.getString("user",null);
            String s4=sep.getString("id",null);

            textView.setText(s3);
            textView1.setText(s4);
        }

Upvotes: 0

Sven Affeld
Sven Affeld

Reputation: 341

If your TextView hasn't already got an id, for example "finalTextView", you can add that id in the layout by adding this line in the xml:

android:id="@+id/finalTextView"

So in your final activity, just add these lines:

TextView myTextView = findViewById(R.id.finalTextView);
myTextView.setText(stuff);

That should be all.

Upvotes: 0

Siddhesh Golatkar
Siddhesh Golatkar

Reputation: 295

    // in 1 activity ->
    String idValue = "hellow world";
    Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
    intent.putExtra("ID", idValue);
    startActivity(intent);
    //in second Activity->
    String i = getIntent().getStringExtra("ID");

Upvotes: 2

Related Questions