Main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:text="Show toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="abc"/>< </LinearLayout>
custom_toast_layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#ff9900"
android:gravity="center"
android:padding="20dp"
android:id="@+id/custom_toast_id">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the custom toast"
/>
</LinearLayout>
Java
package com.kc.ctoast;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void abc(View view){
LayoutInflater iii=getLayoutInflater();
View lll=iii.inflate(
R.layout.custom_toast_layout,
(ViewGroup) findViewById(R.id.custom_toast_id));
Toast ttt =Toast.makeText(getApplicationContext(),"haha you clicked",Toast.LENGTH_SHORT);
ttt.setGravity(Gravity.BOTTOM,0,0);
ttt.setView(lll);
ttt.show();
}
}
For more support Watch the video and try it
0 comments:
Post a Comment