Android วิธี set up Intel HAXM

โดยปกติเมื่อเราโหลดตัว ADT (Eclipse) หรือ Android Studio มา emu จะมี CPU ให้เลือกใช้สำหรับการรัน emu เพียงตัวเดียวคือ ARM ในการรัน emu แต่ละครั้งนานมาก จากปัญหานี้นักพัฒนาส่วนใหญ่เลยหันไปเลือกใช้ Genymotion มากกว่า

ดังนั้นเราเลยเสนอทางออกอีกวิธีหนึ่ง คือการใช้ Intel HAXM ซึ่งมีความเร็วพอๆ กับตัว Genymotion เราสามารถ set ค่า Intel HAXM ได้ดังต่อไปนี้ครับ

ความต้องการของ Intel HAXM


ที่มา  :  intel

วิธีการติดตั้ง
1. ดาว์โหลดตัว Intel Hardware Accelerated Execution Manager ใน android SDK Manager ก่อน ซึ่งอยู่ใน หมวด extra
2. ดาว์โหลดตัว intel x86 Atom System Image ของ API ที่ต้องการรัน
3. ทำการติดตั้งไฟล์ intel hardware accelerated execution manager (ซึ่งอยู่ในโฟรเดอร์ sdk : .../sdk/extras/intel/Hardware_Accelerated_Execution_Manager)

ภาพตัวอย่างการเซต CPU Intel Atom



วิดีโอสอนและทดสอบตัว Intel Hardware Accelerated Execution Manager



Android Action Bar Refresh Activity

การทำปุ่ม Refresh บน Action Bar จะมีด้วยกัน 3 ไฟล์ คือ activity_main.xml, menu.xml และ MainActivity.java



สำหรับใครที่ต้องการ icon สำหรับทำ action bar
สามารถดาว์โหลดได้ที่ : https://developer.android.com/design/downloads/index.html


หมายเหตุ : สำหรับ project เครื่องใดไม่มี folder menu ให้สร้างขึ้นมาใหม่ได้เลย โดยคลิกขวาที่ folder res เลือก new เลือก Folder ครับ พอสร้าง folder menu สร้างแล้วให้สร้างไฟล์ xml ชื่อว่า menu.xml
Example : menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/ic_action_refresh"
        android:title="Refresh"/>
    <item
        android:id="@+id/action_settings"
        android:title="Settings">
    </item>
</menu>
Example : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
</LinearLayout>
Example : MainActivity.java
package com.example.actionbar3;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        
    }
 
 public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
     
     case R.id.action_refresh:
       Toast.makeText(this, "คุณกำลังกดปุ่ม Refresh อยู่", Toast.LENGTH_SHORT).show();
       
       Intent reload = new Intent(MainActivity.this, MainActivity.class);
       startActivity(reload);
       
       break;
     
     case R.id.action_settings:
       Toast.makeText(this, "คุณกำลังกดปุ่ม Settings อยู่", Toast.LENGTH_SHORT).show();
       break;
     default:
       break;
     }

     return true;
   }
 
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);
      return true;
  } 
 

}

ผลลัพธ์ : 



Android Action Bar การทำปุ่ม Refresh

การทำปุ่ม Refresh บน Action Bar จะมีด้วยกัน 3 ไฟล์ คือ activity_main.xml, menu.xml และ MainActivity.java


สำหรับใครที่ต้องการ icon สำหรับทำ action bar
สามารถดาว์โหลดได้ที่ : https://developer.android.com/design/downloads/index.html


หมายเหตุ : สำหรับ project เครื่องใดไม่มี folder menu ให้สร้างขึ้นมาใหม่ได้เลย โดยคลิกขวาที่ folder res เลือก new เลือก Folder ครับ พอสร้าง folder menu สร้างแล้วให้สร้างไฟล์ xml ชื่อว่า menu.xml
Example : menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/ic_action_refresh"
        android:title="Refresh"/>
    <item
        android:id="@+id/action_settings"
        android:title="Settings">
    </item>
</menu>
Example : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
</LinearLayout>
Example : MainActivity.java
package com.example.actionbar2;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        
    }
 
 public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
     
     case R.id.action_refresh:
       Toast.makeText(this, "คุณกำลังกดปุ่ม Refresh อยู่", Toast.LENGTH_SHORT)
           .show();
       break;
     
     case R.id.action_settings:
       Toast.makeText(this, "คุณกำลังกดปุ่ม Settings อยู่", Toast.LENGTH_SHORT)
           .show();
       break;
     default:
       break;
     }

     return true;
   }
 
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);
      return true;
  } 

}
ผลลัพธ์ : 





Android Action Bar Icon แบบต่างๆ

การทำ Action Bar Icon จะมีด้วยกัน 3 ไฟล์ คือ activity_main.xml, list_item_action_bar.xml และ MainActivity.java





สำหรับใครที่ต้องการ icon สำหรับทำ action bar
สามารถดาว์โหลดได้ที่ : https://developer.android.com/design/downloads/index.html


หมายเหตุ : สำหรับ project เครื่องใดไม่มี folder menu ให้สร้างขึ้นมาใหม่ได้เลย โดยคลิกขวาที่ folder res เลือก new เลือก Folder ครับ พอสร้าง folder menu สร้างแล้วให้สร้างไฟล์ xml ชื่อว่า list_item_action_bar.xml

Example : list_item_action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
     <!-- More -->
    <item
        android:id="@+id/a_More"
        android:icon="@android:drawable/ic_menu_more"
        android:showAsAction="always"
        android:title="More">
        <menu>

            <!-- Save -->
            <item
                android:id="@+id/save"
                android:icon="@android:drawable/ic_menu_save"
                android:showAsAction="never"
                android:title="Save"/>

            <!-- Help -->
            <item
                android:id="@+id/help"
                android:icon="@android:drawable/ic_menu_help"
                android:showAsAction="never"
                android:title="Help"/>

            <!-- Share -->
            <item
                android:id="@+id/share"
                android:icon="@android:drawable/ic_menu_share"
                android:showAsAction="never"
                android:title="Share"/>
        </menu>
    </item>
</menu>

Example : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
</RelativeLayout>
Example : MainActivity.java
package com.example.actionbar1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends Activity {
 
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        
    }
 
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.list_item_action_bar, menu);

        return super.onCreateOptionsMenu(menu);
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.save) {

            return true;
        }
        
        else if (item.getItemId() == R.id.help) {

            return true;
        }
        
        else if(item.getItemId() == R.id.share) {

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
ผลลัพธ์ : 




Android วิธีการทำ Tab Menu คล้ายๆ Line

Tab Menu ของ Line นั้นคล้ายๆ รูปแทนที่จะเป็นตัวอักษร มีความสวยงาม ซึ่งประกอบไปด้วย 10 ไฟล์ คือ activity_main.xml, activity_one.xml, activity_two.xml, activity_three.xml, activity_four.xml , OneActivity.java , TwoActivity.java , ThreeActivity.java และ MainActivity.java



Tab menu ของ Line


Tab Menu ที่จะทำกัน


วิธีนี้เป็นที่นิยมในการทำ android เพราะสามารถปรับแต่งหน้า layout ของแต่ละ Tab ได้อิสระ และยังปรับโค้ดของส่วน java ของแต่ละหน้าได้อีกด้วย

หมายเหตุ : ไฟล์รูปนั้นต้องเตรียมมาเองสำหรับขั้นตอนนี้ โดยจะแบ่งเป็น 2 อย่างคือ รูปที่ยังไม่กดปุ่ม กับ รูปที่กดปุ่มแล้ว

Example : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
         
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
             
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
             
    </LinearLayout>
     
</TabHost>

Example : activity_one.xml
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <TextView android:text="One"
             android:padding="15dip"
             android:textSize="18sp"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"/>
 </LinearLayout>
Example : activity_two.xml
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <TextView android:text="Two"
             android:padding="15dip"
             android:textSize="18sp"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"/>
 </LinearLayout>
Example : activity_three.xml
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <TextView android:text="Three"
             android:padding="15dip"
             android:textSize="18sp"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"/>
 </LinearLayout>
Example : activity_four.xml
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <TextView android:text="Four"
             android:padding="15dip"
             android:textSize="18sp"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"/>
 </LinearLayout>
Example : MainActivity.java
package com.example.tabhost4;

import android.os.Bundle;
import android.os.StrictMode;
import android.content.Intent;
import android.util.Log;
import android.widget.TabHost;
import android.app.TabActivity;
import android.widget.TabHost.OnTabChangeListener;

public class MainActivity extends TabActivity implements OnTabChangeListener {
 
 TabHost tabHost;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        tabHost = getTabHost();
         

        tabHost.setOnTabChangedListener(this);
     
        TabHost.TabSpec spec;
        Intent intent;

        intent = new Intent().setClass(this, OneActivity.class);
        spec = tabHost.newTabSpec("One").setIndicator("")
                      .setContent(intent);
        tabHost.addTab(spec);
   

        intent = new Intent().setClass(this, TwoActivity.class);
        spec = tabHost.newTabSpec("Two").setIndicator("")
                      .setContent(intent);  
        tabHost.addTab(spec);
   
        
        intent = new Intent().setClass(this, ThreeActivity.class);
        spec = tabHost.newTabSpec("Three").setIndicator("")
                      .setContent(intent);
        tabHost.addTab(spec);
        
        intent = new Intent().setClass(this, FourActivity.class);
        spec = tabHost.newTabSpec("Four").setIndicator("")
                      .setContent(intent);
        tabHost.addTab(spec);
   

        tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.clock);
        tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.cloud);
        tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.compass);
           

        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.clipboard2);
         
   
     }

   public void onTabChanged(String tabId) {
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
       {
           if(i==0)
               tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.clipboard);
           else if(i==1)
               tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.clock);
           else if(i==2)
               tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.cloud);
           else if(i==3)
            tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.compass);
       }

    if(tabHost.getCurrentTab()==0)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.clipboard2);
    else if(tabHost.getCurrentTab()==1)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.clock2);
    else if(tabHost.getCurrentTab()==2)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.cloud2);
    else if(tabHost.getCurrentTab()==3)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.compass2);
         
    }

}
Example : OneActivity.java
package com.example.tabhost3;

