Toast giving incorrect answer from Radio box references - android

I am pretty new to this and are not entirely sure why I am having this problem. From my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
RadioButton wavelength1;
RadioButton wavelength2;
double ray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wavelength1 = (RadioButton) findViewById(R.id.lowave);
wavelength2 = (RadioButton) findViewById(R.id.hiwave);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
If (wavelength1.isChecked());{
ray = 0.7125;
}
If (wavelength2.isChecked());{
ray = 0.4436;
}
Toast.makeText(MainActivity.this, String.valueOf(ray), Toast.LENGTH_LONG).show();
}
private void If(boolean checked) {
// TODO Auto-generated method stub
}
});
}
}
When I click on either radio boxes, the same output is given in the toast (as 0.4436) when they should be different. I am really at a loss to know what is going on. The app is aimed at API 10 and I am using Eclipse. (I am sure I am missing something).

Remove semicolor(;) at the end of if statement
If (wavelength1.isChecked()); < -- remove this
And also
use small (i) if it is method If();
remove this method
private void If(boolean checked) {
}

Related

string array changing onClick

I wish to have questions that change to the next question in the sequence onClick but I'm having trouble creating the array of questions and writing the onClick code for that portion.
*Too add more context to the app it will be a questionaire app with a question above an editText field and the users inputs will show up in its respective box at the top of the screen.
package com.example.greg;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class menu extends Activity {
Button mButton;
EditText mEdit;
int questionNumber = 0;
String [] questions;
int numberOfQuestions = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.userAnswereditText);
questions=new String[numberOfQuestions];
questions[0]="This is first question?";
questions[1]="This is second question?";
final TextView [] myTexts = new TextView[2];
myTexts[0]=(TextView)findViewById(R.id.varATextView);
myTexts[1]=(TextView)findViewById(R.id.varBTextView);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
myTexts[questionNumber].setText(mEdit.getText().toString());
mEdit.setText(null);
questionNumber++;
if(questionNumber < numberOfQuestions)
questionTextViewHolder.setText(questions[questionNumber]);
else
Toast.makeText(menu.this,"No more questions!",Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}

SharedPreferences ToggleButton not saving

*I've been going nuts with this problem the last few days.I'm trying to use SharedPreferences to allow my users to save the selected option of the in-game audio via the ToggleButton but everytime when I run my app and hit my "Done" button to go back to the main_activity screen and then click on settings again it never saves.I've done some research on this by googling,my book and this site but by many methods I've tried the result is the same.
I'm new to android development let alone app development so as much as I learned these past few weeks this has been a major roadblock for me and I apologize if this question has been covered already on this site but I'm at a real loss on what I'm doing wrong here.
Here is my code from my settings activity.
package com.fullfrontalgames.numberfighter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
public class Settings extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Button Notifications = (Button) findViewById(R.id.Notifications);
Button Done = (Button) findViewById(R.id.done);
Button AccountSettings = (Button) findViewById(R.id.AccountSettings);
final ToggleButton AT = (ToggleButton) findViewById(R.id.AudioToggle);
AT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
{
if ((AT.isChecked()))
{
SharedPreferences appPrefs =
getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences",
MODE_PRIVATE);
SharedPreferences.Editor editor = appPrefs.edit();
editor.putBoolean("atpref", AT.isChecked()); //value to store
editor.commit();
}
}
}
});
SharedPreferences appPrefs =
getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences",
MODE_PRIVATE);
boolean atpref = appPrefs.getBoolean("atpref", true); //default is true
AT.setChecked(atpref);
Done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent Intent = new Intent(Settings.this,activity_main.class);
Intent.setFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(Intent);
}
});
Notifications.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.fullfrontalgames.numberfighter.Notifications"));
}
});
AccountSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.fullfrontalgames.numberfighter.AccountSettings"));
}
});
}
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
view.setSoundEffectsEnabled(true);
} else {
view.setSoundEffectsEnabled(false);
}
}
}
If you are trying to save the ToggleButton's state when it is false, simply remove the if-statement in your OnClickListener:
if ((AT.isChecked()))

RadioGroup.setEnabled(false) doesn't work as expected

