Assume a group of 4 radio buttons. The user clicks on the first one, then he for example realizes that he made a mistake and he clicked on the 4th one. Is there a way to memorize just the last one clicked and to make the app forgets that the 1st one was clicked too?
Here is the simple example using SharedPreferences. Easier, less cumbersome than Sqlite.
Layout
demo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/rank_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/first_radio"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/first_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First" />
<RadioButton
android:id="#+id/second_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second" />
<RadioButton
android:id="#+id/third_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third" />
<RadioButton
android:id="#+id/fourth_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fourth" />
</RadioGroup>
</LinearLayout>
Activity
DemoAppActivity.java
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
public class DemoAppActivity extends ActionBarActivity implements
OnCheckedChangeListener {
private RadioButton mFirstRadioButton = null;
private RadioButton mSecondRadioButton = null;
private RadioButton mThirdRadioButton = null;
private RadioButton mFourthRadioButton = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_app);
initializeRadioGroup();
}
private void initializeRadioGroup() {
mFirstRadioButton = (RadioButton) findViewById(R.id.first_radio);
mSecondRadioButton = (RadioButton) findViewById(R.id.second_radio);
mThirdRadioButton = (RadioButton) findViewById(R.id.third_radio);
mFourthRadioButton = (RadioButton) findViewById(R.id.fourth_radio);
// Fetching last checked position in preferences
int lastCheckedPosition = PreferenceManager
.getDefaultSharedPreferences(this).getInt("last_checked", 0);
Log.d("TAG", "Fetching last saved position " + lastCheckedPosition);
switch (lastCheckedPosition) {
case 1:
mFirstRadioButton.setChecked(true);
break;
case 2:
mSecondRadioButton.setChecked(true);
break;
case 3:
mThirdRadioButton.setChecked(true);
break;
case 4:
mFourthRadioButton.setChecked(true);
break;
default:
mFirstRadioButton.setChecked(true);
break;
}
mFirstRadioButton.setOnCheckedChangeListener(this);
mSecondRadioButton.setOnCheckedChangeListener(this);
mThirdRadioButton.setOnCheckedChangeListener(this);
mFourthRadioButton.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int newCheckedPos = 0;
if (isChecked) {
switch (buttonView.getId()) {
case R.id.first_radio:
newCheckedPos = 1;
break;
case R.id.second_radio:
newCheckedPos = 2;
break;
case R.id.third_radio:
newCheckedPos = 3;
break;
case R.id.fourth_radio:
newCheckedPos = 4;
break;
}
}
if (newCheckedPos > 0) {
Log.d("TAG", "Saving new checked position " + newCheckedPos);
// Saving checked position in preferences
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putInt("last_checked", newCheckedPos).commit();
}
}
}
Related
I have a tabbed activity and in one of the fragments I have a radio group with 3 radio buttons. I am trying to figure out how to determine which radio buttons is selected. I suspect I needed to make a java file for the fragment. I am not sure if that is correct, though. I followed an example for the java file. I modified it and it is not working. Nothing happens when I click on a radio button.
fragment_calories_want.xml (omitted code not relevant to the question)
<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"
tools:context="layout.CaloriesWant">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="100px"
android:id="#+id/radioGroup"
android:orientation="horizontal"
android:layout_marginTop="82dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RadioButton
android:text="Grams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonGrams"
android:layout_weight="1"
tools:text="Grams" />
<RadioButton
android:text="Ounces"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonOunces"
android:layout_weight="1" />
<RadioButton
android:text="Pounds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonPounds" />
</RadioGroup>
</RelativeLayout>
CaloriesWantFragment.java
package com.example.crims.caloriecalculator;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class CaloriesWantFragment extends Fragment {
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private RadioGroup radioGroupOne;
String buttonSelected;
Activity activity;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_calories_want, container, false);
radioButton1 = (RadioButton) view.findViewById(R.id.radioButtonGrams);
radioButton2 = (RadioButton) view.findViewById(R.id.radioButtonOunces);
radioButton3 = (RadioButton) view.findViewById(R.id.radioButtonPounds);
radioGroupOne = (RadioGroup) view.findViewById(R.id.radioGroup);
radioGroupOne.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch(i){
case R.id.radioButtonGrams:
buttonSelected = "button one selected";
break;
case R.id.radioButtonOunces:
buttonSelected = "button two selected";
break;
case R.id.radioButtonPounds:
buttonSelected = "button three selected";
break;
default:
}
Toast.makeText(activity, "radio button selected " + buttonSelected, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
Initialize Radio Group
radioGroupOne = (RadioGroup) view.findViewById(R.id.radioGroup);
java file
public class CaloriesWantFragment extends AppCompatActivity {
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private RadioGroup radioGroupOne;
String buttonSelected;
Activity activity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
radioButton1 = (RadioButton) findViewById(R.id.radioButtonGrams);
radioButton2 = (RadioButton) findViewById(R.id.radioButtonOunces);
radioButton3 = (RadioButton) findViewById(R.id.radioButtonPounds);
radioGroupOne = (RadioGroup) findViewById(R.id.radioGroup);
radioGroupOne.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.radioButtonGrams:
buttonSelected = "button one selected";
break;
case R.id.radioButtonOunces:
buttonSelected = "button two selected";
break;
case R.id.radioButtonPounds:
buttonSelected = "button three selected";
break;
default:
}
Toast.makeText(CaloriesWantFragment.this, "radio button selected " + buttonSelected, Toast.LENGTH_SHORT).show();
}
});
}
Xml file
<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">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="100px"
android:id="#+id/radioGroup"
android:orientation="horizontal"
android:layout_marginTop="82dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RadioButton
android:text="Grams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonGrams"
android:layout_weight="1"
tools:text="Grams" />
<RadioButton
android:text="Ounces"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonOunces"
android:layout_weight="1" />
<RadioButton
android:text="Pounds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonPounds" />
</RadioGroup>
</RelativeLayout>
I am new to android. At the moment I am working on some examples in my starter book "Android 5"
In the example I am working there is some code which is not working.
XML:
<RadioGroup
android:id="#+id/rg_art"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/rb_art_netto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/txt_netto"
android:textSize="16dp"
android:checked="true" />
<RadioButton
android:id="#+id/rb_art_brutto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/txt_brutto"
android:textSize="16dp" />
</RadioGroup>
Activity:
package com.example.raven.tax_calc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
public class FormularActivity extends Activity {
public static final String BETRAG_KEY = "betrag";
public static final String BETRAG_ART = "art";
public static final String UST_PROZENT = "ust";
// Betrag
public void onClickBerechnen(View button) {
final EditText txtBetrag = (EditText) findViewById(R.id.edt_betrag);
final String tmpBetrag = txtBetrag.getText().toString();
float betrag = 0.0f;
if(tmpBetrag.length() > 0 ){
betrag = Float.parseFloat(tmpBetrag);
}
}
// Art des Betrages (Brutto, Netto)
boolean isNetto = true;
final RadioGroup rg = (RadioGroup) findViewById(R.id.rg_art);
switch (rg.getCheckedRadioButtonId()) {
case R.id.rb_art_netto:
isNetto = "true";
break;
case R.id.rb_art_brutto:
isNetto = false;
break;
default:
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.formular_activity);
}
}
rg.getCheckedRadioButtonId is red highlighted and mouse-over says "Cannot resolve symbol"
What am I doing wrong? Can't find an mistake :-(
You should place following block of code into your on click meaning cut it and paste right after your if statement. The issue is with braces and the fact that you assign string into boolean: (isNetto = "true";)
// Art des Betrages (Brutto, Netto)
boolean isNetto = true;
final RadioGroup rg = (RadioGroup) findViewById(R.id.rg_art);
switch (rg.getCheckedRadioButtonId()) {
case R.id.rb_art_netto:
isNetto = true;
break;
case R.id.rb_art_brutto:
isNetto = false;
break;
default:
}
i have an android application that display a flag and group of radio buttons that each time the user check any radio button the application will display a button that allow to go to the next page using intent .
what i need is that each time the user check the right answer the system must calculate its score until the application finish .
the score will start by 0/0 and will finish after four rounds.
for now i need just to display the score of the user in the second page
i will appreciate any help
MainActivity.java
package com.devleb.flagology;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
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.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btnS;
RadioGroup rdgS;
RadioButton rdS1, rdS2, rdS3, rdS4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rdgS = (RadioGroup) findViewById(R.id.rdg1);
rdgS.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(rdS1.isChecked()||rdS2.isChecked()||rdS3.isChecked()||rdS4.isChecked()){
btnS.setVisibility(View.VISIBLE);
}
}
});
rdS1 = (RadioButton) findViewById(R.id.rd_s1);
rdS2 = (RadioButton) findViewById(R.id.rd_s2);
rdS3 = (RadioButton) findViewById(R.id.rd_s3);
rdS4 = (RadioButton) findViewById(R.id.rd_s4);
btnS = (Button) findViewById(R.id.btn_s);
btnS.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String input;
String result = null;
Double dbl;
Intent i = new Intent(MainActivity.this, SecondActivity.class);
switch (v.getId()) {
case R.id.rd_s1:
input = rdS1.getText().toString();
dbl = Double.parseDouble(input);
i.putExtra("score", 0);
break;
case R.id.rd_s2:
i.putExtra("score", 0);
break;
case R.id.rd_s3:
i.putExtra("score", 25);
break;
case R.id.rd_s4:
i.putExtra("score", 0);
break;
default:
break;
}
startActivity(i);
}
});
}
#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) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.hint_icon:
ShowAlertDialog();
break;
case R.id.about_icon:
Toast.makeText(this, "developed by Georges Matta",
Toast.LENGTH_SHORT).show();
default:
break;
}
return super.onOptionsItemSelected(item);
}
public void ShowAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Hint")
.setMessage("The famouse sport is Bullfighting")
.setCancelable(false)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
edited code
rdS1 = (RadioButton) findViewById(R.id.rd_s1);
rdS2 = (RadioButton) findViewById(R.id.rd_s2);
rdS3 = (RadioButton) findViewById(R.id.rd_s3);
rdS4 = (RadioButton) findViewById(R.id.rd_s4);
btnS = (Button) findViewById(R.id.btn_s);
btnS.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String input;
String result = null;
Double dbl;
Intent i = new Intent(MainActivity.this, SecondActivity.class);
switch (rdgS.getCheckedRadioButtonId()) {
case R.id.rd_s1:
input = rdS1.getText().toString();
dbl = Double.parseDouble(input);
result = String.valueOf(dbl);
i.putExtra("score", result);
break;
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="#drawable/background"
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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="34dp"
android:src="#drawable/spanish" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Gess the Country of the flag"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#CCFF99" />
<RadioGroup
android:id="#+id/rdg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView1" >
<RadioButton
android:id="#+id/rd_s1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Egypt"
android:textColor="#CCFF99" />
<RadioButton
android:id="#+id/rd_s2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="China"
android:textColor="#CCFF99" />
<RadioButton
android:id="#+id/rd_s3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spanish"
android:textColor="#CCFF99" />
<RadioButton
android:id="#+id/rd_s4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/radioGroup1"
android:layout_below="#+id/radioGroup1"
android:text="Italy"
android:textColor="#CCFF99" />
</RadioGroup>
<Button
android:id="#+id/btn_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/rdg1"
android:layout_below="#+id/rdg1"
android:text="Next"
android:visibility="invisible" />
</RelativeLayout>
SecondActivity.java
package com.devleb.flagology;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class SecondActivity extends Activity {
TextView txt_result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
txt_result = (TextView) findViewById(R.id.txtResult);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("score");
txt_result.setText(value);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
activity_second.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=".SecondActivity" >
<TextView
android:id="#+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Simple solution is to modify your switch case as follows:
...
Intent i = new Intent(MainActivity.this, SecondActivity.class);
switch (rdgS.getCheckedRadioButtonId()) {
case R.id.rd_s1:
input = rdS1.getText().toString();
dbl = Double.parseDouble(input);
i.putExtra("score", 0);
break;
case R.id.rd_s2:
i.putExtra("score", 0);
break;
case R.id.rd_s3:
i.putExtra("score", 25);
break;
case R.id.rd_s4:
i.putExtra("score", 0);
break;
default:
break;
}
startActivity(i);
...
...
After this since your are passing in int values, you should use getInt() method. In your oncreate of SecondActivity do this:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = String.valueOf(extras.getInt("score"));
txt_result.setText(value);
}
There is a problem here:
Intent intent = new Intent(MainActivity.this, SecondPage.class);
intent.putExtra("showResult", result);
startActivity(intent);
This should match extras.getString("score"); string in the second activity, so change it to:
Intent intent = new Intent(MainActivity.this, SecondPage.class);
intent.putExtra("score", result);
startActivity(intent);
In your onClick(View v) handler, v refers to the button btn_s, therefore switch (v.getId()) will not work since v.getId() will always return id of the button, i.e. R.id.btn_s.
You need to query states of the radio button separately, for example use
if (rdS1.isChecked()) {
// first ratio button is checked
} else if (rdS2.isChecked()) {
// second is checked
} else if ...
Replace your switch (v.getId()) in onClick(View v) with that if.
Hi I'm trying to develop an application which runs for every interval time, lets say for every 1 minute it will display some Toast message.
But problem is I'm using RadioButton functionality is perfect but when I tap on one radio button it will be green, but when I close and re-open the activity I'll get as none of the radio buttons selected.
Here is my MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_one_min:
if (checked)
{
//some code
}
break;
case R.id.radio_ten_min:
if (checked)
{
//some code
}
break;
case R.id.radio_disable:
if (checked)
{
//some code
}
break;
}
}
}
and here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/radio">
<RadioButton android:id="#+id/radio_disable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disable"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_one_min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 minute"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_ten_min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10 minute"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
Please help me to solve this riddle.
Thanks in advance...
This code is useful for store the ratingbar state, when we start new activity, you will see the previous rating state..
package com.example.ratingbar;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class RatingbarMainActivity extends Activity {
RatingBar ratingbarClick;
Button sub_btn;
TextView textRatingView , textRatingViewSave;
Boolean val = true;
float ans = (float) 0.0;
//--------------------------------------------------------------------------------------------
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ratingbar_main);
ratingbarClick = (RatingBar) findViewById(R.id.ratingBar1);
ratingbarClick.setOnRatingBarChangeListener(rateObj);
SharedPreferences sharePref = PreferenceManager.getDefaultSharedPreferences
(RatingbarMainActivity.this);
ans = sharePref.getFloat("Get_Rating", 0.0f);
System.out.println("--------------------------------------ans = " + ans);
if(val) {
ratingbarClick.setRating(ans);
}
else {
ratingbarClick.setRating(ans);
}
textRatingView = (TextView) findViewById(R.id.ratingView);
}
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
RatingBar.OnRatingBarChangeListener rateObj = new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {
//textRatingView.setText(String.valueOf(rating));
ans = ratingbarClick.getRating();
SharedPreferences sharePref = PreferenceManager.getDefaultSharedPreferences
(RatingbarMainActivity.this);
SharedPreferences.Editor edit = sharePref.edit();
edit.putFloat("Get_Rating", ans);
edit.commit();
val = false;
}
};
//--------------------------------------------------------------------------------------------
}
---------------------------------------------------------------------------------------------------
activity_ratingbar_main.xml file
<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" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ratingBar1"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="23dp"
android:text="Select Your Rating Bar Here"
tools:context=".RatingbarMainActivity" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_marginTop="63dp" />
<TextView
android:id="#+id/ratingView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ratingBar1"
android:text="TextView" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Click To Save Rating In TextBox" />
</RelativeLayout>
it is the simplest way to do so,no need of sharedpreference at all.you will get confused while using it.keep the things simple like this
public class MainActivity extends Activity {
Public static int flag=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(flag==1)
radio_one_min.setChecked(true);
else if(flag==2)
radio_ten_min.setCheckek(true);
else if(flag==3)
radio_disable.setCheckek(true);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_one_min:
if (checked)
{
flag =1;
//some code
}
break;
case R.id.radio_ten_min:
if (checked)
{
flag=2 ;
//some code
}
break;
case R.id.radio_disable:
if (checked)
{
flag=3;
//some code
}
break;
}
}
}
[EDITED]
I found developer.android.com/training/basics/activity-lifecycle/recreating.html document first, so my first guess was using Bundles and on Save/Resume Instace State methods. However this does not seem to work well. Here's my final attempt at a working solution (using SharedPreferences class, as suggested by some users):
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
public class MainActivity extends Activity {
final String PREFERENCES = "prefs";
final String RADIO_BUTTON = "prefsval";
SharedPreferences sp;
Editor e;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = this.getSharedPreferences(PREFERENCES, MODE_PRIVATE);
e = sp.edit();
}
#Override
protected void onResume() {
super.onResume();
if (sp != null) {
if (sp.getInt(RADIO_BUTTON, 0) != 0) {
RadioButton rb;
rb = (RadioButton) findViewById(sp.getInt(RADIO_BUTTON, 0));
rb.setChecked(true);
}
}
}
public void onRadioButtonClicked(View view) {
e.putInt(RADIO_BUTTON, view.getId());
e.apply();
}
}
hi i want to show the result in edittext 2 only after click the convert button
also
the second button (clear) not shown when i run the program how to show it as the screen size is small
here is the code
package converter.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class now extends Activity {
/** defining variables */
String[] currencys1,currencys2;
String e,l,f,u,d;
double EURO=5,dollar=6,franc=7,LE=1,UAE=8,txtvalue;
EditText txt1,txt2;
Button convert,clear;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt1 =(EditText)findViewById(R.id.txt1);
txt2 =(EditText)findViewById(R.id.txt2);
convert=(Button)findViewById(R.id.btn1);
clear=(Button)findViewById(R.id.clear_btn);
currencys1=getResources().getStringArray(R.array.currency);
final Spinner s1=(Spinner)findViewById(R.id.sp1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, currencys1);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
switch(arg2)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
currencys2=getResources().getStringArray(R.array.currency1);
final Spinner s2=(Spinner)findViewById(R.id.sp2);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, currencys2);
s2.setAdapter(adapter1);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
txtvalue = Double.parseDouble(txt1.getText().toString());
switch(arg2)
{
case 0:
final String fromCurrencyCode = s1.getSelectedItem().toString();
final String toCurrencyCode = s2.getSelectedItem().toString();
if (fromCurrencyCode.equals(toCurrencyCode)) {
txt2.setText(txt1.getText());
}
else
{
double EURO1=txtvalue*EURO;
txt2.setText(String.valueOf(EURO1));
}
break;
case 1:
final String fromCurrencyCode1 = s1.getSelectedItem().toString();
final String toCurrencyCode1 = s2.getSelectedItem().toString();
if (fromCurrencyCode1.equals(toCurrencyCode1)) {
txt2.setText(txt1.getText());
}
else
{
double LE1=txtvalue*LE;
txt2.setText(String.valueOf(LE1));
}
break;
case 2:
final String fromCurrencyCode2 = s1.getSelectedItem().toString();
final String toCurrencyCode2 = s2.getSelectedItem().toString();
if (fromCurrencyCode2.equals(toCurrencyCode2)) {
txt2.setText(txt1.getText());
}
else
{
double franc1=txtvalue*franc;
txt2.setText(String.valueOf(franc1));
}
break;
case 3:
final String fromCurrencyCode3 = s1.getSelectedItem().toString();
final String toCurrencyCode3 = s2.getSelectedItem().toString();
if (fromCurrencyCode3.equals(toCurrencyCode3)) {
txt2.setText(txt1.getText());
}
else
{
double UAE1=txtvalue*UAE;
txt2.setText(String.valueOf(UAE1));
}
break;
case 4:
final String fromCurrencyCode4 = s1.getSelectedItem().toString();
final String toCurrencyCode4 = s2.getSelectedItem().toString();
if (fromCurrencyCode4.equals(toCurrencyCode4)) {
txt2.setText(txt1.getText());
}
else
{
double dollar1=txtvalue*dollar;
txt2.setText(String.valueOf(dollar1));
}
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
convert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//i want to put the code here which show result only after click that button
}
});
clear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txt2.setText(" ");
}
});
}
}
here is the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#cad1d9"
>
<TableLayout android:layout_width="match_parent" android:id="#+id/tableLayout1" android:layout_height="wrap_content">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="from" android:textColor="#5c6471"></TextView>
</TableLayout>
<Spinner android:id="#+id/sp1" android:layout_height="50dip" android:layout_width="match_parent"></Spinner>
<TextView android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Amount" android:textColor="#5c6471"></TextView>
<EditText android:text="1" android:id="#+id/txt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:numeric="decimal"></EditText>
<TextView android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="To" android:textColor="#5c6471"></TextView>
<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/sp2"></Spinner>
<TextView android:id="#+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="value" android:textColor="#5c6471"></TextView>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/txt2" android:text="1" android:editable="false"></EditText>
<Button android:editable="false" android:width="100px" android:gravity="center" android:id="#+id/btn1" android:text="Convert" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="#string/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:minWidth="400px" android:id="#+id/clear_btn"></Button>
</LinearLayout>
You can do the following to hide txt2 when you first create the activity:
txt2.setVisibility(View.GONE);
Then in the click handler for the convert button:
txt2.setVisibility(View.VISIBLE);
In the click handler for the clear button, presumably you would want to make it GONE again.
Regarding the problem of the clear button being off the screen: wrap your LinearLayout in a ScrollView (making the ScrollView the top view of the hierarchy with a single LinearLayout child). That way the user can scroll down to see all the content of your layout. You need to change the height of the LinearLayout to wrap_content:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#cad1d9"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
. . .
</LinearLayout
</ScrollView>