import android.app.Activity;
import android.os.Bundle;

public class OneActivity extends Activity {
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
      }

}
Example : TwoActivity.java
package com.example.tabhost3;

import android.app.Activity;
import android.os.Bundle;

public class TwoActivity extends Activity {
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
      }
 

}
Example : ThreeActivity.java
package com.example.tabhost3;

import android.app.Activity;
import android.os.Bundle;

public class ThreeActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three);
      }
}
Example : FourActivity.java
package com.example.tabhost3;

import android.app.Activity;
import android.os.Bundle;

public class ThreeActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_four);
      }
}
หมายเหตุ : เนื่องมีการเปลี่ยนหน้าแบบ intent ดังนั้นต้องไปเพิ่ม activity ในส่วนของ AndroidManifest ด้วย

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity 
            android:name="com.example.tabhost4.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>
        <activity 
            android:name="com.example.tabhost4.OneActivity"
            android:label="@string/app_name" >
        </activity>
        <activity 
            android:name="com.example.tabhost4.TwoActivity"
            android:label="@string/app_name" >
        </activity>
        <activity 
            android:name="com.example.tabhost4.ThreeActivity"
            android:label="@string/app_name" >
        </activity>
        <activity 
            android:name="com.example.tabhost4.FourActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>
ผลลัพธ์ : 





























ที่มา : androidexample

Android Tab Menu Custom Layout 2

การทำ Tab Menu Custom Layout อีกแบบจะมีด้วยกัน 8 ไฟล์ คือ activity_main.xml, activity_one.xml , activity_two.xml , activity_three.xml , OneActivity.java , TwoActivity.java , ThreeActivity.java และ MainActivity.java




วิธีนี้เป็นที่นิยมในการทำ android เพราะสามารถปรับแต่งหน้า layout ของแต่ละ Tab ได้อิสระ และยังปรับโค้ดของส่วน java ของแต่ละหน้าได้อีกด้วย

Example : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
         
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
             
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
             
    </LinearLayout>
     
</TabHost>

Example : activity_one.xml
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <TextView android:text="One"
             android:padding="15dip"
             android:textSize="18sp"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"/>
 </LinearLayout>
Example : activity_two.xml
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <TextView android:text="Two"
             android:padding="15dip"
             android:textSize="18sp"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"/>
 </LinearLayout>
Example : activity_three.xml
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <TextView android:text="Three"
             android:padding="15dip"
             android:textSize="18sp"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"/>
 </LinearLayout>
Example : MainActivity.java
package com.example.tabhost3;

import android.os.Bundle;
import android.os.StrictMode;
import android.content.Intent;
import android.util.Log;
import android.widget.TabHost;
import android.app.TabActivity;
import android.widget.TabHost.OnTabChangeListener;

public class MainActivity extends TabActivity implements OnTabChangeListener {
 
 TabHost tabHost;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        tabHost = getTabHost();
         

        tabHost.setOnTabChangedListener(this);
     
        TabHost.TabSpec spec;
        Intent intent;

        intent = new Intent().setClass(this, OneActivity.class);
        spec = tabHost.newTabSpec("One").setIndicator("หน้าที่หนึ่ง")
                      .setContent(intent);
        tabHost.addTab(spec);
   

