Reputation: 480
I am a beginner when it comes to Android developing and I am trying to write a search application that takes in a string and outputs the text messages that contain that string. My problem is with trying to format the text output on the second screen. I have the text displaying correctly using the code:
TextView textView = new TextView(this);
textView.setTextSize(20);
// Set the text view as the activity layout
setContentView(textView);
..... (find string matches)
textView.append(msg);
However, when I go and edit the .xml file for that file nothing changes (I have tried adding bold, adding a starting text etc.). I copied my TextView xml block to my activity_main.xml and it displayed a bold "hello world" on the first screen and not the second even when the TextView section was an exact copy. What is it that I am missing in the second that I am doing in the first? Is my problem in my TextView declaration? My end goal is to display many text message matches and force them to fit on the screen horizontally and allow the user to scroll vertically, is changing the second xml file the wrong way to do it?
Thanks
Upvotes: 0
Views: 1230
Reputation: 22527
Your problem is this:
// Set the text view as the activity layout
setContentView(textView);
If you set setContentView like this , no matter how you change your xml file you still get the same thing. A text with size 20.
Try to create a new layout and add the Textview on it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Change your code as follow:
setContentView(R.layout.new_layout);
TextView textView = (TextView)findViewById(R.id.TextView1);
..... (find string matches)
textView.append(msg);
Upvotes: 0
Reputation: 45493
It appears you're mixing up the two different ways to 'create' views:
The first approach is where you use an xml file to (statically) declare the layout. I.e. your layout file may be named activity_layout.xml
and include the following entry that defines a TextView
with id textview1
.
<TextView android:id="@+id/textview1" ... />
In order to use a view in such a layout definition, you need to 'inflate' it. A common place for this is in an Activity
. However, you first need to tell Android what layout to inflate from:
// inflate from 'activity_layout.xml'
setContentView(R.layout.activity_layout);
// inflate the TextView with id 'textview1'
TextView textview = (TextView) findViewById(R.id.textview1);
Now, the second approach is to (dynamically) instantiate views in code. This does not require a layout file. Typically you'll be able to differentiate this from inflating because the presence of the 'new' keyword. I.e.
// not xml definition required; all code
TextView textview = new TextView(getActivity());
I hope you're seeing where this is going? Your code snippet suggest you're using the second approach, while your explanation mentions you're modifying a layout file in the hope to see changes. Basically you're changing a layout file you're not currently using, hence you don't see anything happen.
Either change your code to use the views in the layout, or get rid of the layout and do everything in code. Usually the first approach is a more flexible one and easier to use; e.g. you'll be able to benefit from previews in Eclipse and it'll be much easier to manage if the layout gets more complex.
Upvotes: 1