Different Types of Toast
CustomToast.java
package com.example.crudapps;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class CustomToast extends Activity implements OnClickListener {
Button btn_custom_toast,btn_toastone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_toast);
btn_custom_toast= (Button)findViewById(R.id.btn_custom_toasts);
btn_custom_toast.setOnClickListener(this);
btn_toastone = (Button)findViewById(R.id.btn_toastone);
btn_toastone.setOnClickListener(this);
//http://pulse7.net/category/android/page/2/
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btn_toastone:
Toast.makeText(getApplicationContext(), "Simple Toast", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_custom_toasts:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layouts));
ImageView image = (ImageView) layout.findViewById(R.id.imageView1);
image.setImageResource(R.drawable.smiling);
TextView text = (TextView) layout.findViewById(R.id.tv);
text.setText("This is a custom Toast message!");
// Toast
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.custom_toast, menu);
return true;
}
}
activity_custom_toast.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:background="@drawable/bg_background"
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="com.example.crudapps.CustomToast" >
<Button
android:id="@+id/btn_custom_toasts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="194dp"
android:text="Toast3" />
<Button
android:id="@+id/btn_toastone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_custom_toasts"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/btn_custom_toasts"
android:layout_marginTop="93dp"
android:text="Toast1" />
</RelativeLayout>
Shap:===> custom_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#0174DF"/>
<corners android:radius="5dp" />
<stroke android:width="2dp" android:color="#08298A" />
</shape>
Download full source code:
Download Source code.
No comments:
Post a Comment