วันศุกร์ที่ 20 กรกฎาคม พ.ศ. 2555

Android Spinner

-->
 Android Spinner Example Code

SpinnerActivity.java Code
package android.example.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class SpinnerActivity extends Activity {
     
      TextView tvSpinner1,tvSpinner2;
     
      void showToast(CharSequence msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner_1);
       
        tvSpinner1 = (TextView) findViewById(R.id.textViewSpinner1);
        tvSpinner2 = (TextView) findViewById(R.id.textViewSpinner2);
       
        Spinner s1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.colors, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {
                        showToast("Spinner1: position=" + position + " id=" + id);
                        tvSpinner1.setText(" Position=" + position + " id=" + id);
                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                        showToast("Spinner1: unselected");
                    }
                });

        Spinner s2 = (Spinner) findViewById(R.id.spinner2);
        adapter = ArrayAdapter.createFromResource(this, R.array.planets,
                android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
        s2.setAdapter(adapter);
        s2.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {
                        showToast("Spinner2: position=" + position + " id=" + id);
                        tvSpinner2.setText(" Position=" + position + " id=" + id);
                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                        showToast("Spinner2: unselected");
                    }
                });
    }
}



spinner.xml Code

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:background="#660287"
    android:layout_width="match_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22dp"
        android:textStyle="bold"
        android:text="@string/spinner_1_color"/>

    <Spinner android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"      
        android:prompt="@string/spinner_1_color_prompt"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:textStyle="bold"
        android:textSize="22dp"
        android:text="@string/spinner_1_planet"/>

    <Spinner android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:prompt="@string/spinner_1_planet_prompt"/>

    <TextView      
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Spinner 1 Select :"
       
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewSpinner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#f7f887"
        android:textColor="#0000FF"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
       
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Spinner 2 Select :"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewSpinner2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#0000FF"
        android:background="#f7f887"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />
   
</LinearLayout>




Download Android Spinner Example Code 



Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android

สอนเขียน Android  สอนเขียนโปรแกรม Android

ติดต่อ amphancm@gmail.com

-->

Android Scrollview

-->

Android Scrollview

ScrollViewActivity.java Code

package android.example.scrollview;

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


public class ScrollViewActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.scrollbar3);
       
        findViewById(R.id.view3).setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
    }
}

scrollbar.xml Code

<?xml version="1.0" encoding="utf-8"?>

<!-- Demonstrates scrolling with a ScrollView. -->

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099CC"
    android:orientation="vertical">

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="ScrollView ( Vertical )"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="120dip"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:background="#FF0000" >

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

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/scrollbar_2_text" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/scrollbar_2_text" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/scrollbar_2_text" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/scrollbar_2_text" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/scrollbar_2_text" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/scrollbar_2_text" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/scrollbar_2_text" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/scrollbar_2_text" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/scrollbar_2_text" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/scrollbar_2_text" />
            </LinearLayout>
        </ScrollView>

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="120dip"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:background="#00FF00"
            android:paddingRight="12dip" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#60AA60"
                android:text="@string/scrollbar_3_text"
                android:textColor="#000000" />

        </ScrollView>

        <ScrollView
            android:id="@+id/view3"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:layout_height="120dip"
            android:layout_marginLeft="5dp"
            android:background="@android:drawable/edit_text" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:text="@string/scrollbar_3_text" />
        </ScrollView>
    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Horizontal ScrollView"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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


        <HorizontalScrollView
            android:id="@+id/view4"
            android:layout_width="fill_parent"
            android:layout_height="120dip"
            android:scrollbarStyle="outsideOverlay"
            android:background="@android:drawable/edit_text">

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

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/scrollbar_3_text"
                    android:textSize="30dp"
                    android:textColor="#0000FF" />

            </LinearLayout>

        </HorizontalScrollView>
       
        <HorizontalScrollView
            android:id="@+id/view5"
            android:layout_width="fill_parent"
            android:layout_height="120dip"
            android:scrollbarStyle="outsideInset"
            android:background="@android:drawable/edit_text">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent" >
               
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/about" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/exit" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/how" />
                 
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/set" />
                
                 <ImageView
                android:id="@+id/imageView2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                
                  <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/about" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/exit" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/how" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/set" />
                
                
                 <ImageView
                android:id="@+id/imageView3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                
                  <ImageView                
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/about" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/exit" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/how" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                 
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/set" />
                
                
                 <ImageView
                android:id="@+id/imageView4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
                
                 <ImageView               
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/ic_launcher" />
               
            </LinearLayout>
          
           
        </HorizontalScrollView>
       
    </LinearLayout>
</LinearLayout>

string.xml Code

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, Activity!</string>
    <string name="app_name">ScrollView</string>
   
   
    <string name="scrollbar_1_text">Lorem ipsum dolor sit amet.</string>
    <string name="scrollbar_2_text">Lorem ipsum dolor sit amet.</string>
    <string name="scrollbar_3_text">
 The Android platform is a software stack for mobile devices including an
 operating system, middleware and key applications. Developers can create
 applications for the platform using the Android SDK. Applications are written
 using the Java programming language and run on Dalvik, a custom virtual
 machine designed for embedded use which runs on top of a Linux kernel.

 If you want to know how to develop applications for Android, you\'re in the
 right place. This site provides a variety of documentation that will help you
 learn about Android and develop mobile applications for the platform.

 An early look at the the Android SDK is also available. It includes sample
 projects with source code, development tools, an emulator, and of course all
 the libraries you\'ll need to build an Android application. What would it take
 to build a better mobile phone?
    </string>

</resources>



Download Android ScrollView Example Code




Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android

สอนเขียน Android  สอนเขียนโปรแกรม Android