Android

[Android] ImageView, Toast

Jintiago 2022. 3. 9. 17:08

이미지를 화면에 출력하는 ImageView와 팝업 메시지를 송출하는 Toast를 응용해 보았다.

 

-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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv_test"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/kimkeunyook" />
        <!-- 기본 이미지 -->
</LinearLayout>

 

 

-Java 코드-

package com.example.imageviewandtoast;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ImageView iv_test;

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

        iv_test = (ImageView)findViewById(R.id.iv_test);
        iv_test.setOnClickListener(new View.OnClickListener() { // 클릭 시 동작
            @Override
            public void onClick(View view) { // getApplicationContext() : 본 Activity에서!
                Toast.makeText(getApplicationContext(), "내 이름은 김근육 올해로 9수다",
                Toast.LENGTH_SHORT).show(); // 원하는 문자 팝업으로 송출
                // .LENGTH : 팝업 노출 시간
            }
        });
    }
}

이미지를 클릭하면 팝업이 뜬다