seekbar example in android studio
hello, guys welcome to the next tutorial on android app development. in this article, I will tell you how to use seekbar in the android studio. it’s really important to know about seekbar. basically seekbar is like a progress bar or you can say its an extension of progress bar with a thump attached to it.
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <SeekBar android:id="@+id/seekBar1" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:layout_marginTop="200dp" android:max="100" android:indeterminate="false" android:progress="0" /> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/seekBar1" android:layout_below="@+id/seekBar1" android:layout_marginTop="40dp" android:layout_marginLeft="130dp" android:textSize="20dp" android:textStyle="bold"/> </RelativeLayout> |
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import android.app.Activity; import android.os.Bundle; import android.widget.SeekBar; import android.widget.TextView; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.MobileAds; public class Seekbar extends Activity { private AdView mAdView; private SeekBar sBar; private TextView tView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_seekbar); MobileAds.initialize(this,getString(R.string.admob_app_id)); mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); sBar = (SeekBar) findViewById(R.id.seekBar1); tView = (TextView) findViewById(R.id.textview1); tView.setText(sBar.getProgress() + "/" + sBar.getMax()); sBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { int pval = 0; @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { pval = progress; } @Override public void onStartTrackingTouch(SeekBar seekBar) { //write custom code to on start progress } @Override public void onStopTrackingTouch(SeekBar seekBar) { tView.setText(pval + "/" + seekBar.getMax()); } }); } } |