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年5月6日 星期五

String Resources: Formatting and Styling

References:

http://developer.android.com/intl/zh-tw/guide/topics/resources/string-resource.html#FormattingAndStyling


Example:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

TextView 省略文字的用法

Reference:

http://blog.csdn.net/sonicliji/article/details/8523746

illustration:

1.android:ellipsize=”start”—–省略號顯示在開頭
Ex: ......start
2.android:ellipsize=”end”——省略號顯示在結尾
Ex: end.......
3.android:ellipsize=”middle”—-省略號顯示在中間
4.android:ellipsize=”marquee”–以跑馬燈的方式顯示(動畫橫向移動)

2016年4月27日 星期三

Android Studio build error: illegal character: '\ufeff'

References:

http://nobodycare.me/2014/11/07/android-studiode-common-questions/
http://www.codegur.net/36027875/character-set-issue-utf-8
http://lifelongprogrammer.blogspot.tw/2014/05/powershell-and-java-stumble-by-utf8-bom.html

Scenario:

Import android project from Eclipse to Android Studio.

Error message:

illegal character: '\ufeff'


Root cause:

The invisible prefix is \ufeff. U+FEFF is byte order mark (BOM).
But java uses utf8 without bom to read file.

Solution:

Opened the document in notepad and changed the encoding to UTF-8 to save it.

2016年4月19日 星期二

Install Android device driver

References:
http://j796160836.pixnet.net/blog/post/29610725

Step:
1. 到 Android SDK Manager裡安裝 Google USB Driver
2. 進入裝置管理員
3. 找到 Android device,右鍵選擇“更新驅動程式”
4. 瀏覽電腦上的驅動程式軟體
5. 讓我從電腦上的裝置驅動程式清單中挑選
6. 從磁碟安裝
7. 選擇  <你裝的android SDK的路徑>\extras\google\usb_driver\android_winusb
例如:
C:\Users\HueiJheYu\AppData\Local\Android\sdk\extras\google\usb_driver\android_winusb
8. Android ADB Interface