How to display rating bar stars in two lines? - android

I have rating bar widget in my layout and set custom style in layout.
I want to set more that 10 stars in my rating bar, i want to display rating bar stars in two line, as in single line it cut off.
Here is my layout.
<RatingBar
style="#style/starRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="10"
android:stepSize="1" />
Any help is appreciated.

As far as I know, there is no way to linebreak a single RatingBar, so here is what I suggest:
Make TWO RatingBars and set their OnRatingBarChangeListeners to interact with one another.
I wrote up a quick example to show you exactly what I mean:
activity_my.xml
This would be your layout file. I used the default name when creating a new project in AS.
Use RelativeLayout.
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<RatingBar
android:id="#+id/bar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="10"
android:stepSize="1" />
<RatingBar
android:id="#+id/bar2"
android:layout_below="#id/bar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="4"
android:stepSize="1" />
</RelativeLayout>
MyActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RatingBar;
public class MyActivity extends Activity {
private RatingBar ratingBar1;
private RatingBar ratingBar2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
setOnChangeListeners();
}
public void setOnChangeListeners(){
ratingBar1 = (RatingBar) findViewById(R.id.bar1);
ratingBar2 = (RatingBar) findViewById(R.id.bar2);
ratingBar1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
ratingBar2.setRating(0);
ratingBar1.setRating(v);
}
});
ratingBar2.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
ratingBar1.setRating(10);
ratingBar2.setRating(v);
}
});
}
}
As you can see: we use a helper function: setOnChangeListeners() to initialize ratingBar1 and ratingBar2. After that we set onRatingChanged for both bars. For the first line bar made of 10 stars, we make sure ratingBar2 is set to 0. For the second bar, if it is changed, ratingBar1 must be set to 10 so it will be full.
Try it out! Hope it works for you!

Rating bar can't break in two line so add rating bar layout dynamically
int noOfStarts = 8;
int noOfRatingLine = (noOfStarts/5)+1;
LinearLayout[] linear = new LinearLayout[noOfRatingLine];
RatingBar[] ratingBarNames = new RatingBar[noOfRatingLine];
for(int i =0;i<noOfRatingLine;i++)
{
linear[i] = (LinearLayout)this.getLayoutInflater().inflate(R.layout.app_ratingbar, null);
ratingBarNames[i] = (RatingBar)linear[i].findViewById(R.id.rating);
if(noOfStarts<5){
ratingBarNames[i].setNumStars(noOfStarts);
}
else{
ratingBarNames[i].setNumStars(5);
}
ratingBarNames[i].setStepSize(1);
noOfStarts = noOfStarts-5;
ratingbarLayout.addView(linear[i]);
}
XML layout app_ratingbar.xml for inflate
<RatingBar
android:id="#+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RatingBar>

Related

ProgressBar not showing up

Edit: I realized that the code posted below works flawlessly in Nexus 6 (shamu) and Galaxy Tab A, both with android 7.0! And it does not show on Moto G5 Plus (android 7.0).
It's a simple question. My ProgressBar won't show up. At frist I was trying directly at my LoginActivity, but I wasn't having success. Things that i tried:
Put ProgressBar directly inside the layout
Put ProgressBar inside a layout
Set ProgressBar propriety indeterminate to true
Set the visibility of progressbar and parent layout to VISIBLE
Create a separated .xml file and call a Dialog (in this case, if I put a textview inside the text will appear but the progressbar won't)
Change the proprieties layout_width and layout_height
A combination of all the steps above
Last I tryed to create this simple blank activity showing only the progress bar...
activity_test.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="context_name_here">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="visible" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
test.java:
package package_name_here;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class test extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
}
What's the problem? I suspect that is something about the resources... the image of the default progressbar or version of android...
any ideas?
Thanks
I figured out what was the problem.
The Animator duration scale have to be ON at Developer options from device:
Developer options -> Developer duration scale -> 1x (ON)
Try with hard-coded dimension for the width and height for ProgressBar, also check your Colors that might be the same as activity default, also check by removing the style attribute once, or try with a different style for ProgressBar.
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
textView = (TextView) findViewById(R.id.textView);
// Start long running operation in a background thread
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds.
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
Define XML:
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="20dp"
android:indeterminate="false"
android:max="100"
android:minHeight="50dp"
android:minWidth="200dp"
android:progress="1" />
<ProgressBar
android:id="#+id/progressBar_cyclic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:minWidth="50dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
First you need to declare the progress dialog
private ProgressBar progressBar;
then in your onCreate
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar= new ProgressBar(test.this);
progressBar.show();
to dismiss the dialog or cancel it you can use
progressBar.cancel(); or progressBar.dismiss();

Set initial value to 1st star in rating bar of android

