Membuat seekbar dengan android studio

Tutorial Android

Membuat seekbar dengan android studio

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent" android:layout_height="match_parent">

<SeekBar

android:id="@+id/seekBar"

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/textview"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/seekBar"

android:layout_below="@+id/seekBar"

android:layout_marginTop="40dp"

android:layout_marginLeft="130dp"

android:textSize="20dp"

android:textStyle="bold"/>

</RelativeLayout>

 

MainActivity.java

package com.example.seekbarexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.SeekBar;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private SeekBar sBar;

private TextView tView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

sBar = (SeekBar) findViewById(R.id.seekBar);

tView = (TextView) findViewById(R.id.textview);

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

}

});

}

}

 

 

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *