Fix vectors for drawableStart in API 17

This commit is contained in:
Mikel 2017-08-07 20:44:35 +01:00
parent 04ff70fa5b
commit 7754c7c7c4
5 changed files with 94 additions and 21 deletions

View file

@ -32,6 +32,7 @@ import fr.free.nrw.commons.MediaWikiImageView;
import fr.free.nrw.commons.PageTitle; import fr.free.nrw.commons.PageTitle;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.ui.widget.CompatTextView;
import fr.free.nrw.commons.utils.UiUtils; import fr.free.nrw.commons.utils.UiUtils;
import timber.log.Timber; import timber.log.Timber;
@ -285,17 +286,10 @@ public class MediaDetailFragment extends Fragment {
private View buildCatLabel(final String catName) { private View buildCatLabel(final String catName) {
final View item = getLayoutInflater(null).inflate(R.layout.detail_category_item, null, false); final View item = getLayoutInflater(null).inflate(R.layout.detail_category_item, null, false);
final TextView textView = (TextView)item.findViewById(R.id.mediaDetailCategoryItemText); final CompatTextView textView = (CompatTextView)item.findViewById(R.id.mediaDetailCategoryItemText);
textView.setText(catName); textView.setText(catName);
if (categoriesLoaded && categoriesPresent) { if (categoriesLoaded && categoriesPresent) {
textView.setCompoundDrawablesWithIntrinsicBounds(
AppCompatResources.getDrawable(
getContext(),
R.drawable.ic_info_outline_white_24dp),
null, null, null);
textView.setCompoundDrawablePadding((int) UiUtils.convertDpToPixel(6, getContext()));
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setOnClickListener(view -> { textView.setOnClickListener(view -> {
String selectedCategoryTitle = "Category:" + catName; String selectedCategoryTitle = "Category:" + catName;
Intent viewIntent = new Intent(); Intent viewIntent = new Intent();

View file

@ -0,0 +1,69 @@
package fr.free.nrw.commons.ui.widget;
/**
* Created by mikel on 07/08/2017.
*/
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.AppCompatDrawableManager;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.utils.UiUtils;
public class CompatTextView extends AppCompatTextView {
public CompatTextView(Context context) {
super(context);
init(null);
}
public CompatTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CompatTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
private void init(@Nullable AttributeSet attrs) {
if (attrs != null) {
Context context = getContext();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompatTextView);
// Obtain DrawableManager used to pull Drawables safely, and check if we're in RTL
AppCompatDrawableManager dm = AppCompatDrawableManager.get();
boolean rtl = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
// Grab the compat drawable padding from the XML
float drawablePadding = a.getDimension(R.styleable.CompatTextView_drawablePadding, 0);
// Grab the compat drawable resources from the XML
int startDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableStart, 0);
int topDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableTop, 0);
int endDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableEnd, 0);
int bottomDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableBottom, 0);
// Load the used drawables, falling back to whatever may be set in an "android:" namespace attribute
Drawable[] currentDrawables = getCompoundDrawables();
Drawable left = startDrawableRes != 0 ? dm.getDrawable(context, startDrawableRes) : currentDrawables[0];
Drawable right = endDrawableRes != 0 ? dm.getDrawable(context, endDrawableRes) : currentDrawables[1];
Drawable top = topDrawableRes != 0 ? dm.getDrawable(context, topDrawableRes) : currentDrawables[2];
Drawable bottom = bottomDrawableRes != 0 ? dm.getDrawable(context, bottomDrawableRes) : currentDrawables[3];
// Account for RTL and apply the compound Drawables
Drawable start = rtl ? right : left;
Drawable end = rtl ? left : right;
setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom);
setCompoundDrawablePadding((int) UiUtils.convertDpToPixel(drawablePadding, getContext()));
a.recycle();
}
}
}

View file

@ -1,20 +1,24 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
> >
<TextView <fr.free.nrw.commons.ui.widget.CompatTextView
android:id="@+id/mediaDetailCategoryItemText"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?attr/subBackground"
android:foreground="?attr/selectableItemBackground"
android:gravity="center_vertical"
android:minHeight="48dp" android:minHeight="48dp"
android:padding="12dp" android:padding="12dp"
android:gravity="center_vertical"
android:id="@+id/mediaDetailCategoryItemText"
android:textSize="14sp"
android:textColor="@android:color/white" android:textColor="@android:color/white"
android:background="?attr/subBackground" android:textSize="14sp"
app:drawablePadding="6dp"
app:drawableStart="@drawable/ic_info_outline_white_24dp"
/> />
<fr.free.nrw.commons.media.MediaDetailSpacer <fr.free.nrw.commons.media.MediaDetailSpacer

View file

@ -132,21 +132,20 @@
android:textSize="16sp" android:textSize="16sp"
android:textStyle="bold" /> android:textStyle="bold" />
<TextView <fr.free.nrw.commons.ui.widget.CompatTextView
android:id="@+id/mediaDetailLicense" android:id="@+id/mediaDetailLicense"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="start" android:layout_gravity="start"
android:background="?attr/subBackground" android:background="?attr/subBackground"
android:foreground="?attr/selectableItemBackground" android:foreground="?attr/selectableItemBackground"
android:drawableLeft="@drawable/ic_info_outline_white_24dp"
android:drawablePadding="6dp"
android:drawableStart="@drawable/ic_info_outline_white_24dp"
android:gravity="center_vertical" android:gravity="center_vertical"
android:padding="12dp" android:padding="12dp"
android:text="@string/media_detail_license" android:text="@string/media_detail_license"
android:textColor="@android:color/white" android:textColor="@android:color/white"
android:textSize="14sp" android:textSize="14sp"
app:drawablePadding="6dp"
app:drawableStart="@drawable/ic_info_outline_white_24dp"
tools:text="License link" /> tools:text="License link" />
</LinearLayout> </LinearLayout>
@ -166,21 +165,20 @@
android:textSize="16sp" android:textSize="16sp"
android:textStyle="bold" /> android:textStyle="bold" />
<TextView <fr.free.nrw.commons.ui.widget.CompatTextView
android:id="@+id/mediaDetailCoordinates" android:id="@+id/mediaDetailCoordinates"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="start" android:layout_gravity="start"
android:background="?attr/subBackground" android:background="?attr/subBackground"
android:drawableLeft="@drawable/ic_map_white_24dp"
android:drawablePadding="6dp"
android:drawableStart="@drawable/ic_map_white_24dp"
android:foreground="?attr/selectableItemBackground" android:foreground="?attr/selectableItemBackground"
android:gravity="center_vertical" android:gravity="center_vertical"
android:padding="12dp" android:padding="12dp"
android:text="@string/media_detail_coordinates" android:text="@string/media_detail_coordinates"
android:textColor="@android:color/white" android:textColor="@android:color/white"
android:textSize="14sp" android:textSize="14sp"
app:drawablePadding="6dp"
app:drawableStart="@drawable/ic_map_white_24dp"
tools:text="Coordinates link" /> tools:text="Coordinates link" />
</LinearLayout> </LinearLayout>

View file

@ -13,4 +13,12 @@
<attr name="iconCamera" format="reference"/> <attr name="iconCamera" format="reference"/>
<attr name="iconPhoto" format="reference"/> <attr name="iconPhoto" format="reference"/>
<attr name="iconUndo" format="reference"/> <attr name="iconUndo" format="reference"/>
<declare-styleable name="CompatTextView">
<attr name="drawablePadding" format="dimension"/>
<attr name="drawableStart" format="reference"/>
<attr name="drawableTop" format="reference"/>
<attr name="drawableEnd" format="reference"/>
<attr name="drawableBottom" format="reference"/>
</declare-styleable>
</resources> </resources>