1

I have 2 TextViews but one of them doesn't show any text. I see in LogCat that is there but on the app it doesn't show. I'm sure that the problem is in my xml file but I don't know what exactly. Also I saw a lot of posts like this here and I've tried different thing from them but non is helped me. Here is the layout

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent" >

<LinearLayout 
    android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="0dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_weight="10.18"
        android:background="@drawable/buttons"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/name"
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="" />

        <TextView
            android:id="@+id/text"
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:layout_marginTop="10dp"
            android:text="" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:orientation="vertical" >

        <Button
            android:id="@+id/pressMe"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/bg_button"
            android:gravity="center"
            android:padding="4dp"
            android:text="@string/PressMe" />

        <Button
            android:id="@+id/dontPress"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/bg_button"
            android:gravity="center"
            android:padding="4dp"
            android:text="@string/DontPressMe" />
    </LinearLayout>

</LinearLayout>

</ScrollView>

The problematic TextView is "@+id/name"

Also I get them from another activity via putExtra and getExtra

intent.putExtra("text", stocks[position].text); 
intent.putExtra("name", stocks[position].name);

and

Bundle b = getIntent().getExtras();
    if (b != null) {
        name = b.getString("name");
        textView.setText(name+"");
        text = b.getString("text");
        textView.setText(text+"");

So the text is there but name is missing. On the first Activity both are there.

    textView = (TextView) findViewById(R.id.name);
    textView = (TextView) findViewById(R.id.text);
Goro
  • 499
  • 1
  • 13
  • 31

6 Answers6

3

There is no problem in your Layout

but the problem is in your code, you have to find both the textView in your oncreate method. you did find both, but the problem is that when you set the text, with the same reference of text

here you to take the same reference for both text view. change it like this.

TextView txtName = (TextView)findviewbyid(R.id.name);
TextView txtText = (TextView)findviewbyid(R.id.text);

    Bundle b = getIntent().getExtras();
        if (b != null) {
            name = b.getString("name");
           //change here txtName.setText(name+"");
            text = b.getString("text");
            //change here txtText.setText(text+"");
}
rmanalo
  • 342
  • 3
  • 23
GovindRathod
  • 867
  • 1
  • 8
  • 22
  • Like this is working and name is also there. But why only one textview doesn't show in this case? – Goro Nov 14 '14 at 10:23
2
android:paddingBottom="10dp"

Why you have that?

Try removing it or changing it for:

android:layout_marginBottom="10dp"
Artemio Ramirez
  • 1,116
  • 1
  • 10
  • 23
  • Why should I use `marginBottom` instead of `paddingBottom`? I use padding because I want space between content and the view.. – Goro Nov 14 '14 at 10:37
  • A padding goes inside the layout, so if your text is really there but doesn't show chances are it has not enough space. A margin goes outside the layout. Is not that you should never use it. – Artemio Ramirez Nov 14 '14 at 10:41
  • Yes, I understand now what you mean with your answer. Thank you. – Goro Nov 14 '14 at 10:43
2

You are overwriting texts in the same TextView object using this:

textView = (TextView) findViewById(R.id.name);
textView = (TextView) findViewById(R.id.text);
textView.setText(name+"");
textView.setText(text+"");

Keep two separate textview and do this:

TextView textview1, textview2;
textView1 = (TextView) findViewById(R.id.name);
textView2 = (TextView) findViewById(R.id.text);
textView1.setText("name");
textView2.setText("description");

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
1

Try with below code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:minHeight="50dp"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/chat_add_contact"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/name"
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingTop="5dp"
                android:text="dsgdsfgdfhdgfhfjhgfj" />

            <TextView
                android:id="@+id/text"
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp"
                android:paddingLeft="5dp"
                android:paddingTop="10dp"
                android:layout_marginTop="10dp"
                android:text="sfgdfhgfjgfjhgjhg" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="300dp"
            android:layout_height="fill_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:orientation="vertical" >

            <Button
                android:id="@+id/pressMe"
                android:layout_width="300dp"
                android:layout_height="40dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/ic_launcher"
                android:gravity="center"
                android:padding="4dp"
                android:text="PressMe" />

            <Button
                android:id="@+id/dontPress"
                android:layout_width="300dp"
                android:layout_height="40dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:background="@drawable/ic_launcher"
                android:gravity="center"
                android:padding="4dp"
                android:text="DontPressMe" />
        </LinearLayout>

    </LinearLayout>

</ScrollView>
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
1
TextView txtCont = (TextView) findViewById(R.id.name);
pb772
  • 539
  • 1
  • 3
  • 14
Andres Cárdenas
  • 720
  • 1
  • 5
  • 26
1

change this code may help you,

Bundle b = getIntent().getExtras();
    if (b != null) {
        name = b.getString("name");
        ((TextView)findviewbyid(R.id.name)).setText(name+"");
        text = b.getString("text");
        ((TextView)findviewbyid(R.id.text)).setText(text+"");
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19