Here I have created RatingBar function in android. In android RatingBar can be used to get the rating from the user, it is also used for creating RatingBar. Below code example will described you how to make RatingBar function in Android.
Step(1)- 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>
Step(2)-Activity class-
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) {
String rating=String.valueOf(ratingbar1.getRating());
Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
0 Comment(s)