I have my android rating bar with the following code:
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="28dp"
android:stepSize="1.0" />
I want to initialize the first star value to -5, so that the remaining stars will get the values like -4,-3,-2...
But I don't know how to give that initial value to 1st star of my rating bar in android.
And I want my rating bar with three colors:
for the values -5,-4,-3,-2,-1 the star color should be RED,
for the value 0 star color should be BLUE
and for the values 1,2,3,4,5 the star color should be GREEN.
The minimum rating can be 0, negative numbers are not allowed.
However, you can create a rating bar with 11 stars to represent the values (-5 to +5)
In the listener of the rating bar, map the value to the range of -5 to +5 (by subtracting 6 from the parameter received) dynamically change the color as follows:
0 : Blue
Negative value : Red
Positive value : Green
So, the output will look like this:
Activity:
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private RatingBar ratingBar;
private TextView tvRating;
private LayerDrawable stars;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.original_activity_main);
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
tvRating = (TextView) findViewById(R.id.value);
stars = (LayerDrawable) ratingBar.getProgressDrawable();
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float ratingValue,
boolean fromUser) {
int value = (int) (ratingValue) - 6;
tvRating.setText(String.valueOf(value));
int color = Color.BLUE;
if(value > 0)
color = Color.GREEN;
else if(value < 0)
color = Color.RED;
stars.getDrawable(2).setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
});
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result : " />
<TextView
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/label"
android:text="" />
<RatingBar
android:id="#+id/ratingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/label"
android:isIndicator="false"
android:numStars="11"
android:rating="0.0"
android:stepSize="1.0" />
</RelativeLayout>
As far as I know, the minimum value you can set the RatingBar is 0 (no negative numbers).
If you set the stepSize to 1.0, then you would have to set the ratingbar using an if condition.
For example:
if(yourNumber ==(-5)){
ratingBar.setRating(1.0f); //you need to set it using a Float value
} else if (yourNumber ==(-4)){
ratingBar.setRating(2.0f);
} //And so on....
About changing the star colors - you need to define your own Ratingbar style - read this post.
Good luck!

TextView failure

I'm programming my first android app.
I want to set text on a TextView.
(I already searched here and found out, how to do it...But it still don't work)
Do you know, what the problem is?
This is my Java code.
package com.example.randomcraps;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import com.example.*;
public class MainActivity extends Activity {
TextView text = (TextView) findViewById(R.layout.number);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void getRandomNumber(){
int i;
double savePoint;
savePoint = Math.random();
savePoint = savePoint*100+1;
i = (int)savePoint;
text.setText(i);
}
}
This is my XML Code
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="#string/Button1"
android:onClick="getRandomNumber" />
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/RandomNumber" />
</RelativeLayout>
Change to
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.number); // id of textview in activity_main.xml
}
findViewById looks for a view with the id mentioned in the current inflated layout. So you need to set layout to the activity and then initialize your views.
Also change
text.setText(i);
// looks for a resource with the id mentioned if not found you get ResourceNotFoundException
To
text.setText(String.valueOf(i));
// int i; i is an int value. Use String.valueOf(intvalue)
Edit:
Your method signature is different. It should be
public void getRandomNumber(View V){ // missing View as param
... //rest of the code
}
Coz you have
android:onClick="getRandomNumber"
Quoting from docs
public static final int onClick
Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).
Change your code to this.
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.number);
}
Also
text.setText(String.valueOf(i));
NOTE: findViewByID finds view from content. So basically you have to use that method after you set your content.
Set text color after setText and for guarantee set the font size. Sometimes it makes the color default white.

Android RelativeLayout - show items side by side when space but on separate lines when insufficient space?

