Friday 26 September 2014

Rattingbar simple example

Your activity_main like this;

<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:background="@drawable/blueworld"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RATE ME"
            android:textColor="@android:color/background_light" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >

        <RatingBar
            android:id="@+id/rate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="5"
            android:rating="2.0"
            android:stepSize="0.5" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_Rate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RATE" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt_result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RESULT:"
            android:textColor="@android:color/background_light" />

        <TextView
            android:id="@+id/txt_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:textColor="@android:color/background_light" />
    </LinearLayout>

</LinearLayout>

Your MainActivity.java like this;

package com.example.rattingbar;

import java.security.PublicKey;

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.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
TextView txt_display;
Button btn_rate;
RatingBar rate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

addListenerOnRattingBar();

btn_rate = (Button) findViewById(R.id.btn_Rate);

btn_rate.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Set",
Toast.LENGTH_LONG).show();
}
});

}

private void addListenerOnRattingBar() {
// TODO Auto-generated method stub
txt_display = (TextView) findViewById(R.id.txt_display);
rate = (RatingBar) findViewById(R.id.rate);
rate.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
txt_display.setText(String.valueOf(rating));

}
});

}

@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;
}


}

App like this;




No comments:

Post a Comment