I am creating quiz app using php mysql json parsor, In that ran the program it shows "Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class RadioButton" the error on create xml file.
I am using these codes in QuizActivity.java crash log will throw the error on create content vie and layout inflater
public class QuizActivity extends AppCompatActivity {
private TextView quizQuestion;
private RadioGroup radioGroup;
private RadioButton optionOne;
private RadioButton optionTwo;
private RadioButton optionThree;
private RadioButton optionFour;
private int currentQuizQuestion;
private int quizCount;
private QuizWrapper firstQuestion;
private List<QuizWrapper> parsedObject;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); quizQuestion = (TextView)findViewById(R.id.quiz_question);
radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
optionOne = (RadioButton)findViewById(R.id.radio0);
optionTwo = (RadioButton)findViewById(R.id.radio1);
optionThree = (RadioButton)findViewById(R.id.radio2);
optionFour = (RadioButton)findViewById(R.id.radio3);
Button previousButton = (Button)findViewById(R.id.previousquiz);
Button nextButton = (Button)findViewById(R.id.nextquiz);
AsyncJsonObject asyncObject = new AsyncJsonObject();
asyncObject.execute("");
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
int radioSelected = radioGroup.getCheckedRadioButtonId();
int userSelection = getSelectedAnswer(radioSelected);
int correctAnswerForQuestion = firstQuestion.getCorrectAnswer();
if(userSelection == correctAnswerForQuestion){
// correct answer
Toast.makeText(QuizActivity.this, "You got the answer correct", Toast.LENGTH_LONG).show();
currentQuizQuestion++;
if(currentQuizQuestion >= quizCount){
Toast.makeText(QuizActivity.this, "End of the Quiz Questions", Toast.LENGTH_LONG).show();
return;
}
else{
firstQuestion = parsedObject.get(currentQuizQuestion);
quizQuestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers = firstQuestion.getAnswers().split(",");
uncheckedRadioButton();
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
}
else{
// failed question
Toast.makeText(QuizActivity.this, "You chose the wrong answer", Toast.LENGTH_LONG).show();
return;
}
}
});
previousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentQuizQuestion--;
if(currentQuizQuestion < 0){
return;
}
uncheckedRadioButton();
firstQuestion = parsedObject.get(currentQuizQuestion);
quizQuestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers = firstQuestion.getAnswers().split(",");
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
});
}
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"
tools:context=".QuizActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/question"
android:id="#+id/quiz_question"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:textColor="#000000"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/quiz_question"
android:layout_alignLeft="#+id/quiz_question"
android:layout_alignStart="#+id/quiz_question"
android:layout_marginTop="40dp"
android:id="#+id/radioGroup">
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio0"
android:textSize="15sp"
android:textColor="#000000"
android:text="#string/app_name"
android:layout_marginBottom="10dp"
android:paddingLeft="20dp"
android:button="#drawable/radio_bg"
android:checked="false" />
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio1"
android:textSize="15sp"
android:textColor="#color/black"
android:text="#string/app_name"
android:layout_marginBottom="10dp"
android:paddingLeft="20dp"
android:button="#drawable/radio_bg"
android:checked="false" />
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio2"
android:textSize="15sp"
android:textColor="#color/black"
android:text="#string/app_name"
android:layout_marginBottom="10dp"
android:paddingLeft="20dp"
android:button="#drawable/radio_bg"
android:checked="false" />
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/radio3"
android:textSize="15sp"
android:textColor="#color/black"
android:text="#string/app_name"
android:paddingLeft="20dp"
android:button="#drawable/radio_bg"
android:checked="false" />
</RadioGroup>
<Button
android:layout_height="wrap_content"
android:layout_width="160dp"
android:gravity="center"
android:id="#+id/nextquiz"
android:textColor="#color/white"
android:text="#string/next_questions"
android:background="#drawable/quizbutton"
android:layout_marginRight="10dp"
android:padding="5dp"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#+id/previousquiz"/>
<Button
android:layout_height="wrap_content"
android:layout_width="160dp"
android:gravity="center"
android:id="#+id/previousquiz"
android:textColor="#color/white"
android:text="#string/previous_questions"
android:background="#drawable/quizbutton"
android:layout_below="#+id/radioGroup"
android:layout_alignLeft="#+id/radioGroup"
android:padding="5dp"
android:layout_marginTop="20dp"
android:layout_alignStart="#+id/radioGroup" />
Caused by: android.view.InflateException: Binary XML file line #45: Error inflating class RadioButton
I think you have missed orientation attribute in <RadioGroup> element. Try,
android:orientation = "vertical"
inside your <RadioGroup> element and then try to clean and rebuild your project.
Hello please see this answer : https://stackoverflow.com/a/46646047/6632278
If you have created the file radio_bg in v24/drawable you must copy in drawable too for support to android devices before version 7
I had the same problem while setting custom radio icons in line:
android:button="#drawable/radio_bg"
Because I mistakenly pasted radio_bg.xml or vice versa In drawable-v24 and the error was only in older versions. so pasting same radio_bg.xml in common drawable folder fixed the problem.
Related
I have provided the xml and java code to 4 question. I work in it and every question have 4 radio buttons and in javam if the user choose the right answer it must add score in the end. The result must show the name and score, but I can't seems to continue from there
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFF00"
android:orientation="vertical">
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:inputType="textPersonName"
android:text="#string/name"
android:importantForAutofill="no"
android:hint="#string/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="130dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="22dp"
android:text="#string/Question1"
android:textAllCaps="true"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="20dp"
android:text="#string/firstQuestion"
android:textSize="16sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/GQ11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ11" />
<RadioButton
android:id="#+id/GQ12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ12" />
<RadioButton
android:id="#+id/GQ13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ13" />
<RadioButton
android:id="#+id/GQ14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ14" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="130dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="22dp"
android:text="#string/Questuin2"
android:textAllCaps="true"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="20dp"
android:text="#string/secondQuestion"
android:textSize="16sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/GQ21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ21" />
<RadioButton
android:id="#+id/GQ22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ22" />
<RadioButton
android:id="#+id/GQ23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ23" />
<RadioButton
android:id="#+id/GQ24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ24" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="130dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="22dp"
android:text="#string/Question3"
android:textAllCaps="true"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="20dp"
android:text="#string/thirdQuestion"
android:textSize="16sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/GQ31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ31" />
<RadioButton
android:id="#+id/GQ32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ32" />
<RadioButton
android:id="#+id/GQ33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ33" />
<RadioButton
android:id="#+id/GQ34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ34" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="130dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="22dp"
android:text="#string/Question4"
android:textAllCaps="true"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="20dp"
android:text="#string/fourthQuestion"
android:textSize="16sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/GQ41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ41" />
<RadioButton
android:id="#+id/GQ42"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ42" />
<RadioButton
android:id="#+id/GQ43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ43" />
<RadioButton
android:id="#+id/GQ44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/GQ44" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/end"
android:textSize="16sp"
android:padding="15dp"
android:layout_marginTop="22dp"
android:id="#+id/submit"
android:onClick="result"/>
</LinearLayout>
Java Class:
public class MainActivity extends AppCompatActivity {
int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public int answersQ1(View view) {
RadioButton aq1 = (RadioButton) findViewById(R.id.GQ11);
RadioButton aq2 = (RadioButton) findViewById(R.id.GQ12);
RadioButton aq3 = (RadioButton) findViewById(R.id.GQ13);
RadioButton aq4 = (RadioButton) findViewById(R.id.GQ14);
if (aq1.isChecked()) {
score = score + 1;
} else if (aq2.isChecked() && (aq3.isChecked() && (aq4.isChecked()))) {
score = score + 0;
}
return score;
}
public int answersQ2(View view) {
RadioButton ab1 = (RadioButton) findViewById(R.id.GQ21);
RadioButton ab2 = (RadioButton) findViewById(R.id.GQ22);
RadioButton ab3 = (RadioButton) findViewById(R.id.GQ23);
RadioButton ab4 = (RadioButton) findViewById(R.id.GQ24);
if (ab2.isChecked()) {
score = score + 1;
} else if (ab1.isChecked() && (ab3.isChecked() && (ab4.isChecked()))) {
score = score + 0;
}
return score;
}
public int answersQ3(View view) {
RadioButton ac1 = (RadioButton) findViewById(R.id.GQ31);
RadioButton ac2 = (RadioButton) findViewById(R.id.GQ32);
RadioButton ac3 = (RadioButton) findViewById(R.id.GQ33);
RadioButton ac4 = (RadioButton) findViewById(R.id.GQ34);
if (ac2.isChecked()) {
score = score + 1;
} else if (ac1.isChecked() && (ac3.isChecked() && (ac4.isChecked()))) {
score = score + 0;
}
return score;
}
public int answersQ4(View view) {
RadioButton ad1 = (RadioButton) findViewById(R.id.GQ41);
RadioButton ad2 = (RadioButton) findViewById(R.id.GQ42);
RadioButton ad3 = (RadioButton) findViewById(R.id.GQ43);
RadioButton ad4 = (RadioButton) findViewById(R.id.GQ44);
if (ad1.isChecked()) {
score = score + 1;
} else if (ad2.isChecked() && (ad3.isChecked() && (ad4.isChecked()))) {
score = score + 0;
}
return score;
}
public void result (View view){
// userName
EditText nameField = (EditText) findViewById(R.id.name);
Editable nameEditable = nameField.getText();
String name = nameEditable.toString();
Toast.makeText(getApplicationContext(), name , Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), score, Toast.LENGTH_SHORT).show();
}
}
When I press button submit the app crashes.
Your application crashes because you don't have an onClickListener. You need one to handle the button click. It should look something like this:
Button submitButton = (Button) findViewById(R.id.submit);
clickButton.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
// TODO
}
});
Inside the onClick method you want to perform the checks for the answers and anything you want to happen after the submit button is clicked. Additionally, since you have a long LinearLayout I would suggest you add a ScrollView.
I'm trying to show automaticly "This is the correct answer" or "Try again" just right after the radio button is pressed.
My question is:
How to add two strings
<string name="Good_answer">That is the correct answer</string>
<string name="Wrong_answer">Try again</string>
to this textView
<TextView
android:id="#+id/textViewAnswer"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:layout_below="#id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
so I can proceed this
for (boolean radioAnswer : answer)
correct = correct && radioAnswer;
if (correct)
tv.setText(R.string.Good_answer);
else
tv.setText(R.string.Wrong_answer);
And here is setOnClick... (at first it was with checkbutton 'mbuton')
mbuton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean check = true;
boolean correct = true;
// To check if all questions have been answered
for (boolean radioChecked : checked)
check = check && radioChecked;
if (check) {
// To check if all questions have been answered correctly
for (boolean radioAnswer : answer)
correct = correct && radioAnswer;
if (correct)
tv.setText(R.string.Good_answer);
else
tv.setText(R.string.Wrong_answer);
}
else
tv.setText("Answer all questions");
}
});
Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
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">
<RadioGroup
android:id="#+id/rg1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 1"/>
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<RadioGroup
android:layout_marginTop="16dp"
android:layout_below="#+id/rg1"
android:id="#+id/rg2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 2"/>
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView
android:id="#+id/textViewAnswer"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:layout_below="#id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
And string
<string name="Good_answer">That is the correct answer</string>
<string name="Wrong_answer">Try again</string>
// i have modified your code, please check it.
// i have display message if user does not select any radio button,
//another wise it display correct answer count on textview.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class Main2Activity2 extends Activity {
TextView tv;
Button mbuton;
RadioGroup rg1,rg2;
ArrayList<Integer> arrayListOfRadioGroupId =new ArrayList<Integer>();
int noAnswerCount= 0;
int correctAnswerRadioButtonCount= 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textViewAnswer);
mbuton = (Button) findViewById(R.id.mbuton);
rg1 = (RadioGroup)findViewById(R.id.rg1);
rg2 = (RadioGroup)findViewById(R.id.rg2);
// Store Radio group id to arraylist
arrayListOfRadioGroupId.add(rg1.getId());
arrayListOfRadioGroupId.add(rg2.getId());
mbuton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
noAnswerCount= 0;
correctAnswerRadioButtonCount= 0;
for(int radioGroupId: arrayListOfRadioGroupId)
{
RadioGroup objRadioGroup = (RadioGroup) findViewById(radioGroupId);
int checkedId=objRadioGroup.getCheckedRadioButtonId();
// get Selected Radio button id.
if(checkedId>-1) {
RadioButton rB = (RadioButton) findViewById(checkedId);
String strRadioButtonText = rB.getText().toString();
// get Selected Radio button Text.
if(strRadioButtonText.equals("Correct Option"))
{
correctAnswerRadioButtonCount ++;
noAnswerCount --;
}
}
else
{
noAnswerCount++;
}
}
if(noAnswerCount > 0)
{
tv.setText("Answer all questions");
}
else
{
tv.setText("Correct Answer Count is: " +correctAnswerRadioButtonCount);
}
}
});
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radioButton5) {
RadioButton rB = (RadioButton) findViewById(checkedId);
String strRadioButtonText = rB.getText().toString();
// get Selected Radio button Text.
if(strRadioButtonText.equals("Correct Option")) {
tv.setText("Correct Answer");
}
else
{
tv.setText("Wrong Answer");
}
}
}
});
}
}
// my Xml code is.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
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">
<RadioGroup
android:id="#+id/rg1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 1"/>
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<RadioGroup
android:layout_marginTop="16dp"
android:layout_below="#+id/rg1"
android:id="#+id/rg2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question 2"/>
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Correct Option"
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:text="Wrong Option"
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView
android:id="#+id/textViewAnswer"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textAlignment="center"
android:layout_below="#id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/mbuton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MButton"
android:layout_below="#id/textViewAnswer"/>
</RelativeLayout>
If your problem is just about setting those string in the TextView then, try this:
//For correct answer.
String CorrectAnswer = getString(R.string.Good_answer);
tv.setText(CorrectAnswer);
//For wrong answer.
String WrongAnswer = getString(R.string.Wrong_answer);
tv.setText(WrongAnswer);
The Code is still unclear (I can't Figure out what mButton is) .
However can you also share your layout xml I would suggest having a Radio Group comprising of X number of Radio Buttons (options) that you like and put the on checkedChangeListener
<RadioGroup
android:id="#+id/rgroup"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#drawable/background"
android:gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/test_image"
android:button="#null" />
<RadioButton
android:id="#+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/test_image"
android:button="#null" />
</RadioGroup>
Your Activity should implement the OnCheckedChangeListener and you can then define your Radio Group RadioGroup Options = (RadioGroup) findViewById(R.id.rgroup);
Then you can implement a method in your activity where if a option inside that Radio Button is clicked you can use the below method.
#Override
public void onCheckedChanged(RadioGroup group,
int checkedId)
{
switch (checkedId)
{
case R.id.option1:
// settextview to correct here
break;
case R.id.option2:
//set textview to wrong here
break;
default:
break;
}
}
Assuming you might have answers at dynamic positions you might want to code some logic in the on checked change method . Hope this helps
Layout file; look at those questions and answers. I've hardcoded the string here. You can set it in string resource file and fetch it here using #string or set it dynamically using yourTextView.setText() method.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
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">
<RadioGroup
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your name?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Melisa" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alisha" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lolita" />
</RadioGroup>
<RadioGroup
android:id="#+id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rg1"
android:layout_marginTop="16dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What are you learning?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++" />
<RadioButton
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<RadioButton
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HTML" />
</RadioGroup>
<TextView
android:id="#+id/textViewAnswer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rg2"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
And in your activity.
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroupFirst, radioGroupSecond;
private String[] realAnswers = {"Melisa", "Android"};
private String[] userAnswers;
private TextView tv;
private boolean status = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroupFirst = (RadioGroup) findViewById(R.id.rg1);
radioGroupSecond = (RadioGroup) findViewById(R.id.rg2);
tv = (TextView) findViewById(R.id.textViewAnswer);
userAnswers = new String[2];
radioGroupFirst.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
tv.setText("");
RadioButton nameRadio = (RadioButton) findViewById(checkedId);
Log.e("ID", "" + nameRadio.getText().toString());
userAnswers[0] = nameRadio.getText().toString();
}
});
radioGroupSecond.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton learningRadio = (RadioButton) findViewById(checkedId);
Log.e("ID", "" + learningRadio.getText().toString());
userAnswers[1] = learningRadio.getText().toString();
if (checkStatus(userAnswers))
tv.setText(getString(R.string.Good_answer));
else
tv.setText(getString(R.string.Wrong_answer));
}
});
}
private boolean checkStatus(String[] userAnswers) {
status = true;
for (int i = 0; i < userAnswers.length; i++) {
Log.e(userAnswers[i], realAnswers[i]);
if (!userAnswers[i].equals(realAnswers[i]))
status = false;
}
return status;
}
}
I haven't use the string resources here, if you want to set the text view with the string from resources too, look it in my first answer.
So, I think this will help you.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="1. Do you have an anti-virus installed on your computer :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_virus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_virusyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="assessUserInput"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_virusno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="No" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="48dp"
android:text="If No, skip question 2"
android:textAllCaps="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:text="2. How many anti-viruses do you use on your laptop :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_avnumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_avnumberone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="1" />
<RadioButton
android:id="#+id/radio_avnumbermore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="More than 1" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="3. Do you use pop-up/advert blockers:"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_adblock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_adblockyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="assessUserInput"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_adblockno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="No" />
</RadioGroup>
Instead of writing text to guide a user to skip question 2 after he
selects "No" in question 1, i want to be able to find a way to disable question two using java or xml codes when the user selects a "No". or automatically take question two away.
Activity:
(Note:i dont know why you want the strings so i separated them from the disabling method but with this it will disable the radio buttons if the user pressed no and they will enable when the user presses yes)
public class main extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainz);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_virus);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioGroup rg1 = (RadioGroup) findViewById(R.id.radio_avnumber);
Log.w("","click!");
if (checkedId == R.id.radio_virusno) {
Log.w("","asdasda");
for (int i = 0; i < rg1.getChildCount(); i++) {
rg1.getChildAt(i).setEnabled(false);
}
}else
{
Log.w("","else");
for (int i = 0; i < rg1.getChildCount(); i++) {
rg1.getChildAt(i).setEnabled(true);
}
}
}
});
}
public void assessUserInput(View view){
//// constructor method for Radio Group : Radio_virus
RadioGroup rgVirus=(RadioGroup)findViewById(R.id.radio_virus);
String radioVirus=((RadioButton)findViewById(rgVirus.getCheckedRadioButtonId())).getText().toString();
//// constructor method for Radio Group : Radio_avnumber
RadioGroup rgAvnNumber=(RadioGroup)findViewById(R.id.radio_avnumber);
String radioAvNumber=((RadioButton)findViewById(rgAvnNumber.getCheckedRadioButtonId())).getText().toString();
/// constructor method for Radio Group : Radio_adblock
RadioGroup rgAdBlock=(RadioGroup)findViewById(R.id.radio_adblock);
String radioAdBlock=((RadioButton)findViewById(rgAdBlock.getCheckedRadioButtonId())).getText().toString();
// constructor method for Input field
//EditText nameTextField=(EditText)findViewById(R.id.edit_name);
//String hasName=nameTextField.getText().toString();
}
}
Used xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="1. Do you have an anti-virus installed on your computer :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_virus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_virusyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_virusno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="48dp"
android:text="If No, skip question 2"
android:textAllCaps="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:text="2. How many anti-viruses do you use on your laptop :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_avnumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_avnumberone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<RadioButton
android:id="#+id/radio_avnumbermore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="More than 1" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="3. Do you use pop-up/advert blockers:"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_adblock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_adblockyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_adblockno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
</LinearLayout>
You have to handle onClick of your radiobutton :
public void assessUserInput(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_virusyes:
if (checked){
for(int i = 0; i < radio_avnumber.getChildCount(); i++){
((RadioButton)radio_avnumber.getChildAt(i)).setEnabled(true);}}
break;
case R.id.radio_virusno:
if (checked){
for(int i = 0; i < radio_avnumber.getChildCount(); i++){
((RadioButton)radio_avnumber.getChildAt(i)).setEnabled(false);}
}
break;
}
}
public void assessUserInput(View view) {
//// constructor method for Radio Group : Radio_virus
RadioGroup rgVirus = (RadioGroup) findViewById(R.id.radio_virus);
String radioVirus = ((RadioButton) findViewById(rgVirus.getCheckedRadioButtonId())).getText().toString();
//// constructor method for Radio Group : Radio_avnumber
RadioGroup rgAvnNumber = (RadioGroup) findViewById(R.id.radio_avnumber);
String radioAvNumber = ((RadioButton) findViewById(rgAvnNumber.getCheckedRadioButtonId())).getText().toString();
/// constructor method for Radio Group : Radio_adblock
RadioGroup rgAdBlock = (RadioGroup) findViewById(R.id.radio_adblock);
String radioAdBlock = ((RadioButton) findViewById(rgAdBlock.getCheckedRadioButtonId())).getText().toString();
// constructor method for Input field
EditText nameTextField = (EditText) findViewById(R.id.edit_name);
String hasName = nameTextField.getText().toString();
// Is the button now checked
radioVirus.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId = radio_virusno) {
//set the style of the things you want to disable here
}
}
});
I'm having a problem with my code. Whenever I run it, it shows the error error application stopped unexpectedly. I think the issue is caused by some error in the Java class. What exactly is causing this issue?
Here is my XML file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="10dp"
android:overScrollMode="always"
android:layout_height="match_parent"
android:background="#A52A2A"
tools:context="com.safeermalik.gpa.Start">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Subject :"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject Name"
android:id="#+id/subject"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Marks :"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject Marks"
android:id="#+id/marks"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Credits :"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject Credits"
android:id="#+id/credits"/>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Criteria :"/>
<RadioButton android:id="#+id/radio_old"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Old Criteria"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Criteria"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:padding="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add course"
android:background="#696969"
android:id="#+id/add"
android:onClick="sendMessage"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="#+id/add_table"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Subject "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Marks "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Credits "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="Percentage "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:textStyle="bold"
android:textSize="18sp"
android:text="GPA "/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="Total Marks : "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/total_marks"
android:hint="Total Marks "/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="S-GPA : "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/s_gpa"
android:hint="GPA "/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:text="PERCENTAGE : "/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/final_percentage"
android:hint="Percentage "/>
</TableRow>
</TableLayout>
</LinearLayout>
Here is the Java class:
public class Start extends ActionBarActivity {
EditText sub;
EditText mark;
EditText credit;
Button Adds;
int a=0,b=0,hour=0,x=0,counter=0;
String grade="";
String subject="";
EditText SUM,percent,GPA;
double gpa,gpa_multiple;
TableLayout table;
CalcGPA cal=new CalcGPA();
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio_new:
if (checked)
x=1;
break;
case R.id.radio_old:
if (checked)
x=0;
break;
} }
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
sub=(EditText)findViewById(R.id.subject);
subject=sub.getText().toString();
mark=(EditText)findViewById(R.id.marks);
a=Integer.parseInt(String.valueOf(mark));
credit=(EditText)findViewById(R.id.credits);
b=Integer.parseInt(String.valueOf(credit));
Adds=(Button)findViewById(R.id.add);
table=(TableLayout)findViewById(R.id.add_table);
SUM=(EditText)findViewById(R.id.total_marks);
GPA=(EditText)findViewById(R.id.s_gpa);
percent=(EditText)findViewById(R.id.final_percentage);
Adds.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sub.setText("");
mark.setText("");
credit.setText("");
counter++;
hour +=b;
if(x==0){
gpa=cal.subgpa(a);
grade=cal.Grade(a);
}
else if(x==1){
gpa=cal.ChangedGpa(a);
grade=cal.ChangedGrade(a);
}
add_row(subject,a,b,gpa,grade);
gpa_multiple+=(gpa*b);
SUM.setText(""+(counter*a));
GPA.setText(""+(gpa_multiple/hour));
percent.setText(""+((counter*a)/(counter*100)*100));
}
} );
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.start, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void add_row(String sub, int crdt, int marks, double sgpa, String subgrade ){
TableLayout tb = (TableLayout) findViewById(R.id.add_table);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView subject_name=new TextView(this);
subject_name.setText(counter+""+sub);
subject_name.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_name.setBackgroundResource(R.drawable.border);
TextView subject_marks=new TextView(this);
subject_marks.setText(""+marks);
subject_marks.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_marks.setBackgroundResource(R.drawable.border);
TextView subject_credits=new TextView(this);
subject_credits.setText(""+crdt);
subject_credits.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_credits.setBackgroundResource(R.drawable.border);
TextView subject_gpa=new TextView(this);
subject_gpa.setText(""+sgpa);
subject_gpa.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
subject_gpa.setBackgroundResource(R.drawable.border);
TextView subject_grade=new TextView(this);
subject_grade.setText(""+subgrade);
subject_grade.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
subject_grade.setBackgroundResource(R.drawable.border);
}
}
You have a logical error and a runtime error here.
Why are you parsing your content of your EditText in the onCreate() method?
just remove those two lines :
//a = Integer.parseInt(String.valueOf(mark));
//b = Integer.parseInt(String.valueOf(credit));
First you don't need to parse them in the onCreate method. You parse the content in the onClick() method of your Button.
that was the logical error.
Second:
Your runtime error is being cause by the fact that you are trying to get the valueOf an EditText and not of a String. so mark should be replace by mark.getText().toString() and credit by credit.getText().toString()
Here's my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<TextView android:id="#+id/welcome_text"
android:text = "Log-In"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="40dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/username_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="20dip"
android:layout_marginTop="100dip"
android:layout_marginLeft="30dip"
android:text="Username:"
android:textColor="#000000"/>
<EditText
android:id="#+id/txt_username"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_toRightOf="#id/username_text"
android:layout_alignTop="#id/username_text"/>
<TextView
android:id="#+id/password_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/username_text"
android:layout_marginRight="20dip"
android:layout_marginTop="30dip"
android:layout_marginLeft="30dip"
android:text="Password:"
android:textColor="#000000"/>
<EditText
android:id="#+id/txt_password"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_toRightOf="#id/password_text"
android:layout_alignTop="#id/password_text"
android:layout_below="#id/txt_username"
android:layout_marginLeft="5dip"
android:password="true" />
<Button
android:id="#+id/login_button"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_password"
android:layout_alignParentLeft="true"
android:layout_marginTop="35dip"
android:layout_marginLeft="110dip"
android:text="Submit" />
<TextView
android:id="#+id/tv_error"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:textSize="5pt"
android:layout_alignParentLeft="true"
android:layout_below="#id/txt_password"
android:layout_marginRight="9dip"
android:layout_marginTop="9dip"
android:layout_marginLeft="15dip"
android:textColor="#FF0000"
android:text=""/>
<TextView android:id="#+id/tv_login"
android:text = "#string/Login"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="40dp"
android:layout_below="#id/login_button"
android:layout_centerHorizontal="true"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_login"
android:orientation="horizontal"
android:layout_centerHorizontal="true">
<RadioButton android:id="#+id/on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dip"
android:text="On"
android:textColor="#000000"/>
<RadioButton android:id="#+id/off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dip"
android:layout_marginLeft="20dip"
android:text="Off"
android:textColor="#000000"/>
</RadioGroup>
</RelativeLayout>
and here's my java:
public class LogInActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginactivity);
final RadioButton radio_on = (RadioButton) findViewById(R.id.on);
final RadioButton radio_off = (RadioButton) findViewById(R.id.off);
Button launch = (Button)findViewById(R.id.login_button);
launch.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
EditText usernameEditText = (EditText) findViewById(R.id.txt_username);
EditText passwordEditText = (EditText) findViewById(R.id.txt_password);
TextView loginTextView = (TextView)findViewById(R.id.tv_error);
String sUserName = usernameEditText.getText().toString();
String sPassword = passwordEditText.getText().toString();
if(sUserName.equals("numlock") && sPassword.equals("numlock")) {
Intent intent = new Intent(LogInActivity.this, HomeActivity.class);
startActivity(intent);
}
else {
loginTextView.setText("Login failed. Username and/or password doesn't match.");
}
}
});
}
}
I want to assign the keep me logged in state on the radio buttons. Any help?
you should save the login credentials in sharedPreferences or Sqlite and on login activity check if you find any saved data then login with the saved data else ask user for credentials..
You can do it by using SharedPreference, Static variables or by Application class which will helps you in keeping the state of the User