user2543382
user2543382

Reputation:

LinearLayout nesting

Is it possible to create a method that would inflate a layout (or create it dynamically), add specific views to it (in this case TextViews), then retun the layout as a view, so I could (somehow) incorporate it in the main layout in the other class, like nesting, but with dynamic adding of elements?

Upvotes: 1

Views: 134

Answers (3)

Nick Davis
Nick Davis

Reputation: 204

Yes, you can use the LayoutInflater class to inflate an existing layout. It would go something like:

public static View GetLayout(final Context c){
    final LayoutInflater inflater = LayoutInflater.from(c);
    final View v = inflater.inflate(R.layout.activity_main, null);

    //find the container where you want to insert your dynamic items
    final LinearLayout placeholder = (LinearLayout) v.findViewById(R.id.linearLayout1);

    //create the new textview
    final TextView text = new TextView(c);
    text.setText("Some text");

    placeholder.addView(text, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    return v;

}

The question asked that this view be returned to the "other class", and the other answers are doing so from the same class -- which means it has a context. In order to do so from another class you need to pass in a context like I'm doing here, or some other way (constructor call if you want an instance object, etc).

Upvotes: 3

Shaquil Hansford
Shaquil Hansford

Reputation: 383

Yes, it is. I do a similar thing in my app. I'm writing a flashcard app, and I use a scrollview to show the user all the decks they have created. The code is commented:

public void FillDeckView(){
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        Deck testDeck = Deck.GetDeckFromDB();//Make a deck object from SQLite DB values

        final int DECK_SIZE = 20;//Fill the view with this many decks

        TableLayout scrollTable = (TableLayout) findViewById(R.id.scrollTable);
                         //This tableRow is in my main view
        TableRow newTableRow = (TableRow) findViewById(R.id.tableRow4);
                         //This view I'm inflating is a separate android layout XML file
                         //I created, which shows the icon for the deck the user has made.
        View deckViewBox = inflater.inflate(R.layout.deck_icons, null);
                         //Look, I'm getting a textview that's IN the deckViewBox, which is
                         //a separate View. Below, I'll change its text dynamically
        TextView deckNameTV = (TextView) deckViewBox.findViewById(R.id.deckNameView);

        int viewIndex = 1;
        for(int i = 0; i < DECK_SIZE && testDeck != null; i++){
                            //First I'll change the text of the view, and then I'll add it in
            deckNameTV.setText(testDeck.getName());
            newTableRow.addView(deckViewBox);
            if(i % 2 != 0){
                //If we're on an odd number, make a new tableRow
                newTableRow = new TableRow(getApplicationContext());
                scrollTable.addView(newTableRow, viewIndex);
                ++viewIndex;
            }           
        }
}//FillDeckView

Basically, you need to inflate the view from a new layout and then call findViewById() as a method of the new View you've created. From there, it's just like manipulating the current view the user can see. You can do anything you want from there.

Upvotes: 1

android developer
android developer

Reputation: 116332

yes it is possible:

... onCreate()
  {
  setContentView(...);
  ViewGroup myContainer=(ViewGroup)findViewById(R.id.myContainer);
  View v=inflateMySpecialView();
  //=<set layoutParams for the view if needed
  myContainer.addView(v);
  }

public View inflateMySpecialView()
  {
  ViewGroup viewgroup=(ViewGroup ) getLayoutInflater().inflate(R.layout.my_custom_layout, null,false);
  //do some stuff with the inflated viewgroup.
  return viewgroup;
  }

Upvotes: 1

Related Questions