In this tutorial we are creating Rating bar and some other basic components as Toggle button, Check Box. In rating bar we accepted user rating and displayed the rating as a Toast message.
This RatingBar we can create by two technics here
- using button click listner we can call getRating() method .
buttonrate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String s = String.valueOf(rb1.getRating()) ;
Toast.makeText(getApplicationContext(),s, Toast.LENGTH_LONG).show();
}
});
2. implement OnRatingBarChangeListener to accept user rating and display it as a Toast message.i.e.
rb1.setOnRatingBarChangeListener(this); //implements OnRatingBarChangeListener methods. @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show(); }Here, the rating bar is registered to the android.widget.RatingBar.OnRatingBarChangeListener using the method ratingbar.setOnRatingBarChangeListener(). When the event (user clicking on rating bar) occurs it calls onRatingChanged() method. For simplicity, we display a Toast message with user’s rating.
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:background="#CCFF66"
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="com.example.torach.MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kindly Check the following : "
android:layout_centerHorizontal="true"/>
<ToggleButton
android:id="@+id/tb"
android:layout_below="@id/tv"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/b1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/tb"
android:layout_centerHorizontal="true"
android:text="Toggle it"/>
<CheckBox
android:id="@+id/cb"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/b1"
android:layout_centerHorizontal="true"
android:text="Enable"
android:layout_marginTop="35dip"/>
<Button
android:id="@+id/b2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/cb"
android:layout_centerHorizontal="true"
android:text="Check" />
<RatingBar
android:id="@+id/rb"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/b2"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dip"/>
<Button
android:id="@+id/b3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/rb"
android:layout_centerHorizontal="true"
android:text="Rate"
/>
</RelativeLayout>
MainActivity.java
import android.support.v7.app.ActionBarActivity;import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends ActionBarActivity implements OnRatingBarChangeListener {
ToggleButton tb1;
RatingBar rb1;
CheckBox cb1;
Button bt1, bt2, bt3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb1 = (ToggleButton) findViewById(R.id.tb);
rb1 = (RatingBar) findViewById(R.id.rb);
rb1.setOnRatingBarChangeListener(this);
cb1 = (CheckBox) findViewById(R.id.cb);
bt1 = (Button) findViewById(R.id.b1);
bt2 = (Button) findViewById(R.id.b2);
bt3 = (Button) findViewById(R.id.b3);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String s = (String)tb1.getText();
Toast.makeText(getApplicationContext(), "Button is : " +s, Toast.LENGTH_LONG).show();
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer();
sb.append("Button is : ").append(cb1.isChecked());
Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_LONG).show();
}
});
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String s = String.valueOf(rb1.getRating()) ;
Toast.makeText(getApplicationContext(),s, 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.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "New Rating: " + rating,
Toast.LENGTH_SHORT).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.torach"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment