Androidに入門してみる

Getting Started | Android Developers

This first group, Getting Started, teaches you the bare essentials for Android app development. If you're a new Android app developer, you should complete each of these classes in order.

The Directory Structure of Android Studio

f:id:keiwt:20150703164246p:plain

f:id:keiwt:20150703164501p:plain

f:id:keiwt:20150703164632p:plain

Managing Projects Overview | Android Developers

Create a New Android Studio Project

Blank Activity with Fragment
  • error ~/AndroidStudioProjects/Sunshine/app/src/main/res/layout/fragment_main.xml
Error:(8, 17) Resource id cannot be an empty string (at 'id' with value '@+id/').
android:id="@+id/"
↓
android:id="@+id/XXXXXXXX"

change icon

File→New→Image Assets

Android system architecture

f:id:keiwt:20150703164853p:plain

Android Interfaces | Android Developers

Make Project

fn+Command+F9

gradle

$ ./gradlew tasks

※実行されるタスクはGradle→All tasks

スマホで確認したい

  • Developer Modeにする
Settings→Build numberを7回タップする
USB debuggingをONにする
$ chmod +x gradlew
$ ./gradlew assembleDebug
  • adbのパスを通す
~/Library/Android/sdk/platform-toolsをPATHに設定してください
  • MacBookProとNexus5を接続する
USBはNexus5を購入した時の付属品を使用してください
※コンビニで購入したUSBではPCがNexus5を認識することができません
  • スマホへのアプリのインストール
$ adb install -r app/build/outputs/apk/app-debug-unaligned.apk
$ adb shell am start -n com.example.android.sunshine/com.example.android.sunshine.MainActivity

お持ちのNexus5の画面にHello Worldが表示されればOK

Fragment

A Fragment is a piece of an application's user interface or behavior
that can be placed in an Activity.

Interaction with fragments is done through FragmentManager, can be obtained via Activity.
getFragmentManager() and Fragment.getFragmentManager().  

※UIのパーツです。

The Fragment class can be used many ways to achieve a wide variety of results.
In its core, it represents a particular operation or interface is running within a larger Activity.

A Fragment is closely tied to the Activity it is in, and can not be used apart from one.
Though Fragment defines its own lifecycle, that lifecycle is dependent on its activity:
if the activity is stopped, no fragments inside of it can be started;
when the activity is destroyed, all fragments will be destroyed.  

※フラグメントのライフサイクルはアクティビティに依存します。

All subclasses of Fragment must include a public no-argument constructor.
The framework will often re-instantiate a fragment class when needed,
in particular during state restore, and needs to be able to find this constructor to instantiate it. 

If the no-argument constructor is not available,
a runtime exception will occur in some cases during state restore.  

※フラグメントのサブクラスは引数なしのpublicコンストラクタが必須
※状態を再現する際にコンストラクタが定義していないと実行時例外が発生してしまいます

Layouts

  • FrameLayout

FrameLayout is designed to block out an area on the screen to display a single item.

Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

Child views are drawn in a stack, with the most recently added child on top.
The size of the FrameLayout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits).

Views that are GONE are used for sizing only if setConsiderGoneChildrenWhenMeasuring() is set to true.

※単一のアイテムを表示するレイアウトです
※アプリ起動時のアプリロゴ画面などに使用します

  • LinearLayout
    LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally.

You can specify the layout direction with the android:orientation attribute.

All children of a LinearLayout are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding).

A LinearLayout respects margins between children and the gravity (right, center, or left alignment) of each child.

※縦もしくは横にアイテムを並べる際に使用するレイアウトです。

  • RelativeLayout
    RelativeLayout is a view group that displays child views in relative positions.

The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

A RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested view groups and keep your layout hierarchy flat, which improves performance.

If you find yourself using several nested LinearLayout groups, you may be able to replace them with a single RelativeLayout.

※子ビューを相対的に配置するレイアウトです
※LinearLayoutがネストしている場合はRelativeLayoutにしてください

Adapter

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view.

The Adapter provides access to the data items.
The Adapter is also responsible for making a View for each item in the data set.

※データとビューの仲介者です

  • ArrayAdapter
    public ArrayAdapter (Context context, int resource, int textViewResourceId, List objects)
Parameters
context The current context.
resource    The resource ID for a layout file containing a layout to use when instantiating views.
textViewResourceId  The id of the TextView within the layout resource to be populated
objects The objects to represent in the ListView.
ListAdapter mXXXAdapter = new ArrayAdapter<String>(
    getActivity(),
    R.layout.list_item_XXX,
    R.id.list_item_XXX_textview,
    文字列の配列
);
ListView listView = (ListView) rootView.findViewById(R.id.listview_XXX);
listView.setAdapter(mXXXAdapter);

Android Device Moniter

Android Device Monitor is a stand-alone tool that provides a graphical user interface for several Android application debugging and analysis tools.

※クラッシュの原因はpidでログをフィルタリングすれば分かります