Mon. Sep 26th, 2022

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="androiindians.listview.MainActivity">


    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/grid"/>
</LinearLayout>

Custom.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:textColor="@color/colorAccent"
    android:textSize="20sp"
    android:layout_height="match_parent">
</TextView>

String.xml

<resources>
    <string name="app_name">ListView</string>

    <string-array name="aaa">
        <item>BBB</item>
        <item>CCCC</item>
        <item>DDDD</item>
        <item>EEEE</item>
    </string-array>
</resources>

MainActivity.java

package androiindians.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    GridView gridview;
    String[] Courses={"Android","CoreJava","Adv Java"};
    ArrayAdapter adp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] bbb=getResources().getStringArray(R.array.aaa);

        gridview = (GridView) findViewById(R.id.grid);

        adp=new ArrayAdapter(MainActivity.this,
                R.layout.custom,bbb);
        gridview.setAdapter(adp);

        gridview.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(
                    AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this,"position="+position+" "+"id"+id,Toast.LENGTH_LONG).show();

            }
        });

        /* either you can use above or this
gridview.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(
                    AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(
                        MainActivity.this,
                        "position="+position+" "+"id"+id,Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });*/
    }

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published.