Goro
Goro

Reputation: 499

TextView doesn't show any text

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);

Upvotes: 1

Views: 9976

Answers (6)

Andres C&#225;rdenas
Andres C&#225;rdenas

Reputation: 738

TextView txtCont = (TextView) findViewById(R.id.name);

Upvotes: 1

GovindRathod
GovindRathod

Reputation: 887

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+"");
}

Upvotes: 3

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

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.

Upvotes: 2

Akash Moradiya
Akash Moradiya

Reputation: 3322

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+"");

Upvotes: 1

Pratik Dasa
Pratik Dasa

Reputation: 7439

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>

Upvotes: 1

Artemio Ramirez
Artemio Ramirez

Reputation: 1136

android:paddingBottom="10dp"

Why you have that?

Try removing it or changing it for:

android:layout_marginBottom="10dp"

Upvotes: 2

Related Questions