Thursday 2 January 2014

Seekbar within Listview in Android

main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
         />
</RelativeLayout>

row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:max="100"/>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="click to see row number" />
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0" />
    </LinearLayout>
</LinearLayout>

MainActivity.Java
public class MainActivity extends Activity {
@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

   ListView lv = (ListView) findViewById(R.id.list);
  lv.setAdapter(new ListAdapter(MainActivity.this));
 }

  private class ListAdapter extends BaseAdapter {
  private Context con;
  private int size = 20;
  private int s[] = new int[20];

   public ListAdapter(MainActivity mainActivity) {
   // TODO Auto-generated constructor stub
   this.con = mainActivity;
   for (int i = 0; i < size; i++) {
    s[i] = 0;
   }

   }

   @Override
  public int getCount() {
   // TODO Auto-generated method stub
   return size;
  }

   @Override
  public Object getItem(int position) {
   // TODO Auto-generated method stub
   return null;
  }

   @Override
  public long getItemId(int position) {
   // TODO Auto-generated method stub
   return position;
  }

   @Override
  public View getView(final int position, View convertView,ViewGroup parent) {
   // TODO Auto-generated method stub
   LayoutInflater inflater = LayoutInflater.from(this.con);
   View View = inflater.inflate(R.layout.list_row, null);
   SeekBar seekbar = (SeekBar) View.findViewById(R.id.seekBar1);
   Button btnrow = (Button) View.findViewById(R.id.button1);
   final Button btnseek = (Button) View.findViewById(R.id.button2);

    seekbar.setProgress(s[position]);
   btnseek.setText("" + s[position]);

    seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

     @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

     @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

     @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
      boolean fromUser) {
     // TODO Auto-generated method stub
     btnseek.setText("" + progress);
     s[position] = progress;

     }
   });

    return View;
  }


  }



2 comments: