Monday, August 27, 2012

Android | Seekbar Example

Seekbar is widget and we can use to select the value from selected range.

max property will set the max value for seekbar and progress property will give the current value of where seekbar positioned. ensure, seekbar starts with "0".

below is example of Seekbar which i have verified on Android 4.0.3 emulator.

Code of Layout File:


<LinearLayout 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"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvvalue"
/>
   
    <SeekBar
        android:id="@+id/sbvalue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="99"
        android:progress="0"/>

</LinearLayout>

Code of Strings.xml File:


<resources>

    <string name="app_name">SeekBarExample</string>
    <string name="tvvalue"></string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_seek_bar_example">SeekBar Example</string>

</resources>


Source code of .JAVA file:


package com.example.seekbarexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SeekBarExample extends Activity implements OnSeekBarChangeListener {

private TextView tvValue;
private SeekBar sbValue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_bar_example);
       
        tvValue = (TextView) findViewById(R.id.tvvalue);
        sbValue = (SeekBar) findViewById(R.id.sbvalue);
       
        sbValue.setOnSeekBarChangeListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_seek_bar_example, menu);
        return true;
    }

public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub

tvValue.setText(String.valueOf(arg1));


}

public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}
}

Result:




No comments:

Post a Comment