I want to create a relative layout inside a linear layout dynamically.This relative layout contains a text view and button which are also created in dynamic method.The alignment of text view and button not work properly.The code which i have tried is given below.
final LinearLayout lab_linear = (LinearLayout) findViewById(R.id.contact_list_layout);
lab_linear.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < Size_contact; i++)
{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(lp);
final TextView Questions_value = new
android.widget.TextView(getApplicationContext());
Questions_value.setText(contact_name.get(i));
Questions_value.setTextSize(18);
Questions_value.setId(i);
layout.addView(Questions_value);
Button myButton = new Button(this);
myButton.setBackgroundResource(R.drawable.close);
myButton.setLayoutParams(new LayoutParams(20,20));
layout.addView(myButton);
lab_linear.addView(layout);
}
Try this..
Give layout parems for RelativeLayout individually to TextView and Button and also add addRule(RelativeLayout.ALIGN_PARENT_RIGHT); or addRule(RelativeLayout.ALIGN_PARENT_LEFT); for that views
Or add the below in for Button
lp1.addRule(RelativeLayout.RIGHT_OF, Questions_value.getId());
for (int i = 0; i < Size_contact; i++)
{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
final TextView Questions_value = new
android.widget.TextView(getApplicationContext());
Questions_value.setText(contact_name.get(i));
Questions_value.setTextSize(18);
Questions_value.setId(i);
layout.addView(Questions_value,lp);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//Or below remove above code and uncomment the below code
//lp1.addRule(RelativeLayout.RIGHT_OF, Questions_value.getId());
Button myButton = new Button(this);
myButton.setBackgroundResource(R.drawable.close);
myButton.setLayoutParams(new LayoutParams(20,20));
layout.addView(myButton,lp1);
lab_linear.addView(layout);
}
Might I suggest using LinearLayouts instead of RelativeLayouts in your loop? That way, you won't have the overlap problem.
Related
I have the following:
Button[] buttons = new Button[forSale.size()];
TextView[] textViews = new TextView[forSale.size()];
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < forSale.size(); i++) {
rl = (RelativeLayout)findViewById(R.id.marketView);
textViews[i] = new TextView(this);
textViews[i].setText("\n" + forSale.get(i).getTradeGoodType()
+ "\t$"
+ forSale.get(i).getTradeGoodType().getBasePrice() +
"\n");
textViews[i].setId(i);
goodsForSaleText.append(textViews[i].getText());
buttons[i] = new Button(getApplicationContext());
buttons[i].setText("BUY");
// add the rule that places your button below your TextView object
params.addRule(RelativeLayout.BELOW, textViews[i].getId());
buttons[i].setLayoutParams(params);
rl.addView(buttons[i]);
}
My problem is that my the buttons are being added on top of each other at the top of the screen rather than one after the other below the TextViews.
Basically, it should look like:
TextView
Button
TextView
Button
TextView
Button
I need to dynamically add buttons for each number of TextViews I have (derived from a list). Can anyone help me out?
Use LinearLayout with vertical orientated instead of RelativeLayout. You will get automatically vertical position of objects
I made a design for my app but as far as I can imagine
I have to a button into two different LinearLayouts.
Is it possible under Android?
Edit:
Code
_configureButton = new Button(context);
_callButton = new Button(context);
_messageButton = new Button(context);
_contactImage = new ImageView(context);
LinearLayout _verticalMainview = new LinearLayout(context);
LinearLayout _callAndMessageView = new LinearLayout(context);
LinearLayout _horizontelMainview = new LinearLayout(context);
LinearLayout _settingsAndMessageView = new LinearLayout(context);
//...
_callAndMessageView.addView(_callButton);
_callAndMessageView.addView(_messageButton);
_verticalMainview.addView(_configureButton);
_verticalMainview.addView(_contactImage);
_verticalMainview.addView(_callAndMessageView);
_horizontelMainview.addView(_contactImage);
_horizontelMainview.addView(_settingsAndMessageView);
_settingsAndMessageView.addView(_configureButton);
_settingsAndMessageView.addView(_messageButton);
Now code crashes. Or I should use GridLayout?
It isn't possible that a view has two parents.
I want to make a layout that it
LinearLayout
ScrollView
LinearLayout
but it seems like the ScrollView has cover the rest of LinearLayout.
LinearLayout all = new LinearLayout(this);
LinearLayout upper = new LinearLayout(this);
LinearLayout footer = new LinearLayout(this);
ScrollView sv = new ScrollView(this);
all.setOrientation(LinearLayout.VERTICAL);
upper.setOrientation(LinearLayout.HORIZONTAL);
footer.setOrientation(LinearLayout.HORIZONTAL);
TextView code = new TextView(this);
code.setText("Code : ");
EditText codeEdit = new EditText(this);
upper.addView(code);
upper.addView(codeEdit);
LinkedList<LinearLayout> lk = new LinkedList<LinearLayout>();
TextView[] tt = new TextView[100];
LinearLayout ll= new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
for(int a = 0 ; a < 100; a++){
LinearLayout temp = new LinearLayout(this);
temp.setOrientation(temp.HORIZONTAL);
tt[a] = new TextView(this);
tt[a].setText("Hello " + a);
temp.addView(tt[a]);
lk.add(temp);
ll.addView(temp);
}
sv.addView(ll);
Button next = new Button(this);
next.setText("Next");
footer.addView(next);
all.addView(upper);
all.addView(sv);
all.addView(footer);
setContentView(all);
The scrollview is too large and cover the rest of the linearlayout, the linearlayout after is not showing becasue of the scrollview.
How can I actually solve this?
Do like this
LinearLayout dummy = new LinearLayout(this);
dummy.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
dummy.addView(sv);
add one linearlayout to wrap the scrollview inside it
instead of adding scroolview add this dummy linearlayout
I tried the below code to create multiple buttons through programatically but it creates a single button as per my application i need to create a button based on input. For example if the input is 3 means i need to create three buttons in a layout. For your reference i attached the sample image and my code.
for (int i = 0; i < array_of_btn_input.size(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
main_layer.addView(layout);
}
if in your example, your global container (main_layer) is a relativelayout or a frame layout, you have placed them on top of eachother. So you can't see the one on the back order.
Try this pls,
LinearLayout main_layer= (LinearLayout) findViewById(id.main_layer);
for (int i = 0; i < 10; i++)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
main_layer.addView(layout);
}
You are creating multiple new linear layouts within the for loop. Linear layout need to be taken out of the for loop and just try adding the buttons in it.
1st run of for loop creates a new linear layout and adds a button
2nd run of for loop creates a new linear layout and adds a button on the top of previous added layout
Here you are creating a new LinearLayout per button... Try something more like...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
for (int i = 0; i < array_of_btn_input.size(); i++) {
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
}
main_layer.addView(layout);
Try this
LinearLayout llParent = (LinearLayout) findViewById(R.id.llParent);
llParent.removeAllViews();
for (int i = 0; i < array_of_btn_input.size(); i++) {
BSView b = new BSView(this, new SimpleThumbBean(i + 1, bsList
.get(i).getLabel(), bsList.get(i).getThumbUrl()));
LinearLayout.LayoutParams llDynamic = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
llDynamic.weight = 1f;
llParent.addView(b, llDynamic);
}
llParent is a defined Layout and make sure you call removeAllViews() before adding layouts dynamically into it.
BSView is my own custom View. You can set BSView into ordinary Button as you need it.
I need do design programatically one activity with 6 buttons (same size h and w), and all buttons show a fullsize of activity.
I tried do this: RelativeLayout with buttons and modify for tests.
Show one button!!!
`
setContentView(R.layout.main);
ArrayList<Button> buttons = new ArrayList<Button>();
//RelativeLayout bg = (RelativeLayout) findViewById(R.layout.main);
for (int i = 0; i < 10; i++) {
Button newButton = new Button(this);
newButton.setId(100 + i + 1); // ID of zero will not work
newButton.setText("XXXX");
buttons.add(newButton);
// New layout params for each button
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
// using getId() here in case you change how you assign IDs
int id = buttons.get(i - 1).getId();
lp.addRule(RelativeLayout.RIGHT_OF, id);
}
this.addContentView(newButton, lp);
}`
please look this line if ok: this.addContentView(newButton, lp);
Thanks!!!
mateus
It's a bit not clear from your question whether you want all buttons to distribute evenly across the activity space or each button to take over all activity space?
In first case what you need is a LinearLayout with layout_weight for buttons set to 1.
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.weight = 1;
In the second case I think it's better to use a FrameLayout and just let buttons take over all space by setting
FrameLayout.LayoutParams.FILL_PARENT
to both dimensions.