2013年7月31日 星期三

[Android] RelativeLayout範例介紹

在Android裡,RelativeLayout是一個最彈性的的Layout,
主要是利用上、下、左、右(above, below, left and right)來排列component的位置,
以我們這個例子來說,button2在button1的下面,button3在button2的右下方,button4在button3的右下方。

File : res/layout/main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_toRightOf="@+id/button2"
        android:text="Button3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:layout_toRightOf="@+id/button3"
        android:text="Button4" />

</RelativeLayout>
結果:

[Android] TableLayout 範例介紹

在這個例子,我們來看如何用TableLayout來排列textView和button,
及如何使用“android:layout_span”將button寬度延伸成2欄位(column),
及使用“android:layout_column”來將按鈕放到特定的欄位。

File : res/layout/main.xml 
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <!-- 2個欄位 -->
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
        <TextView
            android:id="@+id/textView1"
            android:text="第 1 欄"
            android:textAppearance="?android:attr/textAppearanceLarge" />
 
        <Button
            android:id="@+id/button1"
            android:text="第 2 欄" />
    </TableRow>
 
    <!-- 將按鈕延伸至2個欄位寬  -->
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
         <Button
            android:id="@+id/button2"
            android:layout_span="2"
            android:text="第 1 和 2 欄" />
         
       
    </TableRow>
 
 
    <!-- 3個欄位 -->
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
        <TextView
            android:id="@+id/textView2"
            android:text="第 1 欄"
            android:textAppearance="?android:attr/textAppearanceLarge" />
 
        <Button
            android:id="@+id/button2"
            android:text="第 2 欄" />
 
        <Button
            android:id="@+id/button3"
            android:text="第 3 欄" />
    </TableRow>
 
    <!-- 利用layout_columnundefinedbase is 0),將按鈕放在第2欄 -->
    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
        <Button
            android:id="@+id/button4"
            android:layout_column="1"
            android:text="第 2 欄" />
    </TableRow>
 
    <!-- 利用layout_columnundefinedbase is 0),將按鈕放在第3欄 -->
    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
        <Button
            android:id="@+id/button5"
            android:layout_column="2"
            android:text="第  3 欄" />
    </TableRow>
 
</TableLayout>

結果:



















延伸閱讀: [Android] RelativeLayout範例介紹

2013年7月21日 星期日

[Android] LinearLayout 範例介紹

LinearLayout是一個很常用的Layout,
主用是用orientation屬性來把component(如button)做垂直及水平的排列,
另外,帶有權重(weight)的component將會填滿剩餘的空間

我們來舉個例子,來看看如何把component做垂直及水平的排列,及weight如何使用

1. LinearLayout – 水平(Horizontal)
打開res/layout/main.xml,加入 android:orientation="horizontal",然後增加3個按鈕,
在這個例子中,我們將權重設在button3,所以它將會填滿剩餘的空間

res/layout/main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3" 
        android:layout_weight="1"/>
 
</LinearLayout>
結果:



2. LinearLayout – 垂直(Vertical)
我們把orientation改成“Vertical”

res/layout/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/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3" 
        android:layout_weight="1"/>
 
</LinearLayout>
結果:

[Android] 解決問題Your project contains errors,please fix them before running your application.

今天開啟eclipse要使用時,發現無法編譯,只出現Your project contains errors,please fix them before running your application.









 檢查所有程式,也找不到任何錯誤,只有project name上出現一個紅色叉叉,
 一般這個問題,可能是無法讀取R.class,我會先刪除gen\R.java檔,然後refresh
 然後Project>clean整個project,最後重新編譯一次。
 但這次這個方法沒用,哈























最後google發現是Debug認證已經過期,解決方法很簡單,
進入windows>Preferences>Android->Build,找到debug.keystore的路徑,刪除就可以了,
系統會在自己產生一個

2013年7月7日 星期日

[Android] adb logcat使用方法

● Logcat是一個即時觀看系統訊息(system messages)的工具,一般會利用ADB(android debug bridge)來啟動,如果是用eclipse開發,就在windows\ShowView裡,選LogCat,就可以打開了。

這裡我們利用ADB來作示範,在裝完android SDK後,裡面就會包含了ADB,
以Windows為例,adb路徑會在android-sdks\platform-tools\adb.exe,所以在開啟terminal後,要先切到這個路徑下。
而Linux,裝好SDK,開啟terminal就可以用了。

使用之前裝置端要先開啟USB偵錯模式,路徑如下,設定>開發人員選項>USB偵錯
然後在電腦端輸入
$ adb devices
會得到下面結果,表示已和裝置端連接成功



● 下列是Log class提供了幾個列印message到logcat的方法
v(String, String) (verbose)
d(String, String) (debug)
i(String, String) (information)
w(String, String) (warning)
e(String, String) (error)

一般在開發程式時,會在每個function的的第一行加上log,方便日後trace code
像下面這樣
Log.i("MyActivity", "test"); //log.i裡的i是指(information)
LogCat就會輸出log成這樣
I/MyActivity( 1555): test


● 如何輸出LOG呢
$ adb logcat
但這種方法log太多了,會印出全部的log,包含verbose/debug/information/warning/error
所以我們需要用到下列的方法過濾出我們要的部分


