I'm using the vogella tutorial, and I'm almost followed the instructions exactly, but it is sort of giving me an error on text and radiobutton saying it cannot be resolved or it is not in field. I'm really new to Android. Would you please help me out with the code.
package convert.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText text;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText1);
}
// This method is called at button click because we assigned the name to the
// "On Click property" of the button
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.button1:
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
if (text.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {
text.setText(String
.valueOf(convertFahrenheitToCelsius(inputValue)));
celsiusButton.setChecked(false);
fahrenheitButton.setChecked(true);
} else {
text.setText(String
.valueOf(convertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
break;
}
}
// Converts to celsius
private float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// Converts to fahrenheit
private float convertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
}
Here is my code to activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/myColor" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="numberSigned|numberDecimal"
>
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/celsius" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fahrenheit" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calc"
android:onClick="myClickHandler">
</Button>
</RelativeLayout>
The error is on editText1, radio0, radio1 and button1... For all them the error is the same,i.e it cannot be resolved or is not in a field.
Here it is...
I used your xml file and it is resolved perfectly
EditText text;
RadioButton r1,r2;
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText)findViewById(R.id.editText1);
r1 =(RadioButton)findViewById(R.id.radio0);
r1 =(RadioButton)findViewById(R.id.radio1);
}
and do like this
b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// your Switch code here
}
});
}
I checked your code it's perfectly working change the activity_main like this
<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"
>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="numberSigned|numberDecimal"
>
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="celsius" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fahrenheit" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/editText1"
android:onClick="myClickHandler"
android:text="calc" />
</RelativeLayout>
Related
I would like to check is any RadioButton is checked and which is checked.
I have
package promedica.test_osobowosci;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
public class Question1 extends AppCompatActivity {
private Button nextButton;
private Button stopButton;
private RadioButton answer1;
private RadioButton answer2;
private RadioButton answer3;
private RadioButton answer4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
setTitle("Pytanie 1 z 10");
answer1 = findViewById(R.id.radioButtonQ1_1);
answer1 = findViewById(R.id.radioButtonQ1_2);
answer1 = findViewById(R.id.radioButtonQ1_3);
answer1 = findViewById(R.id.radioButtonQ1_4);
nextButton = findViewById(R.id.nextTo2Button);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isAnyRadioButtonChecke();
}
});
stopButton = findViewById(R.id.stopButton1);
stopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity();
}
});
}
public void openQuestion2Activity(){
Intent intent = new Intent(this, Question2.class);
startActivity(intent);
}
public void openMainActivity(){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public void noAnswerCheckedAlert() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Nie zaznaczono odpowiedzi!");
alertDialogBuilder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(Question1.this, "Zaznacz odpowiedź", Toast.LENGTH_LONG).show();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
public void isAnyRadioButtonChecked(){
if(answer1.isChecked() || answer2.isChecked() || answer3.isChecked() || answer4.isChecked())
openQuestion2Activity();
else
noAnswerCheckedAlert();
}
}
and I get error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.RadioButton.isChecked()' on a null object reference
at promedica.test_osobowosci.Question1.isAnyRadioButtonChecked(Question1.java:76)
at promedica.test_osobowosci.Question1$1.onClick(Question1.java:38)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
When I click nextButton with checking, without before clicked any RadioButton.
But I click any RadioButton and next click nextButton, the application show alert all the time. I tried declare RadioButtons in OnClickListener button method but is not solve my problems.
What is the problem?
Edit:
activity_question1.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Question1">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="46dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="false"
android:layout_marginTop="40dp"
android:layout_marginLeft="85dp"
android:text="Question1"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title"
app:layout_constraintEnd_toEndOf="parent"
tools:layout_editor_absoluteY="25dp" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="19dp"
android:layout_marginTop="0dp"
android:scaleType="fitXY"
android:cropToPadding="false"
app:srcCompat="#mipmap/obrot" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="19dp"
android:layout_marginTop="0dp"
android:scaleType="fitXY"
android:cropToPadding="false"
app:srcCompat="#mipmap/obrot" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="159dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="109dp"
android:layout_marginLeft="85dp">
<RadioButton
android:id="#+id/radioButtonQ1_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Answer1"
android:inputType="textMultiLine"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
<RadioButton
android:id="#+id/radioButtonQ1_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Answer2"
android:inputType="textMultiLine"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
<RadioButton
android:id="#+id/radioButtonQ1_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Answer3"
android:inputType="textMultiLine"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
<RadioButton
android:id="#+id/radioButtonQ1_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:inputType="textMultiLine"
android:text="Answer4"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
</RadioGroup>
<Button
android:id="#+id/nextTo2Button"
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_marginTop="305dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="161dp"
android:background="#drawable/custom_button"
android:text="Next"
android:textAllCaps="false"
android:textColor="#ffffff" />
<Button
android:id="#+id/stopButton1"
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_marginTop="395dp"
android:layout_centerHorizontal="true"
android:background="#drawable/custom_button_blue"
android:text="Stop"
android:textAllCaps="false"
android:textColor="#ffffff" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="60dp"
android:layout_alignParentStart="true"
app:srcCompat="#mipmap/oie_transparent" />
</RelativeLayout>
Instead of checking for an individual radiobutton do the following:
if (radioGroup.getCheckedRadioButtonId() == -1)
{
noAnswerCheckedAlert();
}
else
{
openQuestion2Activity();
}
<RadioGroup
android:id="#+id/radio"
android:layout_width="match_parent"
android:layout_height="159dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="109dp"
android:layout_marginLeft="85dp">
<RadioButton
android:id="#+id/radioButtonQ1_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Answer1"
android:inputType="textMultiLine"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
<RadioButton
android:id="#+id/radioButtonQ1_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Answer2"
android:inputType="textMultiLine"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
<RadioButton
android:id="#+id/radioButtonQ1_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Answer3"
android:inputType="textMultiLine"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
<RadioButton
android:id="#+id/radioButtonQ1_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:inputType="textMultiLine"
android:text="Answer4"
android:layout_marginRight="50dp"
android:theme="#style/MyRadioButton" />
</RadioGroup>
JAVA CODE:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio);
nextButton = findViewById(R.id.nextTo2Button);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (radioGroup.getCheckedRadioButtonId() == -1) {
// no radio buttons are checked
noAnswerCheckedAlert();
} else {
// one of the radio buttons is checked
openQuestion2Activity();
}
}
});
Use the RadioGroup to check if no RadioButton is checked:
RadioGroup radioGroup;
....
if(radioGroup.getCheckedRadioButtonId() != answer1 || ........)
{
AlertDialog...
}
else
{
openQuestion2Activity();
}
you can use RadioGroup in the xml
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/radiobuttongroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_q1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/option1"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_q2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/options2"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
And Activity file
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radiobuttongroup);
final String[] checkedOption = new String[1];
radioGroup.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radio_q1:
checkedOption[0] = 1;
break;
case R.id.radio_q2:
checkedOption[0] = 2;
break;
.
.
.
}
}
});
I have built a calculator app which uses EditTexts for you to insert the two values, and then I also have 4 Buttons each with an operation (eg.+,-,/,x). It seems though as if the buttons aren't doing its function assigned to it, No value is being sent to the TextView. Could you please help me in making those buttons do its operation?
Here is my code below:
package com.example.user.zidcalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int enter1;
int enter2;
int newHeight;
EditText et1;
EditText et2;
TextView tv;
Button bt1;
Button bt2;
Button bt3;
Button bt4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.edittext1);
et2 = (EditText) findViewById(R.id.edittext2);
tv = (TextView) findViewById(R.id.tvans);
bt1 = (Button) findViewById(R.id.button);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);
bt4 = (Button) findViewById(R.id.button4);
newHeight = et2.getHeight();
tv.setHeight(newHeight);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enter1 = Integer.parseInt(et1.getText().toString());
enter2 = Integer.parseInt(et2.getText().toString());
int answer = enter1 + enter2;
tv.setText(String.valueOf(answer));
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enter1 = Integer.parseInt(et1.getText().toString());
enter2 = Integer.parseInt(et2.getText().toString());
int answer = enter1 - enter2;
tv.setText(String.valueOf(answer));
}
});
bt3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enter1 = Integer.parseInt(et1.getText().toString());
enter2 = Integer.parseInt(et2.getText().toString());
int answer = enter1 * enter2;
tv.setText(String.valueOf(answer));
}
});
bt4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enter1 = Integer.parseInt(et1.getText().toString());
enter2 = Integer.parseInt(et2.getText().toString());
int answer = enter1 / enter2;
tv.setText(String.valueOf(answer));
}
});
}
}
Layout File
<?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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter two numbers below and perform an operation to see the output"
android:gravity="center_horizontal"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter first number here -->"
android:textSize="18sp" />
<EditText
android:id="#+id/edittext1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:inputType="numberSigned"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter second number here -->"
android:textSize="18sp" />
<EditText
android:id="#+id/edittext2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:inputType="numberSigned"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer -->"
android:textSize="18sp"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="#+id/tvans"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<Button
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button"
android:layout_centerHorizontal="true"
android:id="#+id/button2" />
<Button
android:text="x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_alignEnd="#+id/button"
android:layout_marginTop="39dp"
android:id="#+id/button3" />
<Button
android:text="/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button3"
android:layout_alignStart="#+id/button2"
android:id="#+id/button4" />
</RelativeLayout>
When you getHeight of et2, it haven't text, so it height is 0. Then, you set height 0 for tv, you can't see it in screen althought it has text.
Try to run the code giving a constant height and width to answerTextView,tvans in your code(for eg: 30 DP height and 30 DP width).
This issue is not related to button. This is due to the invisibility of textView due to 0 height and width
<TextView
android:id="#+id/tvans"
android:layout_width="30dp"
android:layout_height="30dp"
android:gravity="center_horizontal"/>
I removed the Height variable newHeight and everything associated with it.
I have two problems, my first one is that i'm only getting one out put "score of 0" I don't know what's wrong with my code. It won't add the integers and keeps them at 0. My second one is that a new pop up comes up and then the okay button to dismiss won't work. Thanks for your help!
package xyz.ashraf.whoisdelasalle;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
/**
* Created by Ashraf on 3/2/2016.
*/
public class check_Button extends Pop_sallian{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow_sallian);
CheckBox concern = (CheckBox) findViewById(R.id.concern);
CheckBox faith = (CheckBox) findViewById(R.id.faith);
CheckBox respect = (CheckBox) findViewById(R.id.respect);
CheckBox education = (CheckBox) findViewById(R.id.education);
CheckBox community = (CheckBox) findViewById(R.id.community);
TextView output = (TextView) findViewById(R.id.output);
int con = 0;
int fai = 0;
int res = 0;
int edu = 0;
int com = 0;
if (concern.isChecked()) {
con = con+1;
}
if (faith.isChecked()) {
fai = fai+1;
}
if (respect.isChecked()) {
res = res+1;
}
if (education.isChecked()) {
edu = edu+1;
}
if (community.isChecked()) {
com = com +1;
}
int score = con+fai+res+edu+com;
if(score == 0){
output.setText("score of 0");
} else if(score == 1){
output.setText("score of 1");
} else if(score == 2){
output.setText("score of 2");
} else if(score == 3){
output.setText("score of 3");
} else if(score == 4){
output.setText("score of 4");
} else if(score == 5){
output.setText("score of 5");
} else{
output.setText("Unknown");
}
}
}
code where the out put doesn't work^^
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent" android:layout_height="match_parent">
android:elevation="8dp"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you a Sallian?"
android:id="#+id/textView7"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you meet the following prerequisites, if you do you may be a Sallian"
android:id="#+id/textView8"
android:layout_below="#+id/textView7"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you concerened for the poor and Social Justice?"
android:id="#+id/concern"
android:textSize="18sp"
android:layout_below="#+id/textView8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have faith in the presence of God?"
android:id="#+id/faith"
android:textSize="15sp"
android:layout_below="#+id/concern"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have Respect for all people?"
android:id="#+id/respect"
android:layout_below="#+id/faith"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:textSize="15sp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you value education?"
android:id="#+id/education"
android:textSize="15sp"
android:layout_below="#+id/respect"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you inclusive in your community?"
android:id="#+id/community"
android:layout_below="#+id/education"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="15sp"
android:checked="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="#+id/okButton_sallian"
android:layout_below="#+id/community"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
android:background="#FAFAFA"
android:textColor="#00E676"
android:elevation="2dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:id="#+id/check"
android:textColor="#00E676"
android:elevation="2dp"
android:background="#FAFAFA"
android:layout_alignTop="#+id/okButton_sallian"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/output"
android:layout_below="#+id/community"
android:textColor="#1eff00"
android:layout_alignLeft="#+id/check"
android:layout_alignStart="#+id/check"
android:layout_alignRight="#+id/okButton_sallian"
android:layout_alignEnd="#+id/okButton_sallian"
android:layout_above="#+id/check"
android:textSize="20sp" />
</RelativeLayout>
</ScrollView>
xml pop up window where the okay button won't work when the check button is checked^^
package xyz.ashraf.whoisdelasalle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
/**
* Created by Ashraf on 1/27/2016.
*/
public class Pop_sallian extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow_sallian);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.6));
Button checkButton = (Button) findViewById(R.id.check);
checkButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Pop_sallian.this, check_Button.class));
}
});
Button okButton = (Button) findViewById(R.id.okButton_sallian);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
java code that creates the pop up^^
I have made a very simple android program with help of 3 imagebuttons(circle) ,Now i want is after some clicks on that partcular buttons its background images should be changed..i have tried the code as below but its not working:
main.java
package com.esp.therisemethod.ui;
import com.esp.therisemethod.R;
import com.esp.therisemethod.uc.Header;
import com.esp.therisemethod.uc.Menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class ProgressActivity extends Activity implements OnClickListener {
private ImageView resetall, undo, b1, b2, b3;
int cnt1,cnt2,cnt3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
b1 = (ImageView) findViewById(R.id.b1);
b2 = (ImageView) findViewById(R.id.b2);
b3 = (ImageView) findViewById(R.id.b3);
resetall = (ImageView) findViewById(R.id.reset_all);
undo = (ImageView) findViewById(R.id.undo);
setContentView(R.layout.activity_progress);
Header header =(Header)findViewById(R.id.header);
header = (Header) findViewById(R.id.header);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
header.btn_bak.setVisibility(View.VISIBLE);
header.btn_bak.setVisibility(View.VISIBLE);
Menu menu =(Menu)findViewById(R.id.prog_footer);
menu.setSelectedTab(1);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b1:
cnt1++;
if(cnt1>1 && cnt1<=5)
{
b1.setBackgroundResource(R.drawable.circle_red);
}
else if (cnt1>=6 && cnt1<10) {
b1.setBackgroundResource(R.drawable.circle_orange);
}
else{
b1.setBackgroundResource(R.drawable.circle_green);
}
break;
case R.id.b2:
cnt2++;
if(cnt2<=5)
{
b2.setImageResource(R.drawable.circle_red);
}
else if (cnt2>=6 && cnt2<10) {
b2.setImageResource(R.drawable.circle_orange);
}
else{
b2.setImageResource(R.drawable.circle_green);
}
break;
case R.id.b3:
cnt3++;
if(cnt3<=5)
{
b2.setImageResource(R.drawable.circle_red);
}
else if (cnt3>=6 && cnt3<10) {
b2.setImageResource(R.drawable.circle_orange);
}
else{
b2.setImageResource(R.drawable.circle_green);
}
break;
case R.id.reset_all:
break;
case R.id.undo:
break;
}
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.esp.therisemethod.uc.Header
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_below="#+id/header"
android:layout_above="#+id/prog_footer">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/linearp1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:orientation="vertical" >
<com.esp.therisemethod.uc.EspTextView
android:id="#+id/client_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:tag="800"
android:text="Welcome Back SAM!"
android:textSize="25dp"
android:textStyle="bold" />
<com.esp.therisemethod.uc.EspTextView
android:id="#+id/make_choice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:tag="800"
android:text="Please make your choice:"
android:textSize="20dp" />
</LinearLayout>
<RelativeLayout
android:id="#+id/ralativep1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearp1"
android:layout_centerVertical="true"
android:layout_marginTop="10dp" >
<ImageView
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:src="#drawable/cir_grey1" />
<ImageView
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="#drawable/cir_grey1" />
</RelativeLayout>
<ImageView
android:id="#+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ralativep1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:src="#drawable/cir_grey1" />
<LinearLayout
android:id="#+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/b3"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="30dp"
android:weightSum="2" >
<Button
android:id="#+id/reset_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="#drawable/btn_med"
android:text="Reset all"
android:textColor="#ffffff" />
<Button
android:id="#+id/undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="#drawable/btn_med"
android:text="Undo"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<com.esp.therisemethod.uc.Menu
android:id="#+id/prog_footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
If you have set src attribute in xml, then to update UI use setImageResource()
If you have set background attribute in xml then to update UI use setImageDrawable()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//First set content view..
setContentView(R.layout.activity_progress);
b1 = (ImageView) findViewById(R.id.b1);
b2 = (ImageView) findViewById(R.id.b2);
b3 = (ImageView) findViewById(R.id.b3);
resetall = (ImageView) findViewById(R.id.reset_all);
undo = (ImageView) findViewById(R.id.undo);
Header header =(Header)findViewById(R.id.header);
If Your onClickListener works correct, in Your case You have to change the source not background, for example:
b2.setImageResource(R.drawable.yourImage);
I want, if we choose radiobutton "yes" so we can using checkboxes. if we choose radiobutton "no", checkboxes can't be used. How?
its xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#ffffffff"
android:text="ARE YOU CLASS 2B?" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes" />
<RadioButton
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/jimmy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jimmy" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/diana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Diana" />
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/dina"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dina" />
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/jack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jack" />
</TableRow>
<TextView
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TableRow
android:id="#+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</LinearLayout>
Its java:
package diahp.d;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
public class example extends Activity implements OnClickListener {
CheckBox jimmy,diana,dina,jack;
RadioButton yes,no;
TextView status;
StringBuilder showing,showing2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example);
jimmy = (CheckBox) findViewById(R.id.jimmy);
diana = (CheckBox) findViewById(R.id.diana);
dina = (CheckBox) findViewById(R.id.dina);
jack = (CheckBox) findViewById(R.id.jack);
jimmy.setOnClickListener(this);
diana.setOnClickListener(this);
dina.setOnClickListener(this);
jack.setOnClickListener(this);
yes = (RadioButton) findViewById(R.id.yes);
no = (RadioButton) findViewById(R.id.no);
yes.setOnClickListener(this);
no.setOnClickListener(this);
status = (TextView) findViewById(R.id.status);
}
public void onClick(View v) {
showing = new StringBuilder();
if(yes.isChecked()){(diana,jimmy,dina,jack==true)} //can access the checkboxes
if(no.isChecked()){(diana,jimmy,dina,jack==false)} //can't access the checkboxes
if(diana.isChecked()){
showing.append("Diana");
}
if(jimmy.isChecked()){
showing.append("Jimmy");
}
if(dina.isChecked()){
showing.append("Dina");
}
if(jack.isChecked()){
showing.append("Jack");
}
status.setText("Oh, you are:" +showing);
} }
hmm... how if I choose radio button " yes" can access the checkboxes, and if "no" can't access the checkboxes. .
Try like this.
if(no.isChecked())
{
//diana.setChecked(false);
//jimmy.setChecked(false);
//dina.setChecked(false);
//jack.setChecked(false);
diana.setEnabled(false);
jimmy.setEnabled(false);
dina.setEnabled(false);
jack.setEnabled(false);
}
or else use RadioGroup
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int arg1) {
radioButton = (RadioButton) findViewById(radioGroup
.getCheckedRadioButtonId());
if (radioButton.getText().equals("Yes")) {
//diana.setChecked(true);
//jimmy.setChecked(true);
//dina.setChecked(true);
//jack.setChecked(true);
diana.setEnabled(true);
jimmy.setEnabled(true);
dina.setEnabled(true);
jack.setEnabled(true);
} else {
//diana.setChecked(false);
//jimmy.setChecked(false);
//dina.setChecked(false);
//jack.setChecked(false);
diana.setEnabled(false);
jimmy.setEnabled(false);
dina.setEnabled(false);
jack.setEnabled(false);
}
}
});
I hope this will help you.
disable checkbox on change of radio button.
more info link
You have to use a setOnCheckedChangeListener and Change to RadioGroup http://developer.android.com/reference/android/widget/RadioGroup.html
(radiogroup).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.yes) {
XY
} else if (checkedId == R.id.no) {
XY
} else {
XY
}
}
Hope this helps. Happy coding!