Wassif
Wassif

Reputation: 1

How to link the textview to my XML layout

im making a random fact generator and ive got the code sorted but i want it to appear with my layout in the xml file, the code i have is as follows

public class facts extends Activity {

//implements android.view.View.OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.facts);
    setTitle("Buy - Find a carpool!");



    Random generator= new Random();
    int num;
    String[] quotes;
    quotes= new String[15];
    quotes[0]= "test1)";
    quotes[1]= "test2";
    TextView tv = new TextView(this);
    num=generator.nextInt(15);
    tv.setText(quotes[num]);
    setContentView(R.layout.facts);

My XML code is

      <TextView
 android:gravity="center_vertical|center_horizontal"
 android:id="@+id/factslist"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android.layout_column="15" 
      android:layout_centerVertical="true"
  android:layout_centerHorizontal="true"/>

Does anyone know how to do this?

Upvotes: 0

Views: 512

Answers (3)

Jack
Jack

Reputation: 10613

Why do you create a TextView in code:

TextView tv = new TextView(this);

Why not have a TextView in your .xml file (facts.xml)? Perhaps I don't understand your question. Can you clarify what you mean by "i want it to appear with my layout in the xml file".

If you mean you want the TextView defined in .xml, then add a <TextView> element to the file facts.xml:

   <xml version="1.0" encoding="utf-8">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" 
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
</LinearLayout>

So instead of this code:

Random generator= new Random();
int num;
String[] quotes;
quotes= new String[15];
quotes[0]= "test1)";
quotes[1]= "test2";
TextView tv = new TextView(this);
num=generator.nextInt(15);
tv.setText(quotes[num]);
setContentView(R.layout.facts);

You should have:

Random generator= new Random();
int num;
String[] quotes;
quotes= new String[15];
quotes[0]= "test1)";
quotes[1]= "test2";

TextView tv = (TextView) findViewById(R.id.factslist);

num=generator.nextInt(15);
tv.setText(quotes[num]);

Do you see how you are obtaining a reference to the TextView in you .xml file based in its id, and then setting the value of this id? I also don't think you need to call this twice:

setContentView(R.layout.facts);

Upvotes: 1

Squonk
Squonk

Reputation: 48871

You put the TextView in your facts.xml file and then use...

TextView tv = (TextView) findViewById(R.id.myTextView);

Upvotes: 1

robamaton
robamaton

Reputation: 690

Look at this: Declaring Layout. You have to use findViewById() to get an instance of your TextView. Don't instantiate it yourself. And also, you don't have to setContentView again after changing things.

Upvotes: 1

Related Questions