When a user enters a word, it creates Buttons
- one Button
per letter of the word:
Illustration:
If the user enters "so" it creates 2 Buttons
- 's', 'o'
If the user enters "make" it creates 4 Buttons
- 'm', 'a', 'k', 'e'
I was having a hard time deciding how I should design this. Ultimately I decided to do the following: Each word is added to a vertical LinearLayout
. And for each word, each letter is added to a horizontal LinearLayout
. So it's a LinearLayout
within a LinearLayout
approach.
Here's the code I created which works:
//creates words dynamically
public void makeNewWord(LinearLayout ll, View v, EditText e){
//the horizontal linear layout
LinearLayout linearLayout2 = new LinearLayout(v.getContext());
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
//the parameters for the horizontal linear layout
LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//e is the user input
int size = e.getText().toString().length();
for (int i=0; i<size; i++){
final Button dynamicButtons = new Button(v.getContext());
dynamicButtons.setLayoutParams(rlp);
//add the buttons to the horizontal linear layout
linearLayout2.addView(dynamicButtons, rlp);
}
// ll is the vertical linear layout which I created in xml
// so for each entered word, I am adding horizontal linear layouts to my vertical layout
ll.addView(linearLayout2, 0);
}
But now I realized it's probably more efficient using a ListView
, especially since I want to make the list of words to be expandable and collapsible. But Is it possible to create the above illustration using a ListView
? How would I go about doing so?
I tried creating an ArrayAdapter
as follows: ArrayAdapter<LinearLayout> adapter = new ArrayAdapter<LinearLayout>(this, R.id.listview)
. So basically it would be a ListView
of horizontal LinearLayouts
. Or should I make an ArrayAdapter
of Buttons
instead? What is the correct approach?