2017年7月20日木曜日

色の候補

アプリで使う色でベースカラーは決まってるけど、少し明るい色とか、少し暗い色などベースカラーを基に色を決める時に便利だったサイトをメモ

paletton

http://paletton.com/

色を指定するだけで候補を出してくれるので便利!


2017年7月14日金曜日

チャットっぽいレイアウトのメモ

とあるアプリのいち機能としてコメントの投稿機能を作ろうとしてチャットっぽい画面を作ったのですが、用途に合わずお蔵入り
折角レイアウトを作ったのでメモとして書き残しておきます




チャット部分はRecyclerViewにして、そのAdapterを用意します

ChatAdapter
//重要なところだけで、その他は省略


    @Override
    public ChatAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        switch (i) {
            case VIEW_TYPE_LEFT:
                return new ViewHolder(mInflater.inflate(R.layout.left, viewGroup, false));
            case VIEW_TYPE_RIGHT:
                return new ViewHolder(mInflater.inflate(R.layout.right, viewGroup, false));
            default:
                return new ViewHolder(mInflater.inflate(R.layout.left, viewGroup, false));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mItems.get(position).isShowLeft) {
            return VIEW_TYPE_LEFT;
        } else {
            return VIEW_TYPE_RIGHT;
        }
    }

getItemViewTypeで表示の位置を判定して、それに伴ってonCreateViewHolderで使うlayoutファイルを切替えています

left.xmlでは左に寄った表示レイアウト
right.xmlでは右によった表示レイアウト
これらを用意するだけで画像のようなレイアウトになります


left.xml(right.xmlもほぼ同じなので省略)
※XMLをSyntaxHighlighterで表示しようとしたら、崩れてグチャグチャになっちゃったので、そのままコードを貼ってみた

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="42dp"
        android:text="なまえ"
        android:textColor="@color/lite_gray"
        android:textSize="@dimen/text_small" />

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/user_name"
        android:layout_below="@+id/user_name"
        android:background="@color/lite_gray"
        android:paddingBottom="@dimen/space_small"
        android:paddingLeft="@dimen/space_x_small"
        android:paddingRight="@dimen/space_x_small"
        android:paddingTop="@dimen/space_small"
        android:text="Left message.\nhogehoeg" />


    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/user_image"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_alignBaseline="@+id/message"
        android:layout_marginLeft="@dimen/space_x_small"
        android:clickable="true"
        android:src="@mipmap/ic_launcher"
        app:civ_border_color="#FFFFFF"
        app:civ_border_width="1dp" />

    <TextView
        android:id="@+id/send_at"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/message"
        android:layout_below="@+id/message"
        android:text="7/2 11:00"
        android:textColor="@color/lite_gray"
        android:textSize="@dimen/text_small" />

</RelativeLayout>

2016年12月27日火曜日

場所を選択するUIで地図を表示する必要があり、DialogFragmentでMapFragmentを設定した際に、1回目の表示の時は問題ないけど、2回目に表示しようするとDuplicate idのエラーが発生した


 Duplicate id 0x7f0e00d9, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment


1回目の時にDialogをdismissしてもMapFragmentはクリアされてないらしい
そのため2回目でDuplicateとなってしまっている


結論として、DialogFragmentのdismissをオーバーライドしてmapのFragmentをremoveすることで対応した



<fragment android:id="@+id/map" class="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="250dp" />


@Overridepublic void onDismiss(DialogInterface dialog) {
  super.onDismiss(dialog);
  if (getActivity() != null) {
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.map);
    if (fragment != null) {
      getActivity().getSupportFragmentManager().beginTransaction().remove(fragment).commit();
    }
  }
}


同様の問題がDialogFragmentでなくFragmentで起こる場合には、onDestoroyView()の中で同じようにMapFragmentを削除してやれば問題なく動作すると思う

2016年5月6日金曜日

GoogleApiClientでerrorCode=2が出る

お客様の端末を借りてテストをしてみたらGoogleApiClientを使うと位置情報の取得を開始しておらず「あれ?」と思ったらonConnextionFaildに処理が来ててerrorCodeが2になっていました

調べてみると「SERVICE_VERSION_UPDATE_REQUIRED」とのこと

参考: Google APIs for Android#ConnectionResult


どうやら端末のGooglePlayServicesが古いようですね
GooglePlay開発者サービスをバージョンアップすることで問題解決


こういうのって、利用者にエラー表示すると伝わるんだろ???

「GooglePlay開発者サービスが古いのでバージョンアップしてください」とダイアログでも出せばいいのかな?


「GooglePlay開発者サービスってなに?」ってなりそうだな・・・

2015年3月28日土曜日

Androidで文字の大きさを指定する際はdpではなくspで指定するのは当然なのですが、利用する場所にあわせてdpを使うことも検討しましょう

spを指定した場合、画面の解像度やユーザーのフォントサイズ設定によって文字の大きさが決められます
そのためレイアウトが大きく崩れる場合はdpを使うのも良いと思います
もちろんユーザーの指定にあわせてレイアウトが崩れないようにするのがベストですけどね


今回、驚いたのは父親がスマートフォンに変えたということで電話帳の文字が小さいので大きくしてくれと言われました
そこで、設定からフォントサイズを中→特大に変更したのですが、電話帳の文字は変化することなく小さいままでした
きっとspではなくてdpで指定しているんでしょうね
こういうところはspで指定しないとダメです

使い所に合わせてdpを検討するとしても基本はspなので注意した方がいいですね


2015年1月21日水曜日

製作のお手伝いをさせて頂いたアプリの紹介をした際にGooglePlayからダウンロードする為のリンクを作ろうと探したらGoogleがGeneratorを用意していたので「これは便利だな」と思ったのでご紹介

すでにご存知の方の方が多いと思いますが・・・ 

利用方法


http://developer.android.com/distribute/tools/promote/badges.htmlにアクセス

そうするとGeneratorの画面が表示されます



ボタンを押すとこんなリンクのHTMLが出てくるのでダウンロードボタンを配置したいサイトにコピペするだけ


テスト用の画像も表示されているので、すぐに動作を試せます

とても簡単ですね

以上、ダウンロード画像の作成方法の紹介でした


2014年10月23日木曜日

簡単だと思っていたのに、結構ハマったのでメモとして残しておきます

参考にしたのは、次の2つの記事です




重要なのは2点だけ
  1. GridViewの行になっているレイアウトのbackgroundに?android:attr/activatedBackgroundIndicatorを設定する
  2. activatedBackgroundIndicatorをThemeでオリジナルのものに変更する
ただし、APIレベル11以降のお話です

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="?android:attr/activatedBackgroundIndicator"
>
<TextView
 android:layout_width="fill_parent"
 android:layout_height="40dip"
 android:gravity="center"/>
</LinearLayout>


Theme(style.xml)

<style name="AppTheme" parent="AppBaseTheme">
      <item name="android:activatedBackgroundIndicator">@drawable/grid_background</item>
</style>


grid_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@color/green" />
    <item android:state_selected="true" android:drawable="@color/green" />
    <item android:state_pressed="true" android:drawable="@color/green" />
    <item android:state_checked="true" android:drawable="@color/green" />
    <item android:drawable="@color/blue" />
</selector>


これはメモなので、詳細は上にあげたサイトを見るのが良いと思います