1. layout - activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="112dp" />
<TextView
android:id="@+id/txtSeekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/seekBar1"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:text="0 %"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
2. MainActivity.Java
package com.seekbar;
import
android.app.Activity;
import android.os.Bundle;
import
android.widget.SeekBar;
import
android.widget.SeekBar.OnSeekBarChangeListener;
import
android.widget.TextView;
import
android.widget.Toast;
public class MainActivity extends Activity {
SeekBar dim;
TextView txt;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView)findViewById(R.id.txtSeekBar);
dim=(SeekBar)findViewById(R.id.seekBar1);
// mengeset nilai
maksimum seekbar
dim.setMax(100);
/* event seek bar
,setOnSeekBarChangeListener
* 1. onStopTrackingTouch , fungsi yang
akan di jalankan ketika objek selesai * di gunakan
* 2. onStartTrackingTouch , fungsi yang
di jalankan ketika objek mulai di * gunakan
* 3. onProgressChanged, fungsi yang
akan di jalankan ketika objek sedang di * gunakan,int progress merupakan nilai value dari objek
*
*/
dim.setOnSeekBarChangeListener(new
OnSeekBarChangeListener() {
@Override
public void
onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated
method stub
tampil("stop" );
}
@Override
public void
onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated
method stub
tampil("start" );
}
@Override
public void
onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated
method stub
tampil("progress");
txt.setText(String.valueOf(progress));
}
});
}
private void tampil(String h){
Toast.makeText(getApplication(),
h, Toast.LENGTH_SHORT)
.show();
}
}
3. Running
Tidak ada komentar :
Posting Komentar