Custom ListView (With Seekbar) Example
This example will show you how can you build a custom listview with seek bar application in android.
//Using Array Adapter
Steps:-
1) Create one layout file for the list_helper and one java file for the list_adapter.
2) Add list view in the main.xml file and set the adapter.
3) Create one array list to store the value of each seek bar.
First open the main.xml file and add a list-view.
Main.xml
<LinearLayout >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="51dp"
android:layout_marginTop="61dp" >
</ListView>
</LinearLayout>
Open the Main.java file to set the adapter to the list-view:-
Main.java
public class MainActivity extends Activity {
ListView lname;
int progress1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lname=(ListView) findViewById(R.id.listView1); //bind the list view
String [] name=new String[30]; //create a array
for(int i=0;i<20;i++)
{
name[i]="TextView" + i;
}
//set the adapter
lname.setAdapter(new Myadpter (this,R.id.textView1,
android.R.layout.simple_dropdown_item_1line,name));
}
Create one layout file for the list view.
list_view_helper.xml
<LinearLayout >
<EditText
android:id="@+id/editText1"
android:ems="10" >
<requestFocus />
</EditText>
<SeekBar
android:id="@+id/seekBar1"
android:clickable="false"
android:focusable="false" />
<TextView
android:id="@+id/textView1"
android:layout_marginTop="30dp"
android:text="TextView" />
</LinearLayout>
Create an adapter class for list view:-
Myadpter.java
private class Myadpter extends ArrayAdapter<String>{
String[] name; //array to store data pass by list view in adapter
final int[] value=new int[30]; // int array to hold value(progress) for each item in list view
public Myadpter(Context context, int resource,int simpleDropdownItem1line, String[] name) {
super(context, resource,name);
this.name=name;
for(int i=0;i<30;i++) //loop to initialize value to array
{
value[i]=0;
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflate=(LayoutInflater) getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View v= inflate.inflate(R.layout.listlayout, parent,false);
final EditText editvalue=(EditText) v.findViewById(R.id.editText1);
final TextView t1;
t1=(TextView) v.findViewById(R.id.textView1);
final SeekBar s1=(SeekBar) v.findViewById(R.id.seekBar1);
t1.setText(""+value[position]);
//set the text each time when user scroll the list
s1.setProgress(value[position]);
//set the progress each time when user scroll the list
editvalue.setText(""+value[position]);
//set the text each time when user scroll the list
s1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
progress1=progress;
editvalue.setText(""+progress1);
value[position]=progress1;
t1.setText(""+progress1);
}
});
s1.setClickable(false);
s1.setFocusable(false);
t1.setClickable(false);
t1.setFocusable(false);
editvalue.setClickable(false);
editvalue.setFocusable(false);
editvalue.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
s1.setProgress(Integer.parseInt(s.toString()));
}
});
v.setClickable(false);
return v;
}
}
Output
1st Screen 2nd Screen

//Using Array Adapter
Steps:-
1) Create one layout file for the list_helper and one java file for the list_adapter.
2) Add list view in the main.xml file and set the adapter.
3) Create one array list to store the value of each seek bar.
First open the main.xml file and add a list-view.
Main.xml
<LinearLayout >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="51dp"
android:layout_marginTop="61dp" >
</ListView>
</LinearLayout>
Open the Main.java file to set the adapter to the list-view:-
Main.java
public class MainActivity extends Activity {
ListView lname;
int progress1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lname=(ListView) findViewById(R.id.listView1); //bind the list view
String [] name=new String[30]; //create a array
for(int i=0;i<20;i++)
{
name[i]="TextView" + i;
}
//set the adapter
lname.setAdapter(new Myadpter (this,R.id.textView1,
android.R.layout.simple_dropdown_item_1line,name));
}
Create one layout file for the list view.
list_view_helper.xml
<LinearLayout >
<EditText
android:id="@+id/editText1"
android:ems="10" >
<requestFocus />
</EditText>
<SeekBar
android:id="@+id/seekBar1"
android:clickable="false"
android:focusable="false" />
<TextView
android:id="@+id/textView1"
android:layout_marginTop="30dp"
android:text="TextView" />
</LinearLayout>
Create an adapter class for list view:-
Myadpter.java
private class Myadpter extends ArrayAdapter<String>{
String[] name; //array to store data pass by list view in adapter
final int[] value=new int[30]; // int array to hold value(progress) for each item in list view
public Myadpter(Context context, int resource,int simpleDropdownItem1line, String[] name) {
super(context, resource,name);
this.name=name;
for(int i=0;i<30;i++) //loop to initialize value to array
{
value[i]=0;
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflate=(LayoutInflater) getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View v= inflate.inflate(R.layout.listlayout, parent,false);
final EditText editvalue=(EditText) v.findViewById(R.id.editText1);
final TextView t1;
t1=(TextView) v.findViewById(R.id.textView1);
final SeekBar s1=(SeekBar) v.findViewById(R.id.seekBar1);
t1.setText(""+value[position]);
//set the text each time when user scroll the list
s1.setProgress(value[position]);
//set the progress each time when user scroll the list
editvalue.setText(""+value[position]);
//set the text each time when user scroll the list
s1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
progress1=progress;
editvalue.setText(""+progress1);
value[position]=progress1;
t1.setText(""+progress1);
}
});
s1.setClickable(false);
s1.setFocusable(false);
t1.setClickable(false);
t1.setFocusable(false);
editvalue.setClickable(false);
editvalue.setFocusable(false);
editvalue.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
s1.setProgress(Integer.parseInt(s.toString()));
}
});
v.setClickable(false);
return v;
}
}
Output
1st Screen 2nd Screen


Comments
Post a Comment