Thursday, March 1, 2012

Android seek bar example

This program is for control seek bar in android.




Step 1) Your main java program file is here



package seek.bar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class SeekbarActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        SeekBar sk=(SeekBar)findViewById(R.id.seekBar1);
        final TextView tv=(TextView)findViewById(R.id.textView1);
       
        sk.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                    
                     public void onStopTrackingTouch(SeekBar arg0) {
                           // TODO Auto-generated method stub
                          
                     }
                    
                     public void onStartTrackingTouch(SeekBar arg0) {
                           // TODO Auto-generated method stub
                          
                     }
                    
                     public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                           // TODO Auto-generated method stub
                           tv.setText("Seek bar value is"+ arg1);
                          
                          
                     }
              });
       
       
       
    }
}

Step 2) The code of main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<SeekBar
android:id="@+id/seekBar1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</SeekBar>

<TextView
android:id="@+id/textView1"
android:text="TextView"
android:layout_height="wrap_content"
android:layout_width="216dp"
android:layout_weight="0.04">
</TextView>

</LinearLayout>

2 comments:

  1. I tried doing this, but the program crashes when I try to move the seekbar...

    ReplyDelete