Commit a930451c by JosephusZhou

Optimized the custom layout of Android Toast, using GradientDrawable to set the style.

parent 89ee8941
package io.github.ponnamkarthik.toast.fluttertoast;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
......@@ -16,17 +14,21 @@ import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
/** FluttertoastPlugin */
/**
* FluttertoastPlugin
*/
public class FluttertoastPlugin implements MethodCallHandler {
private Context ctx;
private Toast toast;
private Context mContext;
private Toast mToast;
private FluttertoastPlugin(Context context) {
ctx = context;
mContext = context;
}
/** Plugin registration. */
/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "PonnamKarthik/fluttertoast");
channel.setMethodCallHandler(new FluttertoastPlugin(registrar.context()));
......@@ -36,104 +38,68 @@ public class FluttertoastPlugin implements MethodCallHandler {
public void onMethodCall(MethodCall call, final Result result) {
switch (call.method) {
case "showToast":
String msg = call.argument("msg").toString();
String mMessage = call.argument("msg").toString();
String length = call.argument("length").toString();
String gravity = call.argument("gravity").toString();
Number bgcolor = call.argument("bgcolor");
Number textcolor = call.argument("textcolor");
Number textSize = call.argument("fontSize");
Integer realGravity;
int mGravity;
switch (gravity) {
case "top":
realGravity = Gravity.TOP;
mGravity = Gravity.TOP;
break;
case "center":
realGravity = Gravity.CENTER;
mGravity = Gravity.CENTER;
break;
default:
realGravity = Gravity.BOTTOM;
mGravity = Gravity.BOTTOM;
break;
}
int mDuration;
if (length.equals("long")) {
mDuration = Toast.LENGTH_LONG;
} else {
mDuration = Toast.LENGTH_SHORT;
}
if(bgcolor != null && textcolor != null && textSize != null) {
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (bgcolor != null && textcolor != null && textSize != null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.toast_custom, null);
TextView text = layout.findViewById(R.id.text);
toast = new Toast(ctx);
if (length.equals("long")) {
toast.setDuration(Toast.LENGTH_LONG);
} else {
toast.setDuration(Toast.LENGTH_SHORT);
}
text.setText(msg);
toast.setView(layout);
if (textSize != null)
text.setTextSize(textSize.floatValue());
Drawable shapeDrawable;
// shapeDrawable = ctx.getDrawable(ctx.getResources(), R.drawable.toast_bg, null);
shapeDrawable = ctx.getResources().getDrawable(R.drawable.toast_bg);
if (bgcolor != null && shapeDrawable != null) {
shapeDrawable.setColorFilter(bgcolor.intValue(), PorterDuff.Mode.SRC_IN);
}
layout.setBackground(shapeDrawable);
// if(bgcolor != null) {
// Drawable shapeDrawable;
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// shapeDrawable = ctx.getResources().getDrawable(R.drawable.toast_bg,ctx.getTheme());
// } else {
// shapeDrawable = ctx.getResources().getDrawable(R.drawable.toast_bg);
// }
//
// if (shapeDrawable != null) {
// shapeDrawable.setColorFilter(bgcolor.intValue(), PorterDuff.Mode.SRC_IN);
// if (Build.VERSION.SDK_INT <= 27) {
// toast.getView().setBackground(shapeDrawable);
// } else {
// toast.getView().setBackground(null);
// text.setBackground(shapeDrawable);
// }
// }
// }
if (textcolor != null) {
text.setTextColor(textcolor.intValue());
}
toast.setGravity(realGravity,0,0);
toast.show();
text.setText(mMessage);
// Custom style
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(bgcolor.intValue());
gradientDrawable.setCornerRadius(dp2px(4.0f));
text.setBackground(gradientDrawable);
text.setTextSize(textSize.floatValue());
text.setTextColor(textcolor.intValue());
mToast = new Toast(mContext);
mToast.setDuration(mDuration);
mToast.setView(layout);
} else {
int toastLength;
if (length.equals("long")) {
toastLength = Toast.LENGTH_LONG;
} else {
toastLength = Toast.LENGTH_SHORT;
}
Toast toast = Toast.makeText(ctx, msg, toastLength);
toast.setGravity(realGravity,0,0);
toast.show();
mToast = Toast.makeText(mContext, mMessage, mDuration);
}
if (mGravity == Gravity.CENTER) {
mToast.setGravity(mGravity, 0, 0);
} else if (mGravity == Gravity.TOP) {
mToast.setGravity(mGravity, 0, 100);
} else {
mToast.setGravity(mGravity, 0, 100);
}
mToast.show();
result.success(true);
break;
case "cancel":
if (toast != null)
toast.cancel();
if (mToast != null) {
mToast.cancel();
}
result.success(true);
break;
default:
......@@ -141,4 +107,9 @@ public class FluttertoastPlugin implements MethodCallHandler {
break;
}
}
private float dp2px(float dp) {
final float scale = mContext.getResources().getDisplayMetrics().density;
return dp * scale + 0.5f;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_container"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#DAAA"
>
<TextView android:id="@+id/text"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:singleLine="false"
/>
</LinearLayout>
\ No newline at end of file
android:background="#CC000000"
android:paddingStart="16dp"
android:paddingTop="10dp"
android:paddingEnd="16dp"
android:paddingBottom="10dp"
android:textColor="#FFFFFF"
tools:text="Toast should be short." />
</FrameLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment