Add Buttons dynamically depending on screen width - android

I'm trying to add buttons dynamically depending on screen width.
i.e. if I get 6 buttons then I need to position them accordingly, so that the buttons appear at the center with equal spacings on left parent and right parent.
Here is the piece of code which I'm trying but no result:
private void btmBarBtns(int position) {
RelativeLayout rlLayout;
RelativeLayout.LayoutParams layoutParams;
int leftMargin = scrWidth/pageCount;
CommonMethods.getSystemOutput("Left Margin::::"+leftMargin);
for (int i = 0; i < pageCount; i ++ ) {
rlLayout = (RelativeLayout) findViewById(R.id.ivBottomBar);
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin = leftMargin;
ib = new ImageButton(this);
ib.setId(i);
ib.setLayoutParams(layoutParams);
ib.setBackgroundResource(R.drawable.white_circle_32x32);
rlLayout.addView(ib);
leftMargin = leftMargin + 70;
if (ib.getId() == position) {
ib.setBackgroundResource(R.drawable.black_circle_32x32);
}
}
}
In the above code I have a Relative layout with height 25dp and width fill_parent. I am able to add the buttons but they are not positioned at the center.

If all you want to is center those ImageButtons with equal space left and right then you could simple wrap them in a LinearLayout and then center that LinearLayout in the parent RelativeLayout:
RelativeLayout rlLayout = (RelativeLayout) findViewById(R.id.parent);
LinearLayout container = new LinearLayout(this);
for (int i = 0; i < 5; i++) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageButton ib = new ImageButton(this);
ib.setId(i);
ib.setLayoutParams(layoutParams);
ib.setBackgroundResource(R.drawable.ic_launcher);
container.addView(ib);
if (ib.getId() == position) {
ib.setBackgroundResource(R.drawable.black_circle_32x32);
}
}
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,
RelativeLayout.TRUE);
rlLayout.addView(container, layoutParams);
If you want to write more code just to do the above then you could modify your current layout and add this element as an anchor:
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"
android:id="#+id/anchor" />
and then in code position the ImageButtons to the left and right of this anchor View:
int anchorId = R.id.anchor;
int btnsNr = 6; // this is the number of Buttons
RelativeLayout rlLayout = (RelativeLayout) findViewById(R.id.parent);
if (btnsNr % 2 != 0) {
anchorId = 1000;
btnsNr--;
ImageButton imgb = new ImageButton(this);
imgb.setImageResource(R.drawable.shop_open);
imgb.setId(anchorId);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
rlLayout.addView(imgb, rlp);
}
int whichPart = 1;
while (whichPart >= 0) {
int previousId = anchorId;
for (int i = 0; i < (btnsNr / 2); i++) {
RelativeLayout.LayoutParams tmp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (whichPart == 1) {
tmp.addRule(RelativeLayout.LEFT_OF, previousId);
} else {
tmp.addRule(RelativeLayout.RIGHT_OF, previousId);
}
ImageButton imgb = new ImageButton(this);
previousId += whichPart == 1 ? -1 : 1;
imgb.setId(previousId);
imgb.setImageResource(R.drawable.shop_open);
rlLayout.addView(imgb, tmp);
}
whichPart--;
}
If you want to calculate the number of ImageButtons that fit the screen(and center them horizontally) you should have mentioned.

Related

TextView on the right of dynamically created Buttons

