How to create a SeekBar in Android studio

METHOD 1

Sometimes we want to use SeekBar for better user experience.First we need to edit an XML file with following elements:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="192dp"
        android:layout_height="53dp"
        android:layout_marginStart="101dp"
        android:layout_marginTop="224dp"
        android:layout_marginEnd="118dp"
        android:layout_marginBottom="454dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

STEP 2:

add in Main Activity :

public class MainActivity extends AppCompatActivity {
    private SeekBar seekbar;
    Context context = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
seekbar = findViewById(R.id.seekBar);
.......

With seekbar.setProgress(200); seekbar.setMax(100); seekbar.setMin(0) its possible to play with values.But please be carefull , because seekbar.setMin(0) is only allowed on APIs that are 26 or higher.

In the next step select your parameters and add an anonymous class with OnSeekBarChange Listener:

 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(context, "Seek bar progress is :" + progressChangedValue,
                        Toast.LENGTH_SHORT).show();
            }
        });

METHOD 2

Now we try to create a custom Alertdialog with our own XML layout file.
First we need to create a layout for our dialog

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="276dp"
        android:layout_height="85dp"
        app:srcCompat="@color/colorAccent" />

    <TextView
        android:id="@+id/txTitle"
        android:layout_width="274dp"
        android:layout_height="37dp"
        android:fontFamily="@font/aladin"
        android:text="@string/title"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txContent"
        android:layout_width="272dp"
        android:layout_height="wrap_content"
        android:fontFamily="@font/aldrich"
        android:text="@string/text1"
        android:textSize="12sp" />

    <Button
        android:id="@+id/btOk"
        android:layout_width="274dp"
        android:layout_height="60dp"
        android:text="@android:string/VideoView_error_button" />

    <Button
        android:id="@+id/btCancel"
        android:layout_width="274dp"
        android:layout_height="58dp"
        android:text="@android:string/cancel" />
</LinearLayout>

Second, Initialize views and create a DialogAlert constuctor in the main activity class

final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.dialog);
        TextView tx1 = dialog.findViewById(R.id.txTitle);
        TextView txcontent = dialog.findViewById(R.id.txContent);

        Button btOk = dialog.findViewById(R.id.btOk);
        Button btCancel = dialog.findViewById(R.id.btCancel);

The last step is to add OnClickListeners to the buttons and call dialog.show() method.

  btOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"button Clicked",Toast.LENGTH_SHORT).show();

            }
        });
        btCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
dialog.show();

Result:

Leave a Reply

Your email address will not be published. Required fields are marked *