How to display items listview in an android
each item are displayed vertically scrollable list.
That is easy to display each item's listview with automatically scrollable.
In this blog post, we will use an Adapter to inserted items into the list automatically.
Start android studio.
Name your project.
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(); } }); } }
activity_main.java
<?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>
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>
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>
https://youtu.be/ixrnJD3-FGY
Download Source code