        intent = new Intent().setClass(this, TwoActivity.class);
        spec = tabHost.newTabSpec("Two").setIndicator("หน้าที่สอง")
                      .setContent(intent);  
        tabHost.addTab(spec);
   
        
        intent = new Intent().setClass(this, ThreeActivity.class);
        spec = tabHost.newTabSpec("Three").setIndicator("หน้าที่สาม")
                      .setContent(intent);
        tabHost.addTab(spec);
   

        tabHost.getTabWidget().getChildAt(1);
        tabHost.getTabWidget().getChildAt(2);
           

        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0);
         
   
     }

   public void onTabChanged(String tabId) {
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
       {
           if(i==0)
               tabHost.getTabWidget().getChildAt(i);
           else if(i==1)
               tabHost.getTabWidget().getChildAt(i);
           else if(i==2)
               tabHost.getTabWidget().getChildAt(i);
       }

    if(tabHost.getCurrentTab()==0)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab());
    else if(tabHost.getCurrentTab()==1)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab());
    else if(tabHost.getCurrentTab()==2)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab());
         
    }

}
Example : OneActivity.java
package com.example.tabhost3;

import android.app.Activity;
import android.os.Bundle;

public class OneActivity extends Activity {
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
      }

}
Example : TwoActivity.java
package com.example.tabhost3;

import android.app.Activity;
import android.os.Bundle;

public class TwoActivity extends Activity {
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
      }
 

}
Example : ThreeActivity.java
package com.example.tabhost3;

import android.app.Activity;
import android.os.Bundle;

public class ThreeActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three);
      }
}
หมายเหตุ : เนื่องมีการเปลี่ยนหน้าแบบ intent ดังนั้นต้องไปเพิ่ม activity ในส่วนของ AndroidManifest ด้วย

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity 
            android:name="com.example.tabhost3.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>
        <activity 
            android:name="com.example.tabhost3.OneActivity"
            android:label="@string/app_name" >
        </activity>
        <activity 
            android:name="com.example.tabhost3.TwoActivity"
            android:label="@string/app_name" >
        </activity>
        <activity 
            android:name="com.example.tabhost3.ThreeActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>
ผลลัพธ์ : 





























ที่มา : androidexample

Android Tab Menu Custom Layout

การทำ Tab Menu Custom Layout จะมีด้วยกัน 5 ไฟล์ คือ activity_main.xml, activity_one, activity_two, activity_three และ MainActivity.java




วิธีนี้เป็นที่นิยมในการทำ android เพราะสามารถปรับแต่งหน้า layout ของแต่ละ Tab ได้อิสระ

Example : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            
           <include layout="@layout/activity_one"/>
           <include layout="@layout/activity_two"/>
           <include layout="@layout/activity_three"/>
           
        </FrameLayout>
        
    </LinearLayout>
</TabHost>

Example : activity_one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="One"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
Example : activity_two.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Two"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
Example : activity_three.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Three"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Example : MainActivity.java
package com.example.tabhost2;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;

public class MainActivity extends Activity {

 TabHost mTabHost;
 
   
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();
        
        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("One").setContent(R.id.tab1));
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Two").setContent(R.id.tab2));
        mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Three").setContent(R.id.tab3));
        
        mTabHost.setCurrentTab(0);
      
    }
    
}

ผลลัพธ์ : 



























ที่มา : Thaicreate

Android Tab Menu แบบง่ายๆ

การทำ Tab Menu จะมีด้วยกัน 2 ไฟล์ คือ activity_main.xml และ MainActivity.java



กรณีนี้จะใช้เพียง activity_main เดียวในการสลับหน้าไปมา

Example : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="One" />
                
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Two" />
                    
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Three" />
            
            </LinearLayout>
            
        </FrameLayout>
    </LinearLayout>
</TabHost>

Example : MainActivity.java
package com.example.tabhost1;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;

public class MainActivity extends Activity {

 TabHost mTabHost;
 
   
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();
        
        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("One").setContent(R.id.tab1));
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Two").setContent(R.id.tab2));
        mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Three").setContent(R.id.tab3));
        
        mTabHost.setCurrentTab(0);
      
    }
 
}

ผลลัพธ์ : 





























ที่มา : Thaicreate

Android Dialog Toast

การทำ Dialog Toast จะมีด้วยกัน 2 ไฟล์ คือ activity_main.xml และ MainActivity.java




Example : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="Input Text ?" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="Show Toast" />

</RelativeLayout>

Example : MainActivity.java
package com.example.alertdialog2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
               
        final Button btn1 = (Button) findViewById(R.id.button1);

        final TextView editT1 = (TextView)findViewById(R.id.editText1);

        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String strTxt = editT1.getText().toString();               
        Toast.makeText(MainActivity.this,
            "ข้อความของคุณคือ : " + strTxt,
            Toast.LENGTH_LONG).show();                
            }
        });
        
    }
}

