Sai
Sai

Reputation: 49

Creating multiple(26) buttons in one layout screen

Trying to create a simple ABCs and 123s app. My initial layout has two buttons, 1 for alphabet and one for numbers. If alphabet button is clicked, I want to go to the next fragment which would hold 26 buttons for all alphabet (A-Z). For numbers, 10 buttons (0-9). MY question is, how do i efficiently populate a layout file with 26 buttons/10 buttons without writing each one of them in the XML file?

Upvotes: 0

Views: 427

Answers (2)

SaravInfern
SaravInfern

Reputation: 3388

use gridview

activity xml

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

your activity

public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      GridView gridview = (GridView) findViewById(R.id.gridview);
      gridview.setAdapter(new GridAdapter (this));
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
}

gridView adapter

public class GridAdapter extends BaseAdapter {
   private Context mContext;

   // Constructor
   public GridAdapter (Context c) {
      mContext = c;
   }

   public int getCount() {
      return alphabets.length;
   }

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

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

   // create a new TextView for each item referenced by the Adapter
   public View getView(final int position, View convertView, ViewGroup parent) {
      TextView txView;

      if (convertView == null) {
         txView= new TextView (mContext);
         txView.setLayoutParams(new GridView.LayoutParams(85, 85));
         txView.setPadding(8, 8, 8, 8);
      } 
      else 
      {
         txView= (TextView) convertView;
      }
      txView.setText(alphabets[position]);

       **Update**
       txView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
            //your code here
               switch(position){
                case 0:
                    Toast.makeText(this,"A",Toast.LENGTH_LONG).show();
                break;
                case 1:
                    Toast.makeText(this,"B",Toast.LENGTH_LONG).show();
                break;
                ........SoOn
               }

           }});


      return txView;
   }

   //  array
   public String[] alphabets= {
      "A","B","C","D"
   };
}

Upvotes: 0

RestingRobot
RestingRobot

Reputation: 2978

You can programmatically add buttons to a view. Try this in your onCreateView function of you fragment.

//mView is your fragment root, and container is defined in XML
LinearLayout container = (LinearLayout)mView.findViewById(R.id.alpha_container)

String[] letters = {A, B, C, ..., Z};

for(int i=0; i<letters.length; i++) { 
   Button letter_btn = new Button(getActivity());
   letter.setText(letters[i]);
   container.addView(letter_btn, i);
}

This will programmatically create a button for each element in the letters array. You can attach onClickListener(s) accordingly and track the buttons by text content or give them a tag/id.

Upvotes: 1

Related Questions