An alert dialog with an image will appear once LinearLayout is clicked - android

Symptoms.java
package com.example.des;
import java.util.ArrayList;
import com.example.des.R;
import com.example.des.Symptoms;
import com.example.des.Learn_Dengue;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Symptoms extends Activity implements OnClickListener{
//set variables
CheckBox q1; CheckBox q2; CheckBox q3; CheckBox q4; CheckBox q5; CheckBox q6;
CheckBox q7; CheckBox q8; CheckBox q9 ;CheckBox q10; CheckBox q11; CheckBox q12;
LinearLayout sas1; LinearLayout sas2; LinearLayout sas3; LinearLayout sas4; LinearLayout sas5; LinearLayout sas6;
LinearLayout sas7; LinearLayout sas8; LinearLayout sas9; LinearLayout sas10; LinearLayout sas11; LinearLayout sas12;
Button btndone;
Button btnlearn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
q1 = (CheckBox)findViewById(R.id.q1a); //Question number 1--main symptom
q2 = (CheckBox)findViewById(R.id.q2a);
q3 = (CheckBox)findViewById(R.id.q3a);
q4 = (CheckBox)findViewById(R.id.q4a);
q5 = (CheckBox)findViewById(R.id.q5a);
q6 = (CheckBox)findViewById(R.id.q6a);
q7 = (CheckBox)findViewById(R.id.q7a);
q8 = (CheckBox)findViewById(R.id.q8a);
q9 = (CheckBox)findViewById(R.id.q9a);
q10 = (CheckBox)findViewById(R.id.q10a);
q11 = (CheckBox)findViewById(R.id.q11a);
q12 = (CheckBox)findViewById(R.id.q12a);
sas1 = (LinearLayout)findViewById(R.id.sas1view);
sas2 = (LinearLayout)findViewById(R.id.sas2view);
sas3 = (LinearLayout)findViewById(R.id.sas3view);
sas4 = (LinearLayout)findViewById(R.id.sas4view);
sas5 = (LinearLayout)findViewById(R.id.sas5view);
sas6 = (LinearLayout)findViewById(R.id.sas6view);
sas7 = (LinearLayout)findViewById(R.id.sas7view);
sas8 = (LinearLayout)findViewById(R.id.sas8view);
sas9 = (LinearLayout)findViewById(R.id.sas9view);
sas10 = (LinearLayout)findViewById(R.id.sas10view);
sas11 = (LinearLayout)findViewById(R.id.sas11view);
sas12 = (LinearLayout)findViewById(R.id.sas12view);
btndone = (Button) findViewById(R.id.done);
btndone.setOnClickListener(this);
btnlearn = (Button) findViewById(R.id.learn);
btnlearn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// When button "Result" is pressed
switch (v.getId()) {
case R.id.done:
ArrayList<CheckBox> cbList = new ArrayList<CheckBox>();
// Array CheckBox 2 - 12 inside an Array (sub symptoms)
cbList.add((CheckBox)findViewById(R.id.q2a));
cbList.add((CheckBox)findViewById(R.id.q3a));
cbList.add((CheckBox)findViewById(R.id.q4a));
cbList.add((CheckBox)findViewById(R.id.q5a));
cbList.add((CheckBox)findViewById(R.id.q6a));
cbList.add((CheckBox)findViewById(R.id.q7a));
cbList.add((CheckBox)findViewById(R.id.q8a));
cbList.add((CheckBox)findViewById(R.id.q9a));
cbList.add((CheckBox)findViewById(R.id.q10a));
cbList.add((CheckBox)findViewById(R.id.q11a));
cbList.add((CheckBox)findViewById(R.id.q12a));
// Starts Looping if CheckBox number 1 is selected and the others (result SUSPICIOUS)
if (q1.isChecked()) {
int count = 0;
for (CheckBox cb : cbList) {
if (cb.isChecked()) {
count++;
}
}
if (count == 2 || count == 1 || count == 0) {
new AlertDialog.Builder(this).setMessage(R.string.suspicious).show();
}
}
// START Looping if CheckBox number 1 is selected but 11 and 12 are not (result MOST LIKELY)
if (q1.isChecked()) {
int count = 0;
for (CheckBox cb : cbList) {
if (cb.isChecked()) {
count++;
}
}
if (count >= 3 && count < 11) {
new AlertDialog.Builder(this).setMessage(R.string.most_likely).show();
}
}
// START Looping if all CheckBox are selected(result POSITIVE)
if (q1.isChecked() && q2.isChecked() && q3.isChecked() && q4.isChecked() &&
q5.isChecked() && q6.isChecked() && q7.isChecked() && q8.isChecked() &&
q9.isChecked() && q10.isChecked() && q11.isChecked() && q12.isChecked()) {
int count = 0;
for (CheckBox cb : cbList) {
if (cb.isChecked()) {
count++;
}
}
if (count == 11) {
new AlertDialog.Builder(this).setMessage(R.string.positive).show();
}
}
// Starts Looping if CheckBox number 1 is not selected and the others are checked
if (!q1.isChecked()) {
int count = 0;
for (CheckBox cb : cbList) {
if (cb.isChecked()) {
count++;
}
}
if (count <= 1 || count > 1) {
//this Toast will show when only 1 or more check box will be checked(excluding check box #1)
/*Toast toast= Toast.makeText(getApplicationContext(), getString(R.string.negative), Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
*/
new AlertDialog.Builder(this).setMessage(R.string.negative).show();
}
}
break;
// When button "Learn More" is pressed
case R.id.learn:
Intent q = new Intent(this, Learn_Dengue.class);
startActivity(q);
break;
}
Here start my question.
sas1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
}
}
questions.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
style="#style/styleName"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/warning"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas1view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="#string/sas1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas2view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q3a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas3view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q4a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas4view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas4"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q5a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas5view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q6a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas6view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas6"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q7a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas7view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas7"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q8a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas8view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas8"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q9a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas9view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas9"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q10a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas10view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas10"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q11a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas11view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas11"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q12a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas12view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas12"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" >
<Button
android:id="#+id/done"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="20dp"
android:background="#drawable/btn_orange"
android:text="#string/done"
android:textColor="#ffffff"
android:textSize="15sp" />
<Button
android:id="#+id/learn"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:background="#drawable/btn_black"
android:text="#string/learn"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
here is my full code as you requested #mobie.. the Symptoms.java and the question.xml..dont matter the other code..just on the alert dialog that i asked.

Create Layout Like Below for Dialog Box : dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/image"/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/image"
/>
</RelativeLayout>
Then, Where you want to place the Alert, Do like below
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Your Title");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Text");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();

Related

Check is any RadioButton is checked in Android

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;
.
.
.
}
}
});

Avd showing old version of my application

So i've make some change's in my app (it was about nesting layouts - My root layout, was linear layout,i wanted to change it to relative layout,i remember,it was something wrong with lines like xlmns:tools etc. I've copy those lines from my previous app,but then i've got an error that something is missing,so i google it,and somebody wrote about delete .idea and .gradle folders so i did,and now,my avd showing me "old content" just app before some changes. How i can solve it? The application that i've trying to make,it's just an typical clicker at case of my course. It's about to counting score of two teams. Screen with an issue https://imgur.com/a/U9gianT
<?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_height="match_parent"
android:layout_width="match_parent">
<Button
android:layout_gravity="center"
android:id="#+id/reset_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/resetBTN"
android:layout_marginTop="15dp"
android:onClick="reset_score"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
/>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_a"
android:textSize="14sp"
android:textColor="#616161"
android:fontFamily="sans-serif-medium"
android:layout_marginTop="16dp"/>
<TextView
android:layout_gravity="center"
android:id="#+id/team_a_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_a_points"
android:layout_marginTop="16dp"
android:textColor="#000000"
android:fontFamily="sans-serif-light"
android:textSize="56sp"/>
<Button
android:layout_gravity="center"
android:id="#+id/three_points_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_3_points_btn"
android:layout_marginTop="24dp"
android:onClick="addThreePointsA"/>
<Button
android:layout_gravity="center"
android:id="#+id/two_points_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_2_points_btn"
android:layout_marginTop="24dp"
android:onClick="addTwoPointsA"/>
<Button
android:layout_gravity="center"
android:id="#+id/free_throw_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/free_throw_btn"
android:layout_marginTop="24dp"
android:onClick="addOnePointA"/>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="340dp"
android:background="#android:color/darker_gray"
android:layout_marginTop="15dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:layout_marginTop="16dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_b"
android:textSize="14sp"
android:textColor="#616161"
android:fontFamily="sans-serif-medium"/>
<TextView
android:layout_gravity="center"
android:id="#+id/team_b_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_b_score"
android:layout_marginTop="16dp"
android:textSize="56sp"
android:textColor="#000000"
android:fontFamily="sans-serif-light"/>
<Button
android:layout_gravity="center"
android:id="#+id/three_points_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_3_points_btn"
android:layout_marginTop="24dp"
android:onClick="addThreePointsB"/>
<Button
android:layout_gravity="center"
android:id="#+id/two_points_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_2_points_btn"
android:layout_marginTop="24dp"
android:onClick="addTwoPointsB"/>
<Button
android:layout_gravity="center"
android:id="#+id/free_throw_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/free_throw_btn"
android:layout_marginTop="24dp"
android:onClick="addOnePointB"/>
</LinearLayout>
package com.example.kacper.courtcounter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int team_a_score = 0;
int team_b_score = 0;
private void display_a_score(int score){
TextView scoreView = (TextView) findViewById(R.id.team_a_points);
scoreView.setText(String.valueOf(score));
}
private void display_b_score(int score){
TextView scoreView = (TextView) findViewById(R.id.team_b_points);
scoreView.setText(String.valueOf(score));
}
public void addThreePointsA(View view) {
team_a_score = team_a_score+3;
display_a_score(team_a_score);
}
public void addTwoPointsA(View view) {
team_a_score = team_a_score+2;
display_a_score(team_a_score);
}
public void addOnePointA(View view) {
team_a_score = team_a_score+1;
display_a_score(team_a_score);
}
public void addThreePointsB(View view) {
team_b_score = team_b_score+3;
display_b_score(team_b_score);
}
public void addTwoPointsB(View view) {
team_b_score = team_b_score+2;
display_b_score(team_b_score);
}
public void addOnePointB(View view) {
team_b_score = team_b_score+1;
display_b_score(team_b_score);
}
public void reset_score(View view) {
team_a_score = 0;
team_b_score = 0;
display_a_score(team_a_score);
display_b_score(team_b_score);
}
}
Just Clean and rebuild your project and then uninstall app from device and then run again in device it will works fine (If you really make changes in code)