As seen in the picture, everything works fine except that myTextView, instead of appearing just on the right of the last Button, it does on top of 16, 17 and 18. I can’t manage these 3 Buttons to appear bellow the rest. Here is my essential code, where I create dynamically the Buttons and myTextView:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout1); // id del XML
layout.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < 4; i++) {
LinearLayout fila = new LinearLayout(this);
fila.setLayoutParams(new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 5; j++) {
if (i==3 && j==3){
TextView myTextView = new TextView(this);
LinearLayout.LayoutParams layoutParams=newLinearLayout.LayoutParams(490, 40);
layoutParams.setMargins(870, 30, 0, 0);
myTextView.setTextSize(26);
myTextView.setLayoutParams(layoutParams);
layout.addView(myTextView);
break;
}
Button btnTag = new Button(this); //
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setLayoutParams(new LinearLayout.LayoutParams(255, 166));
fila.addView(btnTag);
btnTag.setId(j + 1 + (i * 5));
btnTag.setOnClickListener(prueba);
}
layout.addView(fila);
}
}
At last I have solved the problem. Instead of a LinearLayout I have to use a RelativeLayout. Like this, I can set the TextView wherever I want on the screen throughout the xml. Here is the code, where with rel_btn I set the Buttons wherever I want, too:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1); // id del XML
for (int i = 1; i < 19; i++) {
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
switch(i) {
case 1: rel_btn.leftMargin = 0; rel_btn.topMargin = 0; break;
case 2: rel_btn.leftMargin = 255; rel_btn.topMargin = 0; break;
case 3: rel_btn.leftMargin = 510; rel_btn.topMargin = 0; break;
…… as many as Buttons requiered
}
rel_btn.width = 255; rel_btn.height = 165;
Button btnTag = new Button(this);
btnTag.setLayoutParams(rel_btn);
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setId(0+i); // les pone el ID
btnTag.setOnClickListener(prueba);
layout.addView(btnTag);
}
}
1. Set layout weight sum
fila.setWeightSum(5);
2. Set width = 0dp and weight = 1 for all buttons
btnTag.setLayoutParams(new LinearLayout.LayoutParams(0, 166, weight));
3. Set width=0dp and weight = 2 for textview
LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 40);
layoutParams.weight = 2;
//layoutParams.setMargins(870, 30, 0, 0);
myTextView.setTextSize(26);
myTextView.setLayoutParams(layoutParams);
Updated onCreate method
public void onCreate(Bundle savedInstanceState) {
LinearLayout layout = (LinearLayout) findViewById(R.id.layout1); // id del XML
layout.setOrientation(LinearLayout.VERTICAL);
int width = 0; //0dp, we will use weight to set width
int weight = 1;
for (int i = 0; i < 4; i++) {
LinearLayout fila = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, weight);
fila.setLayoutParams(params);
fila.setWeightSum(5);
for (int j = 0; j < 5; j++) {
if (i==3 && j==3){
TextView myTextView = new TextView(this);
LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 40);
layoutParams.weight = 2;
//layoutParams.setMargins(870, 30, 0, 0);
myTextView.setTextSize(26);
myTextView.setLayoutParams(layoutParams);
layout.addView(myTextView);
break;
}
Button btnTag = new Button(this); //
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 166, weight));
fila.addView(btnTag);
btnTag.setId(j + 1 + (i * 5));
btnTag.setOnClickListener(prueba);
}
layout.addView(fila);
}
}

Adding elements programmatically to FrameLayout using a for loop

Why when I add the elements to the FrameLayout flCatScroll only one shows and not ten are they perhaps overlapping and or why dosn't the margin increase.
myactivity.runOnUiThread(new Runnable() {
#Override
public void run() {
//Main Framelayout
FrameLayout flCatScroll = (FrameLayout) myactivity.findViewById(R.id.flCatScroll);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.TOP;
params.topMargin = 20;
for(int i = 0; i < 10; i++) {
//FrameLayout mainItemLayout = new FrameLayout(myactivity);
listFramelayouts.add(new FrameLayout(myactivity));
}
for(int i = 0; i < listFramelayouts.size(); i++) {
params.topMargin = 20 * i;
TextView itemName = new TextView(myactivity);
itemName.setText("Test" + i);
listFramelayouts.get(i).addView(itemName);
flCatScroll.addView(listFramelayouts.get(i), params);
}
}
});
Reference semantics strike again!
Because you're reusing the params object instead of creating a new one each time, They all share the same margin at the end regardless of what the margin was when you added the view. So they are, indeed, all overlapping.
Does this make sense?
dcow is right. You are reusing the same LayoutParams object for each new TextView. So at the end all of them have the same topMargin. You need to create params per each new view. You can add new function like:
private FrameLayout.LayoutParams getLayoutParamsFor(final int i) {
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.TOP;
params.topMargin = 20 * i;
return params;
}
and then inside your loop you call it in addView(...) call:
flCatScroll.addView(listFramelayouts.get(i), getLayoutParamsFor(i));

Creating Buttons Programmatically on Android

