Android プログラミング【 カウントダウンタイマー① 】~ 10秒で終了 ~

Android 開発 カウントダウンタイマー1

カウントダウンタイマーを学習します。
細かく理解するには「Java中級」程度の知識が必要ですが、再現だけならカンタンです。

【 注意事項 】
この動画には問題点もあります。
以下と組み合わせてご覧ください。

カウントダウンタイマー① 10秒で終了
カウントダウンタイマー② 画面切り替え
カウントダウンタイマー③ 画面再表示
カウントダウンタイマー④ 改正版

プロジェクト、カンパニードメイン、パッケージネームを同じにするとコピペエラーが減ります。

Application name MyCountDown
Company Domain test.com
Package name com.test.mycountdown

新規プロジェクト

activity_main_xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.test.mycountdown.MainActivity">

    <TextView
        android:id="@+id/tv"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

MainActivity_java

package com.test.mycountdown;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CountDownTimer countDownTimer = new CountDownTimer(10000, 100) {
            @Override
            public void onTick(long millisUntilFinished) {
                int time = (int)millisUntilFinished /1000;
                ((TextView)findViewById(R.id.tv)).setText("あと" + time + "秒");
            }
            @Override
            public void onFinish() {
                finish();
            }
        }.start();
    }
}

CountDownTimer 図解①

Android プログラミング【 Viewの移動 】~ 疑似モグラたたき ~

Android プログラミング【 Viewの移動 】~ 疑似モグラたたき ~

今回はTextViewを使って、擬似的な「モグラたたき」っぽいアプリを作ってみたいと思います。
入力する行は少ないのですが、初めての命令が多いので、初心者の方は少々戸惑うかもしれません。
Java未経験の方は参考程度に抑えてください。

プロジェクト、カンパニードメイン、パッケージネームを同じにするとコピペエラーが減ります。

Application name CountGame
Company Domain test.com
Package name com.test.countgame

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.test.countgame.MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onTv"
        android:text="0"
        android:textSize="42sp" />

</RelativeLayout>
package com.test.countgame;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    int count = 0;

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

    public void onTv(View v) {
        // カウントの加算
        count++;
        ((TextView) findViewById(R.id.tv)).setText("" + count);

        // 表示位置の変更
        int w = findViewById(R.id.activity_main).getWidth();
        int w1 = (int) (w * 0.8);
        int w2 = new Random().nextInt(w1);
        ((TextView) findViewById(R.id.tv)).setTranslationX(w2);

        int h = findViewById(R.id.activity_main).getHeight();
        int h1 = (int) (h * 0.8);
        int h2 = new Random().nextInt(h1);
        ((TextView) findViewById(R.id.tv)).setTranslationY(h2);
    }

}

Android プログラミング【 AlertDialog 】~ 「戻る」ボタンで表示 ~

Android プログラミング【 AlertDialog 】~ 「戻る」ボタンで表示 ~

プログラミング未経験者対象の【 Androidアプリ作成動画 】です。

この動画は「AlertDialog(アラートダイアログ)の利用」を行ってます。
戻るボタンを押したときにダイアログを表示し、YESを選択するとアプリを終了します。

プロジェクト、カンパニードメイン、パッケージネームを同じにするとコピペエラーが減ります。

Application name MyDialog
Company Domain test.com
Package name com.test.mydialog

alertdialog プロジェクト名

alertdialog-mainactivity2

package com.test.mydialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_BACK:
                new AlertDialog.Builder(this)
                        .setTitle("終了確認")
                        .setMessage("アプリを終了しますか?")
                        .setNegativeButton("NO", null)
                        .setPositiveButton("YES",

                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        finish();
                                    }
                                }

                        )
                        .show();
        }
        return super.onKeyDown(keyCode, event);
    }
}

Android プログラミング【 AlertDialog 】 ~ ダイアログ利用 ~

Android プログラミング【 AlertDialog 】 ~ ダイアログ利用 ~

プロジェクト、カンパニードメイン、パッケージネームを同じにするとコピペエラーが減ります。

Application name MyDialog
Company Domain test.com
Package name com.test.mydialog

alertdialog プロジェクト名

alertdialog-activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.test.mydialog.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

alertdialog-mainactivity2

