I wrote the XML for a Quiz App.
There will be (10) questions, some RadioGroup and some CheckBox, but the type of questions will not be in any order. So the 1st is radiogroup, the 2nd checkbox, the 3rd, 4th, and 5th radiogroup, the 6th and 7th checkbox, etc.
The RadioGroup obviously has only one correct answer, and the CheckBox have two or three correct answers.
I want to create a "CHECK QUIZ" button, which displays a message at the top of the scrolling quiz giving the number correct, and also displaying a large red "X" in red at the front of each wrong answer.
I am not sure where to start for the Java code in my MainActivity java file.
MainActivity.java
package com.example.android.quiztest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.EditText;
import android.widget.TextView;
/**
* package com.example.android.quiztest;
* This app displays a radio button and checkbox quiz, and then grades the quiz, * displaying the score and identifying the incorrect answers.
**/
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.android.quiztest.R.layout.activity_main);
}
}
activity_main.xml
<ScrollView 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.example.android.quiztest.MainActivity"
android:fillViewport="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/dimension_1"
android:orientation="vertical">
<EditText
android:id="#+id/name_text_view_STUDENT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/text_person_name"
android:inputType="textPersonName" />
<TextView
android:id="#+id/label_text_view_DIRECTIONS"
style="#style/HeaderTextStyle"
android:textSize="20sp"
android:layout_marginTop="#dimen/dimension_1"
android:text="#string/text_directions" />
<TextView
android:id="#+id/label_text_view_1"
style="#style/HeaderTextStyle"
android:layout_marginTop="#dimen/dimension_1"
android:text="#string/text_1" />
<RadioGroup
android:id="#+id/radioGroup_1"
style="#style/HeaderTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton_1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dimension_1"
android:paddingLeft="#dimen/dimension_4"
android:paddingRight="#dimen/dimension_5"
android:text="#string/text_1a"
android:textSize="#dimen/text_size_1" />
<RadioButton
android:id="#+id/radioButton_1b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dimension_1"
android:paddingLeft="#dimen/dimension_4"
android:paddingRight="#dimen/dimension_5"
android:text="#string/text_1b"
android:textSize="#dimen/text_size_1" />
<RadioButton
android:id="#+id/radioButton_1c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dimension_1"
android:paddingLeft="#dimen/dimension_4"
android:paddingRight="#dimen/dimension_5"
android:text="#string/text_1c"
android:textSize="#dimen/text_size_1" />
</RadioGroup>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginTop="25dp"
android:background="#c0c0c0" />
<TextView
android:id="#+id/label_text_view_2"
style="#style/HeaderTextStyle"
android:layout_marginTop="#dimen/dimension_1"
android:text="#string/text_2" />
<CheckBox
android:id="#+id/checkBox_2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/dimension_4"
android:paddingRight="#dimen/dimension_5"
android:text="#string/text_2a"
android:textSize="#dimen/text_size_1" />
<CheckBox
android:id="#+id/checkBox_2b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dimension_1"
android:paddingLeft="#dimen/dimension_4"
android:paddingRight="#dimen/dimension_5"
android:text="#string/text_2b"
android:textSize="#dimen/text_size_1" />
<CheckBox
android:id="#+id/checkBox_2c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dimension_1"
android:paddingLeft="#dimen/dimension_4"
android:paddingRight="#dimen/dimension_5"
android:text="#string/text_2c"
android:textSize="#dimen/text_size_1" />
</LinearLayout>
</ScrollView>
strings.xml
<resources>
<string name="text_button_1">check answers</string>
<string name="app_name">Quiz Test</string>
<string name="text_person_name">Type student name here.</string>
<string name="text_directions">Select ALL correct answers:</string>
<string name="text_1">1) probability distribution</string>
<string name="text_1a">1a) A smooth curve indicating the frequency distribution for a discontinuous random variable.</string>
<string name="text_1b">1b) A discontinuous dot diagram showing the frequency distribution for a random variable.</string>
<string name="text_1c">1c) A smooth curve indicating the frequency distribution for a continuous random variable.</string>
<string name="text_2">2) normal distribution</string>
<string name="text_2a">2a) A smooth double-peak bell-shaped curve symmetrical about the mean.</string>
<string name="text_2b">2b) A smooth single-peak curve </string>
<string name="text_2c">3c) A bell-shaped curve symmetrical about the mean. </string>
</resources>
I updated the XML to provide the ability to mark a red X in front of the wrong answers:
file: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="false"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/dimension_1"
android:orientation="vertical">
<EditText
android:id="#+id/name_textView_STUDENT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/text_person_name"
android:inputType="textPersonName" />
<TextView
android:id="#+id/textView_directions"
style="#style/HeaderTextStyle"
android:layout_marginTop="#dimen/dimension_1"
android:text="#string/text_directions"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dimension_1"
android:orientation="horizontal">
<TextView
android:id="#+id/incorrect_question_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/dimension_1"
android:gravity="center"
android:text="X"
android:textColor="#ff0000"
android:textSize="30dp"
android:visibility="gone" />
<TextView
android:id="#+id/textView_1"
style="#style/HeaderTextStyle"
android:layout_marginTop="#dimen/dimension_1"
android:text="#string/text_1" />
</LinearLayout>
<RadioGroup
android:id="#+id/radioGroup_1"
style="#style/HeaderTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton_1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dimension_1"
android:paddingLeft="#dimen/dimension_4"
android:paddingRight="#dimen/dimension_5"
android:textSize="#dimen/text_size_1"
android:text="#string/text_1a"
android:onClick="onClick_1a"/>
<RadioButton
android:id="#+id/radioButton_1b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dimension_1"
android:paddingLeft="#dimen/dimension_4"
android:paddingRight="#dimen/dimension_5"
android:text="#string/text_1b"
android:textSize="#dimen/text_size_1"
android:onClick="onClick_2a"/>/>
<RadioButton
android:id="#+id/radioButton_1c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dimension_1"
android:paddingLeft="#dimen/dimension_4"
android:paddingRight="#dimen/dimension_5"
android:text="#string/text_1c"
android:textSize="#dimen/text_size_1"
android:onClick="onClick_3a"/>
</RadioGroup>
And developed the Java grading logic for the radio groups and checkboxes:
file: MainActivity.java
package com.example.android.quiztest2;
/**
* *Below added my unique package name "com.example.android.justjava4"
*/
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
/**
* package com.example.android.justjava4;
* This app displays an order form to order coffee, and displays the information.
* quantity is a global variable
**/
public class MainActivity extends AppCompatActivity {
private RadioButton radioButton_1a, radioButton_1b, radioButton_1c, radioButton_4a, radioButton_4b, radioButton_4c,
radioButton_5a, radioButton_5b, radioButton_5c, radioButton_6a, RadioButton_6b, radioButton_6c,
radioButton_7a, radioButton_7b, radioButton_7c, radioButton_9a, radioButton_9b, radioButton_9c;
private CheckBox checkBox_2a, checkBox_2b, checkBox_2c, checkBox_3a, checkBox_3b, checkBox_3c, checkBox_8a, checkBox_8b, checkBox_8c;
int grade = 0;
public void Score(int grade) {
if (radioButton_1c.isChecked()) grade++;
if (radioButton_4c.isChecked()) grade++;
if (radioButton_5c.isChecked()) grade++;
if (radioButton_6c.isChecked()) grade++;
if (radioButton_7c.isChecked()) grade++;
if (radioButton_9c.isChecked()) grade++;
if (checkBox_2a.isChecked() && !checkBox_2b.isChecked() && checkBox_2c.isChecked()) grade++;
if (checkBox_3a.isChecked() && checkBox_3b.isChecked() && checkBox_3c.isChecked()) grade++;
if (checkBox_8a.isChecked() && !checkBox_8b.isChecked() && checkBox_8c.isChecked()) grade++;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.android.quiztest2.R.layout.activity_main);
final Context currentContext = this;
Button button_grade_quiz = (Button)
findViewById(R.id.button_grade_quiz);
button_grade_quiz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean answerCorrect1 = false;
TextView incorrectQuestionOne = (TextView) findViewById(R.id.incorrect_question_1);
if (!answerCorrect1) {
incorrectQuestionOne.setVisibility(View.VISIBLE);
} else {
incorrectQuestionOne.setVisibility(View.GONE);
}
}
});
}
}
Not sure how to connect the grading function to the other java methods.
Simple Answer: Add a button to your xml, and assign it an id. For example, the xml would most likely look something like this:
<Button
android:id="#+id/quiz_complete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Finish Quiz"
android:textSize="#dimen/text_size_1"
android:layout_gravity="center"/>
Then, in your MainActivity AFTER setContentView is called, you will find your button view by its id, and set an onClickListener to it. My sample code below includes a rough test that will show a Toast message when the button is clicked so that you can see the button is hooked up correctly to the listener.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context currentContext = this;
Button quizAnsweredButton = (Button) findViewById(R.id.quiz_complete_button);
quizAnsweredButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
Toast.makeText(currentContext, "TESTING", Toast.LENGTH_LONG).show();
}
});
}
(Note - to clarify, the bold above is to indicate that you can only reference views once they are shown - which is what is done by setContentView above. If you try to call findViewById before setContentView, you will receive a crash.)
Here is where it gets more complicated.
Without writing the whole of the solution, what I would do in order to reach your goal is to add the entirety of what you would like the quiz to look like, if the user submits the quiz with all answers wrong, in xml. I have created the xml for question 1. (I've added a horizontal LinearLayout in order to preserve the rest of your xml, but for the complexity of your view, I would recommend using a RelativeLayout)
As you can see, the visibility of the incorrect label is set to "gone". This means the view will not be seen, but also will not take up space with the parent view. "invisible" would hide the view, but its space would still be filled.
Now, when the test is submitted, we will show the incorrect answer label. In your complete solution, of course, you will evaluate whether the answer is correct or not.
XML for question one label:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/question_1_incorrect_label"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#color/red"
android:gravity="center"
android:visibility="gone"/>
<TextView
android:id="#+id/label_text_view_1"
style="#style/HeaderTextStyle"
android:layout_marginTop="#dimen/dimension_1"
android:text="#string/text_1" />
</LinearLayout>
Updated MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context currentContext = this;
Button quizAnsweredButton = (Button) findViewById(R.id.quiz_complete_button);
quizAnsweredButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean answer1Correct = false;
//TODO: Evaluate the actual value of answer1Correct boolean.
TextView questionOneIncorrectLabel = (TextView)findViewById(R.id.question_1_incorrect_label);
if (!answer1Correct) {
questionOneIncorrectLabel.setVisibility(View.VISIBLE);
}
else {
questionOneIncorrectLabel.setVisibility(View.GONE);
}
}
});
}
Note - in colors.xml, added:
<color name="red">#FF0000</color
The rest should just be some legwork to get it working, as well as the code within MainActivity to analyze whether or not each answer is correct.
Finally, a hint to simplify your correct answer message. You can create a string constant with a placeholder for a value to be added later. For example:
<string name="text_quiz_complete_answers">You answered %d question(s) correctly.</string>
When pulling this string value from the strings.xml file, you reference it like this:
String.format(getString(R.string.text_quiz_complete_answers), 5)
With 5 just being a placeholder. Instead there should be the numerical value you calculated.
For more info on formatting strings like this, I would reference this post: Are parameters in strings.xml possible?
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
I am creating an android app, I won't to navigate from "test1" to "test2" but when I press "btnNext" nothing happens. I am using the same code that I used for other navigations within my app so I don't understand why it won't work. Can someone help please?
"test1" 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" >
<TextView
android:id="#+id/txtSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTitle"
android:layout_centerHorizontal="true"
android:text="Please Answer the 9 Following Questions"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:text="Depression Test"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/txtQ1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtSubTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:text="Q.1. Have you found little pleasure or interest in doing things?"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioButton
android:id="#+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtQ1"
android:layout_below="#+id/radioButton1"
android:text="On some days" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtQ1"
android:layout_below="#+id/txtQ1"
android:text="No, not at all" />
<RadioButton
android:id="#+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/RadioButton01"
android:layout_below="#+id/RadioButton01"
android:text="On more than half the days" />
<RadioButton
android:id="#+id/RadioButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/RadioButton02"
android:layout_below="#+id/RadioButton02"
android:text="Nearly every day" />
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_toRightOf="#+id/txtQ1"
android:text="Next" />
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/RadioButton03"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/txtTitle"
android:text="Next" />
</RelativeLayout>
"Test1.java" code:
package com.lifematters;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class Test1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
//define Navigation Image Buttons
final Button nextBtn = (Button) findViewById(R.id.btnNext);
//Set up listener for Test
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//listener call this function
openTest2();
}
});
}
//Open test page
public void openTest2() {
//create new textview
Intent i = new Intent(getApplicationContext(), Test2.class);
startActivity(i);
}
}
"Test2.java" code:
package com.lifematters;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class Test2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
//define Navigation Image Buttons
final Button nextBtn = (Button) findViewById(R.id.btnNext);
//Set up listener for Test
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//listener call this function
openTest3();
}
});
}
//Open test page
public void openTest3() {
//create new textview
Intent i = new Intent(getApplicationContext(), Test3.class);
startActivity(i);
}
}
I have declared the activities in the Android Manifest and I am getting zero errors in my log cat.
You have two buttons named "btnNext". That's why it doesn't work.
Double check this:
"#+id/btnNext"
You have the same id set twice...
Change buttons code in xml to this
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_toRightOf="#+id/txtQ1"
android:text="Next" />
<Button
android:id="#+id/btnNext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/RadioButton03"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/txtTitle"
android:text="Next" />
you are having problem because you are calling button with wrong id. check your button id's in test2.xm as well. it will run then
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)
}
});
I can't make Android onClick events work at all. I've rendered a TextView and some buttons in activity_main.xml. In MainActivity.java I tryied to attach an onClick event to all buttons that will append their value to an input field. However, I'm having trouble finding some tutorials, books, etc on the subject of making dialers work. I have three books and none talk about using the call functions or making dialers. The tutorials I find online are all about people showing users how to root and theme their dialer, not how to program one. So my issues:
1) Where am I going wrong with appending a value to my Text view?
2) How will I remove the last value with the del onClick event?
3) How do I stop the Text view from pulling up the keyboard when a user clicks it? I.e. how to make it disabled/readonly (maybe) so only the buttons can update it?
Thanks for your help. In the below code I've cut the buttons down to one. All I've done is copy all the buttons and change the IDs, text, etc. So all the methods are the same but I can't get just one button working. Again, please don't laugh at me too hard for the code below.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/title_two"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:maxLength="15" >
<requestFocus />
</EditText>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/one"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="#+id/two"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="#+id/three"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/four"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:id="#+id/five"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:id="#+id/six"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="6" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/seven"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:id="#+id/eight"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:id="#+id/nine"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="9" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/star"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="*" />
<Button
android:id="#+id/zero"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:id="#+id/pound"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="#" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/callButton"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="Call" />
<Button
android:id="#+id/contacts"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="Con" />
<Button
android:id="#+id/del"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="Del" />
</LinearLayout>
</LinearLayout>
And MainActivity.java:
package com.example.dialertest
import android.app.Activity;
import android.content.Intent;
import android.content.ActivityNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button oneBtn;
Button twoBtn;
Button threeBtn;
Button fourBtn;
Button fiveBtn;
Button sixBtn;
Button sevenBtn;
Button eightBtn;
Button nineBtn;
Button starBtn;
Button zeroBtn;
Button poundBtn;
Button callBtn;
Button conBtn;
Button delBtn;
EditText numTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oneBtn = (Button) findViewById(R.id.one);
twoBtn = (Button) findViewById(R.id.two);
threeBtn = (Button) findViewById(R.id.three);
fourBtn = (Button) findViewById(R.id.four);
fiveBtn = (Button) findViewById(R.id.five);
sixBtn = (Button) findViewById(R.id.six);
sevenBtn = (Button) findViewById(R.id.seven);
eightBtn = (Button) findViewById(R.id.eight);
nineBtn = (Button) findViewById(R.id.nine);
starBtn = (Button) findViewById(R.id.star);
zeroBtn = (Button) findViewById(R.id.zero);
poundBtn = (Button) findViewById(R.id.pound);
callBtn = (Button) findViewById(R.id.callButton);
conBtn = (Button) findViewById(R.id.contacts);
delBtn = (Button) findViewById(R.id.del);
numTxt = (EditText) findViewById(R.id.editText1);
oneBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
oneBtn.setText("1");
numTxt.setText(numTxt.getText() + " " + oneBtn.getText());
//numTxt.append("1");
}
});
/**private void performDial(String numberString) {
if (!numberString.equals("")) {
Uri number = Uri.parse("tel:" + numberString);
Intent dial = new Intent(Intent.ACTION_CALL, number);
startActivity(dial);
}
}*/
/*private void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel: 8880000000"));
startActivity(callIntent);
} catch(ActivityNotFoundException e) {
Log.e("Dialed Number","Call Failed", e);
}
}
}*/
}
}
change your onClicklistener to this :
oneBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// no need to use onBtn here cuz the param v is the clicked View
Button clickedButton = (Button) v;
clickedButton.setText("1");
numTxt.setText(numTxt.getText().toString() + " " + clickedButton.getText());
//numTxt.append("1");
}
});
Edittext's getText() method returns an Editable Object, see :
http://developer.android.com/reference/android/widget/EditText.html#getText()
http://developer.android.com/reference/android/text/Editable.html
for getting the text of edittext you must call toString() after getText()
numTxt.setText(numTxt.getText().toString() + " " + clickedButton.getText());