Overlapping buttons in android - java

In android, I am trying to add buttons programatically, but all the buttons that are added are overlapping. The code I am using is somewhat like this:
for(int i = (int) 'a'; i <= (int) 'z'; i++)
{
Button button = new Button(this);
char letter = (char)i;
String letterOnButton = Character.toString(letter);
button.setText(letterOnButton);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.dynbuttons);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rl.addView(button,lp);
}
It does not throw a button, but I only get to see the "z" button.
Any idea on how to fix this?

As mentioned above LinearLayout would be the best solution, but if u still want to use RelativeLayout, try setting an id to each button and inflate the subsequent with the parameter RIGHT_OF/BELOW..as suggested above, parameter "layout_alignLeft" will produce the same effect, i.e inflate all buttons in the same position
RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout);
int id = 0;
for (int i = (int) 'a'; i <= (int) 'z'; i++) {
Button button = new Button(this);
char letter = (char) i;
String letterOnButton = Character.toString(letter);
button.setText(letterOnButton);
button.setId(i);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, id);
rl.addView(button, lp);
id = i;
}

I would go with a LinearLayout instead of a RelativeLayout, something like this:
LinearLayout ll = (LinearLayout)findViewById(R.id.dynbuttons);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
for(int i = (int) 'a'; i <= (int) 'z'; i++) {
Button button = new Button(this);
char letter = (char)i;
String letterOnButton = Character.toString(letter);
button.setText(letterOnButton);
ll.addView(button,lp);
}
If you still want to use a RelativeLayout you have to set the parameter layout_alignLeft for each button.

Related

Android ConstraintLayout - how to set margins of chain_packed elements programmatically

This is my code:
ConstraintLayout.LayoutParams buttonParams = new ConstraintLayout.LayoutParams(40,105);
buttonParams.setMargins(8,16,24,32);
secretCodeFields = findViewById(R.id.secretCodeFields);
for (int i = 0; i < 4; i++) {
Button b = new Button(this);
vID[i]=View.generateViewId();
b.setId(vID[i]);
b.setLayoutParams(buttonParams);
secretCodeFields.addView(b);
}
set.clone(secretCodeFields);
set.createHorizontalChain(
secretCodeFields.getId(),ConstraintSet.LEFT,
secretCodeFields.getId(),ConstraintSet.RIGHT,
vID,null,ConstraintSet.CHAIN_PACKED
);
set.applyTo(secretCodeFields);
Buttons are added in the same place (so only last one is visible). The same resultat if I delete "b.setLayoutParams(buttonParams);" line and use
secretCodeFields.addView(b,buttonParams);
But if I add button with width and height params it works, but there are no more margins:
secretCodeFields.addView(b,90,90);
What I do wrong?
EDIT:
secretCodeFields is ConstraintLayout
I found solution. I added margins AFTER set.applyTo... :
for (int i = 0; i < codeLenght; i++) {
Button b = (Button)findViewById(vID[i]);
ConstraintLayout.LayoutParams p = (ConstraintLayout.LayoutParams) b.getLayoutParams();
p.setMargins(6,0,6,0);
b.setLayoutParams(p);
}
It works, but I don't know if it is the best solution.
I know this is a late answer but here it is
the ConstraintSet.CHAIN_PACKED will remove all applied margins to your views
you should first create the chain in Constraint Set then after that apply margins using the ConstrainSet connect(int startID, int startSide, int endID, int endSide, int margin) method to indicate margins.

Add Space between LinearLayouts

I'm trying to make a list of database items like the following..
for (int i=0; i < jArray.length(); i++)
{
JSONObject row = jArray.getJSONObject(i);
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,100));
Button btn = new Button(MainActivity.this);
//btn.setText(row.getString("subject"));
btn.setText(String.valueOf(i));
btn.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.8));
File imgFile = new File(file_path);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView iv = new ImageView(MainActivity.this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageBitmap(myBitmap);
iv.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.2));
ll.addView(btn);
ll.addView(iv);
LinearLayout sv = findViewById(R.id.sv_layout);
sv.addView(ll);
}
The result is like this:
But I want to have space between Linear Layouts and make Image height shorter to be equal with button height
How can I achieve this? Thank you,
Please Try this code
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
params.setMargins(0, 5, 5, 10);
ll.setLayoutParams(params);
you can add your specific margins in this line params.setMargins(0, 5, 5, 10);
Take button and image in linear layout with horizontal orientation. Set Button and Imageview height to wrap content. Now for gap between Linear Layouts give appropriate margin.
Set the margins in the layout params and pass them to your ImageView object.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.2);
int dp = getDps(8);
layoutParams.setMargins(0,dp,0,dp);
iv.setLayoutParams(layoutParams);
Function getDps():
public int getDps(int sizeInDp){
float scale = getResources().getDisplayMetrics().density;
return (int) (sizeInDp*scale + 0.5f);
}
This looks like:

