15 Program to display 15 buttons using GridView in Android Studio.

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.blogspot.codingatharva.manualprograms">



<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>


MainActivity.java:

package com.blogspot.codingatharva.manualprograms;



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 arr[] = new String[15];



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        gridview = findViewById(R.id.gridview);





        for (int i = 0; i < 15; i++) {

            arr[i] = Integer.toString(i + 1);

        }



        ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.activity_listview, R.id.btn, arr);



        gridview.setAdapter(ad);



    }



}


activity_main.xml:

<?xml version= "1.0" encoding= "utf-8" ?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp"
    tools:context=".MainActivity"></GridView>

activity_listview.xml: under res/layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</LinearLayout>

Output:

GridView in Android Studio.

Previous
Next Post »