Andrew Kor
Andrew Kor

Reputation: 603

How to show TextView programmatically?

I have a RelativeLayout called current_layout which I place my views on. When I attempt to addView(TextView) , nothing is displayed. However when adding an ImageView, it works just fine. Why is my TextView not displaying?

public static void draw_shard(int x, int y, int amount_collected){//X and Y are GAMESURFACE values. Needs to increment by gamesurface y.

    ImageView shard = create_iv(); // Creates a new instance of an ImageView (parameter is the context of MainActivity)
    shard.setBackgroundDrawable(shard_icon);
    shard.setX(x);
    shard.setY(y+ImageLoader.get_score_bar_height());
    TextView tv = new TextView(MainActivity.current_context);
    tv.setX(shard.getX() + shard.getWidth());
    tv.setY(shard.getY());
    tv.setTypeface(Variables.joystix);
    tv.setTextSize(shard.getHeight());
    tv.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    tv.setText("+" + amount_collected);
    tv.setTextColor(Color.WHITE);
    current_layout.addView(shard);
    current_layout.addView(tv);
}

I am adding the TextView on top of a black background also.

Upvotes: 0

Views: 247

Answers (5)

Manu Garg
Manu Garg

Reputation: 186

It will help you to understand

LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL); 

TextView textView = new TextView(this);
textView.setText("Your Text that you want to add");
linearLayout.addView(textView);

Thanks

Upvotes: 0

chandan
chandan

Reputation: 117

But why are you adding TextView using java code?

You can easily do it in XML.

<TextView
    android:layout_width="wrap_content"
    android:id="@+id/textView"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:background="@color/colorPrimary"
    android:textColor="@color/colorPrimary" />

Upvotes: 0

Monali
Monali

Reputation: 171

// Create LinearLayout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);

// Create TextView
TextView product = new TextView(this);
product.setText(" Product");
ll.addView(product);

Please try it.

Upvotes: 0

Andrew Kor
Andrew Kor

Reputation: 603

The problem was with shard.getWidth() and shard.getHeight() , which were returning 0.

Upvotes: 1

letsintegreat
letsintegreat

Reputation: 3366

An alternate and easy way to do that is:

Add the TextView in the layout file and set its visibility to gone, and when you need to show the TextView, just change the visibility of that TextView.

Sample code for XML file:

<TextView
    android:id="@+id/textview"
    android:layout_height="wrap_content"
    android:layout-width="wrap_content"
    android:visibility="gone">
    <!-- Add other attributes too -->

And when you need that TextView, add this line of code:

findViewById("textview").setVisibility(View.VISIBLE);
findViewById("textview").setText("" + amount_collected);

Upvotes: 0

Related Questions