1. 그래들 코드 

build.gradle (Project)
allprojects {
    repositories {
        google()
        jcenter()
        maven {url "https://jitpack.io"}
        
    }
}

 

build.gradle(app)

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.gms:play-services-ads:15.0.1'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}

 

2. XML 코드

<com.github.mikephil.charting.charts.BarChart
android:id="@+id/stacked_BarChart"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

 

3. MainActivity.java 코드

package com.jwlee.stackedbarchart;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    BarChart stackedChart;
    int[] colorArray = new int[] {Color.RED, Color.YELLOW, Color.BLUE};

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

        stackedChart = findViewById(R.id.stacked_BarChart);

        BarDataSet barDataSet = new BarDataSet(dataValues1(),"Bar Set");
        barDataSet.setColors(colorArray);


        BarData barData = new BarData(barDataSet);
        stackedChart.setData(barData);

    }

    private ArrayList dataValues1() {
        ArrayList dataVals = new ArrayList<>();

        dataVals.add(new BarEntry(0, new float[]{2,5.5f,4}));
        dataVals.add(new BarEntry(1, new float[]{2,8f,4.2f}));
        dataVals.add(new BarEntry(2, new float[]{3,5.5f,4}));
        dataVals.add(new BarEntry(3, new float[]{4,6.5f,4}));
        dataVals.add(new BarEntry(4, new float[]{2,5.5f,3.3f}));


        return dataVals;
    }
}

 

안드로이드 수명 계산기 만들기 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">

 

    <ScrollView

        android:layout_width="match_parent"

        android:layout_height="match_parent">

 

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="vertical"

            android:padding="10dp">

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="Name" />

 

            <EditText

                android:id="@+id/et_name"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

            />

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="Age" />

 

            <EditText

                android:id="@+id/et_age"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:digits="0123456789"

                android:inputType="number"/>

            <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginTop="20dp"

            android:text="Gender" />

 

            <RadioButton

                android:id="@+id/rb_male"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="Male"/>

 

            <RadioButton

                android:id="@+id/rb_female"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="Female"/>

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginTop="20dp"

                android:text="Smoking" />

 

            <RadioButton

                android:id="@+id/rb_smoking_yes"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="YES"/>

 

            <RadioButton

                android:id="@+id/rb_smoking_no"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="NO"/>

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginTop="20dp"

                android:text="Drugs" />

 

            <RadioButton

                android:id="@+id/rb_drugs_yes"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="YES"/>

 

            <RadioButton

                android:id="@+id/rb_drugs_no"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="NO"/>

 

            <Button

                android:id="@+id/b_calculator"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginTop="10dp"

                android:text="CALCULATE"

                android:textSize="32sp"/>

 

            <TextView

                android:id="@+id/tv_result"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginTop="10dp"

                android:text=""

                android:textSize="32sp"/>


        </LinearLayout>

    </ScrollView>


</LinearLayout>

 

 

액티비티 자바 코드 (안드로이드 라디오 버튼 다루는 방법을 터특한다.)

 

package com.jwlee.deathcalculator;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.TextView;

 

import java.util.Random;

 

public class MainActivity extends AppCompatActivity {

 

    EditText et_name, et_age;

    RadioButton rb_male, rb_female, rb_smoking_yes, rb_smoking_no, rb_drugs_yes, rb_drugs_no;

    Button b_calculate;

    TextView tv_result;

    Random r;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        et_name = findViewById(R.id.et_name);

        et_age = findViewById(R.id.et_age);

        rb_male = findViewById(R.id.rb_male);

        rb_female = findViewById(R.id.rb_female);

        rb_smoking_yes = findViewById(R.id.rb_smoking_yes);

        rb_smoking_no = findViewById(R.id.rb_smoking_no);

        rb_drugs_yes = findViewById(R.id.rb_drugs_yes);

        rb_drugs_no = findViewById(R.id.rb_drugs_no);

        b_calculate = findViewById(R.id.b_calculator);

        tv_result =findViewById(R.id.tv_result);

 

        r= new Random();

 

        //init stuff

        et_name.setText("Android");

        et_age.setText("30");

 

        rb_male.setChecked(true);

        rb_female.setChecked(false);

 

        rb_smoking_yes.setChecked(true);

        rb_smoking_no.setChecked(false);

 

        rb_drugs_yes.setChecked(true);

        rb_drugs_no.setChecked(false);

 

        tv_result.setText("");

 

        rb_male.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                rb_male.setChecked(true);

