How can I create like this?
I can't create this.
How can I create like this?
I can't create this.
You can achieve that with putting that in your MainActivity.java
(I made some Strings
as data for saving time):
package com.example.myapplication;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
GridView gridView;
String [] yourData = {"Data1","Data2", "Data3", "Data4", "Data 5", "Data6","Data7", "Data8", "Data9", "Data10", "Data11", "Data12"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.GridView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item, yourData);
gridView.setAdapter(adapter);
}
}
Now you define the GridView
in your activity_main.xml
layout:
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:id="@+id/GridView"/>
</LinearLayout>
Don't forget to put that into your build.gradle(:app)
folder under dependencies
:
dependencies {
implementation 'androidx.gridlayout:gridlayout:1.0.0'
That's the result you will get:
Cheers and try that with your ImageView
, CardView
or ImageButton
instead of my Strings
. Happy Coding :)
Use GridLayout (Except for the top row, which is called Toolbar
or ActionBar
).