I'm laying out an app which presents the results of a search in a ListView. I've defined each item to have a custom layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dp"
android:paddingRight="?android:attr/scrollbarSize" >
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceSearchResultTitle"
android:textIsSelectable="false" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/title"
android:layout_toRightOf="#+id/subtitle"
android:gravity="bottom|right"
android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
android:textIsSelectable="false" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/title"
android:gravity="bottom|left"
android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
android:textIsSelectable="false" />
</RelativeLayout>
This looks great when subtitle and date are of appropriate length to fit on a single line, however it looks awful if the subtitle consumes most of the line and forces date to take a very thin width and so wrap vertically.
What I'd like to do is have them appear side-by-side when there's space but on separate lines if there isn't. I've tried fiddling with the various layout_* attributes and the gravity to no avail and the question isn't very Google-able (at least, I can't think of the right words to search for). Can anyone point me towards the combination of layout rules that I need to achieve this? Or perhaps a different container if one would be more appropriate?
I believe the code below will do what you want but I can't see a way to do this only in the xml layout.
Basically I have added a textChangedListener to two different TextViews that have the two different layout options I believe you are looking for, both inside their own relative layout with the date displaying textview. When the subtitle is set the first of these is used to hold the text, if it requires more than a single line the second TextView is used and in either case the other has its visibility option set to GONE.
In my example I use a seperate thread to change the subtitle, hopefully this doesn't confuse things too much.
The layout xml is as follows:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceSearchResultTitle"
android:background="#FF009999"
android:textIsSelectable="false"
android:text="#string/my_title" />
<RelativeLayout
android:id="#+id/resizingTextContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title" >
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
android:textIsSelectable="false"
android:textColor="#FFFFFFFF"
android:background="#FF000000" />
<TextView
android:id="#+id/subtitle1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/date"
android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
android:textIsSelectable="false"
android:textColor="#FFFFFF"
android:background="#FF00FF00" />
<TextView
android:id="#+id/subtitle2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/date"
android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
android:textIsSelectable="false"
android:visibility="gone"
android:textColor="#FFFFFF"
android:background="#FF00FF00" />
</RelativeLayout>
<Button
android:id="#+id/startTestBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/resizingTextContainer"
android:layout_centerHorizontal="true"
android:text="Click To Begin"
/>
</RelativeLayout>
And the main activity code with the textview switching logic:
package com.example.code.examples.changelayoutwithtextlength;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextWatcher textChangeDisplayCheck = new TextWatcher(){
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
displayLatestSubtitle(s);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView DateTextView = (TextView)findViewById(R.id.date);
DateTextView.setText("29th April 2013");
Button b = (Button)findViewById(R.id.startTestBtn);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
v.setEnabled(false);
Thread thread = new testSubtitleThread();
thread.start();
}
});
TextView tv = (TextView)findViewById(R.id.subtitle1);
tv.addTextChangedListener(textChangeDisplayCheck);
TextView tv2 = (TextView)findViewById(R.id.subtitle2);
tv2.addTextChangedListener(textChangeDisplayCheck);
}
private void displayLatestSubtitle(CharSequence newSubtitle)
{
TextView tv = (TextView)findViewById(R.id.subtitle1);
TextView tv2 = (TextView)findViewById(R.id.subtitle2);
tv.removeTextChangedListener(textChangeDisplayCheck);
tv2.removeTextChangedListener(textChangeDisplayCheck);
tv.setVisibility(View.GONE);
tv2.setVisibility(View.GONE);
tv2.setText("");
tv.setText(newSubtitle);
if(tv.getLineCount() > 1)
{
tv.setText("");
tv2.setText(newSubtitle);
tv2.setVisibility(View.VISIBLE);
}
else
{
tv.setVisibility(View.VISIBLE);
}
tv.addTextChangedListener(textChangeDisplayCheck);
tv2.addTextChangedListener(textChangeDisplayCheck);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class testSubtitleThread extends Thread
{
String[] subtitles = new String[] { "a short one", "a really long winded subtitle that will take over more than the allowed space", "tiny",
"Really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, long.",
".....", "text just to long to fit on my device"};
private android.os.Handler handler = new android.os.Handler()
{
#Override
public void handleMessage(android.os.Message msg)
{
if(msg.what < subtitles.length)
{
TextView tv = (TextView)findViewById(R.id.subtitle1);
tv.setText(subtitles[msg.what]);
}
else
{
Button b = (Button)findViewById(R.id.startTestBtn);
b.setEnabled(true);
}
}
};
#Override
public void run()
{
for(int i = 0; i <= subtitles.length; i++)
{
handler.sendEmptyMessage(i);
try
{
Thread.sleep(3000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
I hope this helps.
In your layout, I would define two Views per item (subtitle and date), one in the same line and one in the line below.
Then I would check the length of those 2 fields (or their sum) and I would write this piece of code:
if (length > MAX_LENGTH_OF_LINE) > {
findViewById(R.id.subtitle_line_below).setVisibility(View.VISIBLE);
findViewById(R.id.subtitle_same_line).setVisibility(View.GONE);
} else {
findViewById(R.id.subtitle_line_below).setVisibility(View.GONE);
findViewById(R.id.subtitle_same_line).setVisibility(View.VISIBLE);
}

How to show top rated items into another activity using Custom ListView when using Rating Bar?

I have made an Android Rating Bar program to accept user rating, calculate the total ratings and display it in indicator rating bar.
Now i want to add two more functionalities in my Rating Bar program:
I want to use my existing Rating Bar in ListView
Secondly want to show high to low rated rating items in another activity like MyRatings
My Java Code:-
public class InteractiveRatingBarActivity extends Activity implements
OnRatingBarChangeListener {
RatingBar getRatingBar;
RatingBar setRatingBar;
TextView countText;
int count;
float curRate;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewsById();
setRatingBar.setRating(curRate);
getRatingBar.setOnRatingBarChangeListener(this);
}
private void findViewsById() {
getRatingBar = (RatingBar) findViewById(R.id.getRating);
setRatingBar = (RatingBar) findViewById(R.id.setRating);
countText = (TextView) findViewById(R.id.countText);
}
public void onRatingChanged(RatingBar rateBar, float rating,
boolean fromUser) {
DecimalFormat decimalFormat = new DecimalFormat("#.#");
curRate = Float.valueOf(decimalFormat.format((curRate * count + rating)
/ ++count));
Toast.makeText(InteractiveRatingBarActivity.this,
"New Rating: " + curRate, Toast.LENGTH_SHORT).show();
setRatingBar.setRating(curRate);
countText.setText(count + " Ratings");
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RatingBar
android:id="#+id/getRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0" />
<RatingBar
android:id="#+id/setRating"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/getRating"
android:isIndicator="true"
android:numStars="5"
android:stepSize="0.1" />
<TextView
android:id="#+id/countText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/getRating"
android:layout_toRightOf="#+id/setRating"
android:paddingLeft="10dp" />
</RelativeLayout>

Categories

Resources