Tomas
Tomas

Reputation: 129

Inflate TextViews to LinearLayout

I want to inflate TextViews to LinearLayout.

I have a layout activity_play_game and java class PlayGameActivity. Currently, for hardcoded number of TextViews it looks like this:

enter image description here

Now I tried to add number of TextViews according to the value of variable numberForbiddenWords. I added a piece of code that looks like this:

LinearLayout playGame = (LinearLayout) findViewById(R.id.activity_play_game);
TextView temp;
TextView[] textViews = new TextView[numberForbiddenWords];
for(int i = 0; i < numberForbiddenWords; i++) {
    temp = new TextView(this);
    playGame.addView(temp);
    textViews[i] = temp;
}

And I have a method that adds textViews to the activity_play_game. Unfortunately now it looks like this:

enter image description here

As you can see it added TextViews at the bottom. So I removed all TextViews from activity_play_game and added LinearLayout to inflate TextViews:

enter image description here

How can I inflate TextViews to this LinearLayout above the buttons? I found that kind of solution:

activity_to_add_words:

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

    <TextView
        android:id="@+id/textout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

In the java class:

final View addView;
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addView = layoutInflater.inflate(R.layout.activity_to_add_words,
        null);
final TextView textOut = (TextView) addView.findViewById(R.id.textout);

but this is for a View, not TextView. Do you know how to do it?

Upvotes: 0

Views: 743

Answers (3)

Alex
Alex

Reputation: 3382

As far as I see your initial problem was about adding the items dynamically before the three buttons. If you want to add them before the three buttons simply call playGame.addView(temp,0); This will always add the item to the first position in the layout. If you need a specific order of the added views you need to play with the for loop to add them in the right order.

Upvotes: 0

Ajaykumar Mistry
Ajaykumar Mistry

Reputation: 964

make one layout file using textview like this :

layout_textview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/txt_spin"
android:textSize="18sp"
android:background="#fff"
android:padding="5dp"
android:textColor="#000"
android:layout_height="wrap_content"/>

then apply following code in your java file:

 LinearLayout playGame = (LinearLayout) findViewById(R.id.container_blue);      
 LayoutInflater li =(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View view = li.inflate(R.layout.layout_textview, null);
 TextView txt= (TextView) view.findViewById(R.id.txt_spin);
 playGame.addView(view);

Hope it works for you...happy coding..:)

Upvotes: 1

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21756

Get reference of your LinearLayout container_blue and add TextView's to it.

Try this:

LinearLayout playGame = (LinearLayout) findViewById(R.id.container_blue);
TextView temp;
TextView[] textViews = new TextView[numberForbiddenWords];
for(int i = 0; i < numberForbiddenWords; i++) {
    temp = new TextView(this);
    temp.setText("TextView " + String.valueOf(i));
    temp.setId(i);
    temp.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

    playGame.addView(temp);
    textViews[i] = temp;
}

Upvotes: 1

Related Questions