BipBapApps
BipBapApps

Reputation: 73

My TextView text is not visible

Despite much fiddling, I am unable to make my TextView visible.

I've set up a string which is supposed to contain the text to be displayed in the TextView, but the text is never seen in the Graphical Layout. Even if I use "android:text="text", and change the size, appearance, etc, nothing changes.

My Java code:

    public class MainClass extends Activity {

    float goldCount = 0.0f;
ImageView minionClick;
TextView textGoldCount;
String textTotal;

@Override
public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainlayout);

    minionClick = (ImageView) findViewById(R.id.minioncentreid);
    textGoldCount = (TextView) findViewById(R.id.textviewtop);

    textTotal = goldCount + " Gold";


    textGoldCount.setText(textTotal);`

My XML code:

`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayoutid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/mainbackground"
>

<TextView
    android:id="@+id/textviewtop"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="25">

</TextView>
<ImageView
    android:id="@+id/minioncentreid"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="50"
    android:contentDescription="@+string/desc"
    android:src="@drawable/minioncentrethree"
    />
<TextView 
    android:id="@+id/textviewbottom"
    android:layout_weight="25"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    >
    </TextView>

`

The TextView at the bottom is there to that the image in the middle is centred. And I just deleted it and checked, but it did nothing to solve the problem.

Upvotes: 3

Views: 4869

Answers (2)

Sebastien Bianchi
Sebastien Bianchi

Reputation: 722

I'll also add to Filo's answer that you should use android:weightSum attribute in your LinearLayout which should be 100 (25 + 50 + 25) according to your layout.

Upvotes: 1

Blo
Blo

Reputation: 11988

Not sure, but maybe it's related to the values in weight and height attributes. You must to declare the height to 0 as follows:

<TextView
    android:id="@+id/textviewtop"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >  // 25 and 50 can be replaced by 1 and 2

<ImageView
    android:id="@+id/minioncentreid"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="2"
    android:contentDescription="@+string/desc"
    android:src="@drawable/minioncentrethree" />

<TextView 
    android:id="@+id/textviewbottom"
    android:layout_weight="1"
    android:layout_height="0dip"
    android:layout_width="wrap_content" />  

Let me know if this works.

Upvotes: 1

Related Questions