DavidNg
DavidNg

Reputation: 2836

Android: Align many buttons nicely

I have a RelativeLayout with 12 buttons, I want to align them nicely in the center of the screen and with equal distance to each others and equal left and right margins (like calculator buttons or telephone buttons). What I have done is in this XML file and the result in the pic, but it is still not perfect. Do you guys have any better solution for this?

enter image description here

 <RelativeLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabview1">
    <TextView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="www.fasttest.me" 
        android:textColor="@android:color/white"/>

    <Button
        android:id="@+id/ql0"
        android:layout_width="@dimen/qlbutton_width"
        android:layout_height="@dimen/qlbutton_height"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="10dp"
        android:text="b0" />
   <Button
        android:id="@+id/ql1"
        android:layout_width="@dimen/qlbutton_width"
        android:layout_height="@dimen/qlbutton_height"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="80dp"
        android:text="b1" />
   <Button
        android:id="@+id/ql2"
        android:layout_width="@dimen/qlbutton_width"
        android:layout_height="@dimen/qlbutton_height"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="150dp"
        android:text="b2" />
    <Button
        android:id="@+id/ql3"
        android:layout_width="@dimen/qlbutton_width"
        android:layout_height="@dimen/qlbutton_height"
        android:layout_marginTop="100dp"
       android:layout_marginLeft="220dp"
        android:text="b3" />
    <Button
        android:id="@+id/ql4"
        android:layout_width="@dimen/qlbutton_width"
        android:layout_height="@dimen/qlbutton_height"
        android:layout_marginTop="180dp"
        android:layout_marginLeft="10dp"
        android:text="b4" />
   <Button
        android:id="@+id/ql5"
        android:layout_width="@dimen/qlbutton_width"
        android:layout_height="@dimen/qlbutton_height"
        android:layout_marginTop="180dp"
        android:layout_marginLeft="80dp"
        android:text="b5" />
   <Button
        android:id="@+id/ql6"
         android:layout_width="@dimen/qlbutton_width"
        android:layout_height="@dimen/qlbutton_height"
        android:layout_marginTop="180dp"
        android:layout_marginLeft="150dp"
        android:text="b6" />
    <Button
        android:id="@+id/ql7"
        android:layout_width="@dimen/qlbutton_width"
        android:layout_height="@dimen/qlbutton_height"
        android:layout_marginTop="180dp"
        android:layout_marginLeft="220dp"
        android:text="b7" />
   <Button
        android:id="@+id/ql8"
        android:layout_width="@dimen/qlbutton_width"
        android:layout_height="@dimen/qlbutton_height"
        android:layout_marginTop="260dp"
       android:layout_marginLeft="10dp"
        android:text="b8" />
   <Button
        android:id="@+id/ql9"
        android:layout_width="@dimen/qlbutton_width"
        android:layout_height="@dimen/qlbutton_height"
        android:layout_marginTop="260dp"
        android:layout_marginLeft="80dp"
        android:text="b9" />
   <Button
        android:id="@+id/ql10"
        android:layout_width="@dimen/qlbutton_width"
        android:layout_height="@dimen/qlbutton_height"
        android:layout_marginTop="260dp"
       android:layout_marginLeft="150dp"
        android:text="b10" />
    <Button
        android:id="@+id/ql11"
        android:layout_width="@dimen/qlbutton_width"
        android:layout_height="@dimen/qlbutton_height"
        android:layout_marginTop="260dp"
       android:layout_marginLeft="2dp"
        android:text="b11" />
   </RelativeLayout>

Upvotes: 0

Views: 1014

Answers (3)

DavidNg
DavidNg

Reputation: 2836

I finally get it done with a gridview of buttons. Here is the code:

public class MainActivity extends Activity {
    public String[] filenames ={"B1","B2","B3","B4","B5","B6"};        
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);           
        GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new ButtonAdapter(this));  
        gridView.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, 
            View v, int position, long id){                
                Toast.makeText(getBaseContext(), 
                        "pic" + (position + 1) + " selected", 
                        Toast.LENGTH_SHORT).show();
            }
        });        
    }

    public class ButtonAdapter extends BaseAdapter {
        private Context context;    
        public ButtonAdapter(Context c){
            context = c;
        }
        public int getCount() {
            return filenames.length;
        }
        public Object getItem(int position) {
            return position;
        }     
        public long getItemId(int position) {
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent){
              Button btn;  
                 if (convertView == null) {   
                  btn = new Button(context);  
                  btn.setLayoutParams(new GridView.LayoutParams(100, 100));  
                  btn.setPadding(8, 8, 8, 8);  
                  btn.setFOcusable(false);
                  btn.setClickable(false);
                  }else {  
                  btn = (Button) convertView;  
                 }               
                 btn.setText(filenames[position]);    
                 btn.setTextColor(Color.WHITE);  
                 btn.setId(position);       
                 return btn; 
        }
    }    
}

Upvotes: 0

blessanm86
blessanm86

Reputation: 31789

You can use this custom GridLayout from Romain Guy. Specifiy the number of rows and columns. And the screen will be divided into equally sized cells.

Here is the link

Upvotes: 1

dinigo
dinigo

Reputation: 7438

I suggest you use buttons inside a GridView. Give each button the same weigh so it stretches itself:

<Button
    android:id="@+id/ql3"
    android:layout_height="@dime/button_height"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:padding="@dimen/buttons_padding"
    android:text="b3" />

Upvotes: 3

Related Questions