SystemClockによるタイムウォッチ

SystemClock.elapsedRealtimeからstartTimeを引いた時間を表示しています。
参考:https://developer.android.com/reference/android/os/SystemClock.html

リフェレンス
handler:A Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it — from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
SimpleDateFormat:is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.

package self.stopwatchapp;

import android.icu.text.SimpleDateFormat;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private long startTime;
    private long elapsedTime = 0l;

    private Handler handler = new Handler();
    private Runnable updateTimer;

    private Button startButton;
    private Button stopButton;
    private Button resetButton;
    private TextView timerLabel;

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


        startButton = (Button) findViewById(R.id.startButton);
        stopButton = (Button) findViewById(R.id.stopButton);
        resetButton = (Button) findViewById(R.id.resetButton);
        timerLabel = (TextView) findViewById(R.id.timerLabel);

        setButtonState(true, false, false);


    }

    public void setButtonState(boolean start, boolean stop, boolean reset){
        startButton.setEnabled(start);
        stopButton.setEnabled(stop);
        resetButton.setEnabled(reset);
    }

    public void startTimer(View view){
        // starttime
        startTime = SystemClock.elapsedRealtime(); //起動からの経過時間

        // 現在の経過時間
        // Handler -> Runnable -> UI
        updateTimer = new Runnable(){
            @Override
            public void run() {
                long t = SystemClock.elapsedRealtime() - startTime + elapsedTime;
                SimpleDateFormat sdf = new SimpleDateFormat("mm:ss.SSS", Locale.US);
                timerLabel.setText(sdf.format(t));
                handler.removeCallbacks(updateTimer);
                handler.postDelayed(updateTimer, 10);
            }
        };
        handler.postDelayed(updateTimer, 10);

        // button
        setButtonState(false, true, false);
    }
    public void stopTimer(View view){
        elapsedTime += SystemClock.elapsedRealtime() - startTime;
        handler.removeCallbacks(updateTimer);
        setButtonState(true, false, true);
    }
    public void resetTimer(View view) {
        elapsedTime = 0l;
        timerLabel.setText("00:00:000");
        setButtonState(true, false, false);
    }

}

LinearLayoutのverticalとhorizontal

LinearLayoutのvertical中に、LinearLayoutのhorizontalで、tableのようなレイアウトを実現しています。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="self.stopwatchapp.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:gravity="center"
        android:textSize="64sp"
        android:textStyle="bold"
        android:id="@+id/timerLabel"
        android:text="00:00.000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_height="0dp">

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="Start"
        android:id="@+id/startButton"
        android:onClick="startTimer"
        android:layout_height="wrap_content"
        />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="Stop"
            android:id="@+id/stopButton"
            android:onClick="stopTimer"
            android:layout_height="wrap_content"
            />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="Reset"
            android:id="@+id/resetButton"
            android:onClick="resetTimer"
            android:layout_height="wrap_content"
            />

    </LinearLayout>
</LinearLayout>

classはbooleanで判定しています。

        startButton = (Button) findViewById(R.id.startButton);
        stopButton = (Button) findViewById(R.id.stopButton);
        resetButton = (Button) findViewById(R.id.resetButton);

        setButtonState(true, false, false);


    }

    public void setButtonState(boolean start, boolean stop, boolean reset){
        startButton.setEnabled(start);
        stopButton.setEnabled(stop);
        resetButton.setEnabled(reset);
    }

AndroidManifest.xml

AndroidManifest.xmlのandroid:parentActivityNameで、Activityを元画面に戻します。

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MyForm">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MyResult"
            android:parentActivityName="self.namescoreapp.MyForm"></activity>
    </application>

また、strings.xmlにtextを記載し、activityのxmlからは@string/で呼び出しています。

<EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/myEditText"
        android:layout_weight="1"
        android:hint="@string/name_hint" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="getScore"
        android:id="@+id/myButton"
        android:text="@string/button_label" />

