I need a really simple thing done, but I keep getting errors. I have multiple switches with each a distinct id. Everytime I use (Switch)findViewById(), it gives me the following error: Cannot cast View to Switch. s is defined as a Switch.
I did some research and I have tried to clean my project and delete my R file, but it still doesn't work.
protected void onCreate(Bundle savedInstanceState) {
...
switches = weekprogram.getSwitches(day); //returns an arraylist with switches
for (int j = 0; j < switches.size(); j++) {
switch (j) {
case 5:
b = (Button) findViewById(R.id.timeBOne);
s = (Switch) findViewById(R.id.switchOne);
break;
case 6:
b = (Button) findViewById(R.id.timeBTwo);
s = (Switch) findViewById(R.id.switchTwo);
break;
case 7:
b = (Button) findViewById(R.id.timeBThree);
s = (Switch) findViewById(R.id.switchThree);
break;
case 8:
b = (Button) findViewById(R.id.timeBFour);
s = (Switch) findViewById(R.id.switchFour);
break;
case 9:
b = (Button) findViewById(R.id.timeBFive);
s = (Switch) findViewById(R.id.switchFive);
break;
case 0:
b = (Button) findViewById(R.id.timeBSix);
s = (Switch) findViewById(R.id.switchSix);
break;
case 1:
b = (Button) findViewById(R.id.timeBseven);
s = (Switch) findViewById(R.id.switchSeven);
break;
case 2:
b = (Button) findViewById(R.id.timeBEight);
s = (Switch) findViewById(R.id.switchEight);
break;
case 3:
b = (Button) findViewById(R.id.timeBNine);
s = (Switch) findViewById(R.id.switchNine);
break;
case 4:
b = (Button) findViewById(R.id.timeBTen);
s = (Switch) findViewById(R.id.switchTen);
break;
}
...
}
XML File:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgroundforapp"
android:orientation="vertical"
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="com.example.hti_thermostat.SelectedDay$PlaceholderFragment" >
<Button
android:id="#+id/timeBNine"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBTen"
android:layout_alignLeft="#+id/timeBTen"
android:text="09:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBEight"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBNine"
android:layout_alignLeft="#+id/timeBFive"
android:text="08:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton"/>
<Button
android:id="#+id/timeBseven"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBEight"
android:layout_alignLeft="#+id/timeBEight"
android:text="07:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBSix"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBseven"
android:layout_alignLeft="#+id/timeBseven"
android:text="06:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBTen"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignLeft="#+id/timeBEight"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:text="10:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBFive"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignLeft="#+id/timeBFour"
android:layout_centerVertical="true"
android:text="05:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBFour"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBFive"
android:layout_alignLeft="#+id/timeBThree"
android:text="04:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBThree"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBFour"
android:layout_alignLeft="#+id/DayofWeek"
android:text="03:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBTwo"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBThree"
android:layout_alignLeft="#+id/timeBThree"
android:text="02:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBOne"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBTwo"
android:layout_alignLeft="#+id/DayofWeek"
android:text="01:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton"/>
<TextView
android:id="#+id/DayofWeek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/timeBOne"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:text="Monday"
android:textColor="#ECDFD5"
android:textSize="35sp" />
<ImageView
android:id="#+id/sunsD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/timeBOne"
android:src="#drawable/sunshine" />
<ImageView
android:id="#+id/moonsD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/sunsD"
android:layout_alignTop="#+id/timeBSix"
android:src="#drawable/moon" />
<Switch
android:id="#+id/switchOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/timeBOne" />
<Switch
android:id="#+id/switchTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/switchOne"
android:layout_alignTop="#+id/timeBTwo" />
<Switch
android:id="#+id/switchThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/timeBThree"
android:layout_alignLeft="#+id/switchTwo" />
<Switch
android:id="#+id/switchFour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/switchThree"
android:layout_below="#+id/timeBThree" />
<Switch
android:id="#+id/switchSix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/switchFive"
android:layout_alignTop="#+id/timeBSix" />
<Switch
android:id="#+id/switchSeven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/switchSix"
android:layout_below="#+id/timeBSix" />
<Switch
android:id="#+id/switchEight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/timeBEight"
android:layout_alignLeft="#+id/switchSeven" />
<Switch
android:id="#+id/switchNine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/timeBNine"
android:layout_alignLeft="#+id/switchEight" />
<Switch
android:id="#+id/switchTen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/timeBTen"
android:layout_alignLeft="#+id/switchNine" />
<Switch
android:id="#+id/switchFive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/timeBFive"
android:layout_alignLeft="#+id/switchFour" />
You have another Switch class in your project and the code is using that instead of android.widget.Switch.
If the other Switch is in another package, just change the imports to import android.widget.Switch.
If it is in the same package, you can explicitly refer to the platform class by replacing Switch with android.widget.Switch in code.
Related
I want to make a quiz app that can calculate the sum of the number in radio button that user click. How to caluclate the sum and how can I display the result in another layout after user click the button to generate the result?
Question page Result page
Here is the code for question layout (activity_main.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:id="#+id/instruction_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/instruction" />
// Question 1
<TextView
android:id="#+id/question1_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question1" />
<RadioGroup
android:id="#+id/question1_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question1_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question1_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<RadioButton
android:id="#+id/question1_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question1_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question1_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question1_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question1_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion1Clicked"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 2
<TextView
android:id="#+id/question2_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question2" />
<RadioGroup
android:id="#+id/question2_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question2_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question2_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="2" />
<RadioButton
android:id="#+id/question2_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question2_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question2_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question2_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question2_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion2Clicked"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 3
<TextView
android:id="#+id/question3_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question3" />
<RadioGroup
android:id="#+id/question3_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question3_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question3_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="2" />
<RadioButton
android:id="#+id/question3_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question3_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question3_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question3_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question3_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion3Clicked"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 4
<TextView
android:id="#+id/question4_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question4" />
<RadioGroup
android:id="#+id/question4_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question4_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question4_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="2" />
<RadioButton
android:id="#+id/question4_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question4_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question4_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question4_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion4Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question4_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 5
<TextView
android:id="#+id/question5_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question5" />
<RadioGroup
android:id="#+id/question5_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question5_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="1" />
<RadioButton
android:id="#+id/question5_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="2" />
<RadioButton
android:id="#+id/question5_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="3" />
<RadioButton
android:id="#+id/question5_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="4" />
<RadioButton
android:id="#+id/question5_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="5" />
<RadioButton
android:id="#+id/question5_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="6" />
<RadioButton
android:id="#+id/question5_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onRadioButtonQuestion5Clicked"
android:text="7" />
</LinearLayout>
</RadioGroup>
<Button
android:id="#+id/jana_keputusan_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jana Keputusan" />
</LinearLayout>
Here is the code for the question activity (MainActivity.java):
package com.android.resilien.resilienminver;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
public int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
generate_result_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
startActivity(generate_result_intent);
}
});
}
// Question 1 Radio Button clicked
public void onRadioButtonQuestion1Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question1_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question1_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question1_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question1_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question1_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question1_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question1_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
// Question 2 Radio Button clicked
public void onRadioButtonQuestion2Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question2_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question2_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question2_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question2_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question2_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question2_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question2_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
// Question 3 Radio Button clicked
public void onRadioButtonQuestion3Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question3_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question3_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question3_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question3_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question3_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question3_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question3_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
// Question 4 Radio Button clicked
public void onRadioButtonQuestion4Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question4_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question4_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question4_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question4_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question4_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question4_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question4_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
// Question 5 Radio Button clicked
public void onRadioButtonQuestion5Clicked(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question5_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question5_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question5_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question5_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question5_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question5_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question5_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
}
First i would strongly recommend to sum up the click method for all RadioButtons to one single method, e.g. onRadioAction:
The layout:
<?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:id="#+id/instruction_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/instruction" />
// Question 1
<TextView
android:id="#+id/question1_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question1" />
<RadioGroup
android:id="#+id/question1_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question1_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question1_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<RadioButton
android:id="#+id/question1_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question1_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question1_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question1_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question1_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 2
<TextView
android:id="#+id/question2_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question2" />
<RadioGroup
android:id="#+id/question2_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question2_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question2_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="2" />
<RadioButton
android:id="#+id/question2_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question2_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question2_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question2_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question2_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 3
<TextView
android:id="#+id/question3_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question3" />
<RadioGroup
android:id="#+id/question3_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question3_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question3_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="2" />
<RadioButton
android:id="#+id/question3_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question3_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question3_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question3_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question3_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 4
<TextView
android:id="#+id/question4_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question4" />
<RadioGroup
android:id="#+id/question4_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question4_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question4_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="2" />
<RadioButton
android:id="#+id/question4_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question4_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question4_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question4_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question4_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
</LinearLayout>
</RadioGroup>
// Question 5
<TextView
android:id="#+id/question5_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/question5" />
<RadioGroup
android:id="#+id/question5_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/question5_radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="1" />
<RadioButton
android:id="#+id/question5_radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="2" />
<RadioButton
android:id="#+id/question5_radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="3" />
<RadioButton
android:id="#+id/question5_radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="4" />
<RadioButton
android:id="#+id/question5_radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="5" />
<RadioButton
android:id="#+id/question5_radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="6" />
<RadioButton
android:id="#+id/question5_radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioButtonAction"
android:text="7" />
</LinearLayout>
</RadioGroup>
<Button
android:id="#+id/jana_keputusan_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jana Keputusan" />
</LinearLayout>
The activity:
package com.android.resilien.resilienminver;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
public int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
generate_result_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
startActivity(generate_result_intent);
}
});
}
public void radioButtonAction(View view) {
// Is the button checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question5_radioButton1:
if(checked) {score = score + 1;}
break;
case R.id.question5_radioButton2:
if(checked) {score = score + 2;}
break;
case R.id.question5_radioButton3:
if(checked) {score = score + 3;}
break;
case R.id.question5_radioButton4:
if(checked) {score = score + 4;}
break;
case R.id.question5_radioButton5:
if(checked) {score = score + 5;}
break;
case R.id.question5_radioButton6:
if(checked) {score = score + 6;}
break;
case R.id.question5_radioButton7:
if(checked) {score = score + 7;}
break;
}
}
To extend your app i would also recommend, that you generate the layout for questions dynamically in your source code, not as hardcoded layout xml.
Finally to display the result e.g. add a Button to the bottom and set the following code to display a dialog with the achieved score. As alternative you can also start a new Activity and show it there via Intent and Bundle.
private onClickedButtonResult(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(this)
//set icon
.setIcon(android.R.drawable.ic_dialog_alert)
//set title
.setTitle("Your score")
//set message
.setMessage("You achieved a score of " + String.valueOf(score) + "!")
//set positive button
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//Call finish to close your app
finish();
}
})
.show();
}
Alternative:
Show score in another Activity
Create an Intent for the other Activity and save the value of score in the intent:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
generate_result_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
Bundle extras = generate_result_intent.getExtras();
extras.putExtra("SCORE", score);
startActivity(generate_result_intent);
}
});
}
And in your other Activity you can read the passed value of score and show it to the user:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
Intent mIntent = getIntent();
if (i.hasExtra("SCORE")) {
int score = mIntent.getIntExtra("SCORE", 0);
// Do something to display the score
}
}
Finally, be aware that the user can increase his own score by clicking multiple times on a RadioButton. That should be addressed in your application logic, because the scoring would be invalid.
You can go to another activity and display result by INTENT. You can pass your score to another activity by using putExtra.
MainActivity:
okAction.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
intent.putExtra("Score", score);
startActivity(intent);
}
});
Another Activity:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null){
int score = bundle.getInt("Score");
restltText.setText("Your score is: "+ score);
}
To get text from RadioButton I offer you to use:
int indexOfButton = radioGroup.indexOfChild(findViewById(radioGroup.getCheckedRadioButtonId()));
String scoreS = (indexOfButton > -1)? ((RadioButton) radioGroup.getChildAt(indexOfButton)).getText().toString() : "";
score += Integer.parseInt(scoreS);
This methods is getting checked RadioButton id and by it finds index in that group. By the index you can get RadioButton text.
To show the score in other activity you can send data throw Intent extras. Like this:
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("result", score);
startActivity(intent);
On ResultActivity.class you can get score from extras like this:
Integer result = 0;
Intent i = getIntent();
if (i.hasExtra("result")) {
result = i.getIntExtra("result");
}
Then you can display it in TextView or in other possible ways.
If you don't understand something, feel free to ask.
I hope it helps you...
I want to add functionality like snapchat's "add caption" in my recent project.
Expected Output: when I click on add text button, one caption bar appears with EditText and open keyboard user can feed text. If I click on layout keyboard, it closes automatically and it looks like a TextView and caption bar can move through screen at y axis.
Tried and failed: when I click on add text button, one caption bar appears with EditText, caption bar can't move through screen at y axis (if I take the TextView it works fine but not with EditText). Because I can't click on layout.
I also tried property like clickable false. But it's not working
My Question: How to show EditText as TextView while there's only touch event, while click on it works as EditText?
I fail to take click and touch on layout after adding edittext text value.
here's my code:
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rel_Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:clickable="true">
<FrameLayout
android:id="#+id/frame_cameraHolder"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_cameraMain"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<RelativeLayout
android:id="#+id/rel_cameraTitleHolder1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp">
<ImageButton
android:id="#+id/ib_cameraClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"
android:src="#drawable/ic_close_white" />
<ImageButton
android:id="#+id/ib_cameraForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="24dp"
android:background="#android:color/transparent"
android:src="#drawable/ic_forward" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rel_cameraTitleHolder2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:visibility="gone">
<ImageButton
android:id="#+id/ib_cameraCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"
android:src="#drawable/ic_close_white" />
<ImageButton
android:id="#+id/ib_cameraBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"
android:src="#drawable/ic_back" />
<ImageButton
android:id="#+id/ib_cameraStickers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_toLeftOf="#+id/ib_cameraDraw"
android:background="#android:color/transparent"
android:src="#drawable/ic_sticker" />
<ImageButton
android:id="#+id/ib_cameraDraw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_toLeftOf="#+id/ib_cameraText"
android:background="#android:color/transparent"
android:src="#drawable/ic_draw" />
<ImageButton
android:id="#+id/ib_cameraText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="24dp"
android:background="#android:color/transparent"
android:src="#drawable/ic_text" />
</RelativeLayout>
<!--<uz.shift.colorpicker.LineColorPicker
android:id="#+id/color_picker"
android:layout_width="match_parent"
android:layout_height="60dp"
app:orientation="horizontal"
android:visibility="gone"/>-->
<RelativeLayout
android:id="#+id/rel_cameraTextHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:visibility="gone">
<ImageButton
android:id="#+id/ib_cameraTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#android:color/transparent"
android:src="#drawable/ic_fontstyle" />
<TextView
android:id="#+id/ib_cameraDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="24dp"
android:text="Done"
android:textColor="#color/white"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
<!--diff-->
<RelativeLayout
android:id="#+id/rel_cameraHolder1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="visible">
<ImageButton
android:id="#+id/ib_cameraflash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:background="#android:color/transparent"
android:src="#drawable/ic_flash" />
<Button
android:id="#+id/btn_cameraCapturePic"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_centerHorizontal="true"
android:background="#drawable/btn_capture" />
<ImageButton
android:id="#+id/ib_cameraReverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="50dp"
android:background="#android:color/transparent"
android:src="#drawable/ic_reverse" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rel_cameraHolder2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="gone">
<FrameLayout
android:id="#+id/frame_cameraTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp">
<ImageButton
android:id="#+id/ib_cameraTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="24dp"
android:background="#android:color/transparent"
android:src="#drawable/ic_blanck_timer" />
<TextView
android:id="#+id/text_cameraTimerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="6dp"
android:text="1"
android:textColor="#color/white"
android:textSize="8dp"
android:textStyle="bold" />
</FrameLayout>
<ImageButton
android:id="#+id/ib_cameraSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_toRightOf="#+id/frame_cameraTimer"
android:background="#android:color/transparent"
android:src="#drawable/ic_save" />
<ImageButton
android:id="#+id/ib_cameraYourStory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_toRightOf="#+id/ib_cameraSave"
android:background="#android:color/transparent"
android:src="#drawable/ic_add" />
<ImageButton
android:id="#+id/ib_cameraSaveForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="24dp"
android:background="#drawable/btn_capture"
android:src="#drawable/ic_forward" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rel_cameraDeleteHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="gone">
<ImageButton
android:id="#+id/ib_cameraDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#android:color/transparent"
android:src="#drawable/ic_delete" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rel_EdtAddedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:orientation="horizontal"/>
</RelativeLayout>
MainActivity.java
private void setAddedEditTextDynamically() {
mRelativeLayoutHolder2.setVisibility(View.GONE);
//create dynamic edit text
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
final EditText edtAddedText = new EditText(MainActivity.this);
lparams.addRule(RelativeLayout.CENTER_IN_PARENT);
edtAddedText.setLayoutParams(lparams);
edtAddedText.setTextColor(getResources().getColor(R.color.white));
edtAddedText.setGravity(Gravity.CENTER_HORIZONTAL);
// add edittext to rel layout
mRelEdtAddedText.addView(edtAddedText);
mRelEdtAddedText.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
mRelEdtAddedText.setOnTouchListener(new View.OnTouchListener() {
PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down
PointF StartPT = new PointF(); // Record Start Position of 'img'
#Override
public boolean onTouch(View v, MotionEvent event) {
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE:
PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
// mLinMain.setX((int)(StartPT.x+mv.x));
mRelEdtAddedText.setX(10);
mRelEdtAddedText.setY((int) (StartPT.y + mv.y));
StartPT = new PointF(mRelEdtAddedText.getX(), mRelEdtAddedText.getY());
break;
case MotionEvent.ACTION_DOWN:
DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF(mRelEdtAddedText.getX(), mRelEdtAddedText.getY());
break;
case MotionEvent.ACTION_UP:
break;
default:
break;
}
return true;
}
});
}
When i'm trying to build my application it gives me the following to errors.
Error:(21) Error parsing XML: not well-formed (invalid token)
Error:Execution failed for task ':app:processDebugResources'.
com.android.ide.common.process.ProcessException: Failed to execute aapt
Here is my XML code, and this is the only XML code, please help me.
package com.example.madhur.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class InnerWorkings extends Activity implements View.OnClickListener {
TextView tvvresults;
Button bvBackSpace, bvClear, bvPlusMinus, bvRoot, bvDivision, bvMultiply, bvSubtract, bvDot, bvEqual, bvAddition;
Button bv0, bv1, bv2, bv3, bv4, bv5, bv6, bv7, bv8, bv9;
int iResults, container, arithmaticCode, finalCalculation, remainder;
protected void onCreate(Bundle calculations) {
super.onCreate(calculations);
Initialization();
}
private void Initialization() {
tvvresults = (TextView) findViewById(R.id.tvResults);
bvBackSpace = (Button) findViewById(R.id.bBackSpace);
bvClear = (Button) findViewById(R.id.bClear);
bvPlusMinus = (Button) findViewById(R.id.bPlusMinus);
bvRoot = (Button) findViewById(R.id.bRoot);
bvDivision = (Button) findViewById(R.id.bDivision);
bvMultiply = (Button) findViewById(R.id.bMultiply);
bvSubtract = (Button) findViewById(R.id.bSubtract);
bvDot = (Button) findViewById(R.id.bDot);
bvEqual = (Button) findViewById(R.id.bEqual);
bvAddition = (Button) findViewById(R.id.bAddition);
bv0 = (Button) findViewById(R.id.b0);
bv1 = (Button) findViewById(R.id.b1);
bv2 = (Button) findViewById(R.id.b2);
bv3 = (Button) findViewById(R.id.b3);
bv4 = (Button) findViewById(R.id.b4);
bv5 = (Button) findViewById(R.id.b5);
bv6 = (Button) findViewById(R.id.b6);
bv7 = (Button) findViewById(R.id.b7);
bv8 = (Button) findViewById(R.id.b8);
bv9 = (Button) findViewById(R.id.b9);
bvBackSpace.setOnClickListener(this);
bvClear.setOnClickListener(this);
bvPlusMinus.setOnClickListener(this);
bvRoot.setOnClickListener(this);
bvDivision.setOnClickListener(this);
bvMultiply.setOnClickListener(this);
bvSubtract.setOnClickListener(this);
bvDot.setOnClickListener(this);
bvEqual.setOnClickListener(this);
bvAddition.setOnClickListener(this);
bv0.setOnClickListener(this);
bv1.setOnClickListener(this);
bv2.setOnClickListener(this);
bv3.setOnClickListener(this);
bv4.setOnClickListener(this);
bv5.setOnClickListener(this);
bv6.setOnClickListener(this);
bv7.setOnClickListener(this);
bv8.setOnClickListener(this);
bv9.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bBackSpace:
remainder = iResults % 10;
iResults = iResults - remainder;
tvvresults.setText(iResults);
break;
case R.id.bClear:
iResults = 0;
tvvresults.setText(iResults);
break;
case R.id.bPlusMinus:
iResults = iResults * (-1);
tvvresults.setText(iResults);
break;
case R.id.bRoot:
break;
case R.id.bDivision:
arithmaticCode = 1;
container = iResults;
iResults = 0;
break;
case R.id.bMultiply:
arithmaticCode = 2;
container = iResults;
iResults = 0;
break;
case R.id.bSubtract:
arithmaticCode = 3;
container = iResults;
iResults = 0;
break;
case R.id.bDot:
break;
case R.id.bEqual:
Calculations();
break;
case R.id.bAddition:
arithmaticCode = 4;
container = iResults;
iResults = 0;
break;
case R.id.b0:
iResults = (iResults * 10);
tvvresults.setText(iResults);
break;
case R.id.b1:
iResults = (iResults * 10) + 1;
tvvresults.setText(iResults);
break;
case R.id.b2:
iResults = (iResults * 10) + 2;
tvvresults.setText(iResults);
break;
case R.id.b3:
iResults = (iResults * 10) + 3;
tvvresults.setText(iResults);
break;
case R.id.b4:
iResults = (iResults * 10) + 4;
tvvresults.setText(iResults);
break;
case R.id.b5:
iResults = (iResults * 10) + 5;
tvvresults.setText(iResults);
break;
case R.id.b6:
iResults = (iResults * 10) + 6;
tvvresults.setText(iResults);
break;
case R.id.b7:
iResults = (iResults * 10) + 7;
tvvresults.setText(iResults);
break;
case R.id.b8:
iResults = (iResults * 10) + 8;
tvvresults.setText(iResults);
break;
case R.id.b9:
iResults = (iResults * 10) + 9;
tvvresults.setText(iResults);
break;
}
}
private void Calculations() {
if (arithmaticCode == 1) {
finalCalculation = container / iResults;
tvvresults.setText(finalCalculation);
}
if (arithmaticCode == 2) {
finalCalculation = container * iResults;
tvvresults.setText(finalCalculation);
}
if (arithmaticCode == 3){
finalCalculation = container - iResults;
tvvresults.setText(finalCalculation);
}
if (arithmaticCode == 4){
finalCalculation = container + iResults;
tvvresults.setText(finalCalculation);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:gravity="center"
android:textSize="20dp"
android:text="0"
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/tvResults" />
<Button
android:text="<---"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bBackSpace"
android:layout_marginTop="21dp"
android:layout_below="#id/tvResults"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"/>
<Button
android:text="C"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bClear"
android:layout_below="#id/tvResults"
android:layout_toEndOf="#id/bBackSpace"
android:layout_marginStart="20dp"
android:layout_marginTop="21dp"/>
<Button
android:text="+/-"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bPlusMinus"
android:layout_below="#id/tvResults"
android:layout_toStartOf="#id/bRoot"
android:layout_marginEnd="20dp"
android:layout_marginTop="21dp" />
<Button
android:text="-v"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bRoot"
android:layout_marginEnd="21dp"
android:layout_marginTop="21dp"
android:layout_below="#id/tvResults"
android:layout_alignParentEnd="true" />
<Button
android:text="7"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b7"
android:layout_below="#id/bBackSpace"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="8"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b8"
android:layout_below="#id/bBackSpace"
android:layout_toEndOf="#id/bBackSpace"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="9"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b9"
android:layout_below="#id/bBackSpace"
android:layout_toStartOf="#id/bDivision"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="/"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bDivision"
android:layout_marginEnd="21dp"
android:layout_below="#id/bBackSpace"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="4"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b4"
android:layout_below="#id/b7"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="5"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b5"
android:layout_below="#id/b8"
android:layout_toEndOf="#id/b4"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="6"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b6"
android:layout_below="#id/b9"
android:layout_toStartOf="#id/bMultiply"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="*"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bMultiply"
android:layout_marginEnd="21dp"
android:layout_below="#id/bDivision"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="1"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b1"
android:layout_below="#id/b4"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="2"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b2"
android:layout_below="#id/b5"
android:layout_toEndOf="#id/b4"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="3"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b3"
android:layout_below="#id/b6"
android:layout_toStartOf="#id/bSubtract"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="-"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bSubtract"
android:layout_marginEnd="21dp"
android:layout_below="#id/bMultiply"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="0"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b0"
android:layout_below="#id/b1"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="="
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bEqual"
android:layout_below="#id/b3"
android:layout_toStartOf="#id/bAddition"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="+"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bAddition"
android:layout_marginEnd="21dp"
android:layout_below="#id/bSubtract"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="."
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bDot"
android:layout_below="#id/b2"
android:layout_toEndOf="#id/b1"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</LinearLayout>
Try replacing android:text="<---" by android:text="<---", on line 21.
In Your Layout XML with one of the button you are using
android:text="<---"
This will cause error as "<" has special meaning for XMLs, you must perform escaping there are only five:
" "
' '
< <
> >
& &
android:text="<--"
refer link:
What characters do I need to escape in XML documents?
https://www.liquid-technologies.com/XML/EscapingData.aspx
There are 2 errors in xml
1. use "<---" instead of "<---"
2. in the id multiply, add, subtract, root add the "#+id/" before id ..
Solved xml for your problem.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:gravity="center"
android:textSize="20dp"
android:text="0"
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/tvResults" />
<Button
android:text="<---"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bBackSpace"
android:layout_marginTop="21dp"
android:layout_below="#id/tvResults"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"/>
<Button
android:text="C"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bClear"
android:layout_below="#id/tvResults"
android:layout_toEndOf="#id/bBackSpace"
android:layout_marginStart="20dp"
android:layout_marginTop="21dp"/>
<Button
android:text="+/-"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bPlusMinus"
android:layout_below="#id/tvResults"
android:layout_toStartOf="#+id/bRoot"
android:layout_marginEnd="20dp"
android:layout_marginTop="21dp" />
<Button
android:text="-v"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bRoot"
android:layout_marginEnd="21dp"
android:layout_marginTop="21dp"
android:layout_below="#id/tvResults"
android:layout_alignParentEnd="true" />
<Button
android:text="7"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b7"
android:layout_below="#id/bBackSpace"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="8"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b8"
android:layout_below="#id/bBackSpace"
android:layout_toEndOf="#id/bBackSpace"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="9"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b9"
android:layout_below="#id/bBackSpace"
android:layout_toStartOf="#+id/bDivision"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="/"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bDivision"
android:layout_marginEnd="21dp"
android:layout_below="#+id/bBackSpace"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="4"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b4"
android:layout_below="#id/b7"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="5"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b5"
android:layout_below="#id/b8"
android:layout_toEndOf="#id/b4"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="6"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b6"
android:layout_below="#id/b9"
android:layout_toStartOf="#+id/bMultiply"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="*"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bMultiply"
android:layout_marginEnd="21dp"
android:layout_below="#id/bDivision"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="1"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b1"
android:layout_below="#id/b4"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="2"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b2"
android:layout_below="#id/b5"
android:layout_toEndOf="#id/b4"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="3"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b3"
android:layout_below="#id/b6"
android:layout_toStartOf="#+id/bSubtract"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="-"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bSubtract"
android:layout_marginEnd="21dp"
android:layout_below="#+id/bMultiply"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="0"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b0"
android:layout_below="#id/b1"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="="
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bEqual"
android:layout_below="#id/b3"
android:layout_toStartOf="#+id/bAddition"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="+"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bAddition"
android:layout_marginEnd="21dp"
android:layout_below="#id/bSubtract"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="."
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bDot"
android:layout_below="#id/b2"
android:layout_toEndOf="#id/b1"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</LinearLayout>
I have two RadioGroups in my layout. I want that only one RadioButton should be checked at a time in RadioGroup. I've tried many methods, but none of them work properly. Now I can check every RadioButton in my RadioGroups.
public class MainActivity extends ActionBarActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
String x;
char y;
int c=1;
String num,n,type;
int d,b,o,h;
String dec,bin,hex,oct;
EditText number;
EditText result;
Button convert, clear;
RadioGroup rgFrom, rgTO;
int convertedResult;
int from=R.id.r10From;
int to=R.id.r2TO;
String stNumber; //Liczba przed przeliczeniem - string
int numbResult; //Już int
String iterResult; //Liczba po przeliczeniu - string
int convResult; //Już int
//int id = ((RadioGroup).findViewById( R.id.rgFrom )).getCheckedRadioButtonId();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
number = (EditText) findViewById(R.id.etNumber);
result = (EditText) findViewById(R.id.etResult);
convert = (Button) findViewById(R.id.bCalculate);
clear = (Button) findViewById(R.id.bReset);
convert.setOnClickListener(this);
clear.setOnClickListener(this);
rgFrom = (RadioGroup) findViewById(R.id.rgFrom);
rgTO = (RadioGroup) findViewById(R.id.rgTO);
rgFrom.setOnCheckedChangeListener(this);
rgTO.setOnCheckedChangeListener(this);
}
#Override
public void onClick(View view) {
// from = (String)findViewById(R.id.rgFrom).getSelectedItem();
// int id = ((RadioGroup)findViewById( R.id.rgFrom )).getCheckedRadioButtonId();
//from = getFrom(id);
switch (view.getId()){
case R.id.r2From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r8From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r10From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r16From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r2TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.r8TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.r10TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.r16TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.bCalculate:
stNumber = number.getText().toString();
//iterResult = result.getText().toString();
//calculate();
break;
case R.id.bReset:
//reset();
break;
}
}
#Override
public void onCheckedChanged(RadioGroup rgFrom, int i) {
switch (view.getId()){
case R.id.r2From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r8From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r10From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r16From:
from = rgFrom.getCheckedRadioButtonId();
break;
case R.id.r2TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.r8TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.r10TO:
to = rgTO.getCheckedRadioButtonId();
break;
case R.id.r16TO:
to = rgTO.getCheckedRadioButtonId();
break;
}
}
}
My layout:
<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.converter_numeralsystem.app.MainActivity">
<TextView
android:layout_marginTop="20dp"
android:text="#string/number"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvNumber" />
<TextView
android:layout_marginTop="20dp"
android:layout_below="#id/tvNumber"
android:text="#string/result"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvResult" />
<EditText
android:id="#+id/etNumber"
android:layout_marginLeft="5dp"
android:hint="#string/enter_numb"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/tvNumber"
android:layout_alignBottom="#id/tvNumber"/>
<EditText
android:id="#+id/etResult"
android:layout_marginLeft="5dp"
android:hint="#string/et_result"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/tvNumber"
android:layout_alignBottom="#id/tvResult"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="#string/from"
android:id="#+id/tvFrom"
android:layout_below="#id/tvResult"
android:layout_marginTop="20dp" />
<RadioGroup
android:weightSum="100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tvFrom"
android:id="#+id/rgFrom"
android:layout_marginTop="10dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_weight="50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2From"
android:text="#string/dwa"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r8From"
android:text="#string/osiem"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="75dp" />
<RadioButton
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r10From"
android:text="#string/dziesiec"
android:layout_below="#id/r2From"
android:layout_alignStart="#id/r2From" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r16From"
android:text="#string/szesnascie"
android:layout_alignParentRight="true"
android:layout_below="#id/r8From"
android:layout_alignStart="#id/r8From"/>
</RelativeLayout>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="#string/to"
android:id="#+id/tvTo"
android:layout_below="#id/rgFrom"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvTo"
android:layout_marginTop="10dp"
android:id="#+id/rgTO">
<RelativeLayout
android:id="#+id/rel2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:checked="true"
android:layout_weight="50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2TO"
android:text="#string/dwa"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r8TO"
android:text="#string/osiem"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="75dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r10TO"
android:text="#string/dziesiec"
android:layout_below="#id/r2TO"
android:layout_alignStart="#id/r2TO" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r16TO"
android:text="#string/szesnascie"
android:layout_alignParentRight="true"
android:layout_below="#id/r8TO"
android:layout_alignStart="#id/r8TO"/>
</RelativeLayout>
</RadioGroup>
<LinearLayout
android:layout_marginTop="10dp"
android:weightSum="100"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rgTO">
<Button
android:layout_weight="30"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:id="#+id/bCalculate"
android:layout_toLeftOf="#+id/bReset" />
<Button
android:layout_weight="70"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/reset"
android:id="#+id/bReset"
android:layout_alignBottom="#+id/bCalculate" />
</LinearLayout>
</RelativeLayout>
Remove the RelativeLayout from the RadiGroup. So the code would look something like:
<RadioGroup
android:id="#+id/rgFrom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:weightSum="100" >
<RadioButton
android:id="#+id/r2From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="dwa" />
<RadioButton
android:id="#+id/r8From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="75dp"
android:text="osiem" />
<RadioButton
android:id="#+id/r10From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="dziesiec" />
<RadioButton
android:id="#+id/r16From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="szesnascie" />
</RadioGroup>
This will resolve the issue you are facing about multiple buttons being checked in same group. However, this will create a small issue in UI. I think you want 2 buttons in one line, and other two buttons in different line. As far as I know, you would have to implement your own custom layout for that. Check this post for details. Check here, too. Hope it helps a bit.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How to enter data in my EditText using my own buttons instead of default keyboard (I mean like in calculator apps). Please write me some method or show suitable example. Thanks.
I initialize veriable, even tried to make some algorithm. But there is no reason to go further without knowing how to input data.
And of course I was looking for the answer.
public class MainActivity extends Activity implements OnClickListener{
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bPlus,bMin,bDiv,bMult,bEqual;
String sN1,sN2,func,result;
EditText etEnter;
SoundPool pool; int shot = 0;
TextView tvShow;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
initVars();
}
private void initVars(){
etEnter =(EditText)findViewById(R.id.etCalc);
tvShow = (TextView)findViewById(R.id.tvShow);
b0 = (Button)findViewById(R.id.button0);
b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b4 = (Button)findViewById(R.id.button4);
b5 = (Button)findViewById(R.id.button5);
b6 = (Button)findViewById(R.id.butt6);
b7 = (Button)findViewById(R.id.butt7);
b8 = (Button)findViewById(R.id.butt8);
b9 = (Button)findViewById(R.id.butt9);
bMin = (Button)findViewById(R.id.bMinus);
bPlus = (Button)findViewById(R.id.bPlus);
bDiv = (Button)findViewById(R.id.bDiv);
bMult = (Button)findViewById(R.id.bMult);
bEqual = (Button)findViewById(R.id.bEqual);
b0.setOnClickListener(this);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
bMin.setOnClickListener(this);
bPlus.setOnClickListener(this);
bDiv.setOnClickListener(this);
bMult.setOnClickListener(this);
bEqual.setOnClickListener(this);
pool = new SoundPool(5,AudioManager.STREAM_MUSIC,0);
shot = pool.load(this, R.raw.shot, 1);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button0:
break;
case R.id.button1:
break;
case R.id.button2:
break;
case R.id.button3:
break;
case R.id.button4:
break;
case R.id.button5:
break;
case R.id.butt6:
break;
case R.id.butt7:
break;
case R.id.butt8:
break;
case R.id.butt9:
break;
case R.id.bMinus:
func = "-";
break;
case R.id.bPlus:
func = "+";
break;
case R.id.bDiv:
func = "/";
break;
case R.id.bMult:
func = "*";
break;
case R.id.bEqual:
pool.play(shot, 1, 1, 0, 0, 1);
if(func.contentEquals("+")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1+l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}else if(func.contentEquals("-")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1-l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}else if(func.contentEquals("/")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1/l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}else if(func.contentEquals("*")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1*l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}
break;
}
}
and XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/rainbow"
>
<LinearLayout android:paddingTop="22dp" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<EditText
android:id="#+id/etCalc"
android:layout_width="260dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:gravity="right"
android:textSize="30dp"
android:inputType="number" />
</LinearLayout>
<LinearLayout android:paddingRight="33dp" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvShow"
android:layout_width="100dp"
android:layout_height="45dp"
android:text=":-)"
android:textSize="35dp"
android:paddingRight="10dp"
android:layout_gravity="right"
android:background="#drawable/rect_buttons"
android:gravity="right"
></TextView>"
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="20dp" >
<Button
android:id="#+id/bPlus"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:gravity="center"
android:text="+" />
<Button
android:id="#+id/bMinus"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="-" />
<Button
android:layout_gravity="center"
android:id="#+id/bEqual"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="=" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="#+id/button1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="1"
android:textSize="30dp" />
<Button
android:id="#+id/button2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="2"
android:textSize="30dp" />
<Button
android:id="#+id/button3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="3"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="#+id/button4"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="4"
android:textSize="30dp" />
<Button
android:id="#+id/button5"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="5"
android:textSize="30dp" />
<Button
android:id="#+id/butt6"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="6"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="#+id/butt7"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="7"
android:textSize="30dp" />
<Button
android:id="#+id/butt8"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="8"
android:textSize="30dp" />
<Button
android:id="#+id/butt9"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="9"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="#+id/bDiv"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="/" />
<Button
android:layout_gravity="center"
android:id="#+id/button0"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="0" />
<Button
android:id="#+id/bMult" android:layout_width="70dp" android:layout_height="50dp" android:textSize="30dp" android:text="*" />
</LinearLayout>
</LinearLayout>
You have to make your editText non focusable/editable and modify the text inside when you click on your buttons. Easy !