Trouble shooting

android studio strouble shooting
https://docs.google.com/document/d/1w1Xn_hnSAODAAtdRDp7haYPBtEwX_l7Htpf8Wpgbu6w/pub?embedded=true

Linear layout

<?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.xxx.myapplication.MainActivity">

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

Relative Layout

top edge, left edge, right edge, buttom edge
relative to parent

android:layout_alignParentTop=”true”
android:layout_alignParentBottom=”false”
android:layout_alignParentLeft=”true”
android:layout_alignParentRight=”true”

<RelativeLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:padding="16dp">

	<TextView
		android:text="I'm in this corner"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:layout_alignParentBottom="true"
		android:layout_alignParentLeft="true" />

    <TextView
        android:text="No, up here"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" />
 
    <TextView
        android:text="Wait, I’m here"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" />
 
    <TextView
        android:text="Actually, I’m here"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true" />

</RelativeLayout>

Simple image view

<ImageView
	android:src="@drawable/cake"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:scaleType="center"/>
<ImageView
	android:src="@drawable/cake"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:scaleType="centerCrop"/>

textView android, use google search
https://developer.android.com/reference/android/widget/TextView.html

capitalize

<TextView
	android:text="Oh the possibilites of TextView"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:textSize="36sp"
	android:textAllCaps="false"/>

LinearLayout

<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">

<TextView
	android:text="I'm a lonely TextView"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content" />

<TextView
	android:text="I'm a lonely TextView"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content" />

</LinearLayout>

android orientation
https://developer.android.com/reference/android/widget/LinearLayout.html

200dp, wrap_content, match_parent

wrap_content

<TextView
	android:text="wait, today's your birthday?"
	android:background="@android:color/darker_gray"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content" />

font size

<TextView
	android:text="wait, today's your birthday?"
	android:background="@android:color/darker_gray"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:textSize="56sp" />

appearance

<TextView
	android:text="wait, today's your birthday?"
	android:background="@android:color/darker_gray"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:textAppearance="?android:textAppearanceSmall" />

google design spec style color
https://material.io/guidelines/style/color.html

text color and background color

<TextView
	android:text="wait, today's your birthday?"
	android:background="#ECEFF1"
	android:textColor="#616161"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:textAppearance="?android:textAppearanceSmall" />

Using a TextView

<TextView
	android:text="Happy Birthday!"
	android:background="@android:color/darker_gray"
	android:layout_width="150dp"
	android:layout_height="75dp" />

-weird angle brackets
-don’t know what “android:text” means
-says Happy Birthday which appears on phone

XML Syntax

<LinearLayout
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:orientation="vertical">

	<TextView
		android:text="Happy Birthday"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />

	<TextView
		android:text="You're the best!"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />

</LinearLayout>

Attribute Name, Attribute value in “Quotations”
TextView has default values if you’re ok with default values, don’t set them here.

Android Fragment

Fragmentは、Activity でのユーザー インターフェースの挙動や部位を表すものです。 1 つのアクティビティに複数のフラグメントを組み合わせてマルチペインの UI をビルドしたり、複数のアクティビティでフラグメントを再利用したりできます。

TitlesFragment.java

package self.myfragmentapp;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;


public class TitlesFragment extends ListFragment {

    public TitlesFragment() {}

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(
                getActivity(),
                android.R.layout.simple_list_item_1,
                News.Titles
        ));
    }
}

sqliteの設定

ContractとOpenHelperのクラスを作成します。OepnHelperはSQLiteOpenHelperを継承します。
New->Other->ContentProviderから新規にcomponentを作成し、呼び出します。authorityは、packageとContentProvider名です。(self.mymemoapp.MemeoContentProvider)

package self.mymemoapp;

import android.provider.BaseColumns;


public final class MemoContract {

    public MemoContract(){}

