mercredi 24 février 2016

Toasting with Favourite Buttons 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://ift.tt/nIICcg">
<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://ift.tt/nIICcg"
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




Aucun commentaire:

Enregistrer un commentaire