Reading
Add Comment
What is Rating bar Android ?
Android RatingBar can be utilized to get the rating from the client. The Rating returns a gliding point number. It might be 2.0, 3.5, 4.0 and so forth.
Android RatingBar shows the rating in stars. Android RatingBar is the subclass of AbsSeekBar class.
The getRating() system for android RatingBar class gives back the rating number.
Android RatingBar Example
How about we see the straightforward case of rating bar in android.
Drag the RatingBar and Button from the pallete, now the activity_main.xml record will like this:
File: activity_main.xml
<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ratingBar1"
android:layout_below="@+id/ratingBar1"
android:layout_marginLeft="92dp"
android:layout_marginTop="66dp"
android:text="submit" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ratingBar1"
android:layout_below="@+id/ratingBar1"
android:layout_marginLeft="92dp"
android:layout_marginTop="66dp"
android:text="submit" />
</RelativeLayout>
Activity class
We should compose the code to show the rating of the client.
File: MainActivity.java
package com.example.rating;
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.Toast;
public class MainActivity extends Activity {
RatingBar ratingbar1;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick(){
ratingbar1=(RatingBar)findViewById(R.id.ratingBar1);
button=(Button)findViewById(R.id.button1);
//Performing action on Button Click
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
//Getting the rating and displaying it on the toast
String rating=String.valueOf(ratingbar1.getRating());
Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
}
});
}
@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;
}
}
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.Toast;
public class MainActivity extends Activity {
RatingBar ratingbar1;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick(){
ratingbar1=(RatingBar)findViewById(R.id.ratingBar1);
button=(Button)findViewById(R.id.button1);
//Performing action on Button Click
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
//Getting the rating and displaying it on the toast
String rating=String.valueOf(ratingbar1.getRating());
Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
}
});
}
@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;
}
}
0 comments:
Post a Comment