activity_main.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=".MainActivity">
<ListView
android:id="@+id/listview1"
android:layout_width="378dp"
android:layout_height="429dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.424"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ListView Example"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.045"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.027" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.myapplication;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
final String[] book = {"Computer Architecture", "Algorithm", "Artificial Intelligence",
"Computer Graphics", "Data Structure", "Database", "Network", "Operating System",
"Mobile Programming"};
final Integer[] item = {R.drawable.computer_architecture, R.drawable.algorithm,
R.drawable.artificial_intelligence, R.drawable.computer_graphics, R.drawable.database,
R.drawable.network, R.drawable.operating_system, R.drawable.mobile_programming};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("List View Example");
ListView myListView = (ListView) findViewById(R.id.listview1);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, book);
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
final Dialog imgDialog = new Dialog(MainActivity.this);
imgDialog.setContentView(R.layout.dialog_book);
Button img_dlg_btn = (Button) imgDialog.findViewById(R.id.button);
final ImageView imageView = (ImageView) imgDialog.findViewById(R.id.imageView);
Bitmap b = BitmapFactory.decodeResource(getResources(), item[position]);
imageView.setImageBitmap(b);
img_dlg_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imgDialog.dismiss();
}
});
imgDialog.show();
}
});
}
}
dialog_book.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="342dp"
android:layout_height="186dp"
android:layout_marginStart="34dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="35dp"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/screen_background_dark" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="39dp"
android:text="OK"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
'안드로이드 Android' 카테고리의 다른 글
[모바일프로그래밍] 12-3 갤러리 (0) | 2020.11.28 |
---|---|
[모바일프로그래밍] 12-2 그리드 뷰 (0) | 2020.11.26 |
[모바일프로그래밍] 11-4 SQLite를 활용한 파일 처리 (0) | 2020.11.22 |
[모바일프로그래밍] 11-3 외부 저장 공간(SD 카드) 사용 (0) | 2020.11.22 |
[모바일프로그래밍] 11-2 내부 저장 장소를 사용하는 경우 (0) | 2020.11.22 |