Skip to content

Commit e7c7839

Browse files
author
zedoref
committed
Bootstrap Drop Down implementation
1 parent ebd42d2 commit e7c7839

14 files changed

Lines changed: 770 additions & 4 deletions

File tree

AndroidBootstrap/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ android {
1515

1616
dependencies {
1717
compile 'com.android.support:support-annotations:23.1.1'
18+
compile 'com.android.support:support-v4:23.1.1'
1819
}

AndroidBootstrap/src/main/java/com/beardedhen/androidbootstrap/BootstrapDrawableFactory.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
import android.content.Context;
44
import android.content.res.ColorStateList;
5+
import android.graphics.Bitmap;
6+
import android.graphics.Canvas;
57
import android.graphics.Color;
8+
import android.graphics.Paint;
9+
import android.graphics.Path;
10+
import android.graphics.drawable.BitmapDrawable;
611
import android.graphics.drawable.Drawable;
712
import android.graphics.drawable.GradientDrawable;
813
import android.graphics.drawable.LayerDrawable;
@@ -13,6 +18,7 @@
1318
import com.beardedhen.androidbootstrap.api.attributes.BootstrapBrand;
1419
import com.beardedhen.androidbootstrap.api.attributes.ViewGroupPosition;
1520
import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapBrand;
21+
import com.beardedhen.androidbootstrap.api.defaults.ExpandDirection;
1622
import com.beardedhen.androidbootstrap.utils.ColorUtils;
1723
import com.beardedhen.androidbootstrap.utils.DimenUtils;
1824

@@ -314,6 +320,74 @@ private static int[] getColorList(int defaultColor, int activeColor, int disable
314320
}
315321
}
316322

323+
static StateListDrawable bootstrapDropDownArrow(Context context, int width, int height, ExpandDirection expandDirection, boolean outline, BootstrapBrand brand) {
324+
StateListDrawable stateListDrawable = new StateListDrawable();
325+
326+
int defaultColor = outline ? brand.defaultFill(context) : brand.defaultTextColor(context);
327+
int activeColor = outline ? ColorUtils.resolveColor(android.R.color.white, context) : brand.activeTextColor(context);
328+
int disabledColor = outline ? brand.disabledFill(context) : brand.disabledTextColor(context);
329+
330+
if (Build.VERSION.SDK_INT >= 14) {
331+
stateListDrawable.addState(new int[]{android.R.attr.state_hovered}, createArrowIcon(context, width, height, activeColor, expandDirection));
332+
}
333+
334+
stateListDrawable.addState(new int[]{android.R.attr.state_activated}, createArrowIcon(context, width, height, activeColor, expandDirection));
335+
stateListDrawable.addState(new int[]{android.R.attr.state_focused}, createArrowIcon(context, width, height, activeColor, expandDirection));
336+
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, createArrowIcon(context, width, height, activeColor, expandDirection));
337+
stateListDrawable.addState(new int[]{android.R.attr.state_selected}, createArrowIcon(context, width, height, activeColor, expandDirection));
338+
stateListDrawable.addState(new int[]{-android.R.attr.state_enabled}, createArrowIcon(context, width, height, disabledColor, expandDirection));
339+
stateListDrawable.addState(new int[]{}, createArrowIcon(context, width, height, defaultColor, expandDirection));
340+
return stateListDrawable;
341+
}
342+
343+
/**
344+
* Creates arrow icon that depends on ExpandDirection
345+
*
346+
* @param context context
347+
* @param width width of icon in pixels
348+
* @param height height of icon in pixels
349+
* @param color arrow color
350+
* @param expandDirection arrow direction
351+
* @return icon drawable
352+
*/
353+
private static Drawable createArrowIcon(Context context, int width, int height, int color, ExpandDirection expandDirection) {
354+
Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
355+
Canvas canvas = new Canvas(canvasBitmap);
356+
Paint paint = new Paint();
357+
paint.setStyle(Paint.Style.FILL_AND_STROKE);
358+
paint.setStrokeWidth(1);
359+
paint.setColor(color);
360+
paint.setAntiAlias(true);
361+
Path path = new Path();
362+
path.setFillType(Path.FillType.EVEN_ODD);
363+
switch (expandDirection) {
364+
case UP:
365+
path.moveTo(0, (height / 3) * 2);
366+
path.lineTo(width, (height / 3) * 2);
367+
path.lineTo(width / 2, height / 3);
368+
path.lineTo(0, (height / 3) * 2);
369+
break;
370+
case DOWN:
371+
path.moveTo(0, height / 3);
372+
path.lineTo(width, height / 3);
373+
path.lineTo(width / 2, (height / 3) * 2);
374+
path.lineTo(0, height / 3);
375+
break;
376+
}
377+
path.close();
378+
canvas.drawPath(path, paint);
379+
return new BitmapDrawable(context.getResources(), canvasBitmap);
380+
}
381+
382+
static ColorStateList bootstrapDropDownViewText(Context context) {
383+
384+
int defaultColor = context.getResources().getColor(R.color.bootstrap_gray_dark);
385+
int activeColor = context.getResources().getColor(android.R.color.black);
386+
int disabledColor = context.getResources().getColor(R.color.bootstrap_gray_light);
387+
388+
return new ColorStateList(getStateList(), getColorList(defaultColor, activeColor, disabledColor));
389+
}
390+
317391
private static void setInsetOnLayers(LayerDrawable[] ary, int l, int t, int r, int b) {
318392
for (LayerDrawable ld : ary) {
319393
ld.setLayerInset(0, l, t, r, b);

0 commit comments

Comments
 (0)