Toasting with Favourite Buttons Android - android

I'm making a cooking application and I have created a favourite button that seems to work ok i click on it and it changes colour. I tried to create a toast where when I click on the button a message wil come up to say
added to favourites
and
removed from favourites
the code I have used for the toast doesn't have an errors and the application runs perfectly but the toast does not appear
here is my code that I have used
Toast.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;
public class Favourite_Toast extends Activity {
private CheckBox fav;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe);
addListenerOnChk();
}
public void addListenerOnChk() {
fav = (CheckBox) findViewById(R.id.checkBox);
fav.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(Favourite_Toast.this,
"Added to Favourites", Toast.LENGTH_LONG).show();
}
}
});
}
}
favourite.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/heart_red" />
<item android:state_checked="false"
android:drawable="#drawable/heart_grey" />
</selector>
recipe.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/jbsbackground2"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_detail"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:src="#drawable/barbecuedporkribs" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_below="#+id/iv_detail"
android:background="#3D3C3A" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button1" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#+id/scrollView1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:text="#string/textView2"
android:layout_toLeftOf="#id/favourites"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvName"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBox"
android:button="#drawable/favourite"
android:checked="false"
android:layout_alignTop="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="46dp"
android:layout_marginEnd="46dp"
android:clickable="true" />
<TextView
android:id="#+id/tvTD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvTD"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
android:layout_below="#+id/checkBox"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/tvIngredients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvTD"
android:layout_marginTop="2dp"
android:text="#string/tvIngredients"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic" />
<TextView
android:id="#+id/tvK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvIngredients"
android:layout_marginTop="5dp"
android:text="#string/tvK"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/tvPreparation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvK"
android:layout_marginTop="2dp"
android:text="#string/tvPreparation"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I've used a checkbox for the favourite button could that be the reason why the toast is not working
Also would anyone be able to tell me how to make the code so that once you have clicked on the favourite button it will stay clicked, because as it is it does not stay clicked on
Thanks in advance

I recommend you change your OnClickListener with OnCheckedChangeListener. It will get invoked when the user checks / unchecks the state of the CheckBox. IMHO, it's the better listener for this widget.

Try the code below
if ((fav.isChecked()) {
Toast.makeText(Favourite_Toast.this, "Added to Favourites", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Favourite_Toast.this, "Removed From Favourites", Toast.LENGTH_LONG).show();
}

Use this code in MainActivity... THIS WILL SOLVE YOUR PROBLEM:
public class MainActivity extends Activity {
private CheckBox chk;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chk = (CheckBox) findViewById(R.id.checkBox1);
chk.setOnClickListener(checkboxClickListener);
}
View.OnClickListener checkboxClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
boolean checked = ((CheckBox) view).isChecked();
if (checked) {
Toast.makeText(MainActivity.this,"Added to favourite", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"Removed from favourite", Toast.LENGTH_LONG).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;
.
.
.
}
}
});

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

how to make Relative layout onclick work?

I have a Relative layout as:
<RelativeLayout
android:id="#+id/iPay_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="· Use iPay"
android:textColor="#686A86"
android:textSize="18sp" />
<Button
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/i_pay"
android:textAllCaps="false"
android:textColor="#ffffff" />
</RelativeLayout>
I want my relative layout to perform onCLickListener but does not work.
I am able to do the setonclicklistener but it works only with the textview part and not for the image.
btniPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//for ipay
}
});
This works for the text part only but not for the image. Any idea what I might be missing.
Alternatively, you can add android:clickable="false" in your button xml attribute.
<RelativeLayout
android:id="#+id/iPay_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="· Use iPay"
android:textColor="#686A86"
android:textSize="18sp" />
<Button
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/i_pay"
android:textAllCaps="false"
android:clickable="false"
android:textColor="#ffffff" />
</RelativeLayout>
may be you can find some good example thats use with imageview and buttons but the easy way i did it by using adding TextView instead of images and buttons.
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:clickable="true"
android:gravity="right">
<TextView
android:id="#+id/englishheading1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/urduheading1"
android:text="English text"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="#+id/urduheading1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:text="urdu text1"
android:textColor="#ffffff"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="8dp"
android:background="#ffffff" />
</RelativeLayout>
Here is java code to perform onClick.
RelativeLayout layout1 = (RelativeLayout) view.findViewById(R.id.layout1);
layout1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something
}
});
see my code easily run change your code:
.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:id="#+id/iPay_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="· Use iPay"
android:textColor="#686A86"
android:textSize="18sp" />
<Button
android:id="#+id/btn"
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/ic_launcher"
android:textAllCaps="false"
android:textColor="#ffffff" />
</RelativeLayout>
</RelativeLayout>
.java file
public class MainActivity extends Activity {
TextView tv;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.btn);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
}
});
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "button", Toast.LENGTH_SHORT).show();
}
});
}
}
Just mark android:clickable="true" in your RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_id"
android:clickable="true">
Using ButterKnife lib make bind here:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xml_name);
ButterKnife.bind(this);
}
#OnClick(R.id.activity_id)
void onActivityClick() {
// do smth ...
}