Buttons Overlap

I made a stopwatch using chronometer with four buttons but when i use the visibility modes to make stop and pause button appear they overlap.... Pls explain why...Below is the code.....
Assume the layout file with buttons in relative layout...
public class StopWatchFragment extends Fragment {
Chronometer chronometer;
Button startStopWatch;
Button stopStopWatch;
Button resetStopWatch;
Button pauseStopWatch;
Button resumeStopWatch;
private long lastPause;
//RelativeLayout relativeLayout;
private int check = 0;
public StopWatchFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.stopwatch_layout,container,false);
chronometer = (Chronometer) rootView.findViewById(R.id.stopwatch);
startStopWatch = (Button) rootView.findViewById(R.id.startStopWatch);
stopStopWatch = (Button) rootView.findViewById(R.id.stopStopWatch);
resetStopWatch = (Button) rootView.findViewById(R.id.resetStopWatch);
pauseStopWatch = (Button) rootView.findViewById(R.id.pauseStopWatch);
resumeStopWatch = (Button) rootView.findViewById(R.id.resumeStopWatch);
relativeLayout = (RelativeLayout) rootView.findViewById(R.id.parentRelativeLayout);
pauseStopWatch.setVisibility(View.GONE);
//final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(relativeLayout.getLayoutParams());
startStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
startStopWatch.setVisibility(View.GONE);
pauseStopWatch.setVisibility(View.VISIBLE);
if(check == 1){
resumeStopWatch.setClickable(true);
}
else{
resumeStopWatch.setClickable(false);
}
//params.setMargins(16,16,16,16);
//pauseStopWatch.setLayoutParams(params);
}
});
pauseStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
check = 1;
lastPause = SystemClock.elapsedRealtime();
chronometer.stop();
}
});
resumeStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
chronometer.start();
resumeStopWatch.setClickable(false);
}
});
stopStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.stop();
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
resumeStopWatch.setClickable(false);
}
});
resetStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.stop();
resumeStopWatch.setClickable(false);
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
}
});
return rootView;
}
}
This is the layout file pls refer for this....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
This is the way buttons work when click on start button
You are hiding the button, but you are still listening with the OnClickListener. Try adding:
pauseStopWatch.setOnClickListener(null);
Then, when you make your button visible:
pauseStopWatch.setOnClickListener(whatever);
Try this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:visibility="gone"
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</LinearLayout>
</LinearLayout>
In the above, the pause button will be set to Gone. You make it visible later when user clicks start button

