2013年11月26日 星期二

Android dialog fullscreen

Dialog fullscreen有個問題

就是邊邊會有空隙

沒有真正的fullscreen

解決方法

第一步: dialog 套用以下style

<style name="full_screen_dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>


第二步: 程式碼加上此行

dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);

2013年11月22日 星期五

Android 防止螢幕方向更改時 activity執行onCreate()


第一步防止螢幕旋轉,鎖定在portrait方向

Manifest.xml

<activity
...
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
...
>


第二步override onConfigurationChanged(),內容放空

第二步非必需但以下情形要
當你在程式中開啟其它activity然後其它activity有旋轉方向!

例如:
開啟相機,然後onActivityResult()取回圖檔
此時因相機旋轉方向且Android釋放記憶activity 跑了Destroy();
此時取回圖檔時會多跑onCreate()!
所以要override onConfigurationChanged(),防止跑onCreate()!


public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
   // Checks the orientation of the screen
    if(newConfig.orientation ==Configuration.ORIENTATION_LANDSCAPE){
       //Toast.makeText(this,"landscape",Toast.LENGTH_SHORT).show();
    }else if(newConfig.orientation ==Configuration.ORIENTATION_PORTRAIT)     {
       //Toast.makeText(this,"portrait",Toast.LENGTH_SHORT).show();
    }
}

2013年11月19日 星期二

Android detect dialog closed


萬解
不管是因為按back鍵或按到dialog外面區域或按dialog內元件產生的dialog關閉,通通會跑來這裡

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
//do something you wanna here
    dialog.dismiss();
}
});

2013年11月12日 星期二

Android double no scientific notation

不想印出科學符號

ex: 1.33E10  ->  13300000000

Double temp=Double.parseDouble(textview.getText().toString());
s=new BigDecimal(temp).toPlainString();
textview.setText(s);


另一個解法是使用 DecimalFormat


Android TextView 刪除線


TextView textview = (TextView) findViewById(R.id.textview1);
Paint paint = textview.getPaint();
paint.setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
paint.setAntiAlias(true);

2013年11月10日 星期日

Android main activity restart programmatically


整個APP重啟

Intent i = new Intent(getBaseContext(), MainActivity.class);
 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 startActivity(i);

Android change language programmatically


取得系統目前語系

Locale currentLanguage = getResources().getConfiguration().locale;
String language=currentLanguage .toString();


更改語系

Locale locale;
locale = new Locale("zh","TW");   //locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = a.getBaseContext().getResources().getConfiguration();
config.locale = locale;
a.getBaseContext().getResources().updateConfiguration(config,
a.getBaseContext().getResources().getDisplayMetrics());

2013年11月9日 星期六

Android 關閉鍵盤輸入

InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(my_editText.getWindowToken(), 0);