How to implement custom Tab Host in Android

How to make this kind of tab in Android [http://i.stack.imgur.com/rIbUX.png]
just mention that see the overlap area, when you click on of the tab, the overlap changed.
here is one chinese version example in which I think that's what I want to have, but they don't provide the complete code, and I have no idea how to go on this project.
http://www.oschina.net/question/54100_29061
I am currently using Android 3.2 and ICS 4+ to implement this project, so welcome if that's possible to implement competible with Framgment.
I finally I made the solution by myself. I just leave the native tab host implementation, make up my own.
here is the screen shot:
[http://i.stack.imgur.com/1mXLZ.png]
here is the Activity code:
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class PelesysTabHostActivity extends Activity {
private ImageView ib1, ib2, ib3, last;
private Drawable pressed, released;
private TextView tv,tv1,tv2,tv3;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ib1 = (ImageView) this.findViewById(R.id.imageView1);
ib2 = (ImageView) this.findViewById(R.id.imageView2);
ib3 = (ImageView) this.findViewById(R.id.imageView3);
tv = (TextView) this.findViewById(R.id.textView1);
tv1= (TextView) this.findViewById(R.id.textView11);
tv2= (TextView) this.findViewById(R.id.textView22);
tv3= (TextView) this.findViewById(R.id.textView33);
pressed = getResources().getDrawable(R.drawable.ui_table_tab_button);
released = getResources().getDrawable(
R.drawable.ui_table_tab_button_disabled);
ib1.setImageDrawable(pressed);
last = ib1;
ib1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
last.setImageDrawable(released);
ib1.setImageDrawable(pressed);
last = ib1;
tv.setText("I am super 1");
ib1.bringToFront();
tv1.bringToFront();
}
});
ib2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
last.setImageDrawable(released);
ib2.setImageDrawable(pressed);
last = ib2;
tv.setText("TWO!!! I am super 2");
ib2.bringToFront();
tv2.bringToFront();
}
});
ib3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
last.setImageDrawable(released);
ib3.setImageDrawable(pressed);
last = ib3;
tv.setText(" III !!! I am super 3");
ib3.bringToFront();
tv3.bringToFront();
}
});
}
}
here is the layout file I used (main.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" >
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="330dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/ui_table_bg_coursedetail" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/relativeLayout2"
android:layout_alignParentLeft="true"
android:layout_marginLeft="326dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ui_table_tab_button_disabled" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:src="#drawable/ui_table_tab_button_disabled" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="180dp"
android:src="#drawable/ui_table_tab_button_disabled" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView1"
android:layout_marginLeft="50dp"
android:text="1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000" />
<TextView
android:id="#+id/textView22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView2"
android:layout_marginLeft="50dp"
android:text="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000" />
<TextView
android:id="#+id/textView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView3"
android:layout_marginLeft="50dp"
android:text="3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000" />
</RelativeLayout>
Just a week now I used this : https://github.com/AdilSoomro/Iphone-Tab-in-Android
Very well worked! You can customize it as its in your first picture

Changing background color with Radio Buttons Android

I am attempting to change the background of a tab of my application by selecting a Radio Button from a RadioGroup, however I am not sure how to go about this.
So far I have
Favs.java:
import android.app.Activity;
import android.os.Bundle;
public class Favs extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favs);
}
}
which points to this .xml file -> favs.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your favorite color?"
android:padding="3dip"/>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
android:onClick="onClick" />
<RadioButton android:id="#+id/radio_yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow" />
</RadioGroup>
</LinearLayout>
I understand that the android:onClick="onClick" needs to be there however I have no idea what to do after this.
hi use below code for changing the background according to radio button selection
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical"
android:id="#+id/LinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your favorite color?"
android:padding="3dip"/>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/Group1"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
/>
<RadioButton android:id="#+id/radio_yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow" />
</RadioGroup>
</LinearLayout>
Activity
public class Change extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LinearLayout ll=(LinearLayout) findViewById(R.id.LinearLayout);
final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
final RadioButton radio_yellow = (RadioButton) findViewById(R.id.radio_yellow);
radio_red.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ll.setBackgroundColor(Color.RED);
}
});
radio_yellow.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ll.setBackgroundColor(Color.YELLOW);
}
});
}
}

Categories

Resources