Показаны сообщения с ярлыком android. Показать все сообщения
Показаны сообщения с ярлыком android. Показать все сообщения

вторник, 25 февраля 2014 г.

Android: custom Toast

XML layout


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/error_toast_background"
    android:gravity="center_vertical">

    <ImageView
        android:padding="@dimen/small_content_padding"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/toast_warning"/>

    <TextView
        android:id="@+id/toast_message"
        style="@style/FIS.Error.Toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/accept"/>
</LinearLayout>

Java


public static Toast createErrorToast(LayoutInflater inflater, Context context, String text) {

        Toast toast = new Toast(context);
        toast.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);

        View toastContent = inflater.inflate(R.layout.toast_content, null);
        assert toastContent != null;

        TextView message = (TextView) toastContent.findViewById(R.id.toast_message);
        assert message != null;

        message.setText(Html.fromHtml(text));

        toast.setView(toastContent);
        return toast;
    }

четверг, 18 июля 2013 г.

Android: warning "SetJavaScriptEnabled"

Используя WebView есть риск получить варнинг setJavaScriptEnabled.

        WebView webView = (WebView) findViewById(R.id.web_view);
        webView.getSettings().setJavaScriptEnabled(true);

Это предупреждение о потенциальной XSS уязвимости.
Для решения этой проблемы можно отключить JS (by default значение равно false), или, если вы уверены в том, что эксплуатация XSS уязвимостей невозможна (например, вы отображаете свой HTML контент), можно "задавить" предупреждение аннотацией на класс или метод:

        @SuppressLint("SetJavaScriptEnabled")

Или комментарием для IDE (проверено в IntellijIdea/AndroidStudio):

        //noinspection AndroidLintSetJavaScriptEnabled
        webView.getSettings().setJavaScriptEnabled(true);

среда, 20 февраля 2013 г.

Android: How to get current screen orientation?


private int getScreenOrientation() {
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        int orientation;
        // if the device's natural orientation is portrait:
        if ((rotation == Surface.ROTATION_0
                || rotation == Surface.ROTATION_180) && height > width ||
                (rotation == Surface.ROTATION_90
                        || rotation == Surface.ROTATION_270) && width > height) {
            switch (rotation) {
                case Surface.ROTATION_0:
                case Surface.ROTATION_180:
                    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                    break;
                case Surface.ROTATION_90:
                case Surface.ROTATION_270:
                    orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                    break;
                default:
                    Utils.logDebug(TAG, "Unknown screen orientation. Defaulting to portrait.");
                    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                    break;
            }
        } else {
            // if the device's natural orientation is landscape or if the device
            // is square:
            switch (rotation) {
                case Surface.ROTATION_0:
                case Surface.ROTATION_180:
                    orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                    break;
                case Surface.ROTATION_90:
                case Surface.ROTATION_270:
                    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                    break;
                default:
                    Utils.logDebug(TAG, "Unknown screen orientation. Defaulting to portrait.");
                    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                    break;
            }
        }
 
        return orientation;
    }