Update value from seek bar and add it to progress bar


Objective:

This is the simple seekbar to demonstrate how to get value from seekbar and apply it to progress bar. When user slide the seekbar, the percentage result will be displayed.

Project Info:

Application Name: seekbars
Build SDK: API 15, Android 4.0.3 (ICS)
Miniumum Required SDK: API 8, Android 2.2 (Froyo)
Additional Files created: None
Default Files edited: Two

1. Create a default project. The project tree will look like this.





2. Edited MainAcitvity.java


package com.example.seekbars;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

    SeekBar seekbar; //SeekBar variable
    TextView value; //TextView variable
    ProgressBar progressbar; //ProgressBar variable

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        seekbar = (SeekBar) findViewById(R.id.seekBar1); //link "seekbar" with "seekBar1" from xml
        value = (TextView) findViewById(R.id.textView1); //link "value" with "textView1" from xml
        progressbar = (ProgressBar) findViewById(R.id.progressBar1); //link "progress" with "progressBar1" from xml

        seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { //link "seekbar" with seek bar change listener
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                value.setText("SeekBar at " + progress + "%"); //update "progress" value and pass it to textview
                progressbar.setProgress(progress); // also update "progress" value and pass it to progress bar
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

3. Edit activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_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="28dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="86dp"
        android:gravity="center" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1" />

</RelativeLayout>



Note:




Project download link: https://osdevlab.googlecode.com/svn/trunk/tut3.0





No comments:

Post a Comment