                rb_female.setChecked(false);

 

            }

        });

 

        rb_female.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                rb_male.setChecked(false);

                rb_female.setChecked(true);

 

            }

        });

 

        rb_smoking_yes.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                rb_smoking_yes.setChecked(true);

                rb_smoking_no.setChecked(false);

 

            }

        });

 

        rb_smoking_no.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                rb_smoking_yes.setChecked(false);

                rb_smoking_no.setChecked(true);

 

            }

        });

 

        rb_drugs_yes.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                rb_drugs_yes.setChecked(true);

                rb_drugs_no.setChecked(false);

 

            }

        });

 

        rb_drugs_no.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                rb_drugs_yes.setChecked(false);

                rb_drugs_no.setChecked(true);

 

            }

        });

 

        b_calculate.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                if(!et_name.getText().toString().equals("") && !et_age.getText().toString().equals("")) {

                    //get age and set it to 20 if lower

                    int currentAge = Integer.parseInt(et_age.getText().toString());

                    if(currentAge<20){

                        currentAge = 20;

                    }

 

                    //set random seed according to the name

                    int seed = et_name.getText().toString().hashCode();

                    r.setSeed(seed);

 

                    //calculate dying age

                    int ageOfDying = r.nextInt(120-currentAge) + currentAge;

 

                    //get the human stats

                    boolean isMale = rb_male.isChecked();

                    boolean isSmoking = rb_smoking_yes.isChecked();

                    boolean isTakingDrugs = rb_drugs_yes.isChecked();

 

                    //remove years for smoking, taking drugs or being a male

 

                    if(isMale){

                        ageOfDying = ageOfDying - 5;

 

                    }

 

                    if(isSmoking) {

                        ageOfDying = ageOfDying-5;

                    }

 

                    if(isTakingDrugs){

                        ageOfDying-=5;

 

                    }

                    if(ageOfDying <= currentAge)

                        ageOfDying = currentAge + 10;

 

                    tv_result.setText("" + ageOfDying);

                }

            }

        });

 

    }

}

 

 

1.  다음고 같은 권총 그림파일 세개를 만들어 리소트 drawable에 추가한다. 

 

2. 안드로이드 러시아 룰렛 게임 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:padding="20dp"

    tools:context=".MainActivity">

 

    <ImageView

        android:id="@+id/imageView"

        android:scaleType="centerInside"

        android:layout_width="300dp"

        android:layout_height="300dp"

        android:src="@drawable/gun"

        android:layout_centerInParent="true"

        />

 

    <Button

        android:id="@+id/button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:text="ROLL"

        />

 

    <TextView

        android:id="@+id/textView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true"

        android:gravity="center"

        android:text="Click on the image to shoot!"

        />

 

</RelativeLayout>

 

ㅁㅁㅁ

 

3. 액티비티 자바 코드 

 

package com.jwlee.russianroulettegame;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

 

public class MainActivity extends AppCompatActivity {

 

    ImageView imageView;

    Button button;

    TextView textView;

 

    List<String> bullets;

    boolean shuffled = true;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        imageView = findViewById(R.id.imageView);

        button = findViewById(R.id.button);

        textView = findViewById(R.id.textView);



        bullets = new ArrayList<>();



        bullets.add("NO");

        bullets.add("YES");

 

        //roll the barrel

        Collections.shuffle(bullets);

 

        button.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                Collections.shuffle(bullets);

                imageView.setImageResource(R.drawable.gun);

                shuffled = true;

                textView.setText("Click on the image to shoot!");

 

            }

        });

 

        imageView.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                if(shuffled) {

                    String currentBullet = bullets.get(0);

                    if(currentBullet.equals("YES")){

                        imageView.setImageResource(R.drawable.bang);

                        textView.setText("You are daed!");

                    }else {

                        imageView.setImageResource(R.drawable.click);

                        textView.setText("You are alive! Pass the gun to the next player!");

                    }

                    shuffled = false;

                }else {

                    textView.setText("First Roll the barrel!");

                }




            }

        });

    }

}

 

4. 동영상보고 따라하기 

 

 

안드로이드 랜덤카드 발생하기, android random card generator

 

 

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

    tools:context=".MainActivity">

 

   <Button

       android:id="@+id/button"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_centerInParent="true"

       android:text="GET A CARD"

       />

 

    <TextView

        android:id="@+id/textView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_above="@+id/button"

        android:layout_centerHorizontal="true"

 

        android:layout_marginBottom="40dp"

        android:text=""

        android:textSize="30sp"

        />



</RelativeLayout>

 

 

2. 액티비티 자바 코드 

package com.jwlee.randomcardgenerator;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

 

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

 

public class MainActivity extends AppCompatActivity {

 

    Button button;

    TextView textView;

 

    List<String> cardValues, cardsColors;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        button = findViewById(R.id.button);

        textView = findViewById(R.id.textView);

 

        cardValues = new ArrayList<>();

        cardsColors = new ArrayList<>();

 

        cardValues.add("2");

        cardValues.add("3");

        cardValues.add("4");

        cardValues.add("5");

        cardValues.add("6");

        cardValues.add("7");

        cardValues.add("8");

        cardValues.add("9");

        cardValues.add("10");

        cardValues.add("J");

        cardValues.add("Q");

        cardValues.add("K");

        cardValues.add("A");

 

        cardsColors.add("clubs");

        cardsColors.add("diamonds");

        cardsColors.add("heart");

        cardsColors.add("spades");




        button.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

 

                Random r = new Random();

 

                String randomValue = cardValues.get(r.nextInt(cardValues.size()));

                String randomColor = cardsColors.get(r.nextInt(cardsColors.size()));

 

                textView.setText(randomValue + " of " + randomColor);

 

            }

        });

 

    }

}

 

+ Recent posts