본문 바로가기

Development/Android Studio

(Android Studio) Chapter2. ImageView, Package Structure

1. ImageView + Toast message

    - .xml

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="앱 개발 테스트" />

    <EditText
        android:id="@+id/et_test"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_move"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이동" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"> <!--- 정렬하는 방법 -->

    <ImageView
        android:id="@+id/test"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher" />  <!-- 이미지 띄우기 기능 -->

    </LinearLayout>


</LinearLayout>

    - .java

package com.example.intentexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btn_move;
    private EditText et_test;
    private String str;
    ImageView test;                 // Toast 메세지 구현 전 지역변수 지정

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

        et_test = findViewById(R.id.et_test);

        btn_move = findViewById(R.id.btn_move);
        btn_move.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str = et_test.getText().toString(); // getText를 string 형태로 입력해라
                Intent intent = new Intent(MainActivity.this, SubActivity.class);
                intent.putExtra("str", str);    // 이름정보, 실제 이름
                startActivity(intent);  // 엑티비티 이동
            }
        });

        test = (ImageView) findViewById(R.id.test);             // Toast popup message 기능 구현
        test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "이미지를 누르셨네요?", Toast.LENGTH_SHORT).show();
            }    //팝업 유지 시간 지정
        });
    }
}

 

2. 패키지 구조 & 역할

  1) AndroidManifest.xml

    - 어플리케이션 기본정보, 엑티비티와 서브엑티비티

  2) drawable

    - 이미지 저장

  3) layout

    - 엑티비티와 연결되는 xml 파일들이 저장

  4) mipmap

    - 해상도 별 이미지 저장

    - drawable과의 차이

구분drawablemipmap

목적 모든 종류의 이미지 저장 런처 아이콘 이미지 저장
선택되는 이미지 크기 디바이스 픽셀밀도에 따라 달라짐 이미지 크기에 따라 달라짐
지원버전 모든 버전 Android 2.3 Honeycomb (API 11)
사용예시 android:background="@drawable/ic_image" android:icon="@mipmap/ic_launcher"

  5) values

    - 색깔들 미리 선언

    - 문자열 미리 선언

    - 앱 테마 또는 디자인 미리 선언