Custom Toast :
If a simple text message isn't enough, you can create a
customized layout for your toast notification. To create a custom layout,
define a View layout, in XML or in your application code.
We created a custom_toast.xml layout file in layout folder
for custom toast. Also set the id of the layout.
LayoutInflater : Instantiates
a layout XML file into its corresponding View objects. It is never used
directly. Instead, use getLayoutInflater() or getSystemService(String) to
retrieve a standard LayoutInflater instance that is already hooked up to the
current context.
inflate(): Inflate a new view hierarchy from the specified xml resource.
inflate(int resource, ViewGroup root)
inflate(int resource, ViewGroup root)
To create custom toast Notification write following piece of code :
MainActivity.java :
public class MainActivity extends Activity {
Button
btn1;
ImageView
iv;
TextView
tv;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)
findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
LayoutInflater
li = getLayoutInflater();
View
layout = li.inflate(R.layout.custom_toast, (ViewGroup)
findViewById(R.id.custom_toast));
iv = (ImageView)
layout.findViewById(R.id.imageView1);
iv.setImageResource(R.drawable.a2);
tv = (TextView)
layout.findViewById(R.id.textView1);
tv.setText("Hello i am
Android logo");
AnalogClock
ac = (AnalogClock) layout.findViewById(R.id.analogClock1);
Toast
toast = new Toast(MainActivity.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the
menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="97dp"
android:layout_marginTop="38dp"
android:text="@string/button1" />
</RelativeLayout>
custom_toast.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:orientation="vertical"
android:id="@+id/custom_toast">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textView" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<AnalogClock
android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mbl: 9953584548
Write us at: - smruti@apextgi.com and pratap@apextgi.com
Combating cockroaches with magic dough in Riyadh*Steam cleaning company in Khamis Mushait*Insulation of bathrooms and kitchens*Moving furniture in Khamis Mushait*Detection of water leaks in Khamis Mushait*Furniture transfer in Riyadh 300 riyals*Repair and maintenance of houses in Riyadh*Insulation of roofs in Riyadh in a manner of waterproofing
ReplyDelete