- numbers
- strings
- booleans
- lists (also known as arrays)
- maps
- runes (for expressing Unicode characters in a string)
- symbols
Monthly Archives: October 2018
Dart: HelloWorld
- HelloWorld
- การเรียกใช้ฟังก์ชัน
- การ print ตัวแปร และ expression
- การประกาศตัวแปร
- Default value และการใช้ assert()
- Final และ const
Beauty
res/values/colors.xml
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#008577</color> <color name="colorPrimaryDark">#00574B</color> <color name="colorAccent">#D81B60</color> <color name="splashScreenBg">#FCE4EC</color> <color name="beautyTitleBar">#D81B60</color> <color name="beautyAppBar">#E91E63</color> </resources> |
style.xml
1 2 3 4 5 6 7 8 9 10 |
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/beautyAppBar</item> <item name="colorPrimaryDark">@color/beautyTitleBar</item> <item name="colorAccent">@color/colorAccent</item> </style> ... |
สร้างโปรเจ็กส์ใหม่แบบ Tabbed Activity (Action Bar with View Pager)
การใช้ ListView ใน ScrollView
ภาพซ้ายเป็น ListView
อย่างเดียว
ภาพขวาเป็น ListView
ใน ScrollView
ที่จำกัดความสูงไว้ที่ 300dp แล้วค่อย Scroll ดูไอเทมที่เหลือ
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <ScrollView android:layout_width="match_parent" android:fillViewport="true" android:layout_height="300dp" android:scrollbars="vertical"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout> |
บรรทัดที่ 14 : ต้องใส่ไม่งั้น ListView
จะเห็นแค่ item เดียว
Android: การใช้ ListView
ใช้ android.R.layout.simple_list_item_1
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout> |
Android: การใช้ ProgressBar
จำลองการทำงานบางอย่าง 3 วินาทีแล้วปิด
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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:background="#EEE" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Do some stuff" /> <ProgressBar android:id="@+id/progressBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:visibility="gone" /> </LinearLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.phaisarns.acrosuredemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ProgressBar progressBar = findViewById(R.id.progressBar); Button button = findViewById(R.id.button); // when button is clicked, start the task button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressBar.setVisibility(View.VISIBLE); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } //update ui on UI thread runOnUiThread(new Runnable() { @Override public void run() { progressBar.setVisibility(View.GONE); } }); } }).start(); } }); } } |
บรรทัดที่ 23 : แสดง progressBar
บรรทัดที่ 35 : เมื่อ Thread ทำงานเสร็จ จะมาทำฟังก์ชันนี้เพื่อทำการอัพเดท UI
บรรทัดที่ 38 : ซ่อน progressBar
Link
Android: RadioGroup และ RadioButton แบบ programmatically
คำอธิบายดูที่ Kotlin
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> </LinearLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.phaisarns.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String[] options = {"Mango", "Papaya", "Orange", "Apple"}; // create a radio group RadioGroup radioGroup = new RadioGroup(this); radioGroup.setOrientation(RadioGroup.VERTICAL); for (int i = 0; i < options.length; i++) { // create a radio button RadioButton radioButton = new RadioButton(this); // set text for the radio button radioButton.setText(options[i]); // assign an automatically generated id to the radio button radioButton.setId(View.generateViewId()); // add radio button to the radio group radioGroup.addView(radioButton); } // add radio group to the linear layout LinearLayout linearLayout = findViewById(R.id.linearLayout); linearLayout.addView(radioGroup); } } |
Link
Android: EditText ที่ แสดง หรือ ซ่อน password
คำอธิบายดูที่ Kotlin
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:gravity="center" android:orientation="horizontal"> <EditText android:id="@+id/pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" android:minWidth="200sp" /> <Button android:id="@+id/showHideBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" /> </LinearLayout> </LinearLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.phaisarns.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.method.HideReturnsTransformationMethod; import android.text.method.PasswordTransformationMethod; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText pwd = findViewById(R.id.pwd); final Button showHideBtn = findViewById(R.id.showHideBtn); showHideBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (showHideBtn.getText().toString().equals("Show")) { pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); showHideBtn.setText("Hide"); } else { pwd.setTransformationMethod(PasswordTransformationMethod.getInstance()); showHideBtn.setText("Show"); } } }); } } |
Android: EditText รับอีเวนท์ onTextChanged()
คำอธิบายดูที่ Kotlin
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="10sp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="50sp" android:gravity="center" android:text="TutorialKart\nEditText On Text Change" android:textSize="25sp" /> <TextView android:id="@+id/textViewSample" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="50sp" android:textSize="20sp" /> <EditText android:id="@+id/editTextSample" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter Text ..." android:textSize="20sp" /> </LinearLayout> </android.support.constraint.ConstraintLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.phaisarns.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView textViewSample = findViewById(R.id.textViewSample); EditText editTextSample = findViewById(R.id.editTextSample); editTextSample.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { textViewSample.setText("Text in EditText : " + s); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); } } |
Link