Well, the issue is... my code is supposed to output the amount from the radiobutton clicked by the user, but it's not outputting the amount at all... I'm really not sure what the problem is. I created a radiogroup and used if-statements to determine if a radiobutton was checked or not. I think the issue is somewhere in calculating the amount.
Do I need to make a button? I thought it would calculate the amount automatically with the if-statements when a radiobutton were clicked. If I need a button, how would I go about creating one? (I'm trying to avoid using listener for now)
I'm a complete noob at android/java. I really would appreciate it if someone could explain what's wrong with my code. Thanks!
JAVA CODE
package com.Rox.crazyjoe;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final double smallcst = 1.25;
final double mediumcst = 2.00;
final double largecst = 3.50;
double amount;
RadioButton small = (RadioButton) findViewById(R.id.radioSmall);
RadioButton medium = (RadioButton) findViewById(R.id.radioMedium);
RadioButton large = (RadioButton) findViewById(R.id.radioLarge);
if (small.isChecked())
{
amount = smallcst;
}
else if(large.isChecked())
{
amount = largecst;
}
else if(medium.isChecked())
{
amount = mediumcst;
}
else
{
amount = 0;
}
DecimalFormat money = new DecimalFormat("$##.00");
TextView t0 = (TextView)findViewById(R.id.textAmount);
t0.setText("Total Amount: " + money.format(amount));
}
}
XML CODE:
<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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="Size: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radioSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_toRightOf="#+id/textView1"
android:text="Small" />
<RadioButton
android:id="#+id/radioMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/radioButton1"
android:layout_below="#+id/radioButton1"
android:text="Medium" />
<RadioButton
android:id="#+id/radioLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/radioMedium"
android:layout_below="#+id/radioMedium"
android:text="Large" />
</RadioGroup>
<TextView
android:id="#+id/textAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="40dp"
android:layout_marginRight="54dp"
android:text="TextView" />
</RelativeLayout>
You will need an event to trigger the checking of the radio buttons. At the moment, it is just checking the if statement in the onCreate and that's it (if you had a radiobutton checked by default, a value would be set here). I recommend attaching a listener to the entire RadioGroup
radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton) findViewById(checkedId);
//other logic here (like what do you want to do with this selected RadioButton)
}
});
Related
I'm learning to build apps for Android by making a trivia app with 3 questions: 1 using radio buttons, 1 free text field, 1 with checkboxes.
The logic is that once a user chooses or enters an answer, the radio buttons, checkboxes and EditText will be grayed out, then the user will click on the score button to get a Toast with the score and a custom message.
I have 2 problems:
(1) The EditText doesn't gray out - I have tried multiple variations using setFocusable() and setEnabled() to no avail.
(2) The score variable doesn't assign question 2 (the one with the EditText) 1 point even when the answer entered is correct.
I have started over from scratch but I haven't been able to fix these things. Any leads?
My XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:padding="16dp"
android:background="#fefcf1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/seinfeld_trivia_logo"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Who are the 4 main characters?"
android:textColor="#000"
android:textSize="16sp"
android:layout_marginBottom="5dp"/>
<RadioGroup
android:id="#+id/question_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/question_one_answer_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jerry, Elaine, George, Kessler"
android:textSize="14sp"
android:onClick="onRadioButtonQ1Clicked"/>
<RadioButton
android:id="#+id/question_one_answer_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jerry, Elaine, George, Kramer"
android:textSize="14sp"
android:onClick="onRadioButtonQ1Clicked"/>
<RadioButton
android:id="#+id/question_one_answer_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Newman, Jerry, Kramer, George"
android:textSize="14sp"
android:onClick="onRadioButtonQ1Clicked"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is Kramer's given name?"
android:textColor="#000"
android:textSize="16sp"
android:layout_marginBottom="5dp"/>
<EditText
android:id="#+id/question_two_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onQ2AnswerEntered"
android:enabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is correct about Festivus? Check all that apply."
android:textColor="#000"
android:textSize="16sp"
android:layout_marginBottom="5dp"/>
<CheckBox
android:id="#+id/question_three_answer_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="There is an aluminum pole"
android:layout_marginBottom="5dp"
android:onClick="onCheckBoxesQ3Clicked"/>
<CheckBox
android:id="#+id/question_three_answer_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="You eat ham"
android:layout_marginBottom="5dp"
android:onClick="onCheckBoxesQ3Clicked"/>
<CheckBox
android:id="#+id/question_three_answer_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="You air your grievances"
android:layout_marginBottom="5dp"
android:onClick="onCheckBoxesQ3Clicked"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp">
<Button
android:id="#+id/reset_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#03A9F4"
android:textColor="#fff"
android:text="Try Again"
android:layout_gravity="center_horizontal"
android:layout_marginRight="30dp"
android:onClick="resetQuiz"/>
<Button
android:id="#+id/score_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#03A9F4"
android:textColor="#fff"
android:text="Show me my score"
android:layout_gravity="center_horizontal"
android:padding="20dp"
android:onClick="showScoreMessage"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
My Java
package com.example.android.seinfeldtrivia;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
//Declaring variables - Question 1
RadioButton Q1A1, Q1A2, Q1A3;
RadioGroup Q1RadioGroup;
//Declaring variables - Question 2
EditText Q2AnswerEditText;
//Declaring variable for extracting the string from the EditText on Q2
String question2Answer;
//Declaring variables - Question 3
CheckBox Q3A1, Q3A2, Q3A3;
//Declaring variables to check the state of the checkboxes on Q3
boolean checkedQ3A1, checkedQ3A2, checkedQ3A3;
//Declaring variable - Score
int score;
//Declaring variable for the score and reset button
Button scoreMe, resetQuiz;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing views - Question 1
Q1RadioGroup = (RadioGroup) findViewById(R.id.question_1);
Q1A1 = (RadioButton) findViewById(R.id.question_one_answer_one);
Q1A2 = (RadioButton) findViewById(R.id.question_one_answer_two);
Q1A3 = (RadioButton) findViewById(R.id.question_one_answer_three);
//Initializing views - Question 2
Q2AnswerEditText = (EditText) findViewById(R.id.question_two_answer);
//Initializing views - Question 3
Q3A1 = (CheckBox) findViewById(R.id.question_three_answer_one);
Q3A2 = (CheckBox) findViewById(R.id.question_three_answer_two);
Q3A3 = (CheckBox) findViewById(R.id.question_three_answer_three);
//Initializing views - Score & Reset buttons
scoreMe = (Button) findViewById(R.id.score_me);
resetQuiz = (Button) findViewById(R.id.reset_me);
}
//To disable the radio groups once the user has chosen an answer
public static void enableQuestion(View view, boolean enabled) {
view.setEnabled(enabled);
view.setFocusable(enabled);
if (view instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) view;
for (int i = 0; i < vg.getChildCount(); i++)
enableQuestion(vg.getChildAt(i), enabled);
}
}
//Getting the values of the answers entered
/*
* Checking which answer was selected - Question 1
*/
public void onRadioButtonQ1Clicked(View view) {
// Check that the user chose an answer
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question_one_answer_one:
if (checked) {
yourAnswerQ1 = Q1A1;
enableQuestion(Q1RadioGroup, false);
break;
}
//Correct answer
case R.id.question_one_answer_two:
if (checked) {
yourAnswerQ1 = Q1A2;
score++;
enableQuestion(Q1RadioGroup, false);
break;
}
case R.id.question_one_answer_three:
if (checked) {
yourAnswerQ1 = Q1A3;
enableQuestion(Q1RadioGroup, false);
break;
}
}
}
/*
* Checking which answer was selected - Question 2
*/
public void onQ2AnswerEntered(View view) {
question2Answer = Q2AnswerEditText.getText().toString();
question2Answer = question2Answer.toLowerCase();
if (question2Answer.equals("cosmo")) {
//If the question is not empty, check if the answer is correct and add one point to the score
//and display the score msg
score++;
}
Q2AnswerEditText.setEnabled(false);
}
/*
* Checking which answers were selected - Question 3
*/
public void onCheckBoxesQ3Clicked(View view) {
checkedQ3A1 = Q3A1.isChecked();
checkedQ3A2 = Q3A2.isChecked();
checkedQ3A3 = Q3A3.isChecked();
if (checkedQ3A1 && checkedQ3A3) {
score++;
}
// Check which checkbox button was checked
switch(view.getId()) {
case R.id.question_four_answer_one:
if (checkedQ3A1) {
yourAnswerQ3A1 = Q3A1;
enableQuestion(Q3A1, false);
break;
}
case R.id.question_four_answer_two:
if (checkedQ3A2) {
yourAnswerQ3A2 = Q3A2;
enableQuestion(Q3A2, false);
break;
}
case R.id.question_four_answer_three:
if (checkedQ3A3) {
yourAnswerQ3A3 = Q3A3;
enableQuestion(Q3A3, false);
break;
}
}
}
public void showScoreMessage(View view) {
String scoreMessage = String.format(getResources().getString(R.string.your_score_is), score);
String customMessage = "";
//To display a short message together with the score, according to what score the user got
if (score == 0 || score == 1) {
customMessage = "No soup for you!";
} else if (score == 2) {
customMessage = "Do a fit of strength and try again.";
} else {
customMessage = "You're a master of your domain.";
}
//Displaying the score message and the score
Toast.makeText(this, scoreMessage + customMessage, Toast.LENGTH_SHORT).show();
}
public void resetQuiz(View view) {
//Enabling the questions
enableQuestion(Q1RadioGroup, true);
//Resetting questions: clearing the checked radio buttons and checkboxes
Q1RadioGroup.clearCheck();
//Resetting questions: Q3 - deleting the text in the EditText
Q3AnswerEditText.setText("");
Q3AnswerEditText.setEnabled(true);
//if the checkboxes are checked, then uncheck and enable them
if (checkedQ3A1) {
Q3A1.setChecked(false);
enableQuestion(Q3A1, true);
}
if (checkedQ3A2) {
Q3A2.setChecked(false);
enableQuestion(Q3A2, true);
}
if (checkedQ3A3) {
Q3A3.setChecked(false);
enableQuestion(Q3A3, true);
}
//Resetting the score variable
score = 0;
}
}
Using android:enabled=false or editText.setEnabled(false)
It will grey out Edittext.
my problem is that I want a Radio Group that has 3 Radio Buttons, using the scheme below.
The three choices are:
1. [] Male
2. [] Female
3. [] Custom: (self-described identity)
However, the problem is that I want the user to type in their self-described identity into an EditText for me to retrieve.
So the following code is from my XML page, with some elements blocked out by "####".
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="####"
android:id="#+id/male_female_custom_choice"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_male"
android:checked="true" />
<RadioButton android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_female"
android:checked="false" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="####"
android:weightSum="1">
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_custom"
android:checked="false" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="####"
android:hint="####"
android:focusableInTouchMode="true"
android:gravity="center"
android:layout_weight="1.05"
android:textSize="14sp" />
<TextView
android:layout_width="42dp"
android:layout_height="43dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="####"
android:id="####"
android:singleLine="true"
android:gravity="center"
android:layout_marginLeft="0dp"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
As you can see, I have tried to use a LinearLayout to isolate the custom option.
However, there are unintended and undesired side effects.
1. The custom option can be selected in addition to the other 2 predefined genders.
2. The custom option cannot be selected on its own.
In the actual Java file for the activity, I have the following code:
// button, radio button, editText, and Spinner fields
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
// get selected radio button from RadioGroup
int selectedId = rSexGroup.getCheckedRadioButtonId();
// find radio button by returned id
rButton = (RadioButton)findViewById(selectedId);
// assign gender based on id of radio button
if (selectedId == 1) {
pat.gender = "male";
}
if (selectedId == 2) {
pat.gender = "female";
}
if (selectedId == 3) {
mEdit = (EditText)findViewById(R.id.####);
pat.gender = (mEdit.getText().toString());
}
Since I am a bit rusty with Java, it may be possible that I have some really newbish errors. Please advise.
Once again, I am looking for a way to get a set of 3 RadioButtons, each on an individual line, with the last RadioButton with an EditText adjacent to it from which I obtain the desired information.
EDIT: Here's a picture of what I want it to look like:
(http://i68.tinypic.com/ao2oow.png)
Unfortunately I need 10 reputation to post images. :(
Mohit's answer gives the EditText on a different line than the custom input.
(http://i63.tinypic.com/68u88x.png)
Please note that the orientation of the EditText is adjacent to the custom, and not below. I apologize for not clearly specifying enough what I wanted.
Because selectedId will not be 1,2 or 3....debug it you will get value..
The custom option cannot be selected on its own.
Remove your 3rd RadioButton from LinearLayout and replace below 2nd RadioButton and put your EditText and TextView inside LinearLayout..
On you listener get getCheckedRadioButtonId and getText() of that RadioButton and check it accordingly...
I dont no what is your task but here is how can get all three RadioButton working and get custom text too....
xml...
UPDATE
<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="com.ex.MainActivity" >
<RadioGroup
android:id="#+id/male_female_custom_choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Male" />
<RadioButton
android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="FeMale" />
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Custom" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/male_female_custom_choice"
android:layout_toRightOf="#+id/male_female_custom_choice"
android:orientation="horizontal"
android:weightSum="1" >
<EditText
android:id="#+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8"
android:ems="10"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="aa"
android:inputType="text"
android:textSize="14sp" />
<TextView
android:id="#+id/text"
android:layout_width="42dp"
android:layout_height="43dp"
android:layout_marginLeft="0dp"
android:layout_weight=".2"
android:gravity="center"
android:singleLine="true"
android:text="aaa"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</LinearLayout>
<Button
android:id="#+id/but"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/male_female_custom_choice"
android:text="Get" />
</RelativeLayout>
.java file..
public class MainActivity extends Activity {
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
public Button but;
public String str = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
but= (Button) findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = rSexGroup.getCheckedRadioButtonId();
rButton = (RadioButton)findViewById(selectedId);
if (rButton.getText().toString().equals("Male")) {
str = "Male";
}
if (rButton.getText().toString().equals("FeMale")) {
str = "FeMale";
}
if (rButton.getText().toString().equals("Custom")) {
mEdit = (EditText)findViewById(R.id.edit);
str = mEdit.getText().toString();
}
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
}
}
you can also set visibility of LinearLayout so that it only visible when custom in checked....
Hope it help..
put radio group in RelativeLayout
set third radiobutton text as empty/null
add EditText as layout_alignParentBottom="true"
now programmatically call EditText's onFocusChangeListener
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b)
thirdRadio.setCheched(true);
}
});
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm learning android programing and i just dont know how turn my xml into a working calculator.
here is my xml, i removed the styling for better reading
<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="com.afm.calculator.MainActivity" >
<TextView
android:id="#+id/tvresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="40dp" />
<Button
android:id="#+id/times"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:textSize="40sp" />
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="40dp" />
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="รท"
android:textSize="40sp" />
<Button
android:id="#+id/mines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="40sp" />
<Button
android:id="#+id/equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:textSize="40sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
here is my java:
package com.afm.cal;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements android.view.View.OnClickListener {
Button mines, plus, divide, times, equal;
EditText eto, ett;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mines = (Button) findViewById(R.id.mines);
plus = (Button) findViewById(R.id.plus);
divide = (Button) findViewById(R.id.divide);
times = (Button) findViewById(R.id.times);
equal = (Button) findViewById(R.id.equal);
eto = (EditText) findViewById(R.id.editText1);
ett = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.tvresult);
}
public void onClick(View arg0) {
}
}
please help me finish it and thx in advance
First add Listener to all your buttons using setOnClickListener method.
Eg: plus.setOnClickListener(this);
Make modifications in your onClick method something like below.
public void onClick(View view) {
if (view == mines) {
// do the calculation here
//update the TextView using tv.setText()
} else if (view == plus) {
// do the calculation here
//update the TextView
}
//.. So on for all buttons
}
Assign each button a method using android:onClick attribute of Button in xml.
For example android:onClick="doSomething" . This button will call the doSomething() method when it is clicked.
Your methods should get values from TextFields and do the operation inside these methods.Then display the result in TextView
I am trying to make an application on Android Studio with the help of a tutorial. I managed to get the User Interface right and I think that I have assigned the correct buttons too. I am unable to get where I am going wrong with my code. I am new to Java and so I am unable to pinpoint the error I have committed. I am posting below the code from the files I was asked to edit in the tutorial.
package com.example.to_dolistapplication.app;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;
public class MainActivity extends Activity implements OnClickListener {
Button btn1;
Button btn2;
Button btn3;
TextView textTitle;
EditText scoreText;
int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button);
btn2 = (Button)findViewById(R.id.button2);
btn3 = (Button)findViewById(R.id.button3);
scoreText = (EditText)findViewById(R.id.textView);
textTitle = (TextView)findViewById(R.id.editText);
//---set on click listeners on the buttons-----
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
// change font size of the text
textTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
}
#Override
public void onClick(View v) {
if (v == btn1){
counter++;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.CYAN);
}
if (v == btn2){
counter--;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.GREEN);
}
if (v == btn3){
counter = 0;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.RED);
}
}
}
Above is the File from MainActivity.java
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.to_dolistapplication.app.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+1"
android:id="#+id/button"
android:onClick="#string/intro"
android:layout_below="#+id/editText"
android:layout_toRightOf="#+id/textView"
android:layout_marginTop="79dp"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-1"
android:id="#+id/button2"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_marginTop="64dp"
android:layout_alignRight="#+id/button2"
android:layout_alignEnd="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textClock"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/textView2"
android:layout_alignBottom="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Score"
android:id="#+id/textView"
android:layout_alignBottom="#+id/editText"
android:layout_toLeftOf="#+id/editText" />
This is from the file activity_main.xml.
The app, when run on emulator, displays Unfortunately Counter App has stopped.
what might be the reason for the app not working? Please help.
**EditText** scoreText = (EditText)findViewById(R.id.textView);
**TextView** textTitle = (TextView)findViewById(R.id.editText);
you missmathed with types: android:id="#+id/editText" is EditText, but in Activity you wrote his id to TextView.
And you missmathed with types: android:id="#+id/textView" is TextView, but but in Activity you wrote his id as EditText.
You've mismatched id's in onCreate() method.
Use the debug mode or just look at your console output
How to disable radio buttons group after clicking on some radio button in Android activity ?
This is my XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tPitanje1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1. Neposredno regulisanje saobracaja na putevima vrse:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tPitanje1"
android:layout_marginLeft="20dp"
android:layout_marginTop="62dp" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="uniformisani komunalni policajci" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unoformisani policijski sluzbenici" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="inspektori za drumski saobracaj" />
</RadioGroup>
</RelativeLayout>
And this is my Java Code
package com.example.autoskola;
import android.app.Activity;
import android.os.Bundle;
public class obs1 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.obs1);
}
}
I'm interested in how to disable to change to click on another radiobutton?
If I'm understanding correctly, you want to disable the RadioGroup once a certain RadioButton is clicked. Just add an OnCheckedChangedListener and if the id of the RadioButton clicked is the right one, then disable the group by using setEnabled(false). Of course, you can't reenable the RadioGroup unless you have some other logic in your code that will do the re-enabling.
RadioGroup radioGroup = (RadioGroup)findViewById (R.id.radioGroup1);
radioGroup.setOnCheckedChangedListener (new OnCheckedChangedListener(){
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if (checkedId == R.id.radio0)
group.setEnabled(false);
}
});
Edit: It seems that setEnabled() doesn't work, so you should try changing your code so it loops through each RadioButton upon checking radio0:
if (checkedId == R.id.radio0){
for(int i = 0; i < group.getChildCount(); i++){
((RadioButton)rg1.getChildAt(i)).setEnabled(false);
}
}