activity_main.xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/grid_view" android:layout_width="300dp" android:layout_height="wrap_content" android:numColumns="3" android:verticalSpacing="2dp" android:horizontalSpacing="2dp" android:background="#ff5555" android:columnWidth="75dp" android:layout_margin="15dp" > <!--content will be generate programmatically--> </GridView> MainActivity.java code snippet
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //initializing a String array String[] array = new String[]{"one","two","three","four","five","six","seven"}; //initializing an ArrayList from array List<String> list = new ArrayList<String>(Arrays.asList(array)); //refer GridView from xml layout file GridView gd = (GridView) findViewById(R.id.grid_view); //data bind GridView with ArrayAdapter gd.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list)); } Imported Class
import android.widget.ArrayAdapter; import android.widget.GridView; import java.util.ArrayList; import java.util.Arrays; import java.util.List;
- How to convert Hex color to int color
- How to create a Button with text and image
- How to change Button text color
- How to span columns and rows in a GridLayout
- How to use ArrayAdapter with ListView
- CountDownTimer example
- How to convert string to int to string
- How to center the text horizontally in a TextView
- How to set TextView layout gravity programmatically
- Align top of one view to the top of another view in a RelativeLayout
Komentar
Posting Komentar