cmd 창에 systeminfo 입력하여 Hyper-V 요구 사항 확인
https://developer.android.com/?hl=ko
접속하여 안드로이드 스튜디오 - 릴리즈노트 - 이전 안드로이드 스튜디오 출시
https://developer.android.com/studio/archive - 약관동의
Android Studio Bumblebee (2021.1.1) RC 1 - January 4, 2022 선택 후
Installers
Windows (64-bit):android-studio-2021.1.1.19-windows.exe 설치하기
cmd 창에 java 입력 후 버전 확인
설치 후 Import Android Studio Settings 창 뜬다.
Don't Import 하기
Install Type에서 Standard - Darcula(UI설정) - Next - 라이센스 전부 동의 후 Finish
Downloading Components 에서 Finish 하여 설치 완료
Welcome to Android Studio의 More Actions - SDK Manager, Virtual Device Manager
1) SDK Manager
SDK Manager - Android SDK - SDK Platforms 에서 Show Package Details 클릭
Android API 33 체크 해제 후, Android 12.0 (S)에서
Android SDK Platform 31 반드시 체크 하고, 나머지는 선택 인데
Google APIs Intel x86 Atom_64 System Image
Google Play Intel x86 Atom_64 System Image
2개 체크
SDK Tools 기본적 된 것 4가지 확인 후 Apply - OK - 라이센스 선택 동의 후 Next
Component Installer 에서 다운로드 된다. 시간이 꽤 걸린다.
Finish 후 OK
2) Virtual Device Manager
Virtual Device Manager - Create Device - Phone의 Pixel 2 - Release Name을 S, API Level을 31
ADV Name인 Pixel 2 API 31 그대로 쓰되 S(Pixel 2 API 31) 이름으로 한다.
즉 API 이름 그대로 하고 Finish
Device Manager 창이 뜬다.
Actions의 ▶ 클릭하면 실행된다. 이건 안드로이드 스튜디오와 별도로 실행된다.
핸드폰 모양의 UI가 뜬다.
한글 언어 설정은, 폰화면 위로드래그 Settings - System - Languages & input - Languages - 한국어
이후 1번에 한국어 드래그, 2번 English
뒤로 나와 맞춤법 검사기에서 사용 해제
설정 - 디스플레이 - 화면 자동 잠금 시간 : 30분
다시 안드로이드 스튜디오 - New Project - Empty Activity(템플릿) 클릭 후 Next
Name : Ch1_App1
Package Name : com.example.ch1_app1
Save location : D:\study\Ch1_App1 (워크스페이스)
Language : Java
Minimum SDK : API 21 : Android 5.0 (Lollipop) (최소버전)
이후 Finish 하면 Ch1_App1 프로젝트가 새 창으로 뜬다.
activity_main_.xml, MainActivity.java 가 뜨며 설치가 진행됨.
아래 Build 탭 눌르면 콘솔에 설치가 보인다.
activity_main.xml : select design 에서 design 만 보이게 하기.
code 탭에서 코드 확인.
constraintlayout 요즘 나왔다.
design 탭에서 TextView 갖다 붙이면 code 탭에 해당 코드도 추가된다.
<TextView
android:text="Hello World!"
/>
에서 바꾸면 text 변경 가능.
MainActivity 클래스는
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //앱 실행
setContentView(R.layout.activity_main); //activity_main_.xml
}
구성됨.
res 폴더가 리소스라고 보면 된다. activity_main_.xml 파일도 들어있음.
res/values/colors.xml 에서 색상 변경 가능.
res/values/strings.xml 에 앱의 이름 존재.
app/manifests/AndroidManifest.xml 에서 앱에 대한 설정을 한다. 중요한 파일.
그림은 drawable 폴더에.
build.gradle에 자바 버전 설정도 가능
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
오류발생시 이 build.gradle 파일 읽으며 고치기.
빌드하기는 Run 'app' ▶ 실행버튼 클릭 (shift + F10)
애플리케이션이 실행되며 콘솔에
BUILD SUCCESSFUL in 34s
30 actionable tasks: 30 executed
가 뜨며 Android Emulator에 Hello World!가 출력된다.
File - New - New Project - Empty Activity
Name : Ch1_App2
Package Name : com.example.ch1_app2
Save location : D:\study\Ch1_App2
Language : Java
Minimum SDK : API 21 : Android 5.0 (Lollipop)
activity_main.xml 코드탭
태그를 androidx.constraintlayout.widget.ConstraintLayout에서
LinearLayout으로 수정하고 이 태그에서 사용하지 않는 속성 삭제
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</LinearLayout>
글자는 dp단위로 쓴다.
android:layout_height="10dp"
wrap 글자 사이즈에 따라 정해지고 match는 부모에 맞춰진다.
android:layout_width="wrap_content"
android:layout_height="match_parent"
방향을 설정하는 android:orientation="vertical"
TextView 태그를 Button으로 변해도 속성은 그대로 사용 가능한게 TextView가 부모이기 때문.
모든 위젯은 필수속성으로 크기 지정해야 한다.
android:textSize="10sp"
android:textColor="red"
글자크기와 색상. 글자는 sp 단위로도 입력.
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="버튼입니다"/>
버튼 추가.
design 탭에서 버튼이 길게 늘어지는 건
android:layout_height="wrap_content"
이렇게 match를 wrap로 수정하면 된다.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="환영합니다!"
android:textSize="10sp"
android:textColor="red"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼입니다"/>
이렇게 match_parent를 wrap_content로 수정한다.
이후 Run 하기
오류 발생 : ERROR:D:\study\Ch1_App2\app\src\main\res\layout\activity_main.xml:7: AAPT: error: 'red' is incompatible with attribute textColor (attr) reference|color.
android:textColor="red" 를 android:textColor="#123456" 로 수정 후 Run
Build - Clean Project를 통해 프로젝트를 Clean 할 수 있다.
기존창 Disconnect로 끄기
File - New - New Project - Empty Activity
Name : Ch1_App3
Package Name : com.example.ch1_app3
Save location : D:\study\Ch1_App3
Language : Java
Minimum SDK : API 21 : Android 5.0 (Lollipop)
activity_main.xml 코드탭
RelativeLayout 태그로 한다. 상대적으로 레이아웃 잡음.
android:background="#E5E7E1" 설정 후 Button 태그 추가.
design 태그에서 생성던 것들 움직인 뒤 code 탭 보면 뭔가 추가되있다.
이후 android:textSize="25sp", android:textStyle="bold", android:id="@+id/tv" 속성과 값을 TextView 태그에 추가
Button 태그에 text 속성과 값 추가
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#E5E7E1"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginStart="140dp"
android:layout_marginTop="331dp"
android:backgroundTint="#B4BFCA"
android:text="시작합니다" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="18dp"
android:layout_marginTop="138dp"
android:layout_marginEnd="30dp"
android:text="안드로이드에 오신것을 환영합니다!"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
결과 코드이다.
New - Activity - Empty Activity 로 새롭게 페이지 생성하면
res/layout/activity_main2.xml, java/MainActivity2.java 가 만들어진다.
res/layout/activity_main2.xml에서
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity2">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#009688"
android:text="환영합니다"
android:textColor="#FAF7F7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="331dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
이렇게 작성
AndroidManifest.xml에서
<activity
android:name=".MainActivity"
android:exported="false" />
<activity
android:name=".MainActivity2"
android:exported="true">
이렇게 설정한다.
res/drawable 폴더에 ic_launcher2.png 붙여넣기.
MainActivity2 클래스 onCreate() 메서드에서 setContentView(R.layout.activity_main2); 부분에
R.layout.activity_main2 원리로 경로를 찾아 activity_main2.xml을 찾아간다.
activity_main2.xml에서 @+id/는 아이디를 추가하겠다는 뜻이다.
MainActivity2.java 클래스는
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//헤더UI 그리기
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher2); //사진삽입
//위젯 가져오기
Button btn = (Button)findViewById(R.id.button); //타입으로 가져옴
//TextView tv = (TextView)findViewById(R.id.) //이런식으로 가져옴
//위젯에 이벤트 설정
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {//View는 Button의 부모객체
//출력해서 보기위해 토스트 구현
Toast.makeText(getApplicationContext(), "환영합니다", Toast.LENGTH_SHORT).show();
}
});
}
}
이렇게 작성했다.
Ch1_App4 프로젝트 생성
- 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">
<Button
android:id="@+id/nate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#817F7F"
android:text="네이트 홈페이지 열기"/>
<Button
android:id="@+id/tel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#4CAF50"
android:text="911 응급전화 걸기"/>
<Button
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#F44336"
android:text="갤러리 열기"/>
<Button
android:id="@+id/end"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#FFEB3B"
android:text="끝내기"/>
</LinearLayout>
match_parent 부모페이지 맞추고, wrap_content 버튼 자체 맞춤
android:orientation="vertical" 세로로 horizontal 가로로
- MainActivity.java
package com.example.ch1_app4;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
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 nate = (Button)findViewById(R.id.nate);
Button tel = (Button)findViewById(R.id.tel);
Button pic = (Button)findViewById(R.id.pic);
Button end = (Button)findViewById(R.id.end);
//위젯에 이벤트 설정
nate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {//홈페이지 열기
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.nate.com"));
startActivity(mIntent);
}
});
tel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {//전화 걸기
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:/911"));
startActivity(mIntent);
}
});
pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {//갤러리 열기
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
startActivity(mIntent);
}
});
end.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {//애플리케이션 종료
finish();
}
});
}
}
에러발생시 sync now
에뮬레이터 에러는 에뮬레이터 종료 후 다시 실행하기
Ch1_App5 프로젝트 생성
- 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
android:layout_margin="10dp">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="글자 나타내기"
android:gravity="center_horizontal"
android:backgroundTint="#A58585"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="홈페이지 열기"
android:gravity="center_horizontal"
android:backgroundTint="#A58585"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical">
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11.0(R)"/>
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12.0(S)"/>
</RadioGroup>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
gravity : 자식들 정렬
layout_gravity : 부모에 맞춰 정렬
res/drawable에 icon.png 이미지 넣기 (이미지 파일명은 반드시 소문자, 대문자면 오류 발생)
res/drawable에 strawberry.png 이미지 넣기
- MainActivity.java
package com.example.ch1_app5;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//위젯 객체 가져오기
EditText et = (EditText)findViewById(R.id.et);
Button btn1 = (Button)findViewById(R.id.btn1);
Button btn2 = (Button)findViewById(R.id.btn2);
RadioButton radio1 = (RadioButton)findViewById(R.id.radio1);
RadioButton radio2 = (RadioButton)findViewById(R.id.radio2);
ImageView img = (ImageView)findViewById(R.id.img);
//위젯 이벤트 처리
btn1.setOnClickListener(new View.OnClickListener() {//글자 나타내기 버튼
@Override
public void onClick(View view) {
String text = et.getText().toString(); //et 객체의 text String으로 가져옴
if(text.length() != 0 && text != null) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), "입력한 문자가 없습니다", Toast.LENGTH_SHORT).show();
}
}
});
btn2.setOnClickListener(new View.OnClickListener() {//홈페이지 열기 버튼
@Override
public void onClick(View view) {
String uri = et.getText().toString();
try {//예외상황 잡기 위함
Intent website = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(website);
}catch (Exception e) {
Toast.makeText(getApplicationContext(), "오류발생", Toast.LENGTH_SHORT).show();
}
}
});
radio1.setOnClickListener(new View.OnClickListener() {//11.0(R) radio 클릭
@Override
public void onClick(View view) {
img.setImageResource(R.drawable.icon);
}
});
radio2.setOnClickListener(new View.OnClickListener() {//12.0(S) radio 클릭
@Override
public void onClick(View view) {
img.setImageResource(R.drawable.strawberry);
}
});
}
}
- 기타 View 클래스의 XML 속성
android:visibility="invisible" : 안보이게 하는 속성
android:enabled="false" : 사용못하게 하는 속성
android:rotation="45" : 각도 돌리는 속성
Ch1_App6 프로젝트 생성 : 위젯 객체를 자바를 통해 조작하기
- 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">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="Java코드로 뷰구성 해보기"
android:layout_gravity="center"/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#08BCD4"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="버튼1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="버튼2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="버튼3"/>
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:maxLines="2"
android:focusable="true"/>
</LinearLayout>
- MainActivity.java
package com.example.ch1_app6;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//위젯 객체 가져오기
TextView tv = (TextView)findViewById(R.id.tv1);
//TextView 객체를 자바를 통해 조작하기
tv.setText("자바코드로 글자를 바꿉니다");
tv.setTextSize(20);
tv.setTextColor(Color.RED);
//위젯 객체 가져오기
Button btn1 = (Button)findViewById(R.id.btn1);
//Button 객체를 자바를 통해 조작하기
btn1.setVisibility(View.VISIBLE);
btn1.setRotation(45);
//위젯 객체 가져오기
Button btn2 = (Button)findViewById(R.id.btn2);
//Button 객체를 자바를 통해 조작하기
btn2.setClickable(false);
btn2.setTextColor(Color.BLACK);
btn2.setBackgroundColor(Color.GREEN);
btn2.setGravity(Gravity.RIGHT|Gravity.TOP);
btn2.setPadding(5, 5, 5, 5);
//위젯 객체 가져오기
Button btn3 = (Button)findViewById(R.id.btn3);
//Button 객체를 자바를 통해 조작하기
btn3.setBackgroundColor(Color.RED);
btn3.setText("수고하셨습니다");
btn3.setScaleX(2);
}
}
'교육 정리 > 안드로이드 앱 제작(Java)' 카테고리의 다른 글
6일차. 데이터 저장과 관리2, 파일, 서비스, 브로드캐스트, 프로바이더, 맵 (2) | 2022.11.12 |
---|---|
5일차. 프래그먼트, 어댑터뷰, 리사이클러, 뷰 페이저, 액티비티와 인텐트, 데이터 저장과 관리 (0) | 2022.11.05 |
4일차. 그래픽과 이미지 (2) | 2022.10.29 |
3일차. 레이아웃 익히기2, 이벤트, 고급 위젯 다루기 (2) | 2022.10.22 |
2일차. 레이아웃 익히기, 고급위젯 다루기 (2) | 2022.10.08 |
댓글