Open your activity_main and paste below code.
Open your single_row and paste below code.
Open your CustomListAdapter and paste below code.
Open your MainActivity and paste below code.
Output :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.ravindra.customlistviewdemo.MainActivity"> | |
<ListView | |
android:id="@+id/customlist" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"/> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/rl_singlerow" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ImageView | |
android:id="@+id/iv_icon" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="@mipmap/ic_launcher" | |
android:layout_margin="20dp"/> | |
<TextView | |
android:id="@+id/txt_name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_toRightOf="@+id/iv_icon" | |
android:layout_toEndOf="@+id/iv_icon" | |
android:layout_marginTop="25dp" | |
android:text="CupCake"/> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ravindra.customlistviewdemo; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
/** | |
* Created by Ravindra on 19-Jan-17. | |
*/ | |
public class CustomListAdapter extends BaseAdapter { | |
private Context context; | |
String [] title={"CupCake","Donut","Eclairs","Froyo","GingerBread","HoneyComb","IceCreamSandwitch","JellyBean"}; | |
int [] icon={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher, | |
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher}; | |
CustomListAdapter(Context context){ | |
this.context=context; | |
} | |
@Override | |
public int getCount() { | |
return 8; | |
} | |
@Override | |
public Object getItem(int i) { | |
return i; | |
} | |
@Override | |
public long getItemId(int i) { | |
return i; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View row = null; | |
if (convertView==null){ | |
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
row = inflater.inflate(R.layout.single_row,parent,false); | |
ImageView imageview= (ImageView) row.findViewById(R.id.iv_icon); | |
TextView textview= (TextView) row.findViewById(R.id.txt_name); | |
imageview.setBackgroundResource(icon[position]); | |
textview.setText(title[position]); | |
}else{ | |
row = convertView; | |
} | |
return row; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ravindra.customlistviewdemo; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.ListView; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ListView listview= (ListView) findViewById(R.id.customlist); | |
listview.setAdapter(new CustomListAdapter(this)); | |
} | |
} |
Output :