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)
<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)
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());
}
});
}
}