    public static abstract class Memos implements BaseColumns {
        public static final String TABLE_NAME = "memos";
        public static final String COL_TITLE = "title";
        public static final String COL_BODY = "body";
        public static final String COL_CREATE = "created";
        public static final String TABLE_UPDATE = "updated";
    }
}
package self.mymemoapp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MemoOpenHelper extends SQLiteOpenHelper {
    public static final String DB_NAME = "myapp.db";
    public static final int DB_VERSION = 1;

    public static final String CREATE_TABLE =
            "create table memos (" +
                "_id integer primary key autoincrement, "+
            "title text, " +
            "body text, " +
            "created datetime default current_timestamp, " +
            "updated datetime default current_timestamp)";

    public static final String INIT_TABLE =
            "insert into memos (title, body) values " +
                    "('t1', 'b1'), " +
                    "('t2', 'b2'), " +
                    "('t3', 'b3'), ";

    public static final String DROP_TABLE =
            "drop table if exists " + MemoContract.Memos.TABLE_NAME;

    public MemoOpenHelper(Context c){
        super(c, DB_NAME, null, DB_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        db.execSQL(CREATE_TABLE);
        db.execSQL(INIT_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        db.execSQL(DROP_TABLE);
        onCreate(db);
    }
}

SQLiteとAndroid

UserOpenHelperでSQLiteOpenHelperを継承します。

package self.mydbapp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by narishige on 2016/11/21.
 */

public class UserOpenHelper extends SQLiteOpenHelper {

    public static final String DB_NAME = "myapp.db";
    public static final int DB_VERSION = 1;
    public static final String CREATE_TABLE =
            "create table " + UserContract.Users.TABLE_NAME + " (" +
                    UserContract.Users._ID + " integer primary key autoincrement," +
                    UserContract.Users.COL_NAME + "name text," +
                    UserContract.Users.COL_SCORE + "score integer)";
    public static final String INIT_TABLE =
            "insert into users (name, score) values " +
                    "('yoshimoto', 55), " +
                    "('kimura', 45), " +
                    "('igarashi', 82)";
    public static final String DROP_TABLE =
            "drop table if exists users";

    public UserOpenHelper(Context c){
        super(c, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        // create table
        sqLiteDatabase.execSQL(CREATE_TABLE);
        // init table
        sqLiteDatabase.execSQL(INIT_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        // drop table
        sqLiteDatabase.execSQL(DROP_TABLE);
        // onCreate
        onCreate(sqLiteDatabase);
    }
}

insert, delete, update

        UserOpenHelper userOpenHelper = new UserOpenHelper(this);
        SQLiteDatabase db = userOpenHelper.getWritableDatabase();
        // open db
        ContentValues newUser = new ContentValues();
        newUser.put(UserContract.Users.COL_NAME, "tanaka");
        newUser.put(UserContract.Users.COL_SCORE, "42");
        long newID = db.insert(
          UserContract.Users.TABLE_NAME,
                null,
                newUser
        );
        
        ContentValues newScore = new ContentValues();
        newScore.put(UserContract.Users.COL_SCORE, 100);
        int updatedCount = db.update(
                UserContract.Users.TABLE_NAME,
                newScore,
                UserContract.Users.COL_NAME + " = ?",
                new String[] {"sakai"}
        );
        int deletedCount = db.delete(
          UserContract.Users.TABLE_NAME,
                UserContract.Users.COL_NAME + " = ?",
                new String[] {"sakai"}
        );
        
        Cursor c = null;
        c = db.query(
                UserContract.Users.TABLE_NAME,
                null, // fields
                null, // where
                null, // where arg
                null, // groupby
                null,
                null
        );

トランザクション

 try {
            db.beginTransaction();
            db.execSQL("update users " +
                    "set score = score + 10 " +
                    "where name = 'kaneko'");
            db.execSQL("update users " +
                    "set score = score + 10 " +
                    "where name = 'muraoka'");
        } catch(SQLException e){
            e.printStackTrace();
        } finally {
            db.endTransaction();
        }

ブラウザ

%e7%84%a1%e9%a1%8c

package self.mybrowser;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private WebView myWebView;
    private EditText urlText;

    private static final String INITIAL_WEBSITE = "http://finance.yahoo.co.jp";



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

        myWebView = (WebView) findViewById(R.id.myWebView);
        urlText = (EditText) findViewById(R.id.urlText);

        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new WebViewClient(){
            @Override
                    public void onPageFinished(WebView view, String url){
                getSupportActionBar().setSubtitle(view.getTitle());
                urlText.setText(url);
            }
        });
        myWebView.loadUrl(INITIAL_WEBSITE);
    }

    public void clearUrl(View view){
        urlText.setText("");
    }

    public void showWebSite(View view){
        String url = urlText.getText().toString().trim();

        if (!Patterns.WEB_URL.matcher(url).matches()){
            urlText.setError("Invalid URL");
        } else {
            if(url.startsWith("http;//") && !url.startsWith("https://")){
                url = "http://" + url;
            }
            myWebView.loadUrl(url);
        }
    }

    @Override
    public void onBackPressed(){
        if (myWebView.canGoBack()){
            myWebView.goBack();
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if(myWebView != null){
            myWebView.stopLoading();
            myWebView.setWebViewClient(null);
            myWebView.destroy();
        }
        myWebView = null;
    }
}
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="self.mybrowserapp.MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_weight="1"
            android:id="@+id/urlText"
            android:layout_width="0dp"
            android:onClick="clearUrl"
            android:inputType="text|textNoSuggestions"
            android:layout_height="wrap_content"/>

        <Button
            android:text="Browse"
            android:onClick="showWebsite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <WebView
        android:layout_weight="1"
        android:id="@+id/myWebView"
        android:layout_width="match_parent"
        android:layout_height="0dp"></WebView>

</LinearLayout>

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);
    }

}