● 過濾log(Filtering Log Output)
每一個Android的log都包含了標籤(tag),和優先權(priority)
例如,I/MyActivity( 1557): test //I是優先權,又分成了下列幾種;MyActivity就是標籤
Android優先權:
V — Verbose (最低優先權)
D — Debug
I — Info
W — Warning
E — Error
F — Fatal
S — Silent (最高優先權,S表示不會用印出任何LOG)
為什麼分成七級呢?看到例子三就明白了

知道了android log之後,要如何過濾呢?我們用下面幾個例子來學習學習
例子一
//如果只想印出這種寫法Log.i("MyActivity", "test")的log,則要這樣寫

$ adb logcat MyActivity:I *:S

//MyActivity:I是表示,印出MyActivity標籤及information優先權的log

//*:S 是指其它所有的log都silent,也就是都不印出來

例子二
//如果我們一次想看這2種logs,則該怎麼寫呢?

//Log.i("MyActivity", "test");

//Log.d("MyActivity2", "test2");

$ adb logcat MyActivity:I MyActivity2:D *:S

例子三
那如果這樣寫$ adb logcat *:W 會發生什麼事呢?

這樣就會印出所有log,且優先權在Warning以上的log,也就是Warning/Error/Fatal



● 延伸
一般我們在做phone的project時,就會常用到
$ adb logcat -v time -b radio
//-v time是指出時間,-b radio是指印出radio和telephony相關訊息


●更多參考資料:
http://developer.android.com/tools/debugging/debugging-log.html#startingLogcat

2013年6月27日 星期四

[Android] Intent用法

● 呼叫電話(call a dial screen)
這裡要注意的是,"tel:"是必要的,不然會有錯誤
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("tel:1234567890"));
        startActivity(intent);


● 直接撥打電話(make a phone call)
如果不想要顯示撥號畫面,想直接撥打電話,則需要使用ACTION_CALL
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:0123456789"));
        startActivity(intent);
因為有使用到ACTION_CALL,所以要在manifest加入權限
<uses-permission android:name="android.permission.CALL_PHONE" />


● 呼叫瀏覽器(Browser)
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.acer.com"));
        startActivity(intent);

加入搜尋字串
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_WEB_SEARCH);
        // 設定搜尋的字串
        intent.putExtra(SearchManager.QUERY, "acer");
        startActivity(intent);


● 呼叫相機(Camera)
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 1);
取得拍攝後的照片
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode != RESULT_OK) {
            // 無法得到正確結果時會跑到這裡
            return;
        }

        if (requestCode == 1) {
            // 這個bitmap是拍攝之照片的資料
            Bitmap bitmap = (Bitmap) data.getExtras().get("data");
            // 在LAYOUT拉一個ImageView,來顯示照片
            ImageView imageView = (ImageView) findViewById(R.id.image);
            imageView.setImageBitmap(bitmap);

        }
}


● 呼叫錄音機(Recorder)
        Intent intent = new Intent();
        intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
        startActivity(intent);


● 呼叫Market應用程式(call market application)
       Intent intent = new Intent(Intent.ACTION_VIEW);
       intent.setData(Uri.parse("market://details?id=comq.android.autoRedial"));
       startActivity(intent);


● 顯示聯絡人清單
    Intent intent  = new Intent(Intent.ACTION_VIEW, People.CONTENT_URI);  
    startActivity(intent );  
顯示第3筆聯絡人的詳細資料
Uri uriContact = ContentUris.withAppendedId(People.CONTENT_URI, 3); //3 是聯絡人ID  
Intent intent   = new Intent(Intent.ACTION_VIEW, uriContact );  
startActivity(intent   );


● 顯示地圖
Uri uri = Uri.parse("geo:25.718058,-100.279487");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
//其他geo的用法 
//geo:latitude,longitude <--上面例子是這個
//geo:latitude,longitude?z=zoom    
//geo:0,0?q=business+near+city  
//google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom 
//geo:0,0?q=my+street+address 
路徑規劃
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"); 
Intent intent  = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(intent ); 


延伸閱讀: 傳送郵件(Send mail)

2013年6月24日 星期一

[Android] 傳送郵件(Send mail)

● 傳送郵件(Send mail)
      Intent intent = new Intent();
      intent.setAction(Intent.ACTION_SENDTO);
      intent.setData(Uri.parse("mailto:chenkanzo@gmail.com"));
      intent.putExtra(Intent.EXTRA_SUBJECT, "這裡是主旨。");
      intent.putExtra(Intent.EXTRA_TEXT, "這是本文內容。");
      startActivity(intent);

● 傳送附件(Send mail with enclosed file)
和傳送郵件不同的利用Intent.ACTION_SEND,
而附件檔案必須利用Uri型別指定。
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        String[] to = {"hoge@example.com"};
        intent.putExtra(Intent.EXTRA_EMAIL, to);
        intent.putExtra(Intent.EXTRA_SUBJECT, "這是主旨。");
        intent.putExtra(Intent.EXTRA_TEXT, "這是本文內容。");
        
        // 選擇一張圖片來加入附件
        // 取得的SD卡資料夾
        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard, "test.jpg");
        intent.putExtra(Intent.EXTRA_STREAM,
                        Uri.parse("file://"+ file.getAbsolutePath()));
        intent.setType("image/jpeg");
        //呼叫應用程式列表
        startActivity(Intent.createChooser(intent,"請選擇郵件應用程式,例如GMAIL"));

延伸閱讀 Intent大全