2015/11/14

ActionBar (ToolBar) の影を消す

getSupportActionBar().setElevation(0); や
styleの<item name="android:windowContentOverlay">@null</item>で消えない場合、
activity_main.xmlのandroid.support.design.widget.AppBarLayoutに
app:elevation="0dp"を入れたら消えた。


<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:elevation="0dp">




2015/10/06

ChromeでGmailやYouTubeなどGoogleのサービスが重いときの対処方法

最近ChromeからGmailやYouTubeなどGoogleのサービスが重かったり繋がらなかったりすることが多かったのでいろいろ調べた結果、chrome://flags/ からQUICプロトコルを無効にしたらあっさり解決した。

ChromeでのGoogleサービスが遅い - Google プロダクト フォーラム
https://productforums.google.com/forum/#!topic/chrome-ja/HCoHi6O0j-0


Google、高速プロトコル「QUIC」をChromeとモバイルのデフォルトに - ITmedia ニュース
http://www.itmedia.co.jp/news/articles/1504/20/news046.html

どうやらアップデートでQUICというプロトコルに勝手に切り替わっていたようです。

高速どころか遅くなったり繋がらなくなったりしたんですが…
自動アップデートだとなかなか気づかないよなあ。

2015/09/01

Activityの二重起動を防止する

Activityを起動するようにしてあるボタンを連打したとき、Activityが2つぐらい開いてしまうのを防ぎたい。

AndroidManifestで起動するactivity内に
android:launchMode="singleTask"
を追加する。

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 になります。
確認等のダイアログがでますが次回からは表示しないにチェックを入れました。

2015/02/19

DialogFragmentの二重起動を防止する

if (getSupportFragmentManager().findFragmentByTag("dialog") == null) { 
 DialogFragment fragment = new CustomDialog();
 Bundle bundle = new Bundle();
 bundle.putInt("data", data);
 fragment.setArguments(bundle);
 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
 fragment.show(ft, "dialog");
}
support.v4を使わないときはgetSupportFragmentManager()をgetFragmentManager()にする。

2015/02/04

FragmentDialogでカスタムレイアウトを使用した場合、タブレットのlandscapeで表示すると通常の表示の上に回転されたレイアウトが重なって表示される症状

この症状は親レイアウトのwidthとheightが同じ値の場合で、子レイアウトにScrollViewを使用してlayout_heightがmatch_parentのときに発生した。
親レイアウトのwidthをheightより大きくすると症状はでなくなった。

スマホではこの症状にならなかったが念のため横幅を大きくしておいた方がいいかもしれない。

タブレットはNexus7(2012) kitkat 4.4.4
lollipopにはまだアプデしてないので試していない。

2015/02/01

PreferenceFragmentでDialogFragmentを使う

始めはDialogFragmentを別ファイルにしたが、showのところでエラーがでてしまいうまくいかなかっので PreferenceFragmentクラスの中にDialogFragmentクラスを書いたらうまくいった。

DialogFragmentはstaticにする必要がある。

public class SampleFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  addPreferencesFromResource(R.xml.pref);


  PreferenceScreen pref1 = (PreferenceScreen) findPreference("prefkey");
  pref1.setOnPreferenceClickListener(new OnPreferenceClickListener() {
   @Override
   public boolean onPreferenceClick(Preference arg0) {  

    //ダイアログを表示
    DialogFragment fragment = new SampleDialogFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("type", 0);
    fragment.setArguments(bundle);
    FragmentTransaction ft = getFragmentManager().beginTransaction();      
    fragment.show(ft, "dialog");

    return false;
   }
  });

 }


 public static class SampleDialogFragment extends DialogFragment {

  public static DBdialogFragment newInstance(String title, String message) {
   SampleDialogFragmentt frag = new SampleDialogFragment();
   Bundle bundle = new Bundle();
   bundle.putInt("type", 0);
   frag.setArguments(bundle);
   return frag;
  }

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
   final int type = getArguments().getInt("type");
   //処理
  }
 }
}