ผลลัพธ์ : 



























ที่มา : Thaicreate

Android Dialog Popup

การทำ Dialog Popup จะมีด้วยกัน 2 ไฟล์ คือ activity_main.xml และ MainActivity.java




Example : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="Input Text ?" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="Show AlertDialog" />

</RelativeLayout>
Example : MainActivity.java
package com.example.alertdialog1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.app.AlertDialog;


public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final AlertDialog.Builder adb = new AlertDialog.Builder(this);

        final Button btn1 = (Button) findViewById(R.id.button1);
        
        final TextView editT1 = (TextView)findViewById(R.id.editText1);

        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog ad = adb.create();
                String strTxt = editT1.getText().toString();

                ad.setMessage("ข้อความของคุณคือ : " + strTxt);
                ad.show();
            }
        });
        
    }

}

ผลลัพธ์ : 



























ที่มา : Thaicreate

Android RatingBar in AlertDialog Popup

นการสร้าง RatingBar บน Dialog Popup สามารถสร้างโดยใช้วิธีการ Set View บน Dialog โดยเรียก Widgets RatingBar ที่ได้จากการสร้างขึ้นมาใหม่บน Dynamic Runtime ประกอบไปด้วย 2 ไฟล์ คือ MainActivity.java กับ activity_main.xml



Example : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:text="Click" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="51dp"
        android:text="result" />

</RelativeLayout>
Example : MainActivity.java
package com.rating.rating2;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class MainActivity extends Activity {

 public TextView txtView;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
        
    if (android.os.Build.VERSION.SDK_INT > 9) {
             StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
             StrictMode.setThreadPolicy(policy);
         }
  
  txtView = (TextView)findViewById(R.id.textView1); // TextView
  
        // Button1
        final Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
             final AlertDialog.Builder popDialog = new AlertDialog.Builder(MainActivity.this);
          final RatingBar rating = new RatingBar(MainActivity.this);
          rating.setMax(5);

          popDialog.setIcon(android.R.drawable.btn_star_big_on);
          popDialog.setTitle("Vote!! ");
          popDialog.setView(rating);

          // Button OK
          popDialog.setPositiveButton(android.R.string.ok,
            new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {
              txtView.setText(String.valueOf(rating.getProgress()));
              dialog.dismiss();
             }

            })

          // Button Cancel
            .setNegativeButton("Cancel",
              new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
               }
              });

          popDialog.create();
          popDialog.show();
            } 
        });
        
 }
}


 สามารถ set เพิ่มหรือลดจำนวนดาวได้ที่โค้ด rating.setMax();

ผลลัพธ์ :

ที่มา : Thaicreate

Android RatingBar in Layout

การเรียกใช้ Rating Bar บน Layout ธรรมดา จะประกอบไปด้วย 2 ไฟล์คือ MainActivity.java กับ activity_main.xml และแก้ไขไฟล์ string.xml




Example : string.xml
<resources>
    <string name="app_name">Rating1</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="text_label_1">Rate</string>
    <string name="button_label">Submit</string>
    <string name="text_label_2">Result: </string>
</resources>
Example : activity_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" >

    <TextView
        android:id="@+id/lblRateMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_label_1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="4"
        android:stepSize="1.0"
        android:rating="2.0" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/lblResult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text_label_2"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/txtRatingValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </LinearLayout>

</LinearLayout>
เราสามารถเพิ่มหรือลดจำนวนดาวได้จากโค้ด android:numSters="" 

Example : MainActivity.java
package com.rating.rating1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

   private RatingBar ratingBar;
   private TextView ratingValue;
   private Button button;

   @Override
   public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  addListenerOnRatingBar();
  addListenerOnButton();

   }

   public void addListenerOnRatingBar() {

  ratingBar = (RatingBar) findViewById(R.id.ratingBar);
  ratingValue = (TextView) findViewById(R.id.txtRatingValue);

  ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

   public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

    ratingValue.setText(String.valueOf(rating));

   }
  });
   }

   public void addListenerOnButton() {

  ratingBar = (RatingBar) findViewById(R.id.ratingBar);
  button = (Button) findViewById(R.id.button);

  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

    Toast.makeText(MainActivity.this, String.valueOf(ratingBar.getRating()), Toast.LENGTH_LONG).show();
   }

  });

   }
 }

ผลลัพธ์ :




























ที่มา : javacodegeeks