
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
How to create a gridView layout in an Android app?
This example demonstrates how do I gridView layout in an android app.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2"/> </RelativeLayout>
Step 3 – Open build.gradle(Module: app) and add the following dependency −
implementation 'com.android.support:gridlayout-v7:28.0.0'
Step 4 − Add the following code to src/MainActivity.java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { GridView gridView; String[] numberInWords = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"}; int[] numberImage = {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four, R.drawable.five,R.drawable.six,R.drawable.seven,R.drawable.eight,R.drawable.nine, R.drawable.ten}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = findViewById(R.id.gridView); MainAdapter mainAdapter = new MainAdapter(MainActivity.this, numberInWords,numberImage); gridView.setAdapter(mainAdapter); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), "You CLicked " + numberInWords[+position], Toast.LENGTH_SHORT).show(); } }); } }
Step 5 – Add the following code to MainAdapter.java
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; class MainAdapter extends BaseAdapter { private Context context; private LayoutInflater layoutInflater; private String[] numbersInWords; private int[] numberImage; private ImageView imageView; private TextView textView; public MainAdapter(Context c, String[] numbersInWords,int[] numberImage){ context = c; this.numberImage = numberImage; this.numbersInWords = numbersInWords; } @Override public int getCount() { return numbersInWords.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (layoutInflater==null) { layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } if (convertView==null){ convertView = layoutInflater.inflate(R.layout.rowitem, null); } imageView = convertView.findViewById(R.id.imageView); textView = convertView.findViewById(R.id.textView); imageView.setImageResource(numberImage[position]); textView.setText(numbersInWords[position]); return convertView; } }
Step 6 – Create a new layout resource file(rowItem) and add the following code in rowitem.xml
<?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:orientation="vertical" android:padding="24sp" android:gravity="center"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/imageView" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" android:text="Numbers" android:textSize="24sp" android:layout_marginTop="8dp" /> </LinearLayout>
Step 7 - Add the following code to androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –
Click here to download the project code.
- Related Articles
- How to create GridView Layout in an Android App using Kotlin?
- How to Create a Tab Layout in Android App?
- How to create Tab Layout in an Android App using Kotlin?
- How to create CollectionView Layout in an iOS App?
- How to create Tab Bar Layout in an iOS App?
- How to create TextToSpeech in an android app?
- How to create a multilevel ListView in an Android app?
- How to create a custom alert dialogs in an android app?
- How to create a WebView in an Android App using Kotlin?
- How to create a WebView in android app?
- How to create android app in facebook?
- How do I create a transparent Activity in an Android App?
- How to create animation using XML file in an Android App?
- How to create custom Push Notification for an Android App?
- How to create a scrollable textView Android app?
