Question

I have two layouts. The first one:

<LinearLayout       xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                <EditText
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/edit_text_val">
                </EditText>
                <Button
                        android:id="@+id/continue"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Continue"
                        android:onClick="newConfirm"/>
</LinearLayout>

and the second one:

<LinearLayout       xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/text_view_val">
                </TextView>
</LinearLayout>

When I click the button of the first layout I go to the second layout and want to set as text value of the TextView the string entered in the EditText of the first layout. I do this in the newConfirm method:

public void newConfirm(View view){
        EditText et = (EditText)findViewById(R.id.edit_text_val);
        TextView tv = (TextView)findViewById(R.id.text_view_val);
        tv.setText(et.getText().toString());
    } 

But when I click the button my app stops working... Unfortunately MobileApp has stopped

Any idea??

Was it helpful?

Solution

You can not directly set the value of your EditText from the first activity.

You are supposed to pass the value of EditText through Intent from first activity to second activity and then in your second activity get from intent and set into TextView.

Try out as below:

public void newConfirm(View view){
        EditText et = (EditText)findViewById(R.id.edit_text_val);

         Intent intent =new Intent(FistLayout.this,Secondlayout.class);
           intent.putExtra("value",et.getText().toString());
           startActivity(intent);
    } 

In your second activity write below code in onCreate

    TextView tv = (TextView)findViewById(R.id.text_view_val);
    String value=getIntent().getString("value");
    tv.setText(value);

OTHER TIPS

This is not working because the text view is in different layout. You need to pass your data by intent to next activity and then display text there.see this example.

Your EditText and TextView resides in two different layouts.Then how can you use findViewbyId for both of them in a single class??Pass the Edittext value through Intent in the 2nd layout:

 Intent i=new Intent(FistLayout.this,Secondlayout.class);
  i.putExtra("key",ed.getText().toString());
  startActivity(i);

In the second layout:

 String str=getIntent().getExtras.getString("key");
  textView.setText(str);

its very simple just define this under you onclick functionality where you intent to next activity

Define Onclick like this under OnCREATE

  YourButton.setOnClickListener(new View.OnClickListener() 
    {
            @Override
            public void onClick(View v) 
            {
                 next();
            }

        });

Then write this function anywhere in your activity

    public void next()
   {

  String strMessage = YourTEXT.getText().toString());

// so on you can directly set values
// you have to just set values in string                      

  Intent i = new Intent(getApplicationContext(), nextactivity.class)   
  i.putExtra("new_variable_name",strMessage);


  startActivity(i);  

}

Then in your next activity retrieve it via

Bundle extras = getIntent().getExtras();
String var="";

      if (extras != null)
      {

        String value= extras.getString("new_variable_name");       
        value= var;
        yourtext2.setText(var);

      }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top