Search code examples
androidsetcontentview

Blank Second Activity page on text input


I am new here, and new to Android Programming as well. So any help on this will be greatly appreciated.

I am running an Android App(basically the example given in developer.android.com).The idea of the app is to let the user enter a message in the 1st screen, and display the message in the second screen.

The problem i have is when i use following code in "OnCreate" in "DisplayMessageActivity.java" then my application works fine and show the text on the second screen perfectly.

public class DisplayMessageActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    setContentView(textView);
}

But when i use ,

setContentView(J.layout.activity_display_message);

instead of

setContentView(textView);

then it shows nothing just a blank second screen.

The code of "activity_display_message.xml" is as follows,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />
</LinearLayout>

So my question is that why contentview ,referring "active_display_message.xml, dosen't working and which is a better way to display text,here.


Solution

  • Ok , here is what you are doing

    TextView textView = new TextView(this);
    

    This means you're creating the TextView dynamically. You don't need to do this when you have TextView in xml file, unless you need. For your purpose here, you don't need to create TextView dynamically.

    What you should do is to use the TextView you have created in xml file. You can access any control (TextView/EditText) that you have defined in xml by their id in java file using findViewById(R.id.control_id), and id will be unique.

    So first assign an id for your TextView

        android:id="@+id/tv_1"  // assigning id
    

    Your layout file should look like this

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

    Then in your activity, do the following

    public class DisplayMessageActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_xml);    // set the view that you want to display
    
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    
    TextView myTextView = (TextView) findViewById(R.id.tv_1)   // make sure the id is correct
    myTextView.setText(message);   // set the value 
    myTextView.setTextSize(40);    // you can dynamically modify the text size
    

    }