Tuesday, 20 January 2015

RatingBar Example2

RatingBar Example with second approach in android




strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Task2.Ratingbar2</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="display">Display</string>
    <string name="tv1"></string>

</resources>

activity_main.xml
<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" >

 <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="61dp" />

  <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="66dp"
        android:layout_marginTop="50dp"
        android:text="@string/display" />

</RelativeLayout>

MainActivity

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
      
              TextView text;
              RatingBar ratingBar;
        
         @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);                      
              ratingBar=(RatingBar)findViewById(R.id.ratingBar1);          
              Button display=(Button)findViewById(R.id.button1);
              display.setOnClickListener(this);              
       }
         @Override
      
public void onClick(View v) {      
  float rate = ratingBar.getRating();            
Toast.makeText(getApplicationContext(), " "+rate, Toast.LENGTH_LONG).show();
       }
}



No comments:

Post a Comment