Main_Activity.java
===================
package com.sam.web.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;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.listViewId);
final String[] countryNames=getResources().getStringArray(R.array.country_names);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,R.layout.sample_view,R.id.textViewId,countryNames);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String value=countryNames[position];
Toast.makeText(MainActivity.this,value+" "+position,Toast.LENGTH_SHORT).show();
}
});
}
}
======================================================================================================
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"
android:background="#ab223cff"
tools:context="com.sam.web.listview.MainActivity">
<ListView
android:listSelector="#27e223"
android:dividerHeight="2dp"
android:divider="#22cbff"
android:id="@+id/listViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
========================================================================================================
string.xml
==========
<resources>
<string name="app_name">ListView</string>
<string-array name="country_names">
<item>Bangladesh</item>
<item>India</item>
<item>Pakistan</item>
<item>Afganistan</item>
<item>SaudiaArabia</item>
<item>London</item>
<item>UK</item>
<item>USA</item>
<item>Japan</item>
<item>Nepal</item>
<item>Srilanka</item>
<item>Uruguaya</item>
<item>Brazil</item>
<item>Rasia</item>
</string-array>
</resources>
=========================================================================================================
sample_view.xml
==============
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:padding="5dp"
android:gravity="center_horizontal"
android:textStyle="bold"
android:textSize="20sp"
android:text="Hellow there"
android:id="@+id/textViewId"
android:layout_width="match_parent"
android:layout_height="50dp" />
</android.support.constraint.ConstraintLayout>
thistimebd Bangladesh Live online newsportal, education, Lifestyle, Health, Photography, gif image etc.
Make your own name or company name website | contact: thistimebd24@gmail.com
Copyright © 2020-2024 News Portal in Bangladesh - THISTIMEBD.COM. ALL Rights Reserved.