Change the background of every new button

for (int j = 0; j < 2; j++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout underRow = new LinearLayout(this);
underRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for (int i = 0; i < MAX_BUTTONS; i++) {
// String buttonID = "btn" + j + i;
// int resID = getResources().getIdentifier(buttonID, "id","com.project.beacontreetech.doublecheckversion1");
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
button = (Button) getLayoutInflater().inflate(R.layout.button_layout, buttonsContainer, false);
// TextView textView = (TextView) getLayoutInflater().inflate(R.layout.text_view, buttonsContainer,false);
int id1 , id2 ;
id1 = i;
id2 = j;
StringBuilder append = new StringBuilder();
append.append(id1);
append.append(id2);
int imgArrLength = imageList.length;
for ( t=0; t < imgArrLength; t++){
}
String yo = (String.valueOf(append));
int id = Integer.valueOf(String.valueOf(append));
button.setHeight(buttonSize);
button.setWidth(buttonSize);
button.setOnClickListener(this);
button.setId(id);
if (button.getId() == 01){
button.setBackgroundResource(imageList[1]);
}
For set background image for button which is in drawable folder then use below code:
button.setBackgroundResource(R.drawable.new_todo_image);
For set color background you should use this:
button.setBackgroundColor(getResources().getColor(R.color.Green));
your color.xml look like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="red">#f00</drawable>
<drawable name="green">#0f0</drawable>
<drawable name="gray">#ccc</drawable>
</resources>
if (button.getId() == 01){
//to set background image on button
button.setBackgroundResource(R.drawable.your_image);
//or set background color use
button.setBackgroundColor(getResources().getColor(R.color.your_color));
}

How to avoid items overlap together when set position random?

on xml layout, I have a FrameLayout with no child. Then I add a list buttons programatically into this framelayout by this code below:
private void addNumbers(){ // Numbers is set with random position
for (int i = 1; i < 20; i++) {
Button btn = new Button(this);
btn.setText("" + i);
btn.setId(i);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(50, 50);
int leftMargin = new Random().nextInt(widthScreen - btnSize );
int topMargin = new Random().nextInt(heightScreen - btnSize);
lp.leftMargin = leftMargin;
lp.topMargin = topMargin;
btn.setLayoutParams(lp);
framelayout1.addView(btn);
//framelayout2.addView(btn);
}
}
And some buttons overlap together, I do not want to happen this. How to avoid this problem ? Thanks in advance.
P/s: Sorry I can not upload image from my company, all upload > 4K is reject, please tell me if the question is not clear.

Android - How to add several buttons with different layout_margins in a LinearLayout programmatically?

I am trying to building a linear layout programmatically, in which, there are 4 buttons in all, each of the button simply positioned below the previous button. Just like the picture shows below:
And as you can see from the picture above, each button has exactly the same size but they have different layout_margins, the first button has a larger value in layout_marginTop while the other 3 buttons has the same value in layout_marginTop.
Basically to build a layout like this by xml is very simple but now I have really come across difficulties in building all this only through java code. I have gone through many posts and now I can easily add 4 buttons in proper sizes but I just could not find a proper way to programmatically set the layout_margin for each of the button.
To simple add four buttons, I could do like this:
public class mainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical"
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++) {
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button " + (j + 1 + (i * 4 )));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
setContentView(layout);
//setContentView(R.layout.main);
}
}
And to programmatically set margins for each button(view), I could do like this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 0, 0);
button.setLayoutParams(params);
But here comes the problem: as long as I setLayoutParams for the 4 buttons, and then add 4 buttons into Linearlayout by "addView(button)", only the first button could be displayed in the proper size with the proper margins. All the other 3 buttons just disappeared. And With many tests I just found it seems that only one layoutParams could be allowed in a linearlayout object. As a result, as long as I set different layout params for different buttons, only the first button could be displayed. But since here the margin params for my 4 buttons are definitely different thus I think I have to use different layoutparams for different buttons.
So pls anyone tell me how could I programmatically set the margin for my each of my buttons and make them display properlly? This has already sucked my life for two days, pls help! :)
linear = (LinearLayout) rootView.findViewById(R.id.linear);
.
.
.
RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(layoutParam);
layout.setOrientation(LinearLayout.VERTICAL);
// Below will add three linear layout with 4 buttons in each
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(getActivity());
row.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
//Here is important
row.setOrientation(LinearLayout.VERTICAL);
for (int j = 0; j < 4; j++) {
Button btnTag = new Button(getActivity());
btnTag.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
btnTag.setText("Button " + (j + 1 + (i * 4)));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
linear.addView(layout);
// You can set also
// setContentView(layout)

Categories

Resources