0

I Have a set button on a Linear Layout, and I set the button configuration programmatically.

buttonEdit = new ArrayList<ButtonData>();
buttonEdit.add(new ButtonData(1, "one", R.drawable.1));
buttonEdit.add(new ButtonData(2, "two", R.drawable.2));
buttonEdit.add(new ButtonData(3, "three", R.drawable.3));
buttonEdit.add(new ButtonData(4, "four", R.drawable.4));
buttonEdit.add(new ButtonData(5, "five", R.drawable.5));

LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
for (ButtonData button : buttonEdit) {
  Button btn = new Button(getApplicationContext());
  btn.setId(button.getButtonID());
  btn.setBackgroundResource(button.getBackgroundResource());
  btn.setOnClickListener(buttonOnclickListener);
  btn.setHeight(56);
  btn.setWidth(48);

  LinearLayout ll = new LinearLayout(getApplicationContext());
  ll.setLayoutParams(lp);
  ll.setOrientation(LinearLayout.VERTICAL);
  ll.setPadding(10, 0, 10, 0);

  btn.setGravity(Gravity.CENTER_HORIZONTAL);
  TextView textLabel = new TextView(getApplicationContext());
  textLabel.setText(button.getButtonName());
  ll.addView(btn);
  ll.addView(textLabel);

  linearLayoutData.put(String.valueOf(button.getButtonID()), ll);
  buttonObject.put(String.valueOf(button.getButtonID()), btn);
}

In list of code, I set a padding left and right 10. So the user can see a separate space between the buttons. So the problems is, when I install the apps in Multiple Resolutions (QVGA, HVGA, WVGA) it is spaced differently. In QVGA it's wider and in WVGA it's narrower. What will I do?

eldarerathis
  • 35,455
  • 10
  • 90
  • 93
Fendy Kusuma
  • 672
  • 7
  • 16

1 Answers1

0

I would recommend you to use layout defined in xml to determine how the objects will look on the screen. When you define layouts in the xml you can use the logical unit of dp which is spacing according to the resolution of the screen. Like that (imagine you define that in file called button_layout.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="4dp">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    android:textSize="16.0sp" />

    <Button android:id="@+id/my_button"
        android:layout_width="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"/>
</LinearLayout>

Note that the dp and dip measurement units are synonyms.

Then if you need to add one more button with together with all these elements programmatically you basically do something like:

LayoutInflater inflater;
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (TableLayout) inflater.inflate(R.id.button_layout, null);
linearLayout.findViewById(R.id.line_id).setText("One"); // Or two or three as you wish
main_activity_layout.addView(linearLayout); //adding yet another button

This comes only as illustration how you use the layout. You can actually define the layout with the properties you need and also set programatically all case-by case specific properties. The most important thing is that: - You get easier to understand layout - You specify the margins and paddings etc in relative units of dp.

EDIT: If you really insist on this being done in code, here is how you specify dp programatically. Still I would recommend using the layout solution as much as possible.

Community
  • 1
  • 1
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • hi @BorisStrandjev, Thanks in suggestion of use layout defined in xml. but my apps is just quite dynamic, so there I dont use the xml. the problems is a different padding measure in multiple resolution. how can i state dp/dip measurement programmatically... – Fendy Kusuma Jan 26 '12 at 10:02