Friday, February 12, 2010

RatingBar

A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBar. The smaller RatingBar style ( ratingBarStyleSmall) and the larger indicator-only style (ratingBarStyleIndicator) do not support user interaction and should only be used as indicators.

RatingBar

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleIndicator"
android:id="@+id/ratingbar_Indicator"
/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:id="@+id/ratingbar_Small"
android:numStars="20"
/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyle"
android:id="@+id/ratingbar_default"
/>
</LinearLayout>


AndroidRatingBar.java
package com.AndroidRatingBar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.Toast;

public class AndroidRatingBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final RatingBar ratingBar_Small = (RatingBar)findViewById(R.id.ratingbar_Small);
final RatingBar ratingBar_Indicator = (RatingBar)findViewById(R.id.ratingbar_Indicator);
final RatingBar ratingBar_default = (RatingBar)findViewById(R.id.ratingbar_default);

ratingBar_default.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener(){

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
ratingBar_Small.setRating(rating);
ratingBar_Indicator.setRating(rating);
Toast.makeText(AndroidRatingBar.this, "rating:"+String.valueOf(rating),
Toast.LENGTH_LONG).show();
}});
}
}


Download the files.

Thursday, February 11, 2010

SeekBar

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:id="@+id/seekbar"
android:max="100"
android:progress="50"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/seekbarvalue"
android:text="50"
/>
</LinearLayout>


package com.AndroidSeekBar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class AndroidSeekBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar);
final TextView seekBarValue = (TextView)findViewById(R.id.seekbarvalue);

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
seekBarValue.setText(String.valueOf(progress));
}

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

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


Download the files.

Friday, February 5, 2010

ProgressBar

ProgressBar is a visual indicator of progress in some operation. Displays a bar to the user representing how far the operation has progressed; the application can change the amount of progress (modifying the length of the bar) as it moves forward.

ProgressBar

This exercise implemenets a default circle ProgressBar and a Horizontal ProgressBar.

For the Horizontal ProgressBar, a Runnable Thread is implemented to send a message to a Handle to increase the progress of the ProgressBar.

Modify main.xml to add two ProgressBar.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressbar_default"
/>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressbar_Horizontal"
android:max="100"
/>
</LinearLayout>


Modify java code.
package com.exercise.AndroidProgressBar;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;

public class AndroidProgressBar extends Activity {

ProgressBar myProgressBar;
int myProgress = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myProgressBar=(ProgressBar)findViewById(R.id.progressbar_Horizontal);

new Thread(myThread).start();
}

private Runnable myThread = new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
while (myProgress<100){
try{
myHandle.sendMessage(myHandle.obtainMessage());
Thread.sleep(1000);
}
catch(Throwable t){
}
}
}

Handler myHandle = new Handler(){

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
myProgress++;
myProgressBar.setProgress(myProgress);
}

};

};
}



Download the files.




Related article:
- ProgressBar running in AsyncTask