How to display item listview in android?
In this post, we will display the listview using a simple method.
Each item is displayed as a vertically scrollable list.
It is easy to display the listview of each item with an automatically scrollable.
This blog post is rendered easy to explain. We will use an adapter to automatically insert items into the list.
For this, we will first type Java logic code in the default activity such as MainActivity.xml.
activity_main.xml
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_editor_absoluteX="180dp" app:layout_editor_absoluteY="136dp" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
MainActivity.java package com.pd.listview; import androidx.appcompat.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.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ListView listView; TextView textView; String[]list={"one","two","three","four","five","six","seven", "eight","nine","ten"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView)findViewById(R.id.listview); textView=(TextView)findViewById(R.id.textview); final ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,R.layout.singledata,R.id.textview,list); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String value=adapter.getItem(position); Toast.makeText(MainActivity.this,value, Toast.LENGTH_SHORT).show(); } }); } }
dimens.xml
dimens.xml <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="mr">5dp</dimen> <dimen name="mt">10dp</dimen> <dimen name="ml">25dp</dimen> <dimen name="ts">20dp</dimen> </resources>
singledata.xml
singledata.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/mt" android:src="@drawable/ic_baseline_arrow_forward_ios_24" /> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginLeft="@dimen/ml" android:layout_marginTop="@dimen/mt" android:text="@string/app_name" android:textSize="@dimen/ts" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignEnd="@+id/textview" android:layout_alignParentEnd="true" android:layout_marginTop="@dimen/mt" android:layout_marginRight="@dimen/mr" android:src="@drawable/ic_baseline_add_24" /> </RelativeLayout>
If you don't understand
watch the video https://youtu.be/ixrnJD3-FGY
SUBSCRIBE to downloadDownload Source code