Adding Buttons Dynamically In Relativelayout To Linearlayout
When the user inputs a word, he creates a number of Buttons equal to the length of the word. For example: if user inputs 'aaaa' he will create 4 Buttons, side by side, in the first
Solution 1:
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
This line says that myButton should be added to right of myButton, which doesn't make any sense.
simple way to resolve this is to use the following line instead
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId()-1);
But this isn't the best way to do this, you should use LinearLayout with horizontal orientation instead.
Solution 2:
The structure should be simple
Just need to add your buttons in 3 different linear layout with orientation horizontal.
Like
<Relative layout>{
<LinearLayout global container with vertical orientation >{
<LinearLayout for'a'type buttons container with horizontal orientation>
<LinearLayout for'b'type buttons container with horizontal orientation>
<LinearLayout for'c'type buttons container with horizontal orientation>
}
}
Solution 3:
You guys are right. It is much easier using a LinearLayout. For those interested
finalLinearLayoutlinearLayout= (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParamsllp=newLinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
newView.OnClickListener()
{
publicvoidonClick(View view)
{
LinearLayoutlinearLayout2=newLinearLayout(view.getContext());
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParamsrlp=newLinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
intsize= enter_txt.getText().toString().length();
for (int i=0; i<size; i++)
{
ButtonmyButton=newButton(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
linearLayout2.addView(myButton, rlp);
}
linearLayout.addView(linearLayout2, llp);
Post a Comment for "Adding Buttons Dynamically In Relativelayout To Linearlayout"