Tous les articles

Comment avoir un Toast personnalisé !

Le code XML :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.formation.toastapplication.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toast!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:onClick="toast_fct"/>

</android.support.constraint.ConstraintLayout>

 

Le code Java :

package com.example.formation.toastapplication;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void toast_fct(View view){
        Toast toast = Toast.makeText(this, "Le son a été désactivé", Toast.LENGTH_LONG);
        View toastview = toast.getView();

        TextView tv = (TextView) toastview.findViewById(android.R.id.message);
        tv.setTextSize(18);
        tv.setTextColor(Color.parseColor("#000000"));
        tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.option, 0, 0, 0);
        tv.setCompoundDrawablePadding(15);
        toastview.setBackgroundColor(Color.parseColor("#00000000"));

        toast.show();
    }
}

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *