در این آموزش میخواهم شما را با toast سفارشی در اندروید یا همان (custom toast) آشنا کنم ، همونطور که می دونید توست یه پیام هست که به کاربر نمایش داده می شود و پس از چند ثانیه محو می شود.
سورس toast سفارشی از قسمت های زیر تشکیل شده :
فایل های بک گراند toast (شامل چهار فایل برای رنگ های مختلف ) که در پوشه drawble قرار خواهد گرفت :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="@color/success_hover" />
<padding android:left="5dp" android:top="0dp"
android:right="5dp" android:bottom="0dp" />
<corners android:radius="2dp" />
<solid android:color="@color/success"/>
</shape>
آیکن های toast سفارشی ( در این سورس من از ۲ آیکن استفاده کردم ) :
آیکن toast سبز و آبی رنگ
آیکن toast قرمز و نارنجی رنگ
فایل layout برای سفارشی کردن toast (شامل چهار قایل برای چهار حالت نمایش toast) :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_margin="5dp"
android:padding="15dp"
android:layout_gravity="center_horizontal"
android:background="@color/transparent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/danger_shape"
android:gravity="center">
<ImageView
android:id="@+id/imageView1"
android:src="@drawable/ic_danger_toast"
android:layout_width="32dp"
android:layout_height="32dp"
android:scaleType="fitXY" />
<com.github.leonardoxh.customfont.FontText
app:font="sans"
android:id="@+id/errorToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:textColor="@color/white"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:text="تمشکی، مرجع برترین های اندروید"
android:layout_marginLeft="5dp"
android:layout_marginTop="1dp" />
</LinearLayout>
</LinearLayout>
رنگ های استفاده شده در toast سفارشی :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent">#00000000</color>
<color name="white">#ffffff</color>
<color name="success">#28b62c</color>
<color name="success_hover">#24a528</color>
<color name="defaullt">#EEEEEE</color>
<color name="defaullt_hover">#E0E0E0</color>
<color name="info">#158cba</color>
<color name="info_hover">#127ba3</color>
<color name="warning">#ff851b</color>
<color name="warning_hover">#ff7701</color>
<color name="danger">#ff4136</color>
<color name="danger_hover">#ff1103</color>
</resources>
کلاس جاوا استفاده شده در toast سفارشی :
package ir.tameshki.toast.tameshkitoast;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import ir.tameshki.toast.tameshkitoast.R;
public class CustomToast {
private static Context context;
public static final int CENTER = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
public static final int BOTTOM = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
public static final int SHORT = Toast.LENGTH_SHORT;
public static final int LONG = Toast.LENGTH_LONG;
public CustomToast(Context context) {
this.context = context;
}
public void Danger(String str, int POSITION, int DURATION) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.customtoast_danger, null);
TextView textView = (TextView) customView.findViewById(R.id.errorToast);
textView.setText(str);
Toast customToast = new Toast(context);
customToast.setView(customView);
customToast.setGravity(POSITION, 0, 0);
customToast.setDuration(DURATION);
customToast.show();
}
public void Success(String str, int POSITION, int DURATION) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.customtoast_success, null);
TextView textView = (TextView) customView.findViewById(R.id.okToast);
textView.setText(str);
Toast customToast = new Toast(context);
customToast.setView(customView);
customToast.setGravity(POSITION, 0, 0);
customToast.setDuration(DURATION);
customToast.show();
}
public void Info(String str, int POSITION, int DURATION) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.customtoast_info, null);
TextView textView = (TextView) customView.findViewById(R.id.normalToast);
textView.setText(str);
Toast customToast = new Toast(context);
customToast.setView(customView);
customToast.setGravity(POSITION, 0, 0);
customToast.setDuration(DURATION);
customToast.show();
}
public void Warning(String str, int POSITION, int DURATION) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.customtoast_warning, null);
TextView textView = (TextView) customView.findViewById(R.id.warningToast);
textView.setText(str);
Toast customToast = new Toast(context);
customToast.setView(customView);
customToast.setGravity(POSITION, 0, 0);
customToast.setDuration(DURATION);
customToast.show();
}
}
و اکتویتی اصلی برای نمایش toast سفارشی که در داخل سورس اندروید این پروژه وجود دارد.
خرید سورس toast سفارشی :
ممنون . خیلی کاربردی و زیبا بود
خواهش می کنم دوست عزیز ، خوشحال شدم که استفاده کردید
سلام سال ۹۵ مبارک
میشه اموزش بهترین و پرکاربردترین روش ست کردن فونت فارسی روی برنامه رو در اندروید استودیو اموزش بدید؟
و اینکه راهی هست که بشه تنها با یک تغییر فونت رو روی کل متن های داخل برنامه اعمال کرد؟
ممنون و خسته نباشید به شما استاد گرامی
سلام بر شما دوست عزیز
سال نو بر شما هم مبارک باشه – انشاالله سال خوبی داشته باشید.
بهترین روشی که سراغ دارم را در مطلب آموزش تغییر فونت فارسی در برنامه اندرویدگذاشتم که به زودی ویدئوی اونا قرار میدم.
شاد و پر انرژی باشید – یا حق