MAXGEN
MAXGEN

Reputation: 765

Android fitting all buttons onto screen

I have a row of buttons but there going outside of the width of the screen. I tried making the buttons smaller but it not having a effect to it. Its a row of 11 circle buttons. What can I do? Eventually I would like to make an grid of 11 by 11, any suggestions on how to do this better then one huge layout xml?

main.xml

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

<TableLayout
    android:id="@+id/tableLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" >

        <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:id="@+id/button1" android:layout_column="1" 
                    android:background = "@drawable/roundedbutton"/>
                  <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:id="@+id/button1" android:layout_column="1" 
                    android:background = "@drawable/roundedbutton"/>
                   <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:id="@+id/button1" android:layout_column="1" 
                    android:background = "@drawable/roundedbutton"/>
                    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:id="@+id/button1" android:layout_column="1" 
                    android:background = "@drawable/roundedbutton"/>
                     <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:id="@+id/button1" android:layout_column="1" 
                    android:background = "@drawable/roundedbutton"/>
                      <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:id="@+id/button1" android:layout_column="1" 
                    android:background = "@drawable/roundedbutton"/>
                       <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:id="@+id/button1" android:layout_column="1" 
                    android:background = "@drawable/roundedbutton"/>
                        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:id="@+id/button1" android:layout_column="1" 
                    android:background = "@drawable/roundedbutton"/>
                         <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:id="@+id/button1" android:layout_column="1" 
                    android:background = "@drawable/roundedbutton"/>
                          <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:id="@+id/button1" android:layout_column="1" 
                    android:background = "@drawable/roundedbutton"/>
                           <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:id="@+id/button1" android:layout_column="1" 
                    android:background = "@drawable/roundedbutton"/>

        </TableRow>


    </TableLayout>

</LinearLayout>

roundedbutton.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#9F2200"/>
    <stroke android:width="2sp" android:color="#fff" />
        <size 
       android:width="5dp"
        android:height="5dp"/>
</shape>

Added request to the answer

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;
    Log.d(TAG, "Position before convertView " + position );
    if (convertView == null) 
        {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.item, null);

            // set value into textview
            Button btn= (Button) gridView.findViewById(R.id.grid_item_label);

            if (position == 3)
            {
                Log.d(TAG, "Position inside convertView " + position );


                btn.setText("G"); 
                btn.setBackgroundColor(Color.parseColor("#2E3135"));
                    //btn.setBackgroundColor(R.drawable.goodguybutton);
            }
            else 
            {
                btn.setText(buttonTextValues[position]);
            }

        } 
    else 
        {
            gridView = (View) convertView;
        }




    return gridView;
}

Upvotes: 0

Views: 376

Answers (2)

simekadam
simekadam

Reputation: 7384

You can just wrap all your buttons into a horizontaly aligned linear layout like this.

<LinearLayout 
    android:layout_width="0dp"
    android:layout_height="match_parent">    
    <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_weight="1" 
      android:text="O"
      android:id="@+id/button1" 
      android:background="@drawable/roundedbutton"/>
    <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_weight="1" 
      android:text="O"
      android:id="@+id/button1" 
      android:background="@drawable/roundedbutton"/>

</LinearLayout>

I spotted that your question has actually two parts..You can just manually create the grid with two nested for loops..

Upvotes: 1

Matej Špil&#225;r
Matej Špil&#225;r

Reputation: 2687

gridView.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</GridView>

item.xml

<Button 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="O"
android:id="@+id/grid_item_label"
android:layout_column="1" 
android:background = "@drawable/roundedbutton"/>

custom button adapter class ... you can use it as inner class

public class ButtonAdapter extends BaseAdapter {
        private Context context;
        private final String[] buttonTextValues;

    public ButtonAdapter(Context context, String[] buttonTextValues) {
        this.context = context;
        this.buttonTextValues = textViewValues;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.item, null);

            // set value into textview
            Button btn= (Button) gridView
                    .findViewById(R.id.grid_item_label);
            btn.setText(buttonTextValues[position]);  
        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

    @Override
    public int getCount() {
        return buttonTextValues.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

}

activity:

public class ButtonGridView extends Activity {

        GridView gv;
        String[] buttonText = {"1", "2"}; //and so on to 11*11

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.gridView);
            gv =(GridView)findViewById(R.id.gridView1);
            gv.setAdapter(new ButtonAdapter(this,buttonText));
        }
    }

also note that you can pass whatever information as you want to adapter with constructor and decide that button will have what color text or whatever you want ...

Upvotes: 1

Related Questions