getIntentとgetStringExtra

Activity間のやりとりは、intentを用いて呼び出します。putExtraで、データを引き渡します。

            Intent intent = new Intent(this, MyResult.class);
            intent.putExtra(EXTRA_MYNAME, myName);
            startActivity(intent);

MyResult.java

        Intent intent = getIntent();
        String myName = intent.getStringExtra(MyForm.EXTRA_MYNAME);
        TextView nameLabel = (TextView) findViewById(R.id.nameLabel);
        nameLabel.setText(myName + "の点数は...");

findViewByIdとgetText().toString()

エラー処理にはsetErrorを使います。その他、ToastやDialogなどがあります。

public void getScore(View view){
        // edit text取得
        EditText myEditText = (EditText)findViewById(R.id.myEditText);
        String myName = myEditText.getText().toString().trim();
        if (myName.equals("")){
        /*
        setError, toast, dialog
         */
         myEditText.setError("Please enter your name!");
        } 

Layout->New->Activityで、新しいActivityを作成できます。
%e7%84%a1%e9%a1%8c

Java new Random()によるおみくじ

public void getOmikuji(View view) {
        // TextViewの取得
        TextView tv = (TextView) findViewById(R.id.myTextView);
        String[] results = {
                "大吉",
                "吉",
                "凶"
        };
        // 乱数の生成
        Random randomGenerator = new Random();
        int num = randomGenerator.nextInt(results.length); // 0-2
        // 結果の表示
        //        String result = Integer.toString(num);
        tv.setText(results[num]);
    }

テーマの変更
rsc -> values -> styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

activity_main.xmlはstrings.xmlへの記載することが推奨されています。
rsc -> values -> strings.xml

<resources>
    <string name="app_name">おみくじアプリ</string>

    <string name="omikuji_label">あなたの運勢は...</string>
    <string name="score_label">____</string>
    <string name="button_label">おみくじを引く</string>
</resources>

Android Studioが重すぎるとき


上の画面を作るのに6時間程度費やしました。
さて、studioが重すぎる時の対応案です。コンパイラであるgradleに起因している可能性があります。
https://developer.android.com/studio/build/index.html

1.Prgram Files > Android > Android Studio > bin > studio64.exe.vmpoptions
編集
※アクセス権がない場合は、アクセス権を変更してください。

-Xms1024m
-Xmx2048m
-XX:MaxPermSize=512m

2. c:\users\name\AndroidStudioProjects\MyAppnameApp\gradle.property 
コメントアウト・追記

org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.configureondemand=true

3.settingでgradleをオフラインモードに変更

4.Setting -> Editor -> General -> Code Completion
autopop系は全てチェックボックスを外す

コンパイルの時間やIDEの操作がモチベーションを左右しますね。

android table layout

dpとはdensity-independent pixelsの略です。
リフェレンス:https://developer.android.com/guide/practices/screens_support.html

match_parent: 親要素の幅
wrap_content: 中身の幅
dp:任意の幅

android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_width="160dp"
android:layout_height="160dp"

余白
android:layout_margin
android:padding
android:gravity, layout_gravity

android:layout_marginBottom="80dp"
android:layout_marginLeft="40dp"
android:paddingTop="10dp"
android:gravity="bottom"
android:layout_gravity="right"

android:layout_weight

android:layout_weight="1"

使用例

    <Button
        android:layout_weight="1"
        android:text="hello_world"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:id="@+id/button2" />

    <Button
        android:layout_weight="1"
        android:text="hello_world"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:id="@+id/button2" />

    <Button
        android:layout_weight="1"
        android:text="hello_world"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:id="@+id/button2" />

RelativeLayout

android:id="@+id/a"
android:layout_centerInParent="true"

android:id="@+id/b"
android:text="b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/a"
android:layout_below="@+id/a"

FrameLayout

    <ImageView
        android:text="a"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#f8bbd0"
        android:layout_centerInParent="true"
        />

    <ImageView
        android:text="a"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#448aff"
        android:layout_centerInParent="true"
        />

    <ImageView
        android:text="a"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#c2185b"
        android:layout_centerInParent="true"
&#91;/xml&#93;

table layout
&#91;xml&#93;
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="1"></Button>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="1"></Button>
    </TableRow>
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="1"></Button>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:text="1"></Button>
    </TableRow>
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="1"></Button>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="1"></Button>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="1"></Button>
    </TableRow>

Android LinearlayoutとRelativeLayout

Paletteでview要素を追加、Component treeからpropertiesを選択し、調整していきます。
%e7%84%a1%e9%a1%8c

activity_main.xmlでrelative.layoutをLinerLayoutに変更します。
LinearLayout(Viewを縦もしくは横の一方向に順番に配置)
RelativeLayout(View同士の位置関係から相対的に配置)

android:orientation=”horizontal”と、android:orientation=”vertical”でレイアウト順序を変更できます。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
    >

    <Button
        android:text="hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2" />

    <Button
        android:text="hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2" />

    <Button
        android:text="hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2" />

</LinearLayout>

sql commands vol.2

.headers on
.mode column
update users set score = 0, name ='* ' || name where score < 60;
delete from users where score = 0;
select * from users;
&#91;/sql&#93;

transaction
&#91;sql&#93;
begin transaction;
update users set score = score - 10 where name = 'sato';
update users set score = score + 10 where name = 'kimura';
commit;
select * from users;
rollback;
&#91;/sql&#93;

trigger
&#91;sql&#93;
.headers on
.mode column
create table messages (message);

create trigger new_winner update of score on users when new.score > 100
  begin
   insert into messages (message) values (
     'name: ' || new name ||
     ' ' || old. score ||
     ' ->' || new.score
   );
   end;

update users set score = score + 30;
select * from messages;

index

create index score_index on users(score);
create unique index name_index on users(name);

.indices users
.schema users

drop index if exists score_index;
drop index if exists name_index;

日時

select datetime('now', '+09:00:00');
select date('2016-12-01', '+3 months', 'start of month', '-1 day');

create table if not exists users (
  id integer primary key,
  name varchar(255) not null,
  score integer default 10 check(score >= 0),
  team text,
  created datetime default (datetime('now', '+09:00:00'))
);

複数テーブルの作成

drop table if exists posts;
create table if not exists posts (
  id integer primary key,
  title text,
  body text,
  created datetime default (datetime('now', '+09:00:00'))
);

insert into posts (id, title, body) values (1, 't1', 'b1');
insert into posts (id, title, body) values (2, 't2', 'b2');

drop table if exists comments;
create table if not exists comments (
    id integer primary key,
    post_id integer,
    comment text,
    created datetime default (datetime('now', '+09:00:00'))
  );
  insert into comments (id, post_id, comment) values (1, 1, 'c1');
  insert into comments (id, post_id, comment) values (2, 1, 'c2');

.headers on
.mode column

select * from posts;
select * from comments;

内部結合 inner join、外部結合 left outer join、交差結合 cross join

select * from posts inner join comments on posts.id = comments.post_id;
select * from posts join comments on posts.id = comments.post_id;
select posts.id, posts.title, comment from posts join comments on posts.id = comments.post_id;
select * from posts left outer join comments on posts.id = comments.post_id;
select * from posts cross join comments;

row id ※integer primary key -> row id

select rowid, * from users;

dump

.output users.dump.sql
.dump users

外部データの取り込み

drop table if exists users;
create table users (
  id integer primary key,
  name,
  score
);
insert into users (name, score) values ('a', 10);
insert into users (name, score) values ('b', 20);
insert into users (name, score) values ('c', 30);

create table temp(name, score);

.mode csv
.import users.csv temp

insert into users(name, score) select name, score from temp;
select * from users;

drop table temp;

.mode csv
.output users_out.csv
select * from users;