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 !
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...
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>
So I would like to have a calculator in my app. User will first click on RadioButton which would be for Imperial and Metric units, and depending on which one does user click, one of the layouts would appear.
As you know imperial units has feets and inches, for which I need two EditTexts, but metric unit needs only one.
So that's what I want, switching between layouts in same activity to avoid using fragments.
Any ideas?
EDIT
xml file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="hr.app.liftme.liftmehr.KalkulatoriBFMan"
tools:showIn="#layout/activity_kalkulatori_bfman"
android:orientation="vertical">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Osnove5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff"
android:layout_marginBottom="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout28"
android:layout_marginBottom="20dp"
android:layout_marginTop="55dp"
android:padding="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_uvodno"
android:id="#+id/textView140"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_tekst_upute"
android:id="#+id/textView141"
android:textColor="#424242" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Drugo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:layout_marginBottom="10dp"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout27"
android:layout_below="#+id/linearLayout28"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="7dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_radio_naslov"
android:id="#+id/textView3"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonBFZeneCmKg"
android:text="#string/bf_radio2"
android:onClick="radioZeneCmKgClicked"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonBFZeneFtLb"
android:text="#string/bf_radio1"
android:onClick="radioZeneBFFtLbClicked"
android:checked="false" />
</RadioGroup>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_tezina"
android:id="#+id/textView133"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTezinaBF"
android:hint="#string/bf_edit_tezina_hint_z"
android:layout_marginBottom="5dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LayoutVisinaMetric">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_visina"
android:id="#+id/textViewVisinaMetric"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editVisinaBF"
android:hint="#string/bf_edit_visina_hint_z"
android:layout_marginBottom="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LayoutVisinaImperial">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_feetinch_naslov"
android:id="#+id/textViewVisinaImperial"
android:textStyle="bold" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTextVisinaFT"
android:layout_weight="1"
android:hint="#string/bf_radio1_hint1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTextVisinaINC"
android:layout_weight="1"
android:hint="#string/bf_radio1_hint2" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_struk"
android:id="#+id/textView135"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editStrukBF"
android:hint="#string/bf_edit_struk_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_bokovi"
android:id="#+id/textView136"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editBokoviBF"
android:hint="#string/bf_edit_bokovi_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_vrat"
android:id="#+id/textView137"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editVratBF"
android:hint="#string/bf_edit_vrat_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_godine"
android:id="#+id/textView138"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editGodineBF"
android:hint="#string/bf_edit_godine_hint_z"
android:layout_marginBottom="15dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout27"
android:layout_marginBottom="20dp"
android:id="#+id/linearLayout29">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bf_btn_izracunaj"
android:id="#+id/btnIzracunajBF"
android:layout_gravity="center_horizontal"
android:background="#424242"
android:textColor="#ffffff"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_tekst_rezultat"
android:id="#+id/textView139"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textRezultatBF"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_rezultat"
android:id="#+id/textView152" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Osnove3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_below="#+id/linearLayout27"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_tekst_tumacenje"
android:id="#+id/textView142"
android:gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_tumacenje_rezultat_z"
android:id="#+id/textView143"
android:gravity="center_horizontal"
android:textColor="#424242" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
java
public class KalkulatoriBFMan extends AppCompatActivity {
EditText editTezina, editVisina, editStruk, editBokovi, editVrat, editGodine, editVisinaFT, editVisinaINC;
Button btnIzracunajBF;
TextView textRezultatBF, textVisinaMetric, textVisinaImperial;
LinearLayout imperial, metric;
public void radioZeneCmKgClicked(View view){
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()){
case R.id.radioButtonBFZeneCmKg:
if (checked)
editVisina.setVisibility(View.VISIBLE);
imperial.setVisibility(View.GONE);
btnIzracunajBF.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// IMPERIAL FORMULA
//Body Fat (women) = 495 / (1.29579 - .35004 * log10(Waist + Hip - Neck) + 0.22100 * log10(Height)) - 450
double prvoPolje = Double.parseDouble(editTezina.getText().toString());
double drugoPolje = Double.parseDouble(editVisina.getText().toString());
double trecePolje = Double.parseDouble(editStruk.getText().toString());
double cetvrtoPolje = Double.parseDouble(editBokovi.getText().toString());
double petoPolje = Double.parseDouble(editVrat.getText().toString());
double sestoPolje = Double.parseDouble(editGodine.getText().toString());
double rezultat = 0;
rezultat = 495 / (1.0324 - 0.19077 * Math.log10(trecePolje - petoPolje) + 0.15456 * Math.log10(drugoPolje)) - 450;
rezultat = Math.round(rezultat * 100.0) / 100.0;
textRezultatBF.setText(Double.toString(rezultat) + " %");
}
});
break;
}
}
public void radioZeneBFFtLbClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButtonBFZeneFtLb:
if (checked)
editVisinaFT.setVisibility(View.VISIBLE);
metric.setVisibility(View.GONE);
btnIzracunajBF.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// IMPERIAL FORMULA
//Body Fat (men) = 495 / (1.29579 - .35004 * log10(Waist - Neck) + 0.22100 * log10(Height)) - 450
double prvoPolje = Double.parseDouble(editTezina.getText().toString());
double drugoPolje = Double.parseDouble(editVisinaFT.getText().toString());
double trecePolje = Double.parseDouble(editStruk.getText().toString());
double cetvrtoPolje = Double.parseDouble(editBokovi.getText().toString());
double petoPolje = Double.parseDouble(editVrat.getText().toString());
double sestoPolje = Double.parseDouble(editGodine.getText().toString());
double sedmoPolje = Double.parseDouble(editVisinaINC.getText().toString());
double rezultat = 0;
rezultat = 495 / (1.29579 - 0.35004 * Math.log10(trecePolje - petoPolje) + 0.22100 * Math.log10(((drugoPolje * 12) + sedmoPolje))) - 450;
rezultat = Math.round(rezultat * 100.0) / 100.0;
textRezultatBF.setText(Double.toString(rezultat) + " %");
}
});
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kalkulatori_bfwoman);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
editTezina = (EditText) findViewById(R.id.editTezinaBF);
editVisina = (EditText) findViewById(R.id.editVisinaBF);
editStruk = (EditText) findViewById(R.id.editStrukBF);
editBokovi = (EditText) findViewById(R.id.editBokoviBF);
editVrat = (EditText) findViewById(R.id.editVratBF);
editGodine = (EditText) findViewById(R.id.editGodineBF);
editVisinaFT = (EditText) findViewById(R.id.editTextVisinaFT);
editVisinaINC = (EditText) findViewById(R.id.editTextVisinaINC);
textVisinaImperial = (TextView) findViewById(R.id.textViewVisinaImperial);
textVisinaMetric = (TextView) findViewById(R.id.textViewVisinaMetric);
imperial = (LinearLayout) findViewById(R.id.LayoutVisinaImperial);
metric = (LinearLayout) findViewById(R.id.LayoutVisinaMetric);
btnIzracunajBF = (Button) findViewById(R.id.btnIzracunajBF);
textRezultatBF = (TextView) findViewById(R.id.textRezultatBF);
}
}
Note that I solved blank space problem with setting visibility to GONE, but when I open activity both EditTexts appear, imperial and metric. I want them both to be invisible until user selects one RadioButton
To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
For example, here are a couple RadioButton objects:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
Within the Activity that hosts this layout, the following method handles the click event for both radio buttons:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.imperial:
if (checked)
EditBox1.setVisibility(Visible)
break;
case R.id.metrics:
if (checked){
EditBox1.setVisibility(Visible)
EditBox2.setVisibility(Visible)
}
break;
}
}
Source: http://developer.android.com/guide/topics/ui/controls/radiobutton.html
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.
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.