Showing posts with label Progressbar. Show all posts
Showing posts with label Progressbar. Show all posts

Tuesday, August 28, 2012

Android | RatingBar Example

Below is an example of Android RatingBar element.

Note: Example is verified with Android 4.0.3 emulator.

activity_rating_bar_example.xml


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

   
    <RatingBar
        android:id="@+id/rbrating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:rating="0"
        android:stepSize=".1"
       />
   
    <TextView
        android:id="@+id/tvratingvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvratingvalue"
/>
    <TextView
        android:id="@+id/tvuser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvuser"
/>  

    <Button
        android:id="@+id/btnok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnok"
/>
</LinearLayout>


strings.xml

<resources>

    <string name="app_name">RatingBarExample</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_rating_bar_example">RatingBar Example</string>
    
    <string name="tvratingvalue"></string>
    <string name="tvuser"></string>
    <string name="btnok">Set rating at 2.5</string>
</resources>

RatingBarExample.java

package com.example.ratingbarexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class RatingBarExample extends Activity implements OnRatingBarChangeListener, OnClickListener {
private RatingBar rbRatingBar  ;
private TextView tvRatingValue, tvUser;
private Button btnOk;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rating_bar_example);
        
        rbRatingBar = (RatingBar) findViewById(R.id.rbrating);
        tvRatingValue = (TextView) findViewById(R.id.tvratingvalue);
        tvUser = (TextView) findViewById(R.id.tvuser);
        btnOk  = (Button) findViewById(R.id.btnok);
        
        rbRatingBar.setOnRatingBarChangeListener(this);
        btnOk.setOnClickListener(this);
        
    }

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

public void onRatingChanged(RatingBar arg0, float arg1, boolean arg2) {
// TODO Auto-generated method stub
tvRatingValue.setText("Rating : " + String.valueOf(arg1));
if (arg2 == true)
tvUser.setText("Changed by User"); //shows when user do by himself/herself
else
tvUser.setText("Changed by function"); // when set by function like clicking on Button
}

public void onClick(View arg0) {
// TODO Auto-generated method stub
rbRatingBar.setProgress(25);
}
}

Result:


Monday, August 27, 2012

Android | Seekbar Example

Seekbar is widget and we can use to select the value from selected range.

max property will set the max value for seekbar and progress property will give the current value of where seekbar positioned. ensure, seekbar starts with "0".

below is example of Seekbar which i have verified on Android 4.0.3 emulator.

Code of Layout File:


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

    <TextView
        android:id="@+id/tvvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvvalue"
/>
   
    <SeekBar
        android:id="@+id/sbvalue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="99"
        android:progress="0"/>

</LinearLayout>

Code of Strings.xml File:


<resources>

    <string name="app_name">SeekBarExample</string>
    <string name="tvvalue"></string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_seek_bar_example">SeekBar Example</string>

</resources>


Source code of .JAVA file:


package com.example.seekbarexample;

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

public class SeekBarExample extends Activity implements OnSeekBarChangeListener {

private TextView tvValue;
private SeekBar sbValue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_bar_example);
       
        tvValue = (TextView) findViewById(R.id.tvvalue);
        sbValue = (SeekBar) findViewById(R.id.sbvalue);
       
        sbValue.setOnSeekBarChangeListener(this);
    }

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

public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub

tvValue.setText(String.valueOf(arg1));


}

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

}

public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}
}

Result: