Description:
This example shows how to Seek Bar in your application.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it SeekBarExample.
2.) Write following code into main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <SeekBar android:id="@+id/seek" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" android:secondaryProgress="75" /> <TextView android:id="@+id/progress" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tracking" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
3.) Run for output.
Steps:
1.) Create a project named SeekBarExample and set the information as stated in the image.
Build Target: Android 2.3.3
Application Name: SeekBarExample
Package Name: com.example
Activity Name: SeekBarExampleActivity
Min SDK Version: 10
2.) Open SeekBarExampleActivity.java file and write following code there:
package com.example; import android.app.Activity; import android.os.Bundle; import android.widget.SeekBar; import android.widget.TextView; public class SeekBarExampleActivity extends Activity implements SeekBar.OnSeekBarChangeListener { SeekBar mSeekBar; TextView mProgressText; TextView mTrackingText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSeekBar = (SeekBar)findViewById(R.id.seek); mSeekBar.setOnSeekBarChangeListener(this); mProgressText = (TextView)findViewById(R.id.progress); mTrackingText = (TextView)findViewById(R.id.tracking); } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { mProgressText.setText(progress + " " + "from touch" + "=" + fromTouch); } public void onStartTrackingTouch(SeekBar seekBar) { mTrackingText.setText("Tracking on"); } public void onStopTrackingTouch(SeekBar seekBar) { mTrackingText.setText("Tracking off"); } }