搜尋此網誌

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

2012年11月22日 星期四

CheckedTextView usage

CheckedTextView can be placed in the listview as a combination of checkbox and textview. This widget is very useful when you decide to let listview have a clickable item. CheckedTextView or you can check the list10 example under the view folder of android apidemos.

Focusable屬性

預設的設定上,使用者無法移動focus(焦點,也就是有外圍邊框的狀態)到此view, 若設為true,則使用者可以 拿到這個view的focus.

2012年11月14日 星期三

android在程式中設定背景顏色(background color)

要使用setBackgroundResource(R.color.white); 不可使用setBackgroundColor

2012年11月13日 星期二

Android 得到identifie resource id

Method 1: Using Java reflection features to get the resource ID.
public static int getResourceId(String name){
        int drawableId = -1 ;
        try {
            Class<drawable> res = R.drawable.class;
            Field field = res.getField(name);
            drawableId = field.getInt(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return drawableId;
    }
Method 2: Using Android Resources API.
public int getIdentifier (String name, String defType, String defPackage)

2012年11月8日 星期四

copy file from Android assets folder (由Android的assets資料夾拷貝檔案)

private void copyFile(String source, String destination) {
        InputStream srcStream;
        FileOutputStream desStream;
        try {
            srcStream = this.getResources().getAssets().open(source);
            desStream = new FileOutputStream(new File(destination));
            byte [] data= new byte [1024];
            while (srcStream.read(data) != -1) {
                desStream.write(data);
            }
            srcStream.close();
            desStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
add below expression in the AndroidManiFest.xml for getting access permission. 把這行加到AndroidManiFest.xml才有寫入權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2012年11月7日 星期三

open sqlite database file (打開sqlite database的檔案)

Step 1:
File dbfile = new File("/sdcard/dbname.sqlite" ); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

Step 2:

Write this permission expression in the AndroidManiFest.xml if you want to write a file into

device.



<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>



2012年11月6日 星期二

打開android DDMS

Eclipse:的使用者可以 Click Window > Open Perspective > Other... > DDMS.

這樣就可以在開發程式的同時, 監控device.

OPEN DDMS

2012年11月1日 星期四

設定TextView的 text color(字體顏色)

myFolderText.setTextColor(getResources().getColor(R.color.blue));

單純用R.color.blue是無效的

2012年10月31日 星期三

Android 事件回傳值的意義.

假如你有set onclick 和 set onTouch event listener在同一個元件, 因為on touch的執行順序高

於on click , 所以會先執行on touch , 在此時如果你return true 則代表你會停留在on touch ,

那麼on click事件將不會被執行到 , 所以一定要回傳false, 讓android 系統知道touch事件已處

理完畢, 可以接著處理接下來的event.

2012年7月6日 星期五

Set UI component width

private void modifyViewWidth(View src, View des){
        src.measure(0, 0);
        int width = src.getMeasuredWidth();
        des.setMinimumWidth(width);
        
    }

2012年6月27日 星期三

Android手機設定全螢幕 . no title

<application
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        >

定義android ,hdpi landscape

在android , 1 dp = 1.5 pixels,

所以當拿到800*480 pixels的圖片,

換算成android dp為

533* 320 

因此要放在res/drawable-hdpi folder-->portrait case

res/drawable-land-hdpi-->landscape case

2012年6月25日 星期一

Android findViewById()

要create UI的Instance, 儘量用findViewById

若要創造非UI的Instance,才用new.

2012年6月13日 星期三

Android FrameLayout

Activity在切換的時候會被移到背景去 

若使用FrameLayout的話可以讓View重疊顯示,而不會移到背景去

而visibility這個屬性可以定義元件是否要被看見.

visible:看得見
invisible:看不見,但還是佔有空間.
gone:看不見,也不占空間.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:id="@+id/pop_btn"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="Click Me" 
        android:onClick="onClickBtn"
        />
    
    <Button android:id="@+id/pop_btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" 
        android:onClick="onClickBtn"
        />
</FrameLayout>

Android popup window demo

MainActivity.java
package com.example.android.popwindow;

import com.example.android.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

public class MainActivity extends Activity {
    
    PopupWindow pw = null;

    boolean isClicked = false;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_main);
        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        pw = new PopupWindow(inflater.inflate(R.layout.pop_content,
                null, false), 100, 100, true);
        pw.setFocusable(false);
        
    }
    
    public void onClickBtn(View v){
        if(isClicked == true){
            pw.dismiss();
            isClicked = false;
        }else if(isClicked == false){
            pw.showAsDropDown(this.findViewById(R.id.pop_btn));
            isClicked = true;
        }
    }

}


We can use showAtLocation  to replace the showAsDropDown. Because this method is

more flexibile than the prior one.

res/layout/pop_content.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Content"
         />

</LinearLayout>
res/layout/pop_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:id="@+id/pop_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" 
        android:onClick="onClickBtn"
        />
</LinearLayout>

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="14" />

    <application android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme">
        
        <activity android:name="com.example.android.popwindow.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Reference: Android Developer Guide

2012年6月11日 星期一

如何獲得android md5 key

因為win7預設是使用sha1而不是md5簽章,

可以輸入下列指令來取得md5加密的簽章:

keytool -v -list -alias androiddebugkey -keystore "你的debug.keystore檔案的位置" -storepass android -keypass android



如何知道android keystore的位置

站長是在win7使用eclipse開發Android程式,

因此可以利用eclipse找到debug keystore的位置.

到preferences-> android-> build-> 就會看到預設的debug store目錄了. 


如何獲得MD5 key

2012年6月8日 星期五

Activity lifecycle, 生命週期.


onCreate() :
當activity第一次被創建出來的時候呼叫此方法.
我們可以在這一個階段作初始化的動作.
接下來進入到onStart().這個階段.

onRestart():
當activity由停止到開始之前呼叫.

onStart():
當activity開始被使用者看到會呼叫.

onResume():
當activity準備開始和使用者互動時呼叫.
在這個時間點,你的activity會在activity stack的頂端.

onPause():
當系統要開始復原前一個activity的時候呼叫.
Called when the system is about to start resuming a previous activity.

onStop():
當這個activity不再被使用者看到的時候呼叫.因為另一個activity已經被恢復了.

onDestroy():
在你的activity被摧毀之前,最後一個呼叫.





Android Button click handler implementation, 按鈕onclick監聽器實作

Here is implementation 1:

You can use this implementation style when your click event is definitely performed every time.

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" 
              android:orientation="vertical" >
    
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" 
            />
</LinearLayout>
MainActivity.java
package com.example.android.demo;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.android.demo.R;

public class MainActivity extends Activity {

    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button button = (Button) findViewById(R.id.button);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 Toast.makeText(MainActivity.class,"Hello, I am a Button" , Toast.LENGTH_SHORT);
             }
         });
    }
   
}

Here is implementation 2:

You can use this implementation style when your click event is not always executed.

This style is a little bit like the later-binding.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" 
              android:orientation="vertical" >
    
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" 
            android:onClick="onClickButton"
            />
</LinearLayout>
MainActivity.java
package com.example.android.demo;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.android.demo.R;

public class MainActivity extends Activity {

    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onClickButton(View v) {
        Toast.makeText(MainActivity.class,"Hello, I am a Button" , Toast.LENGTH_SHORT);
    }
   
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.demo"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="14" />

    <application android:label="@string/app_name">
        <activity android:name=".MainActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


補充:

public static interface

View.OnClickListener


這是一個靜態介面用來定義當view被按下去的時候,要做的動作.



public abstract void onClick (View v)


則是要實作的方法,參數View 為被按下的View本身.
 Reference :Android developer guide

2012年6月4日 星期一

android:gravity vs android:layout_gravity

android:gravity ~~~> 指元件自己放的位置. Specifies how to place the content of an object, both on the x- and y-axis, within the object itself. android:layout_gravity ~~~>指元件放在相對於父容器或元件的位置.
Reference