1. เข้า https:
Download : android-studio-ide-182.5264788-windows.exe 947 MB
แล้วก็ install
- ในเครื่องมี c:\program files\java\jre อยูแล้ว จึงไม่ติดตั้งเพิ่ม
- Data sharing : Settings | Appearance % Behavior | System Settings | Data Sharing
- ตอนติดตั้งไม่ได้เลือก จึงคลิ๊ก Don't send แทนที่จะคลิ๊ก Send usage statistics to Google
- ก็ไม่ได้เปลี่ยน option ตอนติดตั้งกด next อย่างเดียว
- Downloading Component
(repository 339.1 MB, sdk-tools 148.9 MB, sources 40.6MB, platform 72.1 MB,
emulator 257.9 MB, build-tools 55.7 MB)
Failed to install Intel HAXM. For details, please check the installation log:
"C:\Users\LAB\AppData\Local\Temp\haxm_log.txt"
HAXM installation failed. To install HAXM follow the instructions found at: https:
2. Welcome to Android Studio 3.3.1
- [Start a
new
Android Studio project]
- Open an existing Android Studio project
- Check out project from Version Control
- Profile or debug APK
- Import project (Gradle, Eclipse ADT, etc.)
- Import an Android code sample
3. Phone and Tablet
[Navigation Drawer Activity]
Configure your project
Name = My Application
API 15: Android 4.0.3 (IceCreamSandwich)
4. Project
[MyApplication Tab]
.idea
app
gradle
.gitignore
build.gradle
gradle.properties
gradlew
gradlew.bat
local.properties
MyApplication.iml
settings.gradle
[Gradle Scripts]
build.gradle
gradle-wrapper.properties
gradle.properties
settings.gradle
local.properties
5. รอ Build: จนพบ completed successfully
ครั้งแรกรอให้การทำงานใน status bar จบจบ
จะพบ tab: MainActivity.java, content_main.xml
6. [content_main.xml]
กดสลับไปมาระหว่าง
- Design ที่ปรับค่า Attributes ได้
- Text ที่เป็น xml code ได้
เพื่อดูภาพหน้าจอการทำงาน
7. กดปุ่ม > เพื่อ run app
หรือ Menu, Run หรือ Shift + F10
8. ยังไม่พบ device
Create
new
Virtual Device
- Nexus 5X , 5.2in, 1080*1920, 420dpi
- Oreo Download , 26, x86, android 8.0 (733.7 MB)
- Nougat Download , 24, x86, android 7.0 (775.1 MB)
โทรศัพท์ของผมใช้ 8.1 Oreo ซึ่งรองรับ 8.0 แน่นอน
Creat AVD Name = Nexus 5X API 26
Creat AVD Name = Nexus 5X API 24
9. หลังเลือก Device
Instant Run : Install and Continue
10.Event log
Problem : x86 emulation currently requires hardware acceleration!
10.1 check : Tools, SDK Manager, Android SDK, SDK Tools
พบว่า Intel x86 Emulator Accelerator (HAXM instalelr) 7.3.2 : installed
10.2 solution:
C:\Users\LAB\AppData\Local\Android\Sdk\extras\intel\Hardware_Accelerated_Execution_Manager\intelhaxm-android.exe
https:
11. Docker Desktop Service
Stop & Manual
Setting : General
uncheck : Start Docker Desktop when you login
uncheck : Automatically check
for
updates
12. hyper-v
Turn Windows Features On or Off
13 content-main.xml
เปลี่ยน
"hello world!"
เป็น
"hello world"
แก้ไขผ่าน design, attributes tab, TextView text=
"hello world"
14. กด apply change เพื่อทำ instant run
รอสักพัก การเปลี่ยนแปลงก็จะปรากฎใน emulator
15. content_main.xml
ต้องการ คลิ๊ก hello world!
ถ้าค่าของ Textview เป็น 0 ก็ใส่ 1 เข้าไปแทน
- android:id=
"@+id/text1"
- android:clickable=
"true"
16. MainActivity.java
เพิ่ม method ใน public class MainActivity {}
ทำให้เกิดการจัดคู่ระหว่าง onClick ใน content_main.xml และ MainActivity.java
ตำแหน่งแรก
import android.widget.TextView;
ตำแหน่งที่สอง
เพิ่มใน protected void onCreate(){}
TextView t = (TextView)findViewById(R.id.text1);
t.setOnClickListener(
new
View.OnClickListener() {
@Override
public void onClick(View view) {
TextView t = (TextView)findViewById(R.id.text1);
if
(t.getText().equals(
"Hello World!"
)) {
t.setText(String.valueOf(
"0"
));
}
else
{
int t1 = Integer.parseInt(t.getText().toString()) + 1;
t.setText(Integer.toString(t1));
}
}
});
https:
17. การเพิ่มภาพใน drawable
ลากวางได้เลย
ใน MyApplication, app, src, main, res, drawable
พบ sample.png
18. การเพิ่มภาพใน page บน Design view
- Palette, Common, ImageView ลากลงไปใน page
- เลือก Project แล้ว click ภาพ
- ใช้ mouse ปรับขนาดของ layout_width และ layout_height ของ imageView1 ได้
- กำหนดเป็น 100dp
19. เพิ่มคำสั่งใน MainActivity.java
import android.widget.ImageView;
ImageView i = (ImageView)findViewById(R.id.imageView3);
i.setOnClickListener(
new
View.OnClickListener() {
@Override
public void onClick(View view) {
TextView t = (TextView)findViewById(R.id.text1);
if
(t.getText().equals(
"Hello World!"
)) {
t.setText(String.valueOf(
"0"
));
}
else
{
int t1 = Integer.parseInt(t.getText().toString()) + 1;
t.setText(Integer.toString(t1));
}
}
});
20. copy content_main.xml เป็น two.xml
https:
https:
http:
https:
*** https:
https:
[สร้าง layout]
app, res, layout :
new
- Layout resource file
fragment1.xml
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<android.support.constraint.ConstraintLayout
android:layout_height=
"match_parent"
>
</android.support.constraint.ConstraintLayout>
แล้วสร้าง 2 วัตถุ
- android:id=
"@+id/button1"
- android:id=
"@+id/text1"
android:paddingTop=
"50dp"
จะได้ไม่ชนกับ button1
[ปรับ content_main]
content_main.xml
ลบ TextView เปลี่ยนเป็น FrameLayout
<FrameLayout
android:id=
"@+id/content_frame"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
/>
[สร้าง java]
ใน package com.example.navfragment;
menu1.java
package com.example.navfragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class menu1 extends Fragment implements View.OnClickListener {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment1, container,
false
);
Button b = (Button) v.findViewById(R.id.button1);
b.setOnClickListener(
this
);
TextView t = (TextView) v.findViewById(R.id.text1);
t.setOnClickListener(
this
);
View tv = v.findViewById(R.id.text1);
((TextView)tv).setText(
"hello"
);
return
v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super
.onViewCreated(view, savedInstanceState);
getActivity().setTitle(
"Menu 1"
);
}
@Override
public void onClick(View v) {
switch
(v.getId()) {
case
R.id.button1:
View tv1 = v.findViewById(R.id.button1);
((TextView)tv1).setText(
"1"
);
break
;
case
R.id.text1:
TextView tv2 = v.findViewById(R.id.text1);
if
(tv2.getText().equals(
"hello"
)) {
tv2.setText(String.valueOf(
"0"
));
}
else
{
int tmp = Integer.parseInt(tv2.getText().toString()) + 1;
tv2.setText(Integer.toString(tmp));
}
break
;
}
}
}
[MainActivity.java]
ใน app, java, com.example.navfragment
1. สร้าง method
private void displaySelectedScreen(int itemId) {
Fragment fragment =
null
;
switch
(itemId) {
case
R.id.nav_camera:
fragment =
new
fragment();
break
;
case
R.id.nav_gallery:
fragment =
new
menu1();
break
;
}
if
(fragment !=
null
) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
2. เพิ่มบรรทัดสุดท้ายของ on create
displaySelectedScreen(R.id.nav_camera);
3. ใน public boolean onNavigationItemSelected(MenuItem item)
เหลือ 2 บรรทัด
displaySelectedScreen(item.getItemId());
return
true
;
c:/users/%USERNAME%/AndroidStudioProjects/navfragment
Source : <a href=
"navfragment.zip"
>navfragment.zip</a> 142,736 bytes
Build : <a href=
"navfragment.apk"
>navfragment.apk</a> 2,277,379 bytes