팝업 바깥레이어 클릭시 닫히지 않게 하기
1
2
3
4
5
6
7
8
9
10
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //바깥레이어 클릭시 안닫히게
        if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
            return false;
        }
        return true;
    }
 
 
cs


타이틀바 없애기

1
2
3
    //타이틀바 없애기       
    requestWindowFeature(Window.FEATURE_NO_TITLE);
 
cs


사진파일을 갤러리에 추가하기

1
2
    //갤러리에 추가
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(파일객체)));
cs


10MB 파일(File) 생성하여 내부저장소(Storage)에 쓰기(Write)


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    //버튼
    public void mOnFileWriteClick(View v){
        FileWrite();
    }
 
    //파일쓰기
    public void FileWrite(){
        //File  디렉토리 , File명
        String dirPath = Environment.getExternalStorageDirectory()+"/Blog";
        String filename = "TestFile10MB.temp";
 
        //디렉토리 없으면 생성
        File dir = new File(dirPath);
        if(!dir.exists()){
            dir.mkdir();
        }
 
        //파일객체
        File file = new File(dir, filename);
        try{
            //쓰기객체
            FileOutputStream fos = new FileOutputStream(file);
            //버퍼 - 1MB씩쓰기
            byte[] buffer = new byte[1*1024*1024];
            for(int i=0; i<10; i++){    //10MB
                fos.write(buffer, 0, buffer.length);    //1MB씩 10번쓰기
                fos.flush();
            }
            int len = 0;
 
            fos.close();
 
            Toast.makeText(this"File Write Success", Toast.LENGTH_SHORT).show();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
cs


AndroidManifest.xml
1
2
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
cs

내부저장소(Storage)에 쓰기권한



결과

 


MainActivity.java

Property File Write

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    //Property 파일 쓰기
    public void WriteToProperty(){
        //property 파일
        File file = new File(Environment.getDataDirectory()+"/data/"+getPackageName(), "PropTest.properties");
 
        FileOutputStream fos = null;
        try{
            //property 파일이 없으면 생성
            if(!file.exists()){
                file.createNewFile();
            }
 
            fos = new FileOutputStream(file);
 
            //Property 데이터 저장
            Properties props = new Properties();
            props.setProperty("test" , "Property에서 데이터를 저장");   //(key , value) 로 저장
            props.store(fos, "Property Test");
 
            Log.d("prop""write success");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
cs


Property File Read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    //Property 파일 읽기
    public String ReadToProperty(){
        //property 파일
        File file = new File(Environment.getDataDirectory()+"/data/"+getPackageName(), "PropTest.properties");
 
        if(!file.exists()){ return ""; }
 
        FileInputStream fis = null;
        String data = "";
        try{
            fis = new FileInputStream(file);
 
            //Property 데이터 읽기
            Properties props = new Properties();
            props.load(fis);
            data = props.getProperty("test""");  //(key , default value)
 
            Log.d("prop""read success");
        }catch(IOException e){
            e.printStackTrace();
        }
 
        return data;
    }
cs



activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?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:padding="16dp">
 
    <LinearLayout
        android:paddingBottom="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Property파일 읽기/쓰기" />
    </LinearLayout>
 
    <LinearLayout
        android:paddingBottom="8dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btnPropWrite"
            android:text="Property 쓰기"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="mOnPropertyClick" />
        <Button
            android:id="@+id/btnPropRead"
            android:text="Property 읽기"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="mOnPropertyClick"  />
    </LinearLayout>
 
    <LinearLayout
        android:paddingBottom="8dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="결과 : " />
        <TextView
            android:id="@+id/txtProperty"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
 
</LinearLayout>
 
cs



결과

쓰기 버튼을 누르면 Properties 파일에 데이터를 쓰고 읽기버튼을 누르면 Properties 파일에서 데이터를 읽어온다.

 


MainActivity.java

1
2
3
4
5
    public void mOnMenuClick(View v){
        //접근성 설정화면으로 이동
        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
        startActivity(intent);
    }
cs



activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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:padding="16dp">
 
    <LinearLayout
        android:paddingBottom="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="접근성 설정화면으로 이동" />
    </LinearLayout>
 
    <LinearLayout
        android:gravity="center_horizontal"
        android:paddingBottom="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:text="접근성 이동"
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="mOnMenuClick" />
    </LinearLayout>
 
</LinearLayout>
 
cs



결과

 


버튼클릭 -> 화면캡쳐 -> 캡쳐한 이미지를 갤러리에 추가


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    //캡쳐버튼클릭
    public void mOnCaptureClick(View v){
        //전체화면
        View rootView = getWindow().getDecorView();
 
        File screenShot = ScreenShot(rootView);
        if(screenShot!=null){
            //갤러리에 추가
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(screenShot)));
        }
    }
 
    //화면 캡쳐하기
    public File ScreenShot(View view){
        view.setDrawingCacheEnabled(true);  //화면에 뿌릴때 캐시를 사용하게 한다
 
        Bitmap screenBitmap = view.getDrawingCache();   //캐시를 비트맵으로 변환
 
        String filename = "screenshot.png";
        File file = new File(Environment.getExternalStorageDirectory()+"/Pictures", filename);  //Pictures폴더 screenshot.png 파일
        FileOutputStream os = null;
        try{
            os = new FileOutputStream(file);
            screenBitmap.compress(Bitmap.CompressFormat.PNG, 90, os);   //비트맵을 PNG파일로 변환
            os.close();
        }catch (IOException e){
            e.printStackTrace();
            return null;
        }
 
        view.setDrawingCacheEnabled(false);
        return file;
    }
cs



activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootLayout"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:textSize="24dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="화면을 캡쳐합니다." />
 
    <Button
        android:text="캡쳐하기"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="mOnCaptureClick"/>
 
    <ImageView
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
 


cs



AndroidManifest.xml

1
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
cs

저장소에 파일 쓰기 권한



결과

캡쳐하기 버튼을 누르면 화면을 캡쳐하여 갤러리에 추가한다.

 


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
   //버튼
    public void mOnContactAdd(View v){
        if(v.getId()!=R.id.btnContactAdd){ return; }
        ContactAdd();
    }
 
    //연락처 추가
    public void ContactAdd(){
        new Thread(){
            @Override
            public void run() {
 
                ArrayList<ContentProviderOperation> list = new ArrayList<>();
                try{
                    list.add(
                            ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                                    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                                    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                                    .build()
                    );
 
                    list.add(
                            ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 
                                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                                    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "홍길동")   //이름
 
                                    .build()
                    );
 
                    list.add(
                            ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 
                                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "010-1234-5678")           //전화번호
                                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE  , ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)   //번호타입(Type_Mobile : 모바일)
 
                                    .build()
                    );
 
                    list.add(
                            ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
 
                                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                                    .withValue(ContactsContract.CommonDataKinds.Email.DATA  , "hong_gildong@naver.com")  //이메일
                                    .withValue(ContactsContract.CommonDataKinds.Email.TYPE  , ContactsContract.CommonDataKinds.Email.TYPE_WORK)     //이메일타입(Type_Work : 직장)
 
                                    .build()
                    );
 
                    getApplicationContext().getContentResolver().applyBatch(ContactsContract.AUTHORITY, list);  //주소록추가
                    list.clear();   //리스트 초기화
                }catch(RemoteException e){
                    e.printStackTrace();
                }catch(OperationApplicationException e){
                    e.printStackTrace();
                }
            }
        }.start();
    }
cs

Button을 클릭하면 연락처가 추가된다



activity_main.xml

1
2
3
4
5
6
7
8
9
    <Button
        android:id="@+id/btnContactAdd"
        android:text="연락처 추가"
        android:textSize="14dp"
        android:padding="5dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="mOnContactAdd"/>
cs



AndroidMenifest.xml

1
2
3
4
5
 
    <!-- 주소록 권한 -->
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
 
cs

권한추가



결과

 


만약 API23에서 안되면 Target SDK 버전을 22로하자



drawable 폴더 > radiobutton_radiobutton01.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- UNSELECTED -->
    <item android:state_checked="false">
        <shape>
            <stroke android:color="#c4cbd2" android:width="1dp" />
            <corners android:radius="15dp" />
            <solid android:color="#fff" />
        </shape>
    </item>
    <!-- SELECTED -->
    <item android:state_checked="true">
        <layer-list>
            <!-- 배경 -->
            <item>
                <shape>
                    <corners android:radius="15dp" />
                    <solid android:color="#e14f50" />
                </shape>
            </item>
            <!-- 이미지 영역 -->
            <item android:drawable="@drawable/radio_bg" android:top="4dp" android:right="4dp" android:bottom="4dp" android:left="4dp" />
        </layer-list>
    </item>
</selector>
 
 
cs



background 속성에 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    <!-- 라디오버튼 -->
    <RadioGroup
        android:checkedButton="@+id/btnRadio1"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@id/btnRadio1"
            android:layout_marginBottom="5dp"
            android:button="@android:color/transparent"
            android:background="@drawable/radiobutton_radiobutton1"
            android:layout_width="30dp"
            android:layout_height="30dp" />
        <RadioButton
            android:button="@android:color/transparent"
            android:background="@drawable/radiobutton_radiobutton1"
            android:layout_width="30dp"
            android:layout_height="30dp" />
    </RadioGroup>
    <!-- //라디오버튼 -->
cs
<RadioGroup> 속성 android:checkedButton="@+id/btnRadio1" 속성을 이용하여 디폴트(default) 체크를 한다.



결과



drawable folder > checkbox_checkbox1.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Unchecked -->
    <item android:state_checked="false">
        <shape>
            <stroke android:color="#c4cbd2" android:width="1dp" />
            <corners android:radius="4dp" />
            <solid android:color="#fff" />
        </shape>
    </item>
    <!-- Checked -->
    <item android:state_checked="true">
        <layer-list>
            <!-- 배경 -->
            <item>
                <shape>
                    <corners android:radius="4dp" />
                    <solid android:color="#e14f50" />
                </shape>
            </item>
            <!-- 이미지 영역 -->
            <item android:drawable="@drawable/checkbox_bg" android:top="4dp" android:right="4dp" android:bottom="4dp" android:left="4dp" />
        </layer-list>
    </item>
</selector>
 
cs

drawable folder > 체크이미지(checkbox_bg.png) 추가



background 속성에 사용

1
2
3
4
5
6
7
8
    <!-- 체크박스 -->
    <CheckBox
        android:checked="true"
        android:button="@android:color/transparent"
        android:background="@drawable/checkbox_checkbox1"
        android:layout_width="30dp"
        android:layout_height="30dp" />
    <!-- //체크박스 -->
cs

button 속성(attribute)을 투명(transparent)하게 해야한다



결과

Checked


Unchecked


+ Recent posts