package com.test.mydialog;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        new AlertDialog.Builder(this)
                .setTitle("背景色")
                .setMessage("背景色を変更しますか?")
                .setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                findViewById(R.id.activity_main).setBackgroundColor(Color.BLUE);
                            }
                        }
                )
                .setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,"了解しました!",Toast.LENGTH_SHORT).show();
                            }
                        }
                )
                .show();

    }
}

Android Studio入門

Android プログラミング【 AlertDialog 】 ~ ダイアログ表示 ~

Android プログラミング【 AlertDialog 】 ~ ダイアログ表示 ~

プロジェクト、カンパニードメイン、パッケージネームを同じにするとコピペエラーが減ります。

Application name MyDialog
Company Domain test.com
Package name com.test.mydialog

alertdialog プロジェクト名

alertdialog-mainactivity

package com.test.mydialog;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        new AlertDialog.Builder(this)
                .setTitle("ああああ")
                .setMessage("いいいいいいいいいいい")
                .setPositiveButton("YES",null)
                .setNegativeButton("NO",null)
                .show();
        
    }
}
Android Studio入門

Android プログラミング【 Preference 】 ~ 保存型カウンター ~

preferences Android カウンターの作成

アプリ開発初心者用の学習動画です。

この動画はpreferences(プリファレンス・データの保存)を使ったカウンターの作成動画です。

前回と前々回を組み合わせたアプリになります。
【 Preference 】 ~ データ保存 ~
【 タッチイベント 】 ~ カウンターの作成 ~

プロジェクト、カンパニードメイン、パッケージネームを同じにするとコピペエラーが減ります。

Application name MyCount
Company Domain test.com
Package name com.test.mycount

Android アプリ サンプル カウンター

サンプルアプリ カウンターactivity_main.xml-(完成)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center"
    tools:context="com.test.mycount.MainActivity">

    <TextView
        android:id="@+id/count"
        android:textSize="60sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

</RelativeLayout>
package com.test.mycount;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // カウントデータ
    int count = 0;

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

        // データの読込
        SharedPreferences preferences = getSharedPreferences("game_data",MODE_PRIVATE);
        count = preferences.getInt("count" , 0);
        ((TextView)findViewById(R.id.count)).setText("" + count);
    }


    ///////////////////////////////////////////////////
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                // カウントの加算
                count++;
                ((TextView)findViewById(R.id.count)).setText("" + count);
        }
        return super.onTouchEvent(event);
    }


    ///////////////////////////////////////////////////
    @Override
    protected void onPause() {
        super.onPause();

        // データの保存
        SharedPreferences preferences = getSharedPreferences("game_data",MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("count",count);
        editor.commit();
    }
}
Android Studio入門

Android プログラミング【 Preference 】 ~ データ保存 ~

preferences Android データの保存

アプリ開発初心者用の学習動画です。

この動画はpreferences(プリファレンス・データの保存)の基礎学習です。

プロジェクト、カンパニードメイン、パッケージネームを同じにするとコピペエラーが減ります。

Application name MyPreference
Company Domain test.com
Package name com.test.mypreference

Preference Android タイトル

以下を適切な箇所にコピペすると、数値を保存できます。


// ファイルの準備
SharedPreferences preferences = getSharedPreferences("game_data",MODE_PRIVATE);

// データの保存
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("count",7);
editor.commit();

以下を適切な場所にコピペすると、数値を読み込みます。

// ファイルの準備
SharedPreferences preferences = getSharedPreferences("game_data",MODE_PRIVATE);

// データの読込
int count = preferences.getInt("count" , 0);

データ保存ーactivity_main.xml-(完成)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center"
    tools:context="com.test.mypreference.MainActivity">

    <TextView
        android:id="@+id/count"
        android:textSize="60sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

</RelativeLayout>

データ保存ーMainActivity.java(完成)

package com.test.mypreference;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        // ファイルの準備
        SharedPreferences preferences = getSharedPreferences("game_data",MODE_PRIVATE);

        // データの保存
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("count",7);
        editor.commit();

        // データの読込
        int count = preferences.getInt("count" , 0);

        // データ表示
        ((TextView)findViewById(R.id.count)).setText("" + count);

    }
}
Android Studio入門

Android プログラミング【 タッチイベント 】 ~ カウンターの作成 ~

アンドロイド 開発入門 カウンター

アプリ開発初心者用の学習動画です。

タッチイベント(onTouchEvent)を使ったカウンターアプリを作成します。

プロジェクト、カンパニードメイン、パッケージネームを同じにするとコピペエラーが減ります。

Application name MyCount
Company Domain test.com
Package name com.test.mycount

Android アプリ サンプル カウンター

サンプルアプリ カウンターactivity_main.xml-(完成)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center"
    tools:context="com.test.mycount.MainActivity">

    <TextView
        android:id="@+id/count"
        android:textSize="60sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

</RelativeLayout>

タッチイベント--~-カウンターの作成-~

package com.test.mycount;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // カウントデータ
    int count = 0;

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

        ((TextView)findViewById(R.id.count)).setText("0");
    }


    ///////////////////////////////////////////////////
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                // カウントの加算
                count++;
                ((TextView)findViewById(R.id.count)).setText("" + count);
        }
        return super.onTouchEvent(event);
    }

}

Android プログラミング【 タッチイベント 】 ~ Switchの利用 ~

Android プログラミング【 タッチイベント 】 ~ Switchの利用 ~
アプリ開発初心者用の学習動画です。

「画面タッチで画像が変わる」を学習しますが、以下の知識が無いと理解は難しいです。

・JavaのSwitchとオブジェクト指向

プロジェクト、カンパニードメイン、パッケージネームを同じにするとコピペエラーが減ります。

Application name MyTouch
Company Domain test.com
Package name com.test.mytouch

画面タッチ_新規作成

* 注意 *
・画像をご自身で要するにはファイル名やファイルサイズ、拡張子に注意する必要があります。
・ファイル名は半角小文字の英数(a~z 0~9)とアンダーバー( _ )のみで、最初の1文字目は半角英字( a ~ z)のみです。
・慣れてない方はサンプル画像の使用をオススメします。

ノーマル画像
ノーマル画像
ダウン時
ダウン時

ムーブ時
ムーブ時

Android 開発 タッチイベント xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.test.mytouch.MainActivity">

    <ImageView
        android:id="@+id/iv"
        android:src="@drawable/img0"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
</RelativeLayout>

package com.test.mytouch;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        ((ImageView)findViewById(R.id.iv)).setImageResource(R.drawable.img1);
        return super.onTouchEvent(event);
    }
}
package com.test.mytouch;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                ((ImageView)findViewById(R.id.iv)).setImageResource(R.drawable.img1);
                break;
            case MotionEvent.ACTION_UP:
                ((ImageView)findViewById(R.id.iv)).setImageResource(R.drawable.img0);
                break;
        }

        return super.onTouchEvent(event);
    }
}

タップの種類2

switch図解

onTouchEvent図解2

switch図解3

package com.test.mytouch;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                ((ImageView)findViewById(R.id.iv)).setImageResource(R.drawable.img1);
                break;
            case MotionEvent.ACTION_UP:
                ((ImageView)findViewById(R.id.iv)).setImageResource(R.drawable.img0);
                break;
            case MotionEvent.ACTION_MOVE:
                ((ImageView)findViewById(R.id.iv)).setImageResource(R.drawable.img2);
                break;
        }

        return super.onTouchEvent(event);
    }

}
Android Studio入門

Android プログラミング【 タッチイベント 】 ~ 画面タッチで画像変更 ~

アプリ入門 【タッチ処理 画像変更】768

アプリ開発初心者用の学習動画です。

今回は画面タッチ時の処理を勉強する動画で、前回の「画面タッチで文字変更」の復習動画です。

※ 前回だけではよくわからなかった人用の復習動画です。

* 注意 *
・画像をご自身で要するにはファイル名やファイルサイズ、拡張子に注意する必要があります。
・ファイル名は半角小文字の英数(a~z 0~9)とアンダーバー( _ )のみで、最初の1文字目は半角英字( a ~ z)のみです。
・慣れてない方はサンプル画像の使用をオススメします。

タッチ前の画像
タッチ前の画像
タッチ後の画像
タッチ後の画像

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.test.mytouch.MainActivity">

    <ImageView
        android:id="@+id/iv"
        android:src="@drawable/gilli0"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>


タッチ処理_プログラミング


package com.test.mytouch;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

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

    // タッチイベント(画面タッチを感知する)
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        // 画像変更
        ((ImageView)findViewById(R.id.iv)).setImageResource(R.drawable.gilli1);

        return super.onTouchEvent(event);
    }
}