Windows – Alt Key Numeric Codes

การใช้งาน กด Alt พร้อมด้วยรหัสแอสกีในเลขฐาน 10 แบบสี่หลัก เช่น A ก็ Alt 0065
(รึไม่ก็ใช้ Character Map เลย)

Currency Symbols

Windows Alt Code for Currency Symbols

CODE	SYMBOL	NAME
0162	¢	Cent sign
0163	£	British Pound
0128	€	Euro currency
0165	¥	Japanese Yen
0131	ƒ	Dutch Florin
0164	¤	Generic currency symbol

Fractions

These codes produce fractions which are spaced on one line.

Windows Alt Codes – Fractions

CODE	SYMBOL	NAME
0188	¼	Fraction 1/4
0189	½	Fraction 1/2
0190	¾	Fraction 3/4

Other Punctuation

These incude copyright symbols and special section marks.

Windows Alt Codes for Punctuation

CODE	SYMBOL	NAME
0126	~	
0169	©	Copyright symbol
0174	®	Registered symbol
0153	™	Trademark
0149	•	List Dot
0150	–	en-dash
0151	—	em-dash
0182	¶	Paragraph Symbol (Pilcrow)

tlt.psu.edu – Windows – Alt Key Numeric Codes
tlt.psu.edu – Character Map Utility

Auto-complete Suggestions

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.MainActivity">

    <AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/autocomplete_country"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

strings.xml

<resources>
    <string name="app_name">Jack Application</string>

    <string-array name="countries_array">
        <item>Afghanistan</item>
        <item>Albania</item>
        <item>Algeria</item>
        <item>American Samoa</item>
        <item>Andorra</item>
        <item>Angola</item>
        <item>Anguilla</item>
        <item>Antarctica</item>
    </string-array>
</resources>

MainActivity.java

package com.phaisarn.jackapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

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

        // Get a reference to the AutoCompleteTextView in the layout
        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
        // Get the string array
        String[] countries = getResources().getStringArray(R.array.countries_array);
        // Create the adapter and set it to the AutoCompleteTextView
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
        textView.setAdapter(adapter);
    }
}

Provide Auto-complete Suggestions

Notification

การแจ้งเตือน

มีปุ่ม 3 ปุ่ม

  • ปุ่มที่ 1 จะเป็นการแจ้งเตือน
  • ปุ่มที่ 2 จะเป็นการอัพเดทข้อความแจ้งเตือน
  • ปุ่มที่ 3 จะเป็นการยกเลิกการแจ้งเตือน
    • เมื่อผู้ใช้เลือกการแจ้งเตือน จะมีการเรียกหน้า Activity ที่เราเตรียมไว้ให้แสดงขึ้นมา

      activity_main.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/activity_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context="com.phaisarn.jackapplication.MainActivity">
      
          <Button
              android:id="@+id/notify_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Notify" />
      
          <Button
              android:id="@+id/update_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Update" />
      
          <Button
              android:id="@+id/cancel_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Cancel" />
      </LinearLayout>
      

      MainActivity.java

      package com.phaisarn.jackapplication;
      
      import android.app.Notification;
      import android.app.NotificationManager;
      import android.app.PendingIntent;
      import android.content.Intent;
      import android.graphics.Bitmap;
      import android.graphics.BitmapFactory;
      import android.support.v4.app.NotificationCompat;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      
      public class MainActivity extends AppCompatActivity {
      
          private final int NOTIFY_ID = 0;
          private NotificationCompat.Builder mNotifyBuilder;
          private NotificationManager mNotifyManager;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              (findViewById(R.id.notify_button)).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
      
                      PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
      
                      Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.me);
      
                      mNotifyBuilder = new NotificationCompat.Builder(MainActivity.this)
                              .setTicker("New Alert")
                              .setSmallIcon(R.drawable.me)
                              .setLargeIcon(largeIcon)
                              .setContentTitle("You have got new alert")
                              .setContentText("You have got a good news.")
                              .setContentIntent(pendingIntent)
                              .setAutoCancel(true);
      
                      mNotifyBuilder.build();
      
                      mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      
                      mNotifyManager.notify(NOTIFY_ID, mNotifyBuilder.build());
                  }
              });
      
              (findViewById(R.id.update_button)).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      mNotifyBuilder.setContentText("This is an update text.");
      
                      Notification notification = mNotifyBuilder.build();
      
                      mNotifyManager.notify(NOTIFY_ID, notification);
                  }
              });
      
              (findViewById(R.id.cancel_button)).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      mNotifyManager.cancel(NOTIFY_ID);
                  }
              });
          }
      }
      

      activity_notification.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/activity_notification"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context="com.phaisarn.jackapplication.NotificationActivity">
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="This is your notification" />
      </LinearLayout>
      

      NotificationActivity.java

      package com.phaisarn.jackapplication;
      
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      
      public class NotificationActivity extends AppCompatActivity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_notification);
          }
      }
      