I want to dynamically create 10 Buttons with Margin between each Button but things I tried won't work.
Here is the code I'm using:
//Create Button
for(int i = 1; i <= 10; i++){
MarginLayoutParams params = new MarginLayoutParams(MarginLayoutParams.MATCH_PARENT, MarginLayoutParams.MATCH_PARENT);
params.setMargins(10, 0, 10, 0);
params.leftMargin = xpos;
params.topMargin = ypos;
params.width = 250;
params.height = 150;
Button btn = new Button(this);
btn.setId(i);
final int _id = btn.getId();
btn.setLayoutParams(params);
btn.setText("Button " + _id);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(v.getContext(), "Button clicked index =" + _id, Toast.LENGTH_LONG).show();
//Intent einauslagern = new Intent(v.getContext(), JockeyEinauslagern.class);
//startActivityForResult(einauslagern, 0);
}
});
xpos += 20;
ypos += 50;
this.addContentView(btn, params);
}
You might want to get a container in your Activity class like this. (let it be LinearLayout for example).
Your XML -
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:orienation="vertical"
/>
Your Java -
LinearLayout container = (LinearLayout) findViewById(R.id.container);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
//set your margins here
for (int i = 0; i < 10; i++) {
Button button = new Button(this);
// some stuff
container.addView(button, llp);
}
With minor modifications this should work mostly fine.
Try this
for (int i = 0; i < count; i++) {
// creates button
final Button btn = new Button(this);
btn.setLayoutParams(new ViewGroup.LayoutParams(
250,150));
btn.setPadding(0, 8, 0, 8); //or set margin if u need
btn.setTag(i);
yourContainserView.addView(channelBtn, i);
}

Dynamic textview add don't work properly in android

I have a method that adds a textview dynamically in a linearLayout everytime I click on a button, it works good in this case, but in some other cases i wanted to call it progmmatically without clicking on a button but it doesn't work.BUT when I click on the button again, the textviews appear.
This method is contained in a fragment by the way.
Here is my code :
private void add_part() {
int width = bar.getWidth();
int child_count = ((ViewGroup) bar).getChildCount();
if (child_count == 0) {
minus_btn.setEnabled(true);
LayoutParams lparams = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
TextView part = new TextView(getActivity());
part.setLayoutParams(lparams);
part.setGravity(Gravity.CENTER);
bar.addView(part);
} else if (child_count == 1) {
minus_btn.setEnabled(true);
View nextChild = ((ViewGroup) bar).getChildAt(0);
LayoutParams newparams = new LayoutParams(width / 2,
LayoutParams.FILL_PARENT);
nextChild.setLayoutParams(newparams);
LayoutParams lparams = new LayoutParams(width / 2,
LayoutParams.FILL_PARENT);
TextView part = new TextView(getActivity());
part.setLayoutParams(lparams);
bar.addView(part);
} else if (child_count > 1 && child_count < 10) {
minus_btn.setEnabled(true);
for (int i = 0; i < child_count; ++i) {
View nextChild = ((ViewGroup) bar).getChildAt(i);
LayoutParams newparams = new LayoutParams(width
/ (child_count + 1), LayoutParams.FILL_PARENT);
nextChild.setLayoutParams(newparams);
}
LayoutParams lparams = new LayoutParams(width / (child_count + 1),
LayoutParams.FILL_PARENT);
TextView part = new TextView(getActivity());
part.setLayoutParams(lparams);
bar.addView(part);
}
}
and I called it this ways :
add_part();
or
plus_btn.performClick();

how to calculate button width before addView

I adding buttons into Horizonatal linearlayout programally, and I want know this button width before layout.
It's because if buttons's total width over LinearLayout width, App create new LinearLayout below old LinearLayout.
button has 1byte char and multibyte char.
therefore,case of button width when button has only 1byte char, case of button width when button has only multibyte, button width is difference.
public void draw() {
         width = 320;//screen width
Button btn;
int i;
int twidth = 0;
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lparams.setMargins(0, 5, 0, 5);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
buttonParams.setMargins(5, 0, 5, 0);
for (i = -1; +i < size(); i++) {
LinearLayout layout2 = new LinearLayout(context);
layout2.setOrientation(LinearLayout.HORIZONTAL);
addView(layout2, lparams);
twidth = 0;
while (twidth < width && i + 1 < size()) {
btn = new Button(context);
btn.setText(element.get(i + 1).tagName);
btn.setTextColor(Color.BLACK);
btn.setBackgroundColor(Color.WHITE);
btn.setMaxLines(1);
btn.setLayoutParams(buttonParams);
twidth += btn.getTextSize() * btn.length() + 10;
if (twidth >= width) {
i--;
break;
} else {
btn.setPadding(5, 0, 5, 0);
btn.setClickable(true);
btn.setId(element.get(i + 1).tagId);
layout2.addView(btn, buttonParams);
i++;
}
}
}
}
is there solution?

Categories

Resources