- 버튼 누르면 사진 랜덤으로 출력하기 

 

 

<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/btnTest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            app:srcCompat="@drawable/a" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            app:srcCompat="@drawable/b" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            app:srcCompat="@drawable/c" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            app:srcCompat="@drawable/d" />
    </FrameLayout>

</LinearLayout>

 

<MainActivity.java>

package com.example.imageviewapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    Button btnTest;
    ImageView imageView;
    ImageView imageView2;
    ImageView imageView3;
    ImageView imageView4;
    int index = 0;
    Random random = new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.imageView);
        imageView2 = findViewById(R.id.imageView2);
        imageView3 = findViewById(R.id.imageView3);
        imageView4 = findViewById(R.id.imageView4);
        btnTest = findViewById(R.id.btnTest);

        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index = random.nextInt(4);

                switch(index){
                    case 0:
                        imageView.setVisibility(View.VISIBLE);
                        imageView2.setVisibility(View.INVISIBLE);
                        imageView3.setVisibility(View.INVISIBLE);
                        imageView4.setVisibility(View.INVISIBLE);
                        break;
                    case 1:
                        imageView2.setVisibility(View.VISIBLE);
                        imageView.setVisibility(View.INVISIBLE);
                        imageView3.setVisibility(View.INVISIBLE);
                        imageView4.setVisibility(View.INVISIBLE);
                        break;
                    case 2:
                        imageView3.setVisibility(View.VISIBLE);
                        imageView2.setVisibility(View.INVISIBLE);
                        imageView.setVisibility(View.INVISIBLE);
                        imageView4.setVisibility(View.INVISIBLE);
                        break;
                    case 3:
                        imageView4.setVisibility(View.VISIBLE);
                        imageView2.setVisibility(View.INVISIBLE);
                        imageView3.setVisibility(View.INVISIBLE);
                        imageView.setVisibility(View.INVISIBLE);
                        break;
                }
            }
        });
    }
}

-문제 

 

 

[안드로이드 스튜디오] 기초 계산기 어플리케이션 만들기

기본 사칙연산을 위한 간단한 어플리케이션을 제작 디자인 레이아웃 부분은 res -> layout -> activi...

blog.naver.com

 

<MainActivity.java> 

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button0, buttonC, buttonOk;

    EditText edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
        button5 = findViewById(R.id.button5);
        button6 = findViewById(R.id.button6);
        button7 = findViewById(R.id.button7);
        button8 = findViewById(R.id.button8);
        button9 = findViewById(R.id.button9);
        button0 = findViewById(R.id.button0);
        buttonC = findViewById(R.id.buttonC);
        buttonOk = findViewById(R.id.buttonOk);
        edit = findViewById(R.id.edit);

        View.OnClickListener cl = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (v == button1) {
                    edit.setText(edit.getText().toString() + 1);
                } else if (v == button2) {
                    edit.setText(edit.getText().toString() + 2);
                } else if (v == button3) {
                    edit.setText(edit.getText().toString() + 3);
                } else if (v == button4) {
                    edit.setText(edit.getText().toString() + 4);
                } else if (v == button5) {
                    edit.setText(edit.getText().toString() + 5);
                } else if (v == button6) {
                    edit.setText(edit.getText().toString() + 6);
                } else if (v == button7) {
                    edit.setText(edit.getText().toString() + 7);
                } else if (v == button8) {
                    edit.setText(edit.getText().toString() + 8);
                } else if (v == button9) {
                    edit.setText(edit.getText().toString() + 9);
                } else if (v == button0) {
                    edit.setText(edit.getText().toString() + 0);
                } else if (v == buttonC) {
                    edit.setText("");
                } else if (v == buttonOk) {
                    edit.setText("");
                }
            }
        };

        button1.setOnClickListener(cl);
        button2.setOnClickListener(cl);
        button3.setOnClickListener(cl);
        button4.setOnClickListener(cl);
        button5.setOnClickListener(cl);
        button6.setOnClickListener(cl);
        button7.setOnClickListener(cl);
        button8.setOnClickListener(cl);
        button9.setOnClickListener(cl);
        button0.setOnClickListener(cl);
        buttonC.setOnClickListener(cl);
        buttonOk.setOnClickListener(cl);
    }
}

 

 

<activity_main.xml>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">


    <TableLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="8dp"
        android:stretchColumns="*">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0">

            <EditText
                android:id="@+id/edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="3"
                android:layout_weight="0"
                android:ems="10"
                android:text="" />
        </TableRow>


        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="1" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="2" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="3" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="4" />

            <Button
                android:id="@+id/button5"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="5" />

            <Button
                android:id="@+id/button6"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="6" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/button7"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="7" />

            <Button
                android:id="@+id/button8"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="8" />

            <Button
                android:id="@+id/button9"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="9" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1" />

            <Button
                android:id="@+id/button0"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="40dp"
                android:text="0" />

            <Button
                android:id="@+id/buttonC"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:textSize="15dp"
                android:text="Clear" />

        </TableRow>

        <Button
            android:id="@+id/buttonOk"
            android:textSize="30dp"
            android:text="ok" />


    </TableLayout>

</RelativeLayout>

-Activity 2개 만들어서 버튼 누르면 이동하게 만들기 

02-Intent.pdf
0.97MB

1. 첫 화면 

첫 화면 

 

2.  Hello~ 를 입력하고 이동을 누르면 

글을 입력한 후 이동을 누르면 

 

3. 다른 창이 뜨고 메인으로 가고싶으면 버튼 누르기

다른 창이 뜨고 메인으로 가고싶으면 버튼 누르기

 

 

4. 메인으로 돌아옴

메인으로 돌아옴 

<code>

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

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="텍스트를 입력하세요" />
    <Button
        android:id="@+id/btnMove"
        android:layout_width="wrap_content"
        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"
    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=".SubActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnBack"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="메인으로 돌아가기" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal"
        android:text="textView" />
    
</LinearLayout>

(textView 와 btnBack이 빨간색으로 나타났는데 오류없이 실행됐음.)

 

 

 -SubActivity.java

package com.example.intentexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SubActivity extends AppCompatActivity {
    Button btnBack;
    TextView textView;

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

        textView = findViewById(R.id.textView);
        btnBack = findViewById(R.id.btnBack);


        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(); //Main나옴
            }
        });
        Intent intent = getIntent();
        String str = intent.getStringExtra("str");
        textView.setText(str);
    }
}

(textView 와 btnBack이 빨간색으로 나타났는데 오류없이 실행됐음.)

 

 -MainActivity.java

package com.example.intentexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    Button btnMove;
    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText =findViewById(R.id.editText);
        btnMove = findViewById(R.id.btnMove);
        btnMove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tmpStr = editText.getText().toString();
                Intent intent = new Intent(MainActivity.this, SubActivity.class);
                intent.putExtra("str", tmpStr);
                editText.setText("");
                startActivity(intent);
            }
        });
    }
}

- 버튼 

: 원하는 문장 입력 후 BUTTON을 짧게 누르면 글자가 뜨고, 길게 누르면 입력한 문장이 삭제 되는 코드  

 

첫 화면
hihihi친 후 BUTTON누르면 뜸 
BUTTON 길게 누르면 입력한 글 지워짐 

 

- android:hint="원하는 문장을 입력하세요" 를 하면 위의 화면처럼 나옴. 

 

 

<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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="292dp"
        android:text="Hello !!!"
        android:textColor="#3F51B5"
        android:visibility="invisible"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/testButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clickTestButton"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName|text"
        android:hint="원하는 문장을 입력하세요"
        app:layout_constraintEnd_toStartOf="@+id/testButton"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

<MainActivity.java>

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button testButton;
    TextView textView;
    EditText editText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testButton = findViewById(R.id.testButton);
        testButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = editText.getText().toString();
                textView.setText(temp);
                textView.setVisibility(View.VISIBLE);


            }
        });

        testButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                editText.setText("");


                return false;
            }
        });

        editText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);

    }

 

 

- command 창 java 버전확인

javac -version

 

[ File ] → [ Settings.. ] → [ Appearance & Behavior ] → [ System Settings ] → Android SDK 로 이동 

 

1. [SDK Platforms]에서 Android 6.0(Marshmallow) 선택

 

2. [SDK Tools]에서 CMake, Google Play servises ,Google USB services 선택 

 

3. Apply 후  설치 

 

 

- 프로젝트 창 

programing-study.tistory.com/8?category=579832

 

안드로이드 스튜디오 -기초 4강 - 구조

안드로이드 스튜디오 -기초 4강 - 구조 안녕하세요 ADK Studio입니다. 오늘은 안드로이드 스튜디오 기초4강 안드로이드 스튜디오의 구조에 대해 알아보겠습니다. 왼쪽의 프로젝트 창에대해 설명해

programing-study.tistory.com

1. <AndroidManifest.xml>

메니페스트라고 많이 불리는데 이 파일은 여러분이 앱을 개발했을시에 앱에 대한 모든 설정을 넣는 곳

 

2. <MainActivity> ?

메인 엑티비티는 1강에서 프로젝트를 처음 생성할때 Activity Name에 대해 처음 생성된 java파일로 여러개의 화면을 개발하거나 화면이 아닌 단순 메소드들을 만들때 위의 java폴더의 첫번째 폴더 com.example.~에서 만들어 주셔야 합니다. 밑에 두개폴더에는 관심을 가지지 마시길 바랍니다. 

 

3. <res>

res폴더는 resourse의 약자로 리소스 즉 앱에서 사용하는 모든 자원들(레이아웃, 디자인, 그림, 문자, 애니메이션 등등)이 모여있는 곳

 

4. <drawable>

그림(jpg, png)을 넣는 곳 

 

5. <layout>

화면 디자인이라고 생각하시면 편함

 

6. <values>

변수값들은 values에 넣는다고 생각, 미리 정의해놓고 나중에 불러오는 기능으로 많이 사용됨

colors.xml은 색깔을

#000000이 검정색인데 코드를 볼때 한눈에 보기 쉽게하기 위해서 colorDark로 정의해서

나중에 #000000로 사용하는게 아니라 colorDark로 불러올때 사용한다.

strings.xml은 언어계열로

예를들면 다국적 어플을 만들 때 이곳에 정의해놓으면 쉽게 수정할 수 있음.

styles.xml은 앱의 스타일에 관련된 모든것

 

 


- 개발자에 의해 결정되고 만들어진 시스템 이미지, AVD
(Android Virtual Device, 안드로이드 가상 디바이스)

 

recipes4dev.tistory.com/145

 

안드로이드 에뮬레이터에서 AVD 만들고 실행하기. (Running App on Android Emulator)

1. 안드로이드 앱 개발 실행 환경. 안드로이드 스튜디오(Android Studio)에서 안드로이드 앱을 개발하고 나면, 보통 안드로이드 운영체제가 탑재된 단말에서 직접 테스트합니다. "안드로이드 스튜디

recipes4dev.tistory.com

실제 하드웨어가 없어도 앱을 개발하고 테스트할 수 있는 안드로이드 에뮬레이터(Emulator)가 제공된다.

(그냥 단순하게, 안드로이드 단말 기기를 PC 화면으로 옮겨놨다고 생각)

 

하드웨어에서 사용자 입력을 위해 제공되는 터치 인터페이스는 마우스 입력을 통해 처리되고, 전원 및 볼륨 등의 다양한 물리적 버튼도 에뮬레이터 프로그램에 버튼 UI 형태로 제공된다. 또한 네트워크, 오디오, 데이터베이스, 알림과 같이, 안드로이드 서비스를 사용하는 기능도 그대로 사용할 수 있다.

물론, 하드웨어 기기에서 제공되는 모든 기능이 에뮬레이터에서 제공되는 것은 아닙니다. WiFi, 블루투스, 지문시스템, NFC와 같이 시뮬레이션이 불가능한 하드웨어 인터페이스는 에뮬레이터에서 제공되지 않는다.

 

"어떤 버전을 사용할 것인지", "화면의 크기는 어떻게 되는지", "시스템 메모리 크기는 얼마나 되는지"에 대한 결정이며, 안드로이드 에뮬레이터는 개발자의 이런 결정을 통해 만들어진 가상(Virtual) 시스템, 하드웨어 리얼(Real) 디바이스가 아닌 소프트웨어 가상(Virtual) 디바이스를 시뮬레이션 하는 것

안드로이드 스튜디오 다운로드

- 구글 검색창에 : 안드로이드 스튜디오 검색

- 세번째 항목 클릭

- 가운데 DOWNLOAD ANDROID STUDIO 클릭

- 다운로드 된 안드로이드 스튜디오 설치파일

 

안드로이드 스튜디오 설치

- Finish를 클릭하여 설치완료

 

 

셋팅

- setting값 가져오기

- 처음 설치이므로 셋팅값이 없음 : Do not import settings 클릭 -> OK

- Data 전송할것인지 선택

- 셋업 위저드 화면

- 실행화면 창을 밝은색 / 어두운 색 선택

- 설정 확인 -> Finish 누름

- 설치되고 있는 중

- 완료됨 : Finish 누름

 

안드로이드 스튜디오 시작화면

 

+ Recent posts