0

I want to add buttons dynamically according to diffrent screen sizes. How can I achive it.

This is what I have tried. But when I change the first button from WRAP_CONTENT to static height, width none of the button is shown on the screen

public void setButtons(){
    // button one params
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(380,433,10,20);

    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
    params1.setMargins(229,-45,50,20);

    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
    params2.setMargins(239,60,10,20);

    LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
    params3.setMargins(422,-45,10,20);

    LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
    params4.setMargins(592,-455,10,20);

    LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
    params5.setMargins(592,65,10,20);

    //   LinearLayout.LayoutParams params6 = new LinearLayout.LayoutParams(76,76);
    //     params6.setMargins(592,-275,10,20);

    LinearLayout layout = new LinearLayout(getApplicationContext());

    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setLayoutParams(params);
    Button button1 = new Button(getApplicationContext());
    button1.setLayoutParams(params);
    // button1.setText("like");
    button1.setBackgroundDrawable(getResources().getDrawable(R.drawable.like));
    // button1.setBackgroundColor(Color.TRANSPARENT);

    Button button2 = new Button(getApplicationContext());
    button2.setLayoutParams(params1);
    //    button2.setText("share");
    button2.setBackgroundDrawable(getResources().getDrawable(R.drawable.share));
    //      button2.setBackgroundColor(Color.TRANSPARENT);

    Button button3 = new Button(getApplicationContext());
    button3.setLayoutParams(params2);
    //    button3.setText("mute");
    button3.setBackgroundDrawable(getResources().getDrawable(R.drawable.mute));
    //      button3.setBackgroundColor(Color.TRANSPARENT);

    Button button4 = new Button(getApplicationContext());
    button4.setLayoutParams(params3);
 button4.setBackgroundDrawable(getResources().getDrawable(R.drawable.download));


    Button button5 = new Button(getApplicationContext());
    button5.setLayoutParams(params4);
    button5.setBackgroundColor(Color.TRANSPARENT);


    Button button6 = new Button(getApplicationContext());
    button6.setLayoutParams(params5);
    button6.setBackgroundColor(Color.TRANSPARENT);


    layout.addView(button1);
    layout.addView(button2);
    layout.addView(button3);
    layout.addView(button4);
    layout.addView(button5);
    layout.addView(button6);

    buttonLayout.addView(layout);

}

 originalWidth = 1100;
    originalHeight = 2050;

    DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
    width = displayMetrics.widthPixels;
    height = displayMetrics.heightPixels;


}


 int getMyNewX(int originalX)
{
    //originalX : x for line segment
    return width/originalWidth * originalX;
}
int getMyNewY(int originalY)
{
    //originalY : y for line segment
    return height/originalHeight * originalY;
}
Arul Harsh
  • 133
  • 9

4 Answers4

0

You can add weight dynamically this way

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
0

In Android studio

res -> values -> dimens.xml 

enter image description here

declare your dimensions for devices below w820dp in dimens.xml file

enter image description here

dimension for devices w820dp and above in dimens.xml(w820dp) file

enter image description here

then use it in your code like

enter image description here

Android will automatically select required sizes for different resolutions

**w820dp - for screens with more than 820dp of available width. This
         would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).**

this may also help you

Community
  • 1
  • 1
CLIFFORD P Y
  • 16,974
  • 6
  • 30
  • 45
0

There are two solution .

Solution 1:convert dp to pixel

private int convertPxToDp(int px){
return Math.round(px/(Resources.getSystem().getDisplayMetrics().xdpi /DisplayMetrics.DENSITY_DEFAULT));
}

Now you have dynamic value of pixel for all screen size.

Solution 2:Add weight to all your button

If it is vertical orientation then

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, 0);

else

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( 0, LayoutParams.WRAP_CONTENT);

And then

params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
0

You need to use layoutparams with weight property to fix width equally. Find below code.

private void initViews() {
    lytButtons = (LinearLayout) findViewById(R.id.lytButtons);
    btnAddButtons = (Button) findViewById(R.id.btnAddButtons);

    btnAddButtons.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            addButtons();
        }
    });
}

protected void addButtons() {
    try {
        Button btn1 = createButton("Btn 1");
        Button btn2 = createButton("Btn 2");
        Button btn3 = createButton("Btn 3");
        Button btn4 = createButton("Btn 4");
        Button btn5 = createButton("Btn 5");
        Button btn6 = createButton("Btn 6");


        lytButtons.addView(btn1);
        lytButtons.addView(btn2);
        lytButtons.addView(btn3);
        lytButtons.addView(btn4);
        lytButtons.addView(btn5);
        lytButtons.addView(btn6);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Button createButton(String text) {

    Button btn = new Button(getActivity());
    btn.setText(text);
    LinearLayout.LayoutParams prm1 = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    prm1.weight = 1.0f;
    btn.setLayoutParams(prm1);

    return btn;
}
Sivalingaraja
  • 91
  • 1
  • 2
  • 9