顯示具有 UI 標籤的文章。 顯示所有文章
顯示具有 UI 標籤的文章。 顯示所有文章

2018年4月5日 星期四

Add a customized theme to AlertDialog

Reference:

http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/

Code snippet:

Context mThemeContext = new ContextThemeWrapper(this, R.style.customized_theme);
View dialogView = View.inflate(mThemeContext, R.layout.my_dialog, null);
AlertDialog.Builder aBuilder = new AlertDialog.Builder(mThemeContext);
aBuilder.setView(dialogView);

// setup your AlertDialog
......

AlertDialog aDialog = aBuilder.create();
aDialog.show();

2018年2月5日 星期一

Add/Remove Toolbar elevation

Reference:

http://blog.iamsuleiman.com/add-a-toolbar-elevation-on-pre-lollipop/
https://www.reddit.com/r/androiddev/comments/2k50fw/elevation_on_toolbar_not_working_on_prelollipop/
https://www.jianshu.com/p/802545cec682

Code snippet:

Add elevation
toolbar.setElevation(elevation);

Remove elevation
toolbar.setElevation(0);

Change the color of toolbar overflow icon

Reference:

Method 1:
https://snow.dog/blog/how-to-dynamicaly-change-android-toolbar-icons-color/
https://stackoverflow.com/questions/43537958/change-toolbar-overflow-icon-color/43539097
https://futurestud.io/tutorials/android-quick-tips-8-how-to-dynamically-tint-actionbar-menu-icons

Method 2:
https://www.murrayc.com/permalink/2014/10/28/android-changing-the-toolbars-text-color-and-overflow-icon-color/
https://stackoverflow.com/questions/27995461/appcompat-toolbar-change-overflow-icon-color-in-actionmode


Code snippet:

Method 1:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@color/action_bar_text_color</item>
    <!-- change the overflow icon color (three vertical dots) -->    <item name="android:tint">@color/action_bar_overflow_icon_color</item>
    <item name="android:textSize">20sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:fontFamily">Roboto Medium</item>
</style>

2018年2月4日 星期日

Change the color of status bar and icon/text

Reference:

https://developer.android.com/training/material/theme.html
https://solinariwu.blogspot.tw/2017/05/android-status-bar.html

Code snippet:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize the status bar color -->    <item name="android:statusBarColor">@color/status_bar_color</item>
    <!-- If set, the status bar will be drawn such that it is compatible with a light status bar background -->    <item name="android:windowLightStatusBar">true</item>
</style>

2018年2月1日 星期四

Change navigation bar color

Reference:

https://stackoverflow.com/questions/27839105/android-lollipop-change-navigation-bar-color

Code snippet:

styles.xml
<item name="android:navigationBarColor">@color/theme_color</item>

XXX.java
window.setNavigationBarColor(@ColorInt int color)

Completely transparent status bar and navigation bar

Reference:

https://stackoverflow.com/questions/29069070/completely-transparent-status-bar-and-navigation-bar-on-lollipop


Code snippet:


getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        WindowManager.LayoutParams.
FLAG_LAYOUT_NO_LIMITS);



<style name="FullScreenTheme" parent="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>

2017年11月22日 星期三

Notification icon flash on status bar

Problem:


當需要在短時間內持續更新notification的內容時
在建立Notification時,記得使用同一個Notification Builder物件,並且設定 setOnlyAlertOnce(true)
以免SystemUI一直在update notification,造成status bar UI一直在跳動

Code snippet:


return new Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
            mBuilder
                        .setContentTitle(title)
                        .setContentText(contentText)
                        .setContentIntent(contentIntent)
                        .setSmallIcon(R.drawable.noti_icon)
                        .setLargeIcon(appIcon)
                        .setSubText(appLabel)
                        .setColor(color)
                        .setOnlyAlertOnce(true)
                        .addAction(stopAction)
                        //.addAction(optionsAction)

                        .build();

2017年9月22日 星期五

Change ActionBar text color

Reference:

Code sinppet:
AppCompactActivity
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">
    <item name="titleTextStyle">@style/MyTitleTextStyle</item>
</style>

<style name="MyTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">CHANGE_COLOR_HERE</item>
</style>


Settings Activity
<style name="AppTheme" parent="@android:style/Theme.DeviceDefault.Settings">
        <item name="android:colorAccent">@color/your_color</item>
</style>

2017年8月25日 星期五

How to disable scrolling on ViewPager

Reference:

https://stackoverflow.com/questions/7814017/is-it-possible-to-disable-scrolling-on-a-viewpager

Code snippet:

1. Create a new CustomViewPager class
public class CustomViewPager extends ViewPager {

    private boolean isPagingEnabled = true;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onInterceptTouchEvent(event);
    }

    public void setPagingEnabled(boolean b) {
        this.isPagingEnabled = b;
    }
}

2. Replace the <com.android.support.V4.ViewPager> tag with <your_package_name.CustomViewPager > tag in layout.xml

3. Call setPagingEnabled(true) when you want to disable Scrolling and call setPagingEnabled(false) to enable it.

How to disable click event on TabLayout

Reference:

https://stackoverflow.com/questions/37809538/how-can-i-disable-click-on-tablayout-in-android

Code snippet:

mTabLayout = (TabLayout) findViewById(R.id.tab_layout);

LinearLayout tabStrip = (LinearLayout) mTabLayout.getChildAt(0);
for(int i = 0; i < tabStrip.getChildCount(); i++) {
    tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
        @Override        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
}

2016年6月3日 星期五

Detect if TextVIew is ellipsized

Code snippet
//Hide/Show More buttion
final TextView detail   = (TextView) findViewById(R.id.detail);
final TextView more     = (TextView) findViewById(R.id.more);
ViewTreeObserver vto = detail.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
                ViewTreeObserver observer = detail.getViewTreeObserver();
                observer.removeOnPreDrawListener(this);
                String text         = detail.getText().toString();
                String layoutText   = detail.getLayout().getText().toString();
                more.setVisibility((!text.equals(layoutText)) ? View.VISIBLE:View.INVISIBLE);
                return true;
        }
});

2016年5月26日 星期四

Recyclerview inside ScrollView not scrolling smoothly

Method 1:

Reference:

http://stackoverflow.com/questions/33143485/recyclerview-inside-scrollview-not-scrolling-smoothly

Illustration:

RecyclerView view = (RecyclerView) findViewById(R.id.myrecycler);
view .setNestedScrollingEnabled(false);

Drawback:

OutOfMemory when there are many bitmap in the RecyclerView


Method 2:

Wrap all the component into the RecyclerView

Error: ArrayAdapter requires the resource ID to be a TextView

Reference:

http://stackoverflow.com/questions/9280965/arrayadapter-requires-the-resource-id-to-be-a-textview-xml-problems

Illustration:

Layout xml裡只能包含一個 TextView,不能將 TextView包在其他的 layout裡,例如:LinearLayou

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ......
/>

2016年4月7日 星期四

The difference between getMeasuredHeight() and getHeight()

Reference:
http://blog.csdn.net/xujainxing/article/details/8985063

說明:
getMeasuredHeight() 是 View實際的大小
getHeight() 是 View 在 Screen內的大小
如果 View超出 Screen的邊界時,getMeasuredHeight() = getHeight() + View超出 Screen的大小

2015年12月27日 星期日

Center text on a bitmap

Reference:

http://www.skoumal.net/en/android-how-draw-text-bitmap/

Code snippet:

Bitmap cloneMarkerBitmap = oriMarkerBitmap.copy(Bitmap.Config.ARGB_8888,true);
Canvas canvas = new Canvas(cloneMarkerBitmap);
Paint textPaint = new Paint();
textPaint.setColor(mResources.getColor(R.color.white));
textPaint.setTextSize(convertDpToPixel(12));
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);

// draw text to the Canvas center
String markerNumber = Integer.toString(number);
Rect bounds = new Rect();
textPaint.getTextBounds(markerNumber, 0, markerNumber.length(), bounds);
int x = (cloneMarkerBitmap.getWidth())/2;
int y = (cloneMarkerBitmap.getHeight() + bounds.height())/2;
canvas.drawText(markerNumber, x, y, textPaint);

...............

    public float convertDpToPixel(float dp) {
        DisplayMetrics metrics = mResources.getDisplayMetrics();
        float px = dp * metrics.density;
        return px;
    }

Multi-line in EditText

Reference:
http://stackoverflow.com/questions/4233626/allow-multi-line-in-edittext-view-in-android

Code snippet:
<EditText
    android:inputType="textMultiLine" <!-- Multiline input -->
    android:lines="8" <!-- Total Lines prior display -->
    android:minLines="6" <!-- Minimum lines -->
    android:gravity="top|left" <!-- Cursor Position -->
    android:maxLines="10" <!-- Maximum Lines -->
    android:layout_height="wrap_content" <!-- Height determined by content -->
    android:layout_width="fill_parent" <!-- Fill entire width -->
    android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
/>

2015年12月3日 星期四

Display the keyboard automatically on AlertDialog with EditText

Reference:

http://stackoverflow.com/questions/12997273/alertdialog-with-edittext-open-soft-keyboard-automatically-with-focus-on-editte
Updated 2016/1/15:
http://stackoverflow.com/questions/8785023/how-to-close-android-soft-keyboard-programatically

Code snippet:

Builder builder = new Builder(this);
final EditText input = new EditText(this);
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
builder.setTitle(R.string.dialog_title_addsubject)
                .setMessage(R.string.dialog_addsubject)
                .setView(input)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        .......
                        imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
                        /* updated 2016/1/15
                         * to hide it, call the method again
                         * imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                         */
                        
                    }
                })
                .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
                        /* updated 2016/1/15
                         * to hide it, call the method again
                         * imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                         */
                    }

                });
builder.show();
input.requestFocus();
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);