Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 ListView Widget in Android?

.....................................................................................................................................


It is used to display items using List form, List-display multiple rows, we can customize List Widget using CustomAdapter class.

Syntax of ListView Widget in Android:-


<?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=".MainActivity17">

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lstview2"
>

</ListView>

</LinearLayout>


Example 1st:-

Create Normal ListView in Android Application:-

1) Create Activity and provide name Activity17

package com.example.additionapplication;

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.Toast;

public class MainActivity17 extends AppCompatActivity {

ListView lstv;
String arr[] = {"C","C++","JAVA",".NET","PHP","ANDROID","iOS","Data Science","MACHINE Learning","C","C++","JAVA",".NET","PHP","ANDROID","iOS","Data Science","MACHINE Learning"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main17);
lstv = findViewById(R.id.lstview2);
ArrayAdapter aa = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,arr);
lstv.setAdapter(aa);
lstv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),"Selected Course is "+arr[position],Toast.LENGTH_LONG).show();
}
});


}
}


2) <?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=".MainActivity17">

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lstview2"
>

</ListView>

</LinearLayout>



Custom ListView:-


1)  Create Activity and Design ListView:-

package com.example.additionapplication;

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.Toast;

public class MainActivity17 extends AppCompatActivity {

ListView lstv;
int imgid[] = {R.drawable.bg,R.drawable.sss};
String str[] = {"RAMESH","SURESH"};
// String arr[] = {"C","C++","JAVA",".NET","PHP","ANDROID","iOS","Data Science","MACHINE Learning","C","C++","JAVA",".NET","PHP","ANDROID","iOS","Data Science","MACHINE Learning"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main17);
lstv = findViewById(R.id.lstview2);
// ArrayAdapter aa = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,arr);
// lstv.setAdapter(aa);
StuAdapter obj = new StuAdapter(getApplication(),imgid,str);
lstv.setAdapter(obj);
lstv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),"Selected Course is "+str[position],Toast.LENGTH_LONG).show();
}
});


}
}

2)  Design File:-

<?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=".MainActivity17">

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lstview2"
>

</ListView>

</LinearLayout>

3)  Custom LisvIew:-

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

<ImageView
android:layout_width="100dp"
android:layout_height="50dp"
android:id="@+id/custimgview"
>

</ImageView>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/custtextview"
></TextView>

</LinearLayout>


4)  Adpate class:-
package com.example.additionapplication;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;



public class StuAdapter extends BaseAdapter {
int imgid[];
String sname[];
Context ctx;
LayoutInflater inflter;
StuAdapter(Context ctx,int imgid[],String sname[])
{
this.imgid = imgid;
this.sname=sname;
this.ctx =ctx;
inflter = (LayoutInflater.from(ctx));
}
@Override
public int getCount() {
return sname.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View row = inflter.inflate(R.layout.custspinner_layout, parent, false);
ImageView img = (ImageView) row.findViewById(R.id.custimgview);
img.setImageResource(imgid[position]);
TextView label = (TextView) row.findViewById(R.id.custtextview);
label.setText(sname[position]);
return row;
}
}





Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post