SeekBar in Android UI controls

SeekBar is an Android UI Control which is an extension of ProgressBar Class with an addition of drag-able thumb, the user can touch the thumb and drag left or right to set the current Progress level.

seekbar

.xml file:

<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”
android:background=”#ff009924″
tools:context=”com.CoreProgrammers.android.seekbar.MainActivity” >

<SeekBar android:id=”@+id/seekBar1″
android:layout_width=”400dp”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true”
android:layout_marginTop=”26dp”
android:background=”#ffff2244″
android:thumb=”@drawable/ic_launcher”
android:scrollbarStyle=”outsideInset”
android:max=”100″/>

<TextView android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_below=”@+id/seekBar1″
android:layout_marginLeft=”29dp”
android:layout_marginTop=”14dp” />

</RelativeLayout>

Java file:

 

package com.CoreProgrammers.android.seekbar;

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

public class MainActivity extends Activity{

private SeekBar seekBar;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVariables();

textView.setText(“Covered: ” + seekBar.getProgress() + “/” + seekBar.getMax());

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
int progress = 0;

@Override
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser)
{
progress = progresValue;
textView.setText(“Covered: ” + progress + “/” + seekBar.getMax());
}

@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
Toast.makeText(getApplicationContext(), “Started tracking seekbar”, Toast.LENGTH_SHORT).show();
}

@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
Toast.makeText(getApplicationContext(), “Stopped tracking seekbar”, Toast.LENGTH_SHORT).show();
}
});
}

private void initializeVariables()
{
seekBar = (SeekBar) findViewById(R.id.seekBar1);
textView = (TextView) findViewById(R.id.textView1);
}
}

Skip to toolbar