0

I am making the layout for my activity and got puzzelled , I am thinking about making the flexible layout in which my views get width and height according to screen. Please read the case below

What I want: This is the trickiest and hardest part for me.

  1. I need to put the 37 buttons on the screen , such that the each row gets the 3 button and then shift to new row. nevertheless the last row get the single button or two buttons. Well in the case of 3 button in each row yields only single button in last row but that's okay
  2. Each button in each row should have different id , having different picture on background and opening different activity with different intent extras.
  3. Buttons in row must have same sizes so that it should look good.

So these three points are giving me tough time. Also the 2nd point in these point is much much more important.

Please tell me How Can I achieve this I have read about grid layout , Grid view , list view , table layout and also I have used them many time. but I do not know how to use them for this specific purpose.

Note: The buttons in the row should get the same width and height according to screen and the layout should get fit on all devices so we should avoid hard coded values.

MasterCX
  • 13
  • 4
Allay Khalil
  • 674
  • 3
  • 11
  • 31

3 Answers3

1
  1. Simply use grid view. Set Max column to 3 and

  2. Use a custom adapter extending Base adapter to set backgrounds and return different ids on item click listener.

  3. Set layout param for inflated item view dynamically using screen size.

Ishan
  • 1,172
  • 10
  • 25
0

I think you should see these links , link 1 and link2. The gridview is best option is for you as these layouts adjust the size and you can easily Handle them in their adapter.

Community
  • 1
  • 1
Coas Mckey
  • 701
  • 1
  • 13
  • 39
0

Use a Linear layout instead..with Nested concept.that is like

<LinearLayout>

   <LinearLayout>
     .
     .
   And So on..
     .
     .
     .
   </LinearLayout>

</LinearLayout>

For example

----Outer (Horizontal) layout-----
|                                |
|  ---Inner (Vertical) layout-   |
|  |       [Textview]        |   |
|  |       [Button]          |   |
|  |       [Button]          |   |
|  |       [Button]          |   |
|  ---------------------------   |
----------------------------------

Also try the example below

<LinearLayout android:layout_width="match_parent"
 android:layout_weight="1" 
 android:layout_height="wrap_content"
 android:orientation="vertical">
 <LinearLayout android:layout_height="wrap_content"
 android:layout_width="match_parent">
 <TextView android:text="One,One" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" />
 <TextView android:text="One,Two" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" 
 android:layout_weight="1" />
 <TextView android:text="One,Three" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" />
 <TextView android:text="One,Four" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" />
 </LinearLayout>
 <LinearLayout android:layout_height="wrap_content"
 android:layout_width="match_parent">
 <TextView android:text="Two,One" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" />
 <TextView android:text="Two,Two" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" 
 android:layout_weight="1" />
 <TextView android:text="Two,Three" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" />
 <TextView android:text="Two,Four" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" />
 </LinearLayout>
 <LinearLayout android:layout_height="wrap_content"
 android:layout_width="match_parent">
 <TextView android:text="Three,One" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" />
 <TextView android:text="Three,Two" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" 
 android:layout_weight="1" />
 <TextView android:text="Three,Three" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" />
 <TextView android:text="Three,Four" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" />
 </LinearLayout>
 <LinearLayout android:layout_height="wrap_content"
 android:layout_width="match_parent">
 <TextView android:text="Four,One" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" />
 <TextView android:text="Four,Two" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" 
 android:layout_weight="1" />
 <TextView android:text="Four,Three" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" />
 <TextView android:text="Four,Four" 
 android:layout_height="wrap_content"
 android:layout_width="wrap_content" />
 </LinearLayout>
</LinearLayout>

Output

enter image description here

Reference Links: Nested Linear Layout, Nested Example

Update 1 As you said, 37 buttons have to implement on the application with different id,intent extras etc.it is simple job i think. set each buttons with different id and write a onClickListener for the buttons.Use a switch/If case t\o distinguish each buttons.

Example:

public void buttonOnClick(View view)
{
 switch(view.getId())
 {
  case R.id.button1:
  // Code for button 1 click
  break;

  case R.id.button2:
  // Code for button 2 click
  break;

  case R.id.button3:
  // Code for button 3 click
  break;
 }
}

Refer this link for more info:

OnClickevent for buttons

Thanks

Community
  • 1
  • 1
Vanilla Boy
  • 258
  • 2
  • 12