activity_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">
<EditText android:id="@+id/editNum1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="숫자1"
android:inputType="number"/>
<EditText android:id="@+id/editNum2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="숫자2"
android:inputType="number"/>
<Button android:id="@+id/btnCalc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="두 숫자의 곱 계산"/>
</LinearLayout>
activity_sub.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">
<TextView android:id="@+id/textNum1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="숫자1"
android:textSize="16dp"/>
<TextView android:id="@+id/textNum2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="숫자2"
android:textSize="16dp"/>
<Button android:id="@+id/btnQuit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="화면 닫기"/>
</LinearLayout>
MainActivity.java
package com.example.boki.datatrans;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText mEditNum1;
private EditText mEditNum2;
private Button btnCalc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditNum1 = (EditText)findViewById(R.id.editNum1);
mEditNum2 = (EditText)findViewById(R.id.editNum2);
btnCalc = (Button)findViewById(R.id.btnCalc);
btnCalc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SubActivity.class);
intent.putExtra("num1", Integer.parseInt(mEditNum1.getText().toString()));
intent.putExtra("num2", Integer.parseInt(mEditNum2.getText().toString()));
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode == 0 && resultCode == RESULT_OK) {
int result = data.getIntExtra("result", 0);
Toast.makeText(this, "두 숫자의 곱: " + result, Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
SubActivity.java
package com.example.boki.datatrans;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Locale;
public class SubActivity extends Activity {
private int num1;
private int num2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
TextView textNum1 = (TextView)findViewById(R.id.textNum1);
TextView textNum2 = (TextView)findViewById(R.id.textNum2);
Button btnQuit = (Button)findViewById(R.id.btnQuit);
Intent intent = getIntent();
num1 = intent.getIntExtra("num1", -1);
num2 = intent.getIntExtra("num2", -1);
if ( num1 != -1 && num2 != -1) {
textNum1.setText(String.format(Locale.KOREAN, "숫자1: %d", num1));
textNum2.setText(String.format(Locale.KOREAN, "숫자2: %d", num2));
}
btnQuit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("result", num1 * num2);
setResult(RESULT_OK, data);
finish();
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.boki.datatrans">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SubActivity"/>
</application>
</manifest>
activity_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">
<EditText android:id="@+id/edtNum1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="1번째 숫자"/>
<EditText android:id="@+id/edtNum2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="2번째 숫자"/>
<Button android:id="@+id/btnNewActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="더하기"/>
</LinearLayout>
second.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">
<Button android:id="@+id/btnReturn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기"/>
</LinearLayout>
SecondActivity.java
package com.example.boki.dataintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
setTitle("Second 액티비티");
Intent inIntent = getIntent();
final int hapValue = inIntent.getIntExtra("Num1", 0) + inIntent.getIntExtra("Num2", 0);
Button btnReturn = (Button)findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent outIntent = new Intent(getApplicationContext(), MainActivity.class);
outIntent.putExtra("Hap", hapValue);
setResult(RESULT_OK, outIntent);
finish();
}
});
}
}
MainActivity.java
package com.example.boki.dataintent;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("메인 액티비티");
Button btnNewActivity = (Button)findViewById(R.id.btnNewActivity);
btnNewActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText edtNum1 = (EditText)findViewById(R.id.edtNum1);
EditText edtNum2 = (EditText)findViewById(R.id.edtNum2);
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Num1",Integer.parseInt(edtNum1.getText().toString()));
intent.putExtra("Num2",Integer.parseInt(edtNum2.getText().toString()));
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
int hap = data.getIntExtra("Hap", 0);
Toast.makeText(getApplicationContext(), "합계 :" +hap, Toast.LENGTH_SHORT).show();
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.boki.dataintent">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" android:label="Second 액티비티"/>
</application>
</manifest>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.boki.implicitintent">
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_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">
<Button android:id="@+id/btnDial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="전화 걸기"/>
<Button android:id="@+id/btnWeb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="홈페이지 열기"/>
<Button android:id="@+id/btnGoogle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="구글 맵 열기"/>
<Button android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="구글 검색하기"/>
<Button android:id="@+id/btnSms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="문자 보내기"/>
<Button android:id="@+id/btnPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="사진 찍기"/>
</LinearLayout>
MainActivity.java
package com.example.boki.implicitintent;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
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);
setTitle("암시적 인텐트 예제");
Button btnDial = (Button)findViewById(R.id.btnDial);
Button btnWeb = (Button)findViewById(R.id.btnWeb);
Button btnGoogle = (Button)findViewById(R.id.btnGoogle);
Button btnSearch = (Button)findViewById(R.id.btnSearch);
Button btnSms = (Button)findViewById(R.id.btnSms);
Button btnPhoto = (Button)findViewById(R.id.btnPhoto);
btnDial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("tel:01012345678");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
}
});
btnWeb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("http://www.hanbit.co.kr");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
btnGoogle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("http://maps.google.com/maps?q="+37.554264+","+126.913598);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "안드로이드");
startActivity(intent);
}
});
btnSms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.putExtra("sms_body", "안녕하세요?");
intent.setData(Uri.parse("smsto:"+Uri.encode("010-1234-4567")));
startActivity(intent);
}
});
btnPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
}
});
}
}
activity_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">
<GridView android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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:id="@+id/ivPoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
package com.example.boki.movieposter_gridview;
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
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; // context 변수를 다른 메소드에서 사용하기 위함
}
@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.mov01, R.drawable.mov02, R.drawable.mov03, R.drawable.mov04, R.drawable.mov05, R.drawable.mov06, R.drawable.mov07, R.drawable.mov08, R.drawable.mov09, R.drawable.mov10,
R.drawable.mov11, R.drawable.mov12, R.drawable.mov13, R.drawable.mov14, R.drawable.mov15, R.drawable.mov16, R.drawable.mov17, R.drawable.mov18, R.drawable.mov19, R.drawable.mov20,
R.drawable.mov21, R.drawable.mov22, R.drawable.mov23, R.drawable.mov24, R.drawable.mov25};
// 영화제목을 문자 배열로 초기화
String[] posterTitle = {"써니","완득이","괴물","라디오스타","비열한거리","왕의남자","아일랜드","웰컴투동막골","헬보이","빽투더퓨처","여인의향기","쥬라기공원","포레스트검프","그라운드데이","혹성탈출-진화의시작",
"아름다운비행","내이름은칸","해리포터","마더","킹콩을들다","쿵푸팬더2","짱구는못말려-미래의신부를구하라","아저씨","아바타","대부"};
@Override
public View getView(int position, View convertView, ViewGroup parent) { // 영화 포스터를 각 그리드뷰의 칸마다 이미지뷰를 생성해서 보여주게 함
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(200, 300));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); // 그리드뷰 칸의 중앙에 배치
imageView.setPadding(5,5,5,5);
imageView.setImageResource(posterID[position]); // R.drawable.mov0x 이미지를 로드하여 출력
//TODO 이미지를 클릭했을 때, 원래 이미지 크기가 보이도록 함
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(posterTitle[pos]);
dlg.setIcon(R.drawable.movie_icon);
dlg.setView(dialogView);
dlg.setNegativeButton("닫기", null);
dlg.show();
}
});
return imageView;
}
}
}
menu1.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/itemRed"
android:title="배경색(빨강)"/>
<item android:id="@+id/itemGreen"
android:title="배경색(초록)"/>
<item android:id="@+id/itemBlue"
android:title="배경색(파랑)"/>
</menu>
menu2.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/subRotate"
android:title="버튼 45도 회전"/>
<item android:id="@+id/subSize"
android:title="버튼 2개 확대"/>
</menu>
activity_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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout android:id="@+id/baseLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="배경색 변경"/>
<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼 변경"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.boki.contextmenuusingxml;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout baseLayout;
Button button1, button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("배경색 바꾸기(컨텍스트 메뉴)");
baseLayout = (LinearLayout)findViewById(R.id.baseLayout);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
registerForContextMenu(button1);
registerForContextMenu(button2);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater mInflater = getMenuInflater();
if (v == button1) {
menu.setHeaderTitle("배경색 변경");
mInflater.inflate(R.menu.menu1, menu);
}
if (v == button2) {
menu.setHeaderTitle("버튼 변경");
mInflater.inflate(R.menu.menu2, menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// return super.onContextItemSelected(item);
switch (item.getItemId()) {
case R.id.itemRed:
baseLayout.setBackgroundColor(Color.RED);
return true;
case R.id.itemGreen:
baseLayout.setBackgroundColor(Color.GREEN);
return true;
case R.id.itemBlue:
baseLayout.setBackgroundColor(Color.BLUE);
return true;
case R.id.subRotate:
button1.setRotation(45);
button2.setRotation(45);
return true;
case R.id.subSize:
button1.setScaleX(2);
button2.setScaleY(2);
return true;
} return false;
}
}
activity_main.xml
필요없다~! java에서 View를 커스터마이징해줄거니깐
MainActivity.java
package com.example.boki.basicbitmap;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//TODO 클래스를 직접 setContentView할거임
setContentView(new MyGraphicView(this));
}
private static class MyGraphicView extends View {
public MyGraphicView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap picture = BitmapFactory.decodeResource(getResources(),R.drawable.jeju14);
int picX = (this.getWidth() - picture.getWidth()) / 2;
int picY = (this.getHeight() - picture.getHeight()) / 2;
canvas.drawBitmap(picture, picX, picY, null);
picture.recycle();
}
}
}