activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.kiranrating.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hollywood movies"
android:layout_marginTop="49dp"
android:id="@+id/textView" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:id="@+id/ratingBar1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Rating"
android:id="@+id/button"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
MainActivity.java :-
package com.kiranrating;
import android.app.Activity;
import android.os.Bundle;
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 r1;
Button btnsubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick(){
r1=(RatingBar)findViewById(R.id.ratingBar1);
btnsubmit=(Button)findViewById(R.id.button);
btnsubmit.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
String rating=String.valueOf(r1.getRating());
Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
}
});
}
}
Output :-
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.kiranrating.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hollywood movies"
android:layout_marginTop="49dp"
android:id="@+id/textView" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:id="@+id/ratingBar1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Rating"
android:id="@+id/button"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
MainActivity.java :-
package com.kiranrating;
import android.app.Activity;
import android.os.Bundle;
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 r1;
Button btnsubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick(){
r1=(RatingBar)findViewById(R.id.ratingBar1);
btnsubmit=(Button)findViewById(R.id.button);
btnsubmit.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
String rating=String.valueOf(r1.getRating());
Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
}
});
}
}
Output :-
RatingBar fig:1 |
RatingBar fig:2 |
No comments:
Post a Comment