Monday, August 17, 2015

How to show a Toast message from a Fragment in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#c6cabd"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This TextView is under main layout"
        android:layout_marginBottom="25dp"
        android:textSize="35dp"
        />
    <fragment
        android:id="@+id/toast_example_fragment"
        android:name="com.cfsuman.me.androidcodesnippets.ToastExampleFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tv"
        >
    </fragment>
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.os.Bundle;
import android.app.Activity;


public class MainActivity extends Activity {

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

<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="com.cfsuman.me.androidcodesnippets.ToastExampleFragment"
    android:background="#ffed7d"
    >
    <Button
        android:id="@+id/btn_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Toast From Fragment"
        />
</RelativeLayout>

ToastExampleFragment.java

package com.cfsuman.me.androidcodesnippets;


import android.os.Bundle;
import android.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;


public class ToastExampleFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_toast_example,null,false);

        // get the widgets reference from Fragment XML layout
        Button btn_fragment = (Button) view.findViewById(R.id.btn_fragment);

        // Set a click listener for Fragment button
        btn_fragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Create a Toast notification/message
                Toast toast = Toast.makeText(
                        //getActivity(),"Custom Toast From Fragment",Toast.LENGTH_LONG
                        getActivity().getApplicationContext(), "Custom Toast From Fragment", Toast.LENGTH_LONG
                );
                // Set the Toast display position layout center
                toast.setGravity(Gravity.CENTER,0,0);
                // Finally, show the toast
                toast.show();
            }
        });

        // Return the view as Fragment layout
        return view;
    }
}

More android examples