Simple three model of listview is used in this project.
Frist we need to make a project on android studio named "MyListView".
we are create class
The next one is "mylist"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
tools:srcCompat="@mipmap/ic_launcher_round"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="24sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
You can watch the video from YouTube for more detail
Download full code from GitHub : https://github.com/safvanp/MyListView
Frist we need to make a project on android studio named "MyListView".
we are create class
- MainActivity
- Model1
- Model2
- Model3
- MyAdapter (Custom Adapter Class)
xml layout file are
- activity_main
- activity_listview
- activity_os_name
- mylist
Fist we want to create MainActivity class.
MainActivity is:
package com.example.mylistview;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Model_1(View v){
startActivity(new Intent(getApplicationContext(),Model1.class));
}
public void Model_2(View v){
startActivity(new Intent(getApplicationContext(),Model2.class));
}
public void Model_3(View v){
startActivity(new Intent(getApplicationContext(),Model3.class));
}
}
Then create another class name Model1
package com.example.mylistview;
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 androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class Model1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
ListView listView = findViewById(R.id.listview);
String[] name_os = {"Android","Ios","Windows","Linux","ChromeOS"};
final ArrayAdapter arrayAdapter = new ArrayAdapter(Model1.this,R.layout.activity_os_name,R.id.textView,name_os);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(getApplicationContext(),"Cliked:"+arrayAdapter.getItem(position).toString(),Toast.LENGTH_SHORT).show();
}
});
}
}
Create anothe class named "Model2"
package com.example.mylistview;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class Model2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
ListView listView = findViewById(R.id.listview);
ArrayList list=new ArrayList();
for (int i=0; i<50;i++){
list.add("NAME - "+i);
}
ArrayAdapter arrayAdapter = new ArrayAdapter(Model2.this,R.layout.activity_os_name,R.id.textView,list);
listView.setAdapter(arrayAdapter);
}
}
Create another class named "Model3"
package com.example.mylistview;
import android.os.Bundle;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class Model3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
String[] Name = {"FaceBook","Google","Instagram","Twitter","YouTube"};
String[] Sub = {"FaceBook_Sub","Google_Sub","Instagram_Sub","Twitter_Sub","YouTube_Sub"};
int[] images = {R.drawable.facebook,R.drawable.google,R.drawable.instagram,R.drawable.twitter,R.drawable.youtube};
ListView listView = findViewById(R.id.listview);
MyAdapter myAdapter = new MyAdapter(Model3.this,Name,Sub,images);
listView.setAdapter(myAdapter);
}
}
Create another class named"MyAdapter" this is fo custom adapter for the list view
package com.example.mylistview;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<String> {
Activity context;
String[] Name;
String[] sub_titile;
int[] images;
public MyAdapter(Activity context,String[] Name,String[] sub_titile,int[] images){
super(context,R.layout.mylist,Name);
this.context=context;
this.Name=Name;
this.sub_titile=sub_titile;
this.images=images;
}
public View getView(int position, View v, ViewGroup parent){
LayoutInflater inflater = context.getLayoutInflater();
View view = inflater.inflate(R.layout.mylist,null,true);
TextView name;
TextView sub;
ImageView image;
name = view.findViewById(R.id.textView2);
sub = view.findViewById(R.id.textView3);
image = view.findViewById(R.id.imageView);
name.setText(Name[position]);
sub.setText(sub_titile[position]);
image.setImageResource(images[position]);
return view;
}
}
We need XML layout
First activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Model_1"
android:text="Model1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Model_2"
android:text="Model2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Model_3"
android:text="Model3" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Then second layout is "activity_listview"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Then next one is "activity_os_name"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="@android:color/black"
android:textSize="30sp" />
</LinearLayout>
The next one is "mylist"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
tools:srcCompat="@mipmap/ic_launcher_round"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="24sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
Finaly we need to declare all on manifest.
This is AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mylistview">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Model1"/>
<activity android:name=".Model2"/>
<activity android:name=".Model3"/>
</application>
</manifest>
You can watch the video from YouTube for more detail
Download full code from GitHub : https://github.com/safvanp/MyListView
0 Comments