Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.
Custom ListView with item click listener
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:orientation="vertical" tools:context=".MainActivity"> <ListView android:id="@+id/cus_list_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
MainActivity.java
package com.usmtip.myapplication; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { ListView list; String titles[] = {"Home", "Contacts", "Message", "Search", "Movies"}; String description[] = {"The Description 1", "The Description 2", "The Description 3", "The Description 4", "The Description 5"}; int imgs[]={R.drawable.ic_baseline_home_24,R.drawable.ic_baseline_perm_contact_calendar_24,R.drawable.ic_baseline_message_24,R.drawable.ic_baseline_search_24, R.drawable.ic_baseline_movie_24}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = (ListView) findViewById(R.id.cus_list_view); MyAdapter adapter = new MyAdapter(this,titles,imgs,description); list.setAdapter(adapter); //click to go to another activity list.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (position == 0) { Toast.makeText(MainActivity.this, "Home clicked", Toast.LENGTH_SHORT).show(); } else if (position == 1) { Toast.makeText(MainActivity.this, "Contacts clicked", Toast.LENGTH_SHORT).show(); } else if (position == 2) { Toast.makeText(MainActivity.this, "Message clicked", Toast.LENGTH_SHORT).show(); } else if (position == 3) { Toast.makeText(MainActivity.this, "Search clicked", Toast.LENGTH_SHORT).show(); } else if (position == 4) { Toast.makeText(MainActivity.this, "Movies clicked", Toast.LENGTH_SHORT).show(); } } }); } class MyAdapter extends ArrayAdapter<String> { Context context; String myTitles[]; String myDescription[]; int[] imgs; MyAdapter(Context c, String[] titles, int[] imgs, String[] description) { super(c,R.layout.row,R.id.text1,titles); this.context=c; this.imgs=imgs; this.myTitles=titles; this.myDescription=description; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View row = layoutInflater.inflate(R.layout.row, parent, false); ImageView images = (ImageView) row.findViewById(R.id.logo); TextView myTitle = (TextView) row.findViewById(R.id.text1); TextView myDescription = (TextView) row.findViewById(R.id.text2); images.setImageResource(imgs[position]); myTitle.setText(titles[position]); myDescription.setText(description[position]); return row; } } }
Create new only .xml files
row.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:padding="4dp" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="60dp" android:layout_height="60dp" android:padding="5dp" android:id="@+id/logo" android:src="@mipmap/ic_launcher"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="2dp" android:textColor="#33CC33" android:id="@+id/text1" android:layout_marginTop="5dp" android:layout_marginLeft="10dp" android:text="Medium Text"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text2" android:layout_marginLeft="10dp" android:text="TextView" /> </LinearLayout> </LinearLayout>
All Server Links to Download This File:
0 Comments: