Hello friend,
This is simplest way to create custom list view in android, I hope below code is use full for you.
list_control.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<ImageView
android:id="@+id/listimg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/listtext"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="22dp" />
</TableRow>
</TableLayout>
CustomList.java
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] web;
private final Integer[] imageId;
ImageView listImageView;
TextView listTextTitle;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_control, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_control, null, true);
listTextTitle = (TextView) rowView.findViewById(R.id.listtext);
listImageView = (ImageView) rowView.findViewById(R.id.listimg);
listTextTitle.setText(web[position]);
listImageView.setImageResource(imageId[position]);
return rowView;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.spider.customelist.MainActivity">
<ListView
android:id="@+id/mylist"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView list;
String[] com = {
"Google",
"Twitter",
"Windows",
"Bing",
};
Integer[] imageId = {
R.drawable.ic_launcher_background,
R.drawable.ic_launcher_background,
R.drawable.ic_launcher_background,
R.drawable.ic_launcher_background,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList adapter = new
CustomList(MainActivity.this, com, imageId);
list = (ListView) findViewById(R.id.mylist);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You touched " + com[+position], Toast.LENGTH_SHORT).show();
}
});
}
}
This is simplest way to create custom list view in android, I hope below code is use full for you.
list_control.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<ImageView
android:id="@+id/listimg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/listtext"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="22dp" />
</TableRow>
</TableLayout>
CustomList.java
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] web;
private final Integer[] imageId;
ImageView listImageView;
TextView listTextTitle;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_control, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_control, null, true);
listTextTitle = (TextView) rowView.findViewById(R.id.listtext);
listImageView = (ImageView) rowView.findViewById(R.id.listimg);
listTextTitle.setText(web[position]);
listImageView.setImageResource(imageId[position]);
return rowView;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.spider.customelist.MainActivity">
<ListView
android:id="@+id/mylist"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView list;
String[] com = {
"Google",
"Twitter",
"Windows",
"Bing",
};
Integer[] imageId = {
R.drawable.ic_launcher_background,
R.drawable.ic_launcher_background,
R.drawable.ic_launcher_background,
R.drawable.ic_launcher_background,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList adapter = new
CustomList(MainActivity.this, com, imageId);
list = (ListView) findViewById(R.id.mylist);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You touched " + com[+position], Toast.LENGTH_SHORT).show();
}
});
}
}
OUTPUT:-
Comments
Post a Comment