how to store seleced checkbox values in array list in android

I have created simple quiz app. In that I want to get true value of checkbox. there are three checkbox questions. Now how to get right value of selected checkbox using ArrayList. So when multiple answers are selected by the user I need to store them in an arraylist and need to compare the selected answers with that in database. In my code I am storing the selected value id's one by one but at a time it is storing only one value. How to store all the selected checkbox id's in my arraylist?
My code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/activity_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background"
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.pujadudhat.quizapp.MainActivity">
<TextView
android:id="#+id/tv_que1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/que_1" />
<RadioGroup
android:id="#+id/rg_que1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/rb_opt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="questionOne"
android:text="#string/rb_opt1" />
<RadioButton
android:id="#+id/rb_opt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionOne"
android:text="#string/rb_opt2" />
<RadioButton
android:id="#+id/rb_opt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionOne"
android:text="#string/rb_opt3" />
</RadioGroup>
<TextView
android:id="#+id/tv_que2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/que_2" />
<RadioGroup
android:id="#+id/rg_que2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/rb_opt1_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="questionTwo"
android:text="#string/rb_opt1_2" />
<RadioButton
android:id="#+id/rb_opt2_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionTwo"
android:text="#string/rb_opt2_2" />
<RadioButton
android:id="#+id/rb_opt3_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionTwo"
android:text="#string/rb_opt3_2" />
</RadioGroup>
<TextView
android:id="#+id/tv_que3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/que_3" />
<RadioGroup
android:id="#+id/rg_que3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/rb_opt1_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="questionThree"
android:text="#string/rb_opt1_3" />
<RadioButton
android:id="#+id/rb_opt2_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionThree"
android:text="#string/rb_opt2_3" />
<RadioButton
android:id="#+id/rb_opt3_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionThree"
android:text="#string/rb_opt3_3" />
</RadioGroup>
<TextView
android:id="#+id/tv_que4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/que_4" />
<CheckBox
android:id="#+id/cb_prime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionFour"
android:text="#string/cb_opt1_4" />
<CheckBox
android:id="#+id/cb_prime2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionFour"
android:text="#string/cb_opt2_4" />
<CheckBox
android:id="#+id/cb_prime3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionFour"
android:text="#string/cb_opt3_4" />
<TextView
android:id="#+id/tv_que5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/que_5" />
<CheckBox
android:id="#+id/cb_composite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionFive"
android:text="#string/cb_opt1_5" />
<CheckBox
android:id="#+id/cb_composite2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionFive"
android:text="#string/cb_opt2_5" />
<CheckBox
android:id="#+id/cb_composite3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionFive"
android:text="#string/cb_opt3_5" />
<TextView
android:id="#+id/tv_que6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/que_6" />
<CheckBox
android:id="#+id/cb_multiple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionSix"
android:text="#string/cb_opt1_6" />
<CheckBox
android:id="#+id/cb_multiple2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionSix"
android:text="#string/cb_opt2_6" />
<CheckBox
android:id="#+id/cb_multiple3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="questionSix"
android:text="#string/cb_opt3_6" />
<TextView
android:id="#+id/tv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/que_7" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:hint="#string/hint" />
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="#drawable/rolex_logo" />
<TextView
android:id="#+id/tv_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:onClick="calsulateScore"
android:text="#string/btn_text" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:onClick="resetScore"
android:text="#string/btn_reset" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.pujadudhat.quizapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private int myScore = 0;
boolean que1;
boolean que2;
boolean que3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* Get Answer for que. #1
*/
public void questionOne(View view) {
RadioButton opt1RadioButton = (RadioButton) findViewById(R.id.rb_opt1);
que1 = opt1RadioButton.isChecked();
if (opt1RadioButton.isChecked()) {
myScore = myScore + 1;
Toast.makeText(getApplicationContext(), "Right!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Wrong!", Toast.LENGTH_SHORT).show();
}
}
public void questionTwo(View view){
RadioButton opt2RadioButton = (RadioButton) findViewById(R.id.rb_opt2_2);
que2 = opt2RadioButton.isChecked();
if(opt2RadioButton.isChecked()){
myScore = myScore + 1;
Toast.makeText(this, "Right!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Wrong!", Toast.LENGTH_SHORT).show();
}
}
public void questionThree(View view){
RadioButton opt2RadioButton = (RadioButton) findViewById(R.id.rb_opt2_3);
que3 = opt2RadioButton.isChecked();
if(opt2RadioButton.isChecked()){
myScore = myScore + 1;
Toast.makeText(this, "Right!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Wrong!", Toast.LENGTH_SHORT).show();
}
}
}
Please help me to solve this issue
Thank You
you can try to create Custom classes,
public class Answer{
private int questionNumber;
private boolean answer;
public Answer(int questionNumber,boolean answer)
{
this.questionNumber = questionNumber;
this,.answer=answer
}
public void setQuestionNumber(int questionNumber){this.questionNumber=questionNumber;}
public void setAnswer(boolean answer){this.answer=answer;}
public int getQuestionNumber(){return this.questionNumber;}
public boolean getAnswer(){return this.answer;}
}
and apply to your mainActivity :
public class MainActivity extends AppCompatActivity {
private Answer _answer;
private List<Answer> _listAnswer = new ArrayList<>();
public void questionOne(View view) {
RadioButton opt1RadioButton = (RadioButton) findViewById(R.id.rb_opt1);
que1 = opt1RadioButton.isChecked();
if (opt1RadioButton.isChecked()) {
_answer = new Answer(1,true);
myScore = myScore + 1;
Toast.makeText(getApplicationContext(), "Right!", Toast.LENGTH_SHORT).show();
} else {
_answer = new Answer(1,false);
Toast.makeText(getApplicationContext(), "Wrong!", Toast.LENGTH_SHORT).show();
}
//to store the answer into list
_listAnswer.add(_answer);
}
do the rest for question 2,3... total question you have
and for access the list you can try :
for(int i=0;i<_listAnswer.size();i++)
{
String answerValue = if(_listAnswer.get(i).getAnswer()) ? "Right" : "Wrong"
if(_listAnswer.get(i).getAnswer()){ myScore++; }
Toast.makeText(getApplicationContext(), "Question Number : " + _listAnswer.get(i).getQuestionNumber() + " is " + answerValue);
}
hope this will help

Multiplechoice in AlertDialog

I use "lv_del_transl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)" to set this options.
code to create AlertDialog:
Al_tr.setTitle("Title");
RelativeLayout view_T = (RelativeLayout)getLayoutInflater().inflate(R.layout.listofword_cmenu_del_transl, null);
Al_tr.setView(view_T);
TextView del_transl = (TextView)view_T.findViewById(R.id.del_transl);
ListView lv_del_transl = (ListView)view_T.findViewById(R.id.lv_del_transl);
Button del_transl_OK = (Button)view_T.findViewById(R.id.del_transl_OK);
Button del_transl_Cancel = (Button)view_T.findViewById(R.id.del_transl_Cancel);
al_del_tr=Al_tr.create();
Del_transl.setText("word");
Cursor c_adap_tr=cur_del_tr(......);
startManagingCursor(c_adap_tr);
String[] from_r = new String[]{NAME};
int[] to_r = new int[] {R.id.transl };
scAdapter_transl = new SimpleCursorAdapter(this, R.layout.listofword_cmenu_del_transl_item, c_adap_tr, from_r, to_r);
lv_del_transl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // for set Multiple property
lv_del_transl.setAdapter(scAdapter_transl);
del_transl_OK.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String sel="0";
SparseBooleanArray sbArray = lv_del_transl.getCheckedItemPositions();
for (int i = 0; i < lv_del_transl.getCount(); i++) {
if (sbArray.get(i))
sel+=Integer.toString(lv_del_transl.getCheckedItemPosition())+" ";
}
Toast.makeText(getApplicationContext(), Integer.toString(lv_del_transl.getCount())+" "+sel, Toast.LENGTH_SHORT).show();
}
});
al_del_rt.show();
Xml file for ListView
<LinearLayout
android:id="#+id/lv_del_translation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/del_translation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/text_del_translation"
android:textSize="20dp" />
<TextView
android:id="#+id/del_transl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="28dp"
android:text="#string/text_del_transl"
android:textSize="20dp" />
</LinearLayout>
<ListView
android:id="#+id/lv_del_transl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lv_del_translation"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:choiceMode="multipleChoice" />
<LinearLayout
android:id="#+id/l_del_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:background="#drawable/gradient_box"
android:orientation="horizontal"
android:padding="5dp" >
<Button
android:id="#+id/del_transl_OK"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/text_del_word_OK" />
<Button
android:id="#+id/del_transl_Cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/text_del_word_Cancel" />
</LinearLayout>
</RelativeLayout>
where listofword_cmenu_del_transl_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/transl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:textSize="19sp">
</TextView>
<CheckBox
android:id="#+id/checbox_id"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
But SparseBooleanArray sbArray has no effect. I can't define checked position. Please help me find a mistake.
private void showPopUp()
{
final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("");
LayoutInflater inflater = getLayoutInflater();
final View PopupLayout = inflater.inflate(R.layout.jobselection, null);
helpBuilder.setView(PopupLayout);
final AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
okbtn = (Button)PopupLayout.findViewById(R.id.okBtn);
caneclbtn = (Button)PopupLayout.findViewById(R.id.cancelBtn);
selectallbtn = (Button)PopupLayout.findViewById(R.id.selectBtn);
clearallbtn = (Button)PopupLayout.findViewById(R.id.clearallBtn);
jobList = (ListView)PopupLayout.findViewById(R.id.list);
mylist = new ArrayList<HashMap<String, String>>();
for(int i=0;i<Punchedjobs.size();i++)
{
map = new HashMap<String, String>();
map.put("name", Punchedjobs.get(i));
mylist.add(map);
}
sd = new SimpleAdapter(StaffTimeClock.this,mylist,R.layout.jobslist,
new String[]{"name"},new int[]{R.id.jobText});
jobList.setAdapter(sd);
okbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//ur code
}
});
caneclbtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
helpDialog.dismiss();
}
});
selectallbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
CheckBox job_chk;
for(int i=0;i<Punchedjobs.size();i++)
{
job_chk= ((CheckBox)jobList.getChildAt(i).findViewById(R.id.chk));
job_chk.setChecked(true);
}
}
});
clearallbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
CheckBox job_chk;
for(int i=0;i<Punchedjobs.size();i++)
{
job_chk= ((CheckBox)jobList.getChildAt(i).findViewById(R.id.chk));
job_chk.setChecked(false);
}
}
});
}
jobselection.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
tools:context=".StaffTimeClock" >
<TextView
android:id="#+id/img"
android:layout_width="fill_parent"
android:text="#string/selectjob"
android:gravity="center"
android:textSize="30dp"
android:textColor="#fff"
android:background="#203C56"
android:padding="10dp"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:cacheColorHint="#222000" />
<View
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#000"/>
<LinearLayout android:id="#+id/lin00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal">
<Button
android:layout_weight="0.1"
android:contentDescription="#string/ok"
android:id="#+id/okBtn"
android:layout_gravity="center"
android:layout_margin="10dp"
android:padding="10dp"
android:textSize="25dp"
android:background="#drawable/toolbar_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"/>
<Button
android:layout_weight="0.1"
android:contentDescription="#string/ok"
android:id="#+id/selectBtn"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:textSize="25dp"
android:background="#drawable/toolbar_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/selectall"/>
<Button
android:layout_weight="0.1"
android:contentDescription="#string/ok"
android:id="#+id/clearallBtn"
android:layout_gravity="center"
android:layout_margin="10dp"
android:padding="10dp"
android:textSize="25dp"
android:background="#drawable/toolbar_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/clear"/>
<Button
android:layout_weight="0.1"
android:contentDescription="#string/cancel"
android:id="#+id/cancelBtn"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:textSize="25dp"
android:background="#drawable/toolbar_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"/>
</LinearLayout>
</LinearLayout>
jobslist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lin01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:padding="10dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/image"
android:contentDescription="#string/staff"
android:src="#drawable/logo2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/jobText"
android:layout_width="0dp"
android:text="#string/jobtype"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:layout_marginLeft="10dp"
android:textSize="25dp"
android:textColor="#000"
android:layout_height="50dp"/>
<CheckBox
android:id="#+id/chk"
android:layout_width="wrap_content"
android:text=""
android:gravity="center_vertical|right"
android:button="#drawable/checkbox_selector_green"
android:layout_height="wrap_content"/>
</LinearLayout>

Categories

Resources