In Android,
In this tutorial, we will show you 2 common
1.1 Android Layout file – res/layout/main.xml
1.2 Activity
1.3 Demo
2.1 Two Android Layout files
File – res/layout/main.xml
File – res/layout/mobile.xml
2.2 Custom Adapter
2.3 Activity
2.4 Demo
GridView
let you arranges components in a two-dimensional scrolling grid. For detail attribute exaplanation, see GridView reference.In this tutorial, we will show you 2 common
GridView
examples :- Normal way, just display text in
GridView
layout. - Create a custom adapter to display image and text in
GridView
layout.
1. Normal GridView example
Display characters from A to Z inGridView
layout. Quite simple, it should be sef-explanatory.1.1 Android Layout file – res/layout/main.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="50dp" android:stretchMode="columnWidth" android:layout_width="fill_parent" android:layout_height="fill_parent" > </GridView>
package com.mkyong.android; import android.app.Activity; import android.os.Bundle; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.GridView; import android.widget.TextView; import android.widget.Toast; import android.view.View; import android.widget.AdapterView.OnItemClickListener; public class GridViewActivity extends Activity { GridView gridView; static final String[] numbers = new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gridView = (GridView) findViewById(R.id.gridView1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, numbers); gridView.setAdapter(adapter); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); } }); } }
2. Custom Adapter example
In this example, extend aBaseAdapter
to display a group of image and text in GridView
layout. 2.1 Two Android Layout files
File – res/layout/main.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>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" > <ImageView android:id="@+id/grid_item_image" android:layout_width="50px" android:layout_height="50px" android:layout_marginRight="10px" android:src="@drawable/android_logo" > </ImageView> <TextView android:id="@+id/grid_item_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/label" android:layout_marginTop="5px" android:textSize="15px" > </TextView> </LinearLayout>
package com.mkyong.android.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.mkyong.android.R; public class ImageAdapter extends BaseAdapter { private Context context; private final String[] mobileValues; public ImageAdapter(Context context, String[] mobileValues) { this.context = context; this.mobileValues = mobileValues; } 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.mobile, null); // set value into textview TextView textView = (TextView) gridView .findViewById(R.id.grid_item_label); textView.setText(mobileValues[position]); // set image based on selected text ImageView imageView = (ImageView) gridView .findViewById(R.id.grid_item_image); String mobile = mobileValues[position]; if (mobile.equals("Windows")) { imageView.setImageResource(R.drawable.windows_logo); } else if (mobile.equals("iOS")) { imageView.setImageResource(R.drawable.ios_logo); } else if (mobile.equals("Blackberry")) { imageView.setImageResource(R.drawable.blackberry_logo); } else { imageView.setImageResource(R.drawable.android_logo); } } else { gridView = (View) convertView; } return gridView; } @Override public int getCount() { return mobileValues.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } }
package com.mkyong.android; import com.mkyong.android.adapter.ImageAdapter; import android.app.Activity; import android.os.Bundle; import android.widget.AdapterView; import android.widget.GridView; import android.widget.TextView; import android.widget.Toast; import android.view.View; import android.widget.AdapterView.OnItemClickListener; public class GridViewActivity extends Activity { GridView gridView; static final String[] MOBILE_OS = new String[] { "Android", "iOS","Windows", "Blackberry" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gridView = (GridView) findViewById(R.id.gridView1); gridView.setAdapter(new ImageAdapter(this, MOBILE_OS)); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText( getApplicationContext(), ((TextView) v.findViewById(R.id.grid_item_label)) .getText(), Toast.LENGTH_SHORT).show(); } }); } }
Download Source Code
Download it – Android-GridView-Example.zip (21 KB)
chinal ke rand ke suwar ki awlad kisi aur ki article chori karta hey , you will never come up in life , you will never get success in blogging
ReplyDeletei appreciate ua work but its a copyrighted (mkyoung ) content sooo come up wid smthn creative ideas.....
ReplyDeletei persnly follow em sooo plzzzz......
btw all d best broooooo :)
Ha ha - MKYONG !!!!! Naughty !!!!
ReplyDeleteAndroid Tutorial Basic to Advance: Click Here
ReplyDeleteAndroid User Interface Tutorial: Click Here
I like your theme of choice for web journal however need to recommend you for sharing some more data with respect to your subject so we can comprehend your idea all the more unmistakably.. Business Blog Updates
ReplyDeleteThank you intended for Pleasant and Educational Publish. This informative article is really includes much bigger information about This kind of Topic.CRM Nimble
ReplyDeleteYou look numerous other modules have mass SMS sending highlights however in our most exceptional WP-WOO SMS module you can send up-to numerous messages at any cell telephone arrange at whatever time.sms plugin
ReplyDeletedeveloprobots
ReplyDeleteThanks for sharing this information and keep updating us. This is informatics and really useful to me.
ReplyDeleteBest Industrial Training in Noida
Best Industrial Training in Noida
Everythings Works Perfect!
ReplyDeleteBest BCA Colleges in Noida
nike air max 270
ReplyDeletegolden goose sneakers
timberlands
moncler jackets
coach handbags
louboutin shoes
air max 90
moncler outlet
hermes online
balenciaga trainers
mon site Web Dolabuy YSL index réplique louis vuitton répliques de sacs allez vers ces gars sacs à dos de créateurs de répliques
ReplyDeletehttps://bayanlarsitesi.com/
ReplyDeleteEskişehir
Erzincan
Ardahan
Erzurum
4512
izmir
ReplyDeleteErzurum
Diyarbakır
Tekirdağ
Ankara
8TZW6İ
whatsapp görüntülü show
ReplyDeleteücretli.show
DPJ2G
Bolu Lojistik
ReplyDeleteMardin Lojistik
Kocaeli Lojistik
Diyarbakır Lojistik
İstanbul Lojistik
8MCO
62E6F
ReplyDeleteZonguldak Parça Eşya Taşıma
Probit Güvenilir mi
Çerkezköy Çilingir
Bursa Şehir İçi Nakliyat
Manisa Parça Eşya Taşıma
Silivri Duşa Kabin Tamiri
Çerkezköy Marangoz
Kırıkkale Parça Eşya Taşıma
Siirt Lojistik
17174
ReplyDeletebuy testosterone enanthate
boldenone for sale
order testosterone propionat
buy sustanon
trenbolone enanthate
winstrol stanozolol for sale
https://steroidsbuy.net/steroids/
buy pharmacy steroids
buy fat burner
58A60
ReplyDeletebinance indirim
1FB5E
ReplyDeleteçorum sesli sohbet odası
Sinop Bedava Sohbet Odaları
Adana Canlı Sohbet Uygulamaları
mobil sohbet
urfa ücretsiz sohbet odaları
tekirdağ görüntülü sohbet canlı
Uşak Canlı Sohbet Siteleri Ücretsiz
Adana Mobil Sohbet Odaları
bartın rastgele görüntülü sohbet
179DD
ReplyDeleteKwai Takipçi Satın Al
Vector Coin Hangi Borsada
Coin Çıkarma
Görüntülü Sohbet Parasız
Shibanomi Coin Hangi Borsada
Linkedin Takipçi Hilesi
Periscope Takipçi Hilesi
Kripto Para Üretme Siteleri
Binance Ne Zaman Kuruldu
6BE72
ReplyDeleteOnlyfans Takipçi Satın Al
Qlc Coin Hangi Borsada
Dxgm Coin Hangi Borsada
Yeni Çıkacak Coin Nasıl Alınır
Bitcoin Kazanma
Bitcoin Giriş Nasıl Yapılır
Anc Coin Hangi Borsada
Paribu Borsası Güvenilir mi
Arg Coin Hangi Borsada
43A80
ReplyDeletebitbox
ledger live
layerzero
defillama
metamask
quickswap
ellipal
dextools
trezor suite
شركة مكافحة حشرات بالدمام PeczVSe2Sh
ReplyDelete