AlertDialog

Dialog1

ไดอะล็อก Yes/No

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.MainActivity">

    <Button
        android:id="@+id/my_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click" />
</LinearLayout>

MainActivity.java

package com.phaisarn.jackapplication;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        Button myButton = (Button) findViewById(R.id.my_button);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("This is dialog 1.");
                builder.setMessage("Are you OK?");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
                        ;
                    }
                });
                builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "NO", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();
            }
        });
    }
}

Dialog2

ไดอะล็อกตัวเลือกแบบ Single choice แบบที่ 1

MainActivity.java

package com.phaisarn.jackapplication;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        Button myButton = (Button) findViewById(R.id.my_button);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String[] items = {"2557", "2558", "2559", "2560"};
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("This is dialog 2.")
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        });
    }
}

Dialog3

ไดอะล็อกตัวเลือกแบบ Single choice แบบที่ 2

MainActivity.java

package com.phaisarn.jackapplication;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        Button myButton = (Button) findViewById(R.id.my_button);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String[] items = {"2557", "2558", "2559", "2560"};
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("This is dialog 3.")
                        .setSingleChoiceItems(items, -1, null)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ListView listView = ((AlertDialog) dialog).getListView();
                                int i = listView.getCheckedItemPosition();
                                if (i >= 0) {
                                    Toast.makeText(MainActivity.this, items[i], Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .show();
            }
        });
    }
}

Dialog4

ไดอะล็อกตัวเลือกแบบ Multiple choice

MainActivity.java

package com.phaisarn.jackapplication;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {

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

        final ArrayList<Integer> resultList = new ArrayList<>();

        Button myButton = (Button) findViewById(R.id.my_button);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String[] items = {"2557", "2558", "2559", "2560"};
                boolean[] isSelected = new boolean[items.length];
                for (int i : resultList)
                    isSelected[i] = true;

                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("This is dialog 4.")
                        .setMultiChoiceItems(items, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                if (isChecked)
                                    resultList.add(which);
                                else if (resultList.contains(which))
                                    resultList.remove(Integer.valueOf(which));
                            }
                        })
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String str = "";
                                Collections.sort(resultList);
                                for (int i : resultList) {
                                    str += items[i];
                                    if (i < resultList.size()-1)
                                        str += "\n";
                                }
                                if (str.length() > 0)
                                    Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        });
    }
}

Dialog5

ไดอะล็อกปรับแต่งเอง

layout_login.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">

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Username"
        android:textAlignment="center" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Password"
        android:inputType="textPassword"
        android:textAlignment="center" />
</LinearLayout>

MainActivity.java

package com.phaisarn.jackapplication;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {

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

        final ArrayList<Integer> resultList = new ArrayList<>();

        Button myButton = (Button) findViewById(R.id.my_button);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LayoutInflater layoutInflater = getLayoutInflater();
                final AlertDialog builder = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("This is dialog 5")
                        .setView(layoutInflater.inflate(R.layout.layout_login, null))
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                        .create();
                builder.setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        Button button = builder.getButton(builder.BUTTON_POSITIVE);
                        button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                EditText editText = (EditText) builder.findViewById(R.id.username);
                                String str = editText.getText().toString().trim();
                                if (str.length() > 0) {
                                    Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                                }
                                builder.dismiss();
                            }
                        });
                    }
                });
                builder.show();
            }
        });
    }
}

Dialog6

ไดอะล็อกดาวน์โหลดข้อมูล

MainActivity.java

package com.phaisarn.jackapplication;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

        Button myButton = (Button) findViewById(R.id.my_button);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String title = "Downloading data";
                String msg = "Please wait...";
                final ProgressDialog dialog = ProgressDialog.show(MainActivity.this, title, msg);

                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        dialog.dismiss();
                    }
                });
                thread.start();
            }
        });
    }
}

เรียก Activity อื่น

1.เรียกใช้งานแอคทิวิตี้อื่น

เพิ่มแอคทิวิตี้ชื่อ SecondActivity

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.MainActivity">

    <Button
        android:id="@+id/my_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click1" />
</LinearLayout>

MainActivity.java

package com.phaisarn.jackapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button myButton1 = (Button) findViewById(R.id.my_button1);
        myButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_second"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.SecondActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is second activity." />
</LinearLayout>

2.เรียกใช้งานแอคทิวิตี้อื่นพร้อมส่งข้อมูลไปให้

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.MainActivity">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:id="@+id/my_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click1" />
</LinearLayout>

MainActivity.java

package com.phaisarn.jackapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    public static final String MESSAGE1 = "Message1";

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

        Button myButton1 = (Button) findViewById(R.id.my_button1);
        myButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText = (EditText) findViewById(R.id.editText1);
                String msg = editText.getText().toString();

                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                intent.putExtra(MESSAGE1, msg);
                startActivity(intent);
            }
        });
    }
}

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_second"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.SecondActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is second activity."/>
</LinearLayout>

SecondActivity.java

package com.phaisarn.jackapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Intent intent = getIntent();
        String msg = intent.getStringExtra(MainActivity.MESSAGE1);

        TextView textView = (TextView) findViewById(R.id.textView2);
        textView.setText(msg);
    }
}

3.ส่งข้อมูลกลับมาให้แอคทิวิตี้ต้นทาง

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.MainActivity">

    <Button
        android:id="@+id/my_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click1" />
</LinearLayout>

MainActivity.java

package com.phaisarn.jackapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public static final String MESSAGE2 = "Message2";
    public static final int CODE = 1;

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

        Button myButton1 = (Button) findViewById(R.id.my_button1);
        myButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                startActivityForResult(intent, CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == CODE) {
            if (resultCode == RESULT_OK) {
                String msg = intent.getStringExtra(MESSAGE2);
                Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
            }
        }
    }

}

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_second"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.SecondActivity">

    <EditText
        android:id="@+id/editText2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:id="@+id/my_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Return" />
</LinearLayout>

SecondActivity.java

package com.phaisarn.jackapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Button myButton2 = (Button) findViewById(R.id.my_button2);
        myButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText = (EditText) findViewById(R.id.editText2);
                String msg = editText.getText().toString();

                Intent intent = new Intent();
                intent.putExtra(MainActivity.MESSAGE2, msg);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}

Event Handling

อีเวนต์ onClick

แบบที่ 1

ผูกอีเวนต์ไว้ที่หน้า Layout ด้วย android:onClick แล้วเขียนเมธอดไว้ที่หน้า Activity

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Click" />
</LinearLayout>

MainActivity.java

package com.phaisarn.jackapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    public void onClick(View view) {
        Toast.makeText(this, "Hello World#1", Toast.LENGTH_SHORT).show();
    }
}

แบบที่ 2

ผูกอีเวนต์ที่หน้า Activity ไว้กับปุ่มที่หน้า Layout โดยสร้าง Inner Class แบบ Anonymous (เหมาะกับการเรียกใช้แบบครั้งเดียว)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.MainActivity">

    <Button
        android:id="@+id/my_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click" />
</LinearLayout>

MainActivity.java

package com.phaisarn.jackapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        Button myButton = (Button) findViewById(R.id.my_button);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Hello World#2", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

แบบที่ 3

ผูกอีเวนต์ที่หน้า Activity ไว้กับปุ่มที่หน้า Layout โดยสร้าง Inner Class ขึ้นมา (สามารถเรียกใช้ได้หลายครั้ง)

MainActivity.java

package com.phaisarn.jackapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        Button myButton = (Button) findViewById(R.id.my_button);
        myButton.setOnClickListener(new MyListener());
    }

    private class MyListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Hello World#3", Toast.LENGTH_SHORT).show();
        }
    }
}

แบบที่ 4

ผูกอีเวนต์ที่หน้า Activity ไว้กับปุ่มที่หน้า Layout โดย Implement อินเทอร์เฟส View.OnClickListener ไว้ที่ Class หลักเลย

MainActivity.java

package com.phaisarn.jackapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

        Button myButton = (Button) findViewById(R.id.my_button);
        myButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(getApplicationContext(), "Hello World#4", Toast.LENGTH_SHORT).show();
    }
}

แต่ในกรณีที่มีหลายปุ่ม ให้ตรวจสอบว่าอีเวนต์นี้ถูกเรียกมาจากปุ่มไหนด้วย view.getId()

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.MainActivity">

    <Button
        android:id="@+id/my_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click1" />

    <Button
        android:id="@+id/my_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click2" />
</LinearLayout>

MainActivity.java

package com.phaisarn.jackapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

        Button myButton1 = (Button) findViewById(R.id.my_button1);
        myButton1.setOnClickListener(this);

        Button myButton2 = (Button) findViewById(R.id.my_button2);
        myButton2.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_button1:
                Toast.makeText(getApplicationContext(), "Button#1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.my_button2:
                Toast.makeText(getApplicationContext(), "Button#2", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

Spinner

คอมโบบ็อก โดยดึงข้อมูลมาจากฐานข้อมูล

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.phaisarn.jackapplication.MainActivity">

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

        <EditText android:id="@+id/edittext_newcategory" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="New Category" />

        <Button android:id="@+id/button_category" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add" />

    </LinearLayout>

    <Spinner android:id="@+id/spinner_categories" android:layout_width="fill_parent" android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

package com.phaisarn.jackapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private DataBaseHelper db;
    private Spinner spinnerCategories;
    private EditText editTextNewCategory;
    private Button buttonCategory;

    private List<String> listCategories = new ArrayList<>();
    private ArrayAdapter<String> adapter;

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

        // connect database
        db = new DataBaseHelper(this);
        if (db == null)
            db = new DataBaseHelper(this);

        spinnerCategories = (Spinner) findViewById(R.id.spinner_categories);
        editTextNewCategory = (EditText) findViewById(R.id.edittext_newcategory);
        buttonCategory = (Button) findViewById(R.id.button_category);
        buttonCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String newCategory = editTextNewCategory.getText().toString();
                if (newCategory .equalsIgnoreCase("")) {
                    Toast.makeText(getApplicationContext(), "Please Enter New Category", Toast.LENGTH_SHORT).show();
                } else {
                    db.addCategory(new Category(newCategory ));
                    prepareData();
                    editTextNewCategory.setText("");
                    //Toast.makeText(MainActivity.this, "New Category was Successfully added to Database", Toast.LENGTH_SHORT).show();
                }
            }
        });

        prepareData();
    }

    public void prepareData() {
        listCategories = db.getAllCategories();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, android.R.id.text1, listCategories);
        spinnerCategories.setAdapter(adapter);
    }
}

Category.java

package com.phaisarn.jackapplication;

/**
 * Created by jack on 2/9/2017.
 */

public class Category {
    int id;
    String name;

    public Category(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

DataBaseHelper.java

package com.phaisarn.jackapplication;

/**
 * Created by jack on 2/9/2017.
 */

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class DataBaseHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "categories";
    private static final String TABLE_NAME = "category";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";

    private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY,"
            + COLUMN_NAME + " TEXT" + ")";
    private static final String CREATE_UNIQUE_INDEX = "CREATE UNIQUE INDEX IF NOT EXISTS "
            + COLUMN_NAME + " ON "
            + TABLE_NAME + "(" + COLUMN_NAME + ")";

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
        db.execSQL(CREATE_UNIQUE_INDEX);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //Drop table if exists
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        //create the table again
        onCreate(db);
    }

    public void addCategory(Category category) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(COLUMN_NAME, category.getName());
        db.insert(TABLE_NAME, null, cv);
        db.close();
    }

    public List<String> getAllCategories() {
        List<String> list = new ArrayList<>();

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT " + COLUMN_NAME + " FROM " + TABLE_NAME, null);
        if (cursor.moveToFirst()) {
            do {
                list.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return list;
    }
}

Popup Menu

ป๊อบอัพเมนู

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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.MainActivity">

    <Button
        android:id="@+id/buttonShowPopup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showMenu"
        android:text="Popup" />

</RelativeLayout>

menu_popup.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.phaisarn.testapplication.MainActivity">

    <item
        android:id="@+id/popup_menu1"
        android:orderInCategory="101"
        android:title="@string/popup_menu1"
        app:showAsAction="never" />

    <item
        android:id="@+id/popup_menu2"
        android:orderInCategory="101"
        android:title="@string/popup_menu2"
        app:showAsAction="never" />

</menu>

strings.xml

<resources>
    <string name="app_name">Jack Application</string>

    <string name="popup_menu1">Popup Menu1</string>
    <string name="popup_menu2">Popup Menu2</string>
</resources>

MainActivity.java

package com.phaisarn.jackapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    public void showMenu(View v) {
        PopupMenu popup = new PopupMenu(this, v);
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.popup_menu1:
                        Toast.makeText(getApplicationContext(), "Popup1", Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.popup_menu2:
                        Toast.makeText(getApplicationContext(), "Popup2", Toast.LENGTH_SHORT).show();
                        return true;
                    default:
                        return false;
                }
            }
        });
        popup.inflate(R.menu.menu_popup);
        popup.show();
    }
}

ส่งอีเมล์

ปุ่มส่งอีเมล์

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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.phaisarn.jackapplication.MainActivity">

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send" />

</RelativeLayout>

MainActivity.java

package com.phaisarn.jackapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {

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

        Button buttonSend = (Button) findViewById(R.id.buttonSend);
        buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"email@example.com"});
                intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
                intent.putExtra(Intent.EXTRA_TEXT, "body text");
                //File root = Environment.getExternalStorageDirectory();
                //File file = new File(root, Filename);
                //if (!file.exists() || !file.canRead()) {
                //    Toast.makeText(getApplicationContext(), "Attachment Error", Toast.LENGTH_SHORT).show();
                //    finish();
                //    return;
                //}
                //Uri uri = Uri.parse("file://" + file.getAbsolutePath());
                //intent.putExtra(Intent.EXTRA_STREAM, uri);
                startActivity(Intent.createChooser(intent, "Send email..."));
            }
        });
    }
}