I have used setEnabled(false) to set it unable, but it doesn't work and after this method, the value of RadioGroup.isEnabled() is false. The value was changed.
The code is from Android Programming Guide.
PS: The Spinner component use the setEnabled(false) as well.
Code is as follows:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
public class TestRadioGroup extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.radiogroup);
final RadioGroup testRadioGroup = (RadioGroup) findViewById(R.id.testRadioGroup);
final Button changeEnabledButton = (Button) findViewById(R.id.changeEnabledButton);
changeEnabledButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
changeEnabled(testRadioGroup);
}
});
final Button changeBgColorButton = (Button) findViewById(R.id.changeBackgroundColorButton);
changeBgColorButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
changeBgColor(testRadioGroup);
}
});
}
protected void changeBgColor(RadioGroup testRadioGroup) {
// TODO Auto-generated method stub
testRadioGroup.setBackgroundColor(Color.BLUE);
}
protected void changeEnabled(RadioGroup testRadioGroup) {
// TODO Auto-generated method stub
if (testRadioGroup.isEnabled()) {
testRadioGroup.setEnabled(false);
} else {
testRadioGroup.setEnabled(true);
}
}
}
Iterate and disable each button belonging to the radio group via a loop, for example:
for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
testRadioGroup.getChildAt(i).setEnabled(false);
}
Views can be compose by multiple touchable elements. You have to disable them all, like this:
for(View lol : your_spinner.getTouchables() ) {
lol.setEnabled(false);
}
If it is a simple one since it also returns itself:
Find and return all touchable views that are descendants of this view, possibly including this view if it is touchable itself.
View#getTouchables()
You can't use the following code;
for(View lol : your_spinner.getTouchables() ) {
lol.setEnabled(false);
}
Once the views has disabled, there is no touchable child/descentant views anymore.

How can I keep my Java Android code short?

Hi I am developing an Android app using an EditText, and a add en minus button to control it's value.
I got it working now, but I wanna repeat it to multiple values. I know I can just repeat the code with different variables but the code would big really large.
Anyone an idea how to repeat the same code for multiple values?
This is my current code:
package com.lars.MyApp;
import com.lars.MyApp.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAppActivity extends Activity {
int currentValue = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText Value = (EditText) findViewById(R.id.Value);
Button addButton = (Button) findViewById(R.id.addButton);
Button minusButton = (Button) findViewById(R.id.minusButton);
addButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
addValue();
Value.setText("" + currentValue);
}
});
minusButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
minusValue();
Value.setText("" + currentValue);
}
});
}
public void addValue(){
if(currentValue <= 999){
currentValue = currentValue + 1;
}
}
public void minusValue(){
if(currentValue >= 1){
currentValue = currentValue - 1;
}
}
}
You should refactor your OnClickListeners so that they are generic. Probably the easiest way to do this is to change your addValue() method to be addValue(View v), and minusValue() to minusValue(View v). Then, in the layout xml, you add a property android:onClick=addValue or android:onClick=minusValue. You'll have to update the method bodies so that they check the view's id and do the right thing based on that, but you won't have to create and set a whole bunch of OnClickListeners in the onCreate method - you'll get that for free.

How to set numeric value in EditText?

I want to make a simple calculator My code is below:
package som.dev.android.calc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CalcApp extends Activity {
/** Called when the activity is first created. */
Button addValues, subValues, equalsValue;
EditText inputValue;
int inputNum = 0;
public int value1;
public int value2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// initializing Views
inputValue = (EditText) findViewById(R.id.editText1);
addValues = (Button) findViewById(R.id.addBtn);
subValues = (Button) findViewById(R.id.subBtn);
equalsValue = (Button) findViewById(R.id.equalsBtn);
// adding onClick Listeners
addValues.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
pressed = true;
value1 = Integer.parseInt(inputValue.getText().toString());
inputValue.setText("");
}
});
equalsValue.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
value2 = Integer.parseInt(inputValue.getText().toString());
int result = value1+value2;
inputValue.setText(result);// There an exception occurs....
}
});
}
}
But it does not sets result to Edittext in my app and an exception occurs and message comes Unfortunately app is colsed some thing like that so please any one tell me the solution of this problem
use this inputValue.setText(""+result);instead of inputValue.setText(result);// There an exception occurs....
just conversion integer into string
The reason you get an exception is that the EditText thinks you are setting a resource id. You need to convert the integer to a string yourself Integer.toString(result) before using it in the EditText Object. http://developer.android.com/reference/android/widget/TextView.html
change Following Line:-
inputValue.setText(result);
to
inputValue.setText(String.Valueof(result));
then your error is solved.

Categories

Resources