言いたいことはそれだけか

KotlinとかAndroidとかが好きです。調べたことをメモします。٩( 'ω' )و

LollipopでAlertDialogのMultiChoiceItemのcheck buttonの色を変更する

前回に引き続き、色変更したいシリーズです。

やりたいこと

掲題の通り。下記のお悩み解決を目標としました。

android - Changing the color of dialog multichoice items - Stack Overflow

試したこと

まずはcheck boxを表示しているcodeを探してみました。

select_dialog_multichoice_material.xml

     18 <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
     19     android:id="@android:id/text1"
     20     android:layout_width="match_parent"
     21     android:layout_height="wrap_content"
     22     android:minHeight="?android:attr/listPreferredItemHeightSmall"
     23     android:textAppearance="?android:attr/textAppearanceMedium"
     24     android:textColor="?android:attr/textColorAlertDialogListItem"
     25     android:gravity="center_vertical"
     26     android:paddingStart="@dimen/alert_dialog_padding_material"
     27     android:paddingEnd="@dimen/alert_dialog_padding_material"
     28     android:checkMark="?android:attr/listChoiceIndicatorMultiple"
     29     android:checkMarkGravity="start"
     30     android:ellipsize="marquee" />

ここのcheckMarkというattributeに指定している画像がどうやらcheck boxになっているようです。

material themeではここで指定。

<style name="Theme.Material">

    <item name="listChoiceIndicatorMultiple">@drawable/btn_check_material_anim</item>

で、btn_check_material_anim.xmlはこちら。check した時の色はcolorControlActivatedで指定している様子。
http://tools.oesf.biz/android-5.0.1_r1.0/xref/frameworks/base/core/res/res/drawable/btn_check_material_anim.xml

さっそくActivityのthemeにcolorControlActivatedを指定してみたけれど効果なし。
続いてcolorControlActivatedについて調べます。
どうやらsupport libraryではcolorAccentに置き換えられている様子。
ただし、ActivityのthemeにcolorAccentを指定してみたけれどまたまた効果なし。

Cross Reference: /frameworks/support/v7/appcompat/res/values/themes_base.xml

<item name="colorControlActivated">?attr/colorAccent</item>


そういえばDialogってActivityのtheme適用されるんだっけ、と思ってAlertDialogのAPIを確認。
AlertDialog.Builderクラスのconstructorは第2引数にthemeを指定できます。
これでうまくいきました。

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.MyAlertDialogTheme);
    <style name="MyAlertDialogTheme" parent="@android:style/Theme.Material.Light.Dialog">
        <item name="colorAccent">#E91E63</item>
        <item name="android:colorAccent">#E91E63</item>
    </style>

f:id:muumuumuumuu:20150215095518p:plain

LollipopでActionBarのPopup windowの背景色が変更できない

前回の続きです。

LollipopでActionBarの色が変更できない - Let's go and programming!



ActionBarの背景色は変更できました。次はOverflowボタンを押したときに出てくる
Popup windowの背景色を変更してみます。

popupMenuStyleやpopupBackground、actionDropDownStyleなどを
Themeで指定してみたけれど、全く効果なし。

Lから変わってしまった?ならばLのFrameworkのコードを見てみましょう。
Popup windowの背景画像を指定しているのはここでしょうか。

Cross Reference: /frameworks/base/core/res/res/drawable/popup_background_material.xml

     17 <shape xmlns:android="http://schemas.android.com/apk/res/android"
     18        android:shape="rectangle">
     19 
     20     <corners
     21             android:radius="2dp" />
     22     <solid
     23             android:color="?attr/colorBackground" />
     24 
     25 </shape>

colorBackgroundを色に指定しています。
ちなみに、このAttributeを変更してしまうとApplication全体の背景色が変わってしまいます。
気持ち悪い...笑

f:id:muumuumuumuu:20150128082940p:plain

では背景画像にpopup_background_material.xmlをセットしていることろをoverrideすることはできるのでしょうか?背景画像を指定しているstyleはこれ。

    555     <style name="Widget.Material.AutoCompleteTextView" parent="Widget.AutoCompleteTextView">
    556         <item name="dropDownSelector">?attr/listChoiceBackgroundIndicator</item>
    557         <item name="popupBackground">@drawable/popup_background_material</item>
    558         <item name="popupElevation">@dimen/floating_window_z</item>
    559     </style>


ではこのstyleを使用しているところを見てみましょう。

     36     public AppCompatPopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
     37         super(context, attrs, defStyleAttr);
     38 
     39         TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs,
     40                 R.styleable.PopupWindow, defStyleAttr, 0);
     41         mOverlapAnchor = a.getBoolean(R.styleable.PopupWindow_overlapAnchor, false);
     42         // We re-set this for tinting purposes
     43         setBackgroundDrawable(a.getDrawable(R.styleable.PopupWindow_android_popupBackground));
     44         a.recycle();
     45     }


上で指定したpopupBackgroundを、コード上から直接指定しています。
ここのRはsupport libraryのリソースです。
ということは各アプリから変更できないのでは...
(support libraryではない本体側もcom.android.internal.Rから同attributeを参照しています。)

誰かできた人いるのかな?

city, state, country の違い、説明できますか?

通勤中、Fergieのこの曲を聴きながらふと思ったので調べてみました。

Fergie - L.A.LOVE (la la) ft. YG - YouTube

もちろん市/州/国なんだろうけど、これらの違いをはっきり説明できるかというと微妙。
いつもお世話になっているWeblioの英英辞典に聞いてみよう!

city

A large settlement, bigger than a town.
町よりちょっと大きのがcity。規模しかふれられていません。

state

A political division of a federation retaining a degree of autonomy, for example one of the fifty United States
stateになると自治が可能かどうかがポイントになってきます。(だから日本には州がないんですね。)

country

A set region of land having particular human occupation or agreed limits, especially inhabited by members of the same race, language speakers etc., or associated with a given person, occupation, species etc.
countryはそのまま国家ですね。同じ人に治められ、同じ制限を負った土地の集合。

おまけ

都道府県を表すprefectureも調べてみました。

prefecture

The jurisdiction of a prefect; the region administered by a prefect, especially as a translation of certain French and Japanese administrative divisions.

知事によって監督されている地域って感じでしょうか。なんと翻訳においてはフランスと日本の管理区域として特に使われるようです。へー!そういえばあんまり見かけたことない。

中国のなんとか省はどう英語で書くのかな?と思いましたが、どうやら何もつけないようです。
http://topren.net/travel/necessity/hotels/hotel_fujian.html
(Fujian = 福建省)

LollipopでActionBarの色が変更できない

掲題の問題にはまってので、解決するまでのメモ。


やりたいこと

ThemeおよびStyleを使用してActionBarの色を変更する。
下記サイトの現象が再現するか試してみたかった。


試したこと

まずはApplicationにMyAppThemeを設定。

    <!-- Base application theme. -->
    <style name="MyAppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="android:actionBarStyle">@style/MyActionbarStyle</item>
    </style>

    <style name="MyActionbarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>
        <item name="android:textColor">@android:color/white</item>
    </style>

これで色は変わらず。

次に考えたのは、Emulator環境はNexus 5だったので、Material designに対応していないことが原因では?ということ。
https://developer.android.com/training/material/theme.html
(こちらのCustomize the Status Barの項参照。)
res/配下にvalues-v21フォルダを作成してstyles.xmlを追加します。

    <style name="MyAppTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:colorPrimary">@color/primary</item>
    </style>

これでも色は変わらず。

何が悪かったかというと、ThemeがTheme.AppCompatを継承していること。
AndroidStudioでBlankなActivityをつくると勝手にActionBarActivityを継承しており、
Theme.AppCompatを使う必要があるように見える。
Theme.AppCompatはSupport Libraryであり、Android Frameworkのattributeを設定しても効いてくれない。
というわけで、下記のように書くのが正解。

    <style name="MyAppTheme" parent="@style/Theme.AppCompat.Light">
        <item name="colorPrimary">#3F51B5</item>
        <item name="colorPrimaryDark">#303F9F</item>
        <item name="colorAccent">#E91E63</item>
    </style>

先日他人が書いていたcodeでPriorityQueueの使用方法がおかしかったので、ちょっと気になって調べてみました。

誤った使い方としてはこんな感じ。

for (Hoge hoge : mHogePriorityQueue) {

Priorityが高い順にfor文を回していることを期待するようなコードでしたが、
実際にはaddした順に要素がとれているようでした。

ではadd()がコールされた時に何が起こっているのか?

Cross Reference: /libcore/luni/src/main/java/java/util/PriorityQueue.java

PriorityQueueはelementsというメンバの配列を持っており、これはpriorityに応じた配列になっているようです。

一方、PriorityQueue自体を配列として扱おうとする場合、親クラスのtoArray()が適用されるためPriorityQueueが持っている優先度は無視されてしまうという仕組みでした。