how to Create Toasts message in android application
																							
													Toasts 
													
												 
																							
													message 
													
												 
																							
													application 
													
												 
																							
													android 
													
												 
																					
																		- By Code solution
 - Jan 20th, 2021
 - 0 comments
 - 2
 
What Is Toast In Android?
- If We Have To Show Any Message In Our App So We Can Use Toast.
 
In Android There Are Three Types Available For Toast.
- ?Simple Toast
 - Positioning Toast Message
 - Custom Toast
 
Simple Toast:)
- First Create activity_main.xml code :)
 
<Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/button"
       android:text="Click"
       android:onClick="Click"/>
- After That Create MainActivity.java code:)
 
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  
    }
   public void Click(View view)
    {
        Toast.makeText(this,"Hello",Toast.LENGTH_SHORT).show();
    }
- Output:)
 
?
Positioning Toast Message:)
- First Create activity_main.xml code :)
 
<Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/button"
       android:text="Click"
       android:onClick="Click"/>
- After That Create MainActivity.java code:)
 
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
    }
    public void Click(View view)
    {
           Toast toast=Toast.makeText(MainActivity.this,"Toast:Gravity.Top",Toast.LENGTH_SHORT);
   toast.setGravity(Gravity.TOP,0,0);
   toast.show();
   Toast toast1=Toast.makeText(MainActivity.this,"Toast:Center",Toast.LENGTH_SHORT);
   toast1.setGravity(Gravity.CENTER,0,0);
   toast1.show();
   Toast toast2=Toast.makeText(MainActivity.this,"Toast:Botton",Toast.LENGTH_SHORT);
   toast2.setGravity(Gravity.BOTTOM,0,0);
   toast2.show();
    Toast toast3=Toast.makeText(MainActivity.this,"Toast:Right",Toast.LENGTH_SHORT);
   toast3.setGravity(Gravity.RIGHT,0,0);
   toast3.show();
   Toast toast4=Toast.makeText(MainActivity.this,"Toast:Right",Toast.LENGTH_SHORT);
   toast3.setGravity(Gravity.LEFT,0,0);
   toast3.show();
   Toast.makeText(MainActivity.this,"Ram2",Toast.LENGTH_SHORT).show();
    }
Output:)
Custom Toast :)
- First Create activity_main.xml code :)
 
<Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/button"
       android:text="Click"
       android:onClick="Click"/>
- After That Create Control_item.xml code :)
 
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/image_Toast">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/image"
        android:src="@drawable/nature"/>
</LinearLayout>
- After That Create MainActivity.java code :)
 
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void Click(View view)
    {
        Toast t1=new Toast(getApplicationContext());
        LayoutInflater  li=getLayoutInflater();
        ViewGroup vg=findViewById(R.id.image_Toast);
        view=li.inflate(R.layout.Control_item,vg);
        t1.setView(view);
        t1.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
        t1.setDuration(Toast.LENGTH_SHORT);
        t1.show();
    }
}
Output:)