In this tutorial, I will show you how to customized Toast in Android. See output below.
Next is add this image in your drawable folder.
Next is add this custom_toast.xml in your drawable folder.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/toast_corner">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/warning_icon" />
<TextView
android:id="@+id/tv_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/iv_icon"
android:paddingRight="16dp"
android:paddingBottom="5dp"
android:text="Toast"
android:textColor="#FFFFFF"
android:textStyle="bold" />
</RelativeLayout>
Next is add this toast_corner.xml to your drawable folder for the radius of your toast.
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#000000" >
</solid>
<stroke
android:width="2dp"
android:color="#000000" >
</stroke>
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" >
</padding>
<corners
android:radius="11dp" >
</corners>
</shape>
Next is your main_layout.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/btn_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Show Toast" />
</RelativeLayout>
And lastly is your MainActivity.java
package com.client.apps.views.toast;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
Button btn_toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_toast = (Button) findViewById(R.id.btn_toast);
btn_toast.setOnClickListener(this);
}
@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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btn_toast:
Toast("Invalid Entry, Please Check. Just Give me a reason!");
break;
}
}
public void Toast(String toast){
//get the LayoutInflater and inflate the custom_toast layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup)
findViewById(R.id.rl_main));
//get the TextView from the custom_toast layout
TextView text = (TextView) layout.findViewById(R.id.tv_toast);
text.setText(toast);
//create the toast object, set display duration,
//set the view as layout that's inflated above and then call show()
Toast t = new Toast(getApplicationContext());
t.setDuration(Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
t.setView(layout);
t.show();
}
}
No comments:
Post a Comment