Reputation:
I understand how to display some text with the user input, but what I'm confused about is how to add a completely new block of text under the one I have right now (I want to put a new block of text because I want the size of the text to be different). Right now, I display "Welcome [user input]!"
In my activity file I have:
public class DisplayMessageActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = " Welcome " + intent.getStringExtra(MainActivity.EXTRA_MESSAGE) + "!";
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(25);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
And in my fragment.xml file, I have:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.myfirstapp.DisplayMessageActivity$PlaceholderFragment"
android:gravity="center_horizontal"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:text="TEST"
android:layout_height="wrap_content" />
</LinearLayout>
With some help from the feedback I got from other users, I added android:text="TEST" too the second "TextView". And I also added the android:orientation="vertical" to LinearLayout. But the "TEST" still doesn't show up when I run the app! Thanks in advance.
Upvotes: 1
Views: 573
Reputation: 6892
You are not associating your layout to your Activity
. Instead, you are adding a TextView
on runtime, that is created on runtime. That's why only one TextView
appears. You have to use setContentView(R.layout.mylayout);
instead of setContentView(textView);
, so your Activity
fetches your layout.
Edit: In your layout, add ids to your TextViews
, so you can access them from your Activity
code, like this:
<TextView
android:id="@+id/myFirstTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TEST1" />
<TextView
android:id="@+id/mySecondTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TEST2" />
Then on your Activity
, inside onCreate()
:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
// Get the message from the intent
Intent intent = getIntent();
String message = " Welcome " + intent.getStringExtra(MainActivity.EXTRA_MESSAGE) + "!";
TextView textView1 = (TextView) findViewById(R.id.myFirstTextView);
TextView textView2 = (TextView) findViewById(R.id.mySecondTextView);
textView1.setTextSize(25);
textView1.setText(message);
}
Upvotes: 1
Reputation: 21
Assuming fragment.xml is in the res/layout folder
setContentView(R.layout.fragment);
instead of
setContentView(textView);
Upvotes: 1