Friday 26 September 2014

Seekbar bind with Progressbar and Rattingbar

Your activity_main.xml like this;

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <SeekBar
        android:id="@+id/sb"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:max="100" />

    <TextView
        android:id="@+id/txt_dis"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="Display" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <RatingBar
        android:id="@+id/rtbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numStars="7"
        android:rating="0"
        android:stepSize="0.5" />

</LinearLayout>

Your MainActivity.java like this;

package com.example.seekbar;

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

public class MainActivity extends Activity {
SeekBar sb;
TextView txt_dis;
ProgressBar pb;
RatingBar rtb;
int int_pro;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (ProgressBar) findViewById(R.id.progressBar1);
rtb = (RatingBar) findViewById(R.id.rtbar);
sb = (SeekBar) findViewById(R.id.sb);

sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

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

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
txt_dis = (TextView) findViewById(R.id.txt_dis);
txt_dis.setText("" + progress);
pb.setProgress(progress);
float pro = progress / 12;
rtb.setRating(pro);

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


}

App like this;


No comments:

Post a Comment