Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can set the height of each LinearLayout of TextView that you inflate in your GridView's adapter to screenHeight/3.</p> <p>In getView after inflating the textView's xml, cast the inflated view to LinearLayout and set its height as:</p> <pre><code>LayoutInflater li = getLayoutInflater(); LinearLayout ll = (LinearLayout) li.inflate(R.layout.menu_items, null); int screenHeight = ((Activity) context).getWindowManager() .getDefaultDisplay().getHeight(); ll.setMinimumHeight(screenHeight/3); </code></pre> <p><strong>Edit</strong></p> <p>activity_main.xml</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"&gt; </code></pre> <p> </p> <p>text_item.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/grid_item_label" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" tools:ignore="InefficientWeight" android:padding="0dp" android:textSize="12sp" &gt; &lt;/TextView&gt; &lt;/LinearLayout&gt; </code></pre> <p>MainActivity.java</p> <pre><code>public class MainActivity extends Activity { String[] str = { "1", "2", "3", "1", "2", "3", "1", "2", "3" , "1", "2", "3", "1", "2", "3", "1", "2", "3" , "1", "2", "3", "1", "2", "3", "1", "2", "3" , "1", "2", "3", "1", "2", "3", "1", "2", "3" , "1", "2", "3", "1", "2", "3", "1", "2", "3" , "1", "2", "3", "1", "2", "3", "1", "2", "3" , "1", "2", "3", "1", "2", "3", "1", "2", "3" , "1", "2", "3", "1", "2", "3", "1", "2", "3" , "1", "2", "3", "1", "2", "3", "1", "2", "3" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); GridView gridView = (GridView) findViewById(R.id.gridView1); gridView.setAdapter(new myGridAdapter(this, str)); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) { } }); } public class myGridAdapter extends BaseAdapter { private Context context; private String[] mobileValues; public myGridAdapter(MainActivity mainActivity, String[] arrayEmpty) { this.context = mainActivity; this.mobileValues = arrayEmpty; } public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout gridView; if (convertView == null) { // get layout from text_item.xml gridView = (LinearLayout)inflater.inflate(R.layout.text_item, null); int screenHeight = ((Activity) context).getWindowManager() .getDefaultDisplay().getHeight(); gridView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, screenHeight/9)); //gridView.setMinimumHeight(screenHeight/9); // set value into textview TextView textView = (TextView) gridView .findViewById(R.id.grid_item_label); textView.setText(mobileValues[position]); } else { gridView = (LinearLayout) convertView; } return gridView; } @Override public int getCount() { // TODO Auto-generated method stub return mobileValues.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } } } </code></pre> <p>In the getView what i've done is i've inflated the text_item.xml for 9x9 times, in which i've dynamically set the layout height of text_item to screenHeight/9. Let me know if it work for you or not.</p> <p><strong>Edit 2</strong></p> <p>activity_main.xml</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;GridView android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:numColumns="9" android:padding="0dp" android:background="#000000" android:horizontalSpacing="3dp" android:verticalSpacing="3dp" android:stretchMode="columnWidth" &gt; &lt;/GridView&gt; &lt;/LinearLayout&gt; </code></pre> <p>text_item.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:background="#FFFFFF" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/grid_item_label" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" tools:ignore="InefficientWeight" android:padding="0dp" android:textSize="12sp" &gt; &lt;/TextView&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload