SeekBar: SeekBar is one of the very useful user interface element in Android that allows the selection of integer values using a natural user interface. An example of SeekBar is your device’s brightness control and volume control..
Step 1: Open up Android Studio Project through res -> layout -> activity_main.xml (or) main.xml and add below code or just drag and drop the switch to design 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <SeekBar android:id="@+id/seekBar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:max="200" android:progress="60" /> </RelativeLayout>
Step 2:Add below code in MainActivity.java to handle events on seekbar.
package com.bytelogs.example; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.ButtonBarLayout; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.SeekBar; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { SeekBar simpleSeekBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initiate views SeekBar=(SeekBar)findViewById(R.id.seekBar); // perform seek bar change listener event used for getting the progress value SeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { int progressChangedValue = 0; public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { progressChangedValue = progress; } public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } public void onStopTrackingTouch(SeekBar seekBar) { Toast.makeText(MainActivity.this, "Progress is :" + progressChangedValue, Toast.LENGTH_SHORT).show(); } }); } }
Leave a Comment