[직접 풀어보기]
1
activitiy_main.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">
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gridView1"
android:gravity="center"
android:numColumns="4">
</GridView>
</LinearLayout>
dialog.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ivPoster"/>
</LinearLayout>
MainActivity.java
더보기
package com.cookandroid.project11_1;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("그리드뷰 포켓몬 도감");
final GridView gv = (GridView) findViewById(R.id.gridView1);
MyGridAdapter gAdapter = new MyGridAdapter(this);
gv.setAdapter(gAdapter);
}
public class MyGridAdapter extends BaseAdapter{
Context context;
public MyGridAdapter(Context c){
context = c;
}
@Override
public int getCount() {
return posterID.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
Integer[] posterID = {
R.drawable.mov1, R.drawable.mov2, R.drawable.mov3, R.drawable.mov4, R.drawable.mov5, R.drawable.mov6, R.drawable.mov7, R.drawable.mov8, R.drawable.mov9, R.drawable.mov10,
R.drawable.mov1, R.drawable.mov2, R.drawable.mov3, R.drawable.mov4, R.drawable.mov5, R.drawable.mov6, R.drawable.mov7, R.drawable.mov8, R.drawable.mov9, R.drawable.mov10,
R.drawable.mov1, R.drawable.mov2, R.drawable.mov3, R.drawable.mov4, R.drawable.mov5, R.drawable.mov6, R.drawable.mov7, R.drawable.mov8, R.drawable.mov9, R.drawable.mov10,
R.drawable.mov1, R.drawable.mov2, R.drawable.mov3, R.drawable.mov4, R.drawable.mov5, R.drawable.mov6, R.drawable.mov7, R.drawable.mov8, R.drawable.mov9, R.drawable.mov10
};
String[] posterName = {
"아차모", "찌르성게", "이브이", "해무기", "지라치", "란쿨루스", "누니머기", "팽도리", "피츄", "피카츄",
"아차모", "찌르성게", "이브이", "해무기", "지라치", "란쿨루스", "누니머기", "팽도리", "피츄", "피카츄",
"아차모", "찌르성게", "이브이", "해무기", "지라치", "란쿨루스", "누니머기", "팽도리", "피츄", "피카츄",
"아차모", "찌르성게", "이브이", "해무기", "지라치", "란쿨루스", "누니머기", "팽도리", "피츄", "피카츄"
};
public View getView(int position, View convertView, ViewGroup parent){
ImageView imageview = new ImageView(context);
imageview.setLayoutParams(new ViewGroup.LayoutParams(200, 300));
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview.setPadding(5,5,5,5);
imageview.setImageResource(posterID[position]);
final int pos = position;
imageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View dialogView = (View) View.inflate(MainActivity.this, R.layout.dialog, null);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
ImageView ivPoster = (ImageView) dialogView.findViewById(R.id.ivPoster);
ivPoster.setImageResource(posterID[pos]);
dlg.setTitle(posterName[pos]);
dlg.setIcon(R.drawable.ic);
dlg.setView(dialogView);
dlg.setNegativeButton("닫기", null);
dlg.show();
}
});
return imageview;
}}
}
2
activitiy_main.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">
<Gallery
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gallery1"
android:spacing="5dp">
</Gallery>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ivPoster"
android:padding="20dp"/>
</LinearLayout>
toast1.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toastText1"
android:textSize="20dp"
android:textColor="#ffffff"
android:text="TextView"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic"/>
</LinearLayout>
MainActivity.java
더보기
package com.cookandroid.project11_2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView toastText;
View toastView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("갤러리 포켓몬 도감");
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
MyGalleryAdapter galAdapter = new MyGalleryAdapter(this);
gallery.setAdapter(galAdapter);
}
public class MyGalleryAdapter extends BaseAdapter{
Context context;
Integer[] posterID = {
R.drawable.mov1, R.drawable.mov2, R.drawable.mov3, R.drawable.mov4, R.drawable.mov5, R.drawable.mov6, R.drawable.mov7, R.drawable.mov8, R.drawable.mov9, R.drawable.mov10
};
String[] posterName = {
"아차모", "찌르성게", "이브이", "해무기", "지라치", "란쿨루스", "누니머기", "팽도리", "피츄", "피카츄",
};
public MyGalleryAdapter(Context c){
context = c;
}
@Override
public int getCount() {
return posterID.length;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageview = new ImageView(context);
imageview.setLayoutParams(new Gallery.LayoutParams(200, 300));
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview.setPadding(5,5,5,5);
imageview.setImageResource(posterID[position]);
final int pos = position;
imageview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView ivPoster = (ImageView) findViewById(R.id.ivPoster);
ivPoster.setScaleType(ImageView.ScaleType.FIT_CENTER);
ivPoster.setImageResource(posterID[pos]);
Toast toast = new Toast(MainActivity.this);
toastView = (View) View.inflate(MainActivity.this, R.layout.toast1, null);
toastText = (TextView) toastView.findViewById(R.id.toastText1);
toastText.setText(posterName[pos]);
toast.setView(toastView);
toast.show();
return false;
}
});
return imageview;
}
}
}
3
activitiy_main.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">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:id="@+id/ivPoster"/>
</LinearLayout>
MainActivity.java
더보기
package com.cookandroid.project10_3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("스피너 테스트");
final String[] pokemon = {
"아차모", "찌르성게", "이브이", "해무기", "지라치", "란쿨루스", "누니머기", "팽도리", "피츄", "피카츄",
};
final Integer[] posterID = {
R.drawable.mov1, R.drawable.mov2, R.drawable.mov3, R.drawable.mov4, R.drawable.mov5, R.drawable.mov6, R.drawable.mov7, R.drawable.mov8, R.drawable.mov9, R.drawable.mov10
};
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, pokemon);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ImageView ivPoster = (ImageView) findViewById(R.id.ivPoster);
ivPoster.setScaleType(ImageView.ScaleType.FIT_CENTER);
ivPoster.setPadding(5, 5, 5, 5);
ivPoster.setImageResource(posterID[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
'DEVELOPMENT' 카테고리의 다른 글
[Android Studio] Chapter 12 직접 풀어보기 (0) | 2021.07.07 |
---|---|
[Android Studio] Chapter 11 연습문제 (0) | 2021.07.07 |
[Android Studio] Chapter 10 연습문제 (0) | 2021.07.06 |
[Android Studio] Chapter 10 직접 풀어보기 (0) | 2021.07.06 |
[Android Studio] Chapter 9 연습문제 (0) | 2021.07.05 |