0

Suppose I have two activities namely AddView and SeeView.

In SeeView, there is a button that would take me to AddView activity. In AddView activity, I will enter the text that will be shown on the SeeView as TextView.

Then, clicking a button on AddView, I will be taken to the SeeView where the added TextView from AddView is displayed. How do I do this programmatically?

Sufian
  • 6,405
  • 16
  • 66
  • 120
DirkJan
  • 581
  • 2
  • 7
  • 22
  • Send the text for the added TextView along with the Intent to open the new activity. If there's none, the new activity knows that the TextView should not be shown. – Dek Dekku May 19 '13 at 08:13
  • I know how to open activity and passing some extra messages to another activity but not how to pass a textview to another activity, which is my question. Could you tell me how? – DirkJan May 19 '13 at 08:16
  • @DirkJan you should pass the value using intents and then set the value in the second activity. each activity has its own ui. – Raghunandan May 19 '13 at 08:18
  • You don't pass the TextView, just the text. See answer below. – Dek Dekku May 19 '13 at 08:19
  • @DirkJan there's no way to do that. And mode, there's no need. All you need to pass is text. – Andrii Chernenko May 19 '13 at 08:20
  • Do you guys mean that I cannot make a textview from an activity and send it using intents to another activity? My idea is that, after pressing the button, the textview will then just be created and the text containing it would be from the edittext of the addView activity. – DirkJan May 19 '13 at 08:25
  • @DirkJan no that's not possible as each activity has it's own ui – Raghunandan May 19 '13 at 08:34
  • 2
    You haven't mentioned that explicitly, but it seems that AddView is in turn launched from SeeView activity. In this case consider startActivityForResult(). Also if you have significant amount of dynamic data shared between several activities, may be it would be better to implement a singleton Model class or (in case of database-like shared data) ContentProvider. – alexei burmistrov May 19 '13 at 08:36
  • @alexeiburmistrov, that is what I meant, I think? So, in case of too many data shared, how should I make it. Forgive me, I'm a noob. :) – DirkJan May 19 '13 at 08:43
  • If you have **heterogenous** data to be shared, singleton model would work. Singleton is a java class that guarantees only one instance through public static YourModel getInstance(). You call YourModel.getInstance() in every activity that needs shared data. check http://stackoverflow.com/questions/12585720/how-to-use-the-singleton-pattern-in-an-android-project – alexei burmistrov May 19 '13 at 09:31
  • For **homogenous** data, stored in database use ContentProvider wrapping, say, your Sqlite db. As long as all queries pass through it, changes in data propagate through provider's notification methods – alexei burmistrov May 19 '13 at 09:35

2 Answers2

1

Views are neved transferred between activities, what you want to do is to pass the text. For that you should use startActivityForResult() and onActivityResult() methods of your activity:

public class SeeView extends Activity {
    public static final int REQ_CODE_ADD_VIEW;

    private TextView textViewWithText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_of_see_view);

        //this TextView is defined in xml: /res/layout/layout_of_see_view.xml
        //make sure its id is "@+id/text_view_with_text"
        textViewWithText=(TextView) findViewById(R.id.text_view_with_text);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //intent for AddView activity
                Intent intent=...;
                startActivityForResult(intent, REQ_CODE_ADD_VIEW, null);
            }
        })
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);

        if (resultCode==RESULT_OK) {
            if (requestCode==REQ_CODE_ADD_VIEW) {
                String text=data.getStringExtra("text", "");
                textViewWithText.setText(text);
            }
        }
    }
}

your second activity:

public class AddView extends Activity {   
    private EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_of_add_view);

        //this is done in case user quits your activity before
        //button is pressed
        setResult(RESULT_CANCELLED);    

        textViewWithText=(TextView) findViewById(R.id.edit_text);

        findViewById(R.id.another_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //prepare result for SeeView activity
                Intent intent=new Intent();
                intent.putStringExtra("text", editText.getText().toString());
                //set the result, it will be passed to onActivityResult() in SeeView activity
                setResult(RESULT_OK, intent);
                finish();
            }
        })
    }
}
Andrii Chernenko
  • 9,873
  • 7
  • 71
  • 89
  • This, I think, has a TextView added prior to the clicking of the button in AddView activity. Alexei got my idea, which is to addView after clicking the button on addView activity. – DirkJan May 19 '13 at 08:48
  • @DirkJan Yes, you are right, and this is the recommended way. If you need to accumulate text added in AddView activity, you might need to use ListView in your SeeView activity and add each new piece of text as a separate item. – Andrii Chernenko May 19 '13 at 08:51
  • As I review this, I noticed that there needs to be a textview on seeview activity whose id needs to be text_view_with_text. – DirkJan May 19 '13 at 09:37
  • @DirkJan this is just an arbitrary id. The thing is that it should be the same in your xml and activity. – Andrii Chernenko May 19 '13 at 09:40
  • Why should I add a textview via xml? I need to do that programmatically. – DirkJan May 19 '13 at 11:11
0

In your FirstActivity say main activity you have a editext and a button. User enters the values in editext. On Button click get that value. Use intents to pass the value to the secondactivity. In second activity retrieve the value and display the same in textview.

     addView.setOnClickListener(new OnClickListener()
    {
       Intent i= new Intent("com.example.secondActivity");
      // Package name and activity
      // Intent i= new Intent(MainActivity.this,SecondActivity.Class);
      // Explicit intents
      i.putExtra("key",editext.getText().toString());//get value from editext
      // Parameter 1 is the key
      // Parameter 2 is your value
      startActiivty(i);
    });

In your second Activity retrieve it:

      Bundle extras = getIntent().();
      if (extras != null) {
       TextView tv= (TextView) findViewById(R.id.textview) 
      String value = extras.getString("key");
       //get the value based on the key 
       tv.setText(value);
      }

Edit: In your second activity you can do something similar as below

second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="74dp"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="fill_parent"
    android:id="@+id/ll"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="157dp"
    android:orientation="vertical" >

  </LinearLayout>

 </RelativeLayout>

SecondActivity

    public class SecondActivity extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
    Bundle extras = getIntent().getExtras();
    if(extras!=null)
    {
        TextView tv= new TextView(this);
        tv.setText(extras.getString("key").toString());
        ll.addView(tv);
    }
}
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • In the second activity, is the TextView added to the activity or was it added prior to that? At first, there should be no TextView and it will only be added into the activity along with the message after adding it on the other activity. – DirkJan May 19 '13 at 08:22
  • each activity has its own ui. define your own ui xml for second activity. define a textview in xml and initialize the same in activity using findviewbyid. then set the text to textview. you could set the visibility if the view invivisble and make it visible when you want to and set the text to the textview – Raghunandan May 19 '13 at 08:24
  • What if I want the user to add many textviews? Should I make many textviews prior to that and just make them visible when the user added it? – DirkJan May 19 '13 at 08:27
  • i don't understand what you really want – Raghunandan May 19 '13 at 08:29
  • Okay, I'll elaborate my plan. Let Activity1 be the activity where the TextView will be shown. It contains only a linearlayout. Let Activity2 be the activity where the text will be shown. The user will input the text using edittext and then press the button to add it to Activity 1. Then, in Activity1, a TextView will be created to be the "receiver" of the text. What I mean is, at first, there is no TextView and the TextView will only be created after the user pressed the button in Activity2. – DirkJan May 19 '13 at 08:37
  • what you can do is on retrieving the value from 1st activity create a text view programatically like TextView tv= new TextView(thi) in your second activity. Add this textview to your layout. set the text to the textview – Raghunandan May 19 '13 at 08:39
  • 1
    @DirkJan you can check the edit. you can create textview dynamically when you want to and add the same to your layout. create as many textview as you want. add it as your child to your root view. set text to textview. I guess you are looking for something similar to the one in edit – Raghunandan May 19 '13 at 09:08
  • Shouldn't I get the input using onResult? – DirkJan May 19 '13 at 09:41