2015/04/28

WebViewのテキスト選択を無効にする

Javascriptでも無効にできるみたいですがJavaのコードで無効にしてみました。
WebView webView = new WebView(this);
webView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        return true;
    }
});
webView.setLongClickable(false);

2015/04/24

Preferenceのマテリアルデザイン

AppCompat_v7 ver.21以上を導入しマテリアルデザインにしてプリファレンスにtoolbarを使う場合、
継承をActionBarActivityにしてtoolbarやFragmentを含んだlayoutをsetContentViewする。

継承がPreferenceActivityだとマテリアルデザインのチェックボックスの色などを変更した
スタイルが反映されなかった。

追記:ver.22からActionBarActivityが非奨励になりAppCompatActivityというものが導入されました。 そのまま置き換えたところ問題なく動きました。

preference_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        layout="@layout/settings_toolbar" />

    <fragment
        android:id="@+id/settings_fragment"
        android:name="com.sample.PrefFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar" />

</RelativeLayout>


settings_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:navigationContentDescription="@string/abc_action_bar_up_description"
    android:background="?attr/colorPrimary"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:title="@string/menu_setting" />


Preference.java
public class Preference extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preference_layout);

        //ツールバーを表示
        Toolbar actionbar = (Toolbar) findViewById(R.id.toolbar);
        actionbar.setTitle(getString(R.string.menu_setting));
        actionbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

2015/04/15

There are unrecoverable errors which must be corrected first.

Google Play services をインストールしていないと出る模様。

SDK Managerを起動させて、Eclipseのときと同じように
Extrasにある Google Play services にチェックしてインストールする。

ただ、Eclipseで使用したものと同じものをインストールすることになるので
Android Studioで使用するAndroid SDKをEclipseで使用していた場所に
変更することにした。

変更方法
[ファイル]⇒[Other Settings]⇒[Default Project Structure]で
Android SDK locationを変更する。

また、この変更によってEclipseで作ったGoogle Play servicesのライブラリを使用している
Android projectのインポートがすんなりできた。

2015/04/10

Android Studioで一括インデント

Eclipseではコードを選択して、Ctrl + I でしたが、
Android Studioでは Ctlr + Alt + L になります。
確認等のダイアログがでますが次回からは表示しないにチェックを入れました。