2013-03-01 8 views
0

personnalisé Comment ajouter une image personnalisée ajoutée à Toast personnalisé ...dynamiquement ajouté à Toast

+0

Alors? Quel est votre problème? – Barney

+0

pls spécifiez le problème que vous rencontrez avec ce code !! –

+0

je veux ajouter mon image dynamiquement, c'est – Rohit

Répondre

1

SImply ajouter un ImageView dans votre toast_layout puis:

ImageView iv= (ImageView) layout.findViewById(R.id.image); 
iv.setBackgroundResource(R.drawable.img1);//Or wathever you want! 

Cela fonctionne.

+0

voir logcat, cette erreur est affichée – Rohit

+0

Quel logcat? S'il vous plaît ... –

-2

Voici comment je l'ai fait:

LayoutInflater inflater = LayoutInflater.from(getActivity()); 
    View layout = inflater.inflate(R.layout.toast, null); 
    ImageView image = (ImageView) layout.findViewById(R.id.image); 
    image.setImageBitmap(bm); 

    TextView text = (TextView) layout.findViewById(R.id.toast_text); 
    String toastText = "Hi"; 
    text.setText(toastText); 

    Toast toast = new Toast(getActivity().getApplicationContext()); 
    toast.setGravity(Gravity.CENTER, 0, 0); 
    toast.setDuration(Toast.LENGTH_SHORT); 
    toast.setView(layout); 
    toast.show(); 

fichier Mise en page:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/toast_layout_root" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/covu_sdk_background_toast" 
android:orientation="horizontal" 
android:padding="10dp" > 

<ImageView 
    android:id="@+id/image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<TextView 
    android:id="@+id/toast_text" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical" 
    android:textColor="#FFF" /> 

</LinearLayout> 
+0

Expliquer "ne fonctionne pas". Quelle est l'erreur? –

+0

remplace 'getActivity()' par 'this', si c'est l'erreur que vous obtenez. –

0
03-01 15:55:41.040: E/AndroidRuntime(6512): java.lang.NullPointerException 
03-01 15:55:41.040: E/AndroidRuntime(6512): at com.anthem.mathsmagic.maxone$1.onClick(maxone.java:98) 

C'est une NPE assez commune sur votre méthode onclick. Que faites-vous à la ligne 98?

+0

c'est la solution, je réponds à ma question – Rohit

+0

Je veux juste que vous marquez QUELLE ligne est la ligne 98. Je ne peux pas savoir ce qui est nul juste avec cet extrait. – razielsarafan

Questions connexes