Java Code Examples for com.facebook.litho.ComponentContext#obtainStyledAttributes()

The following examples show how to use com.facebook.litho.ComponentContext#obtainStyledAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnLoadStyle
static void onLoadStyle(
    ComponentContext c, Output<Drawable> drawable, Output<ScaleType> scaleType) {

  final TypedArray a = c.obtainStyledAttributes(R.styleable.Image, 0);

  for (int i = 0, size = a.getIndexCount(); i < size; i++) {
    final int attr = a.getIndex(i);

    if (attr == R.styleable.Image_android_src) {
      drawable.set(c.getAndroidContext().getResources().getDrawable(a.getResourceId(attr, 0)));
    } else if (attr == R.styleable.Image_android_scaleType) {
      scaleType.set(SCALE_TYPE[a.getInteger(attr, -1)]);
    }
  }

  a.recycle();
}
 
Example 2
Source File: ProgressSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
static @Nullable Drawable getStyledIndeterminateDrawable(ComponentContext c, int defStyle) {
  Drawable indeterminateDrawable = null;

  final TypedArray styledAttributes = c.obtainStyledAttributes(R.styleable.Progress, defStyle);

  for (int i = 0, size = styledAttributes.getIndexCount(); i < size; i++) {
    final int attr = styledAttributes.getIndex(i);

    if (attr == R.styleable.Progress_android_indeterminateDrawable) {
      indeterminateDrawable =
          ContextCompat.getDrawable(
              c.getAndroidContext(), styledAttributes.getResourceId(attr, 0));
    }
  }

  styledAttributes.recycle();

  return indeterminateDrawable;
}
 
Example 3
Source File: HorizontalScrollSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnLoadStyle
static void onLoadStyle(ComponentContext c, Output<Boolean> scrollbarEnabled) {

  final TypedArray a = c.obtainStyledAttributes(R.styleable.HorizontalScroll, 0);

  for (int i = 0, size = a.getIndexCount(); i < size; i++) {
    final int attr = a.getIndex(i);

    if (attr == R.styleable.HorizontalScroll_android_scrollbars) {
      scrollbarEnabled.set(a.getInt(attr, 0) != 0);
    }
  }

  a.recycle();
}
 
Example 4
Source File: TextInputSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnLoadStyle
static void onLoadStyle(ComponentContext c, Output<Integer> highlightColor) {
  TypedArray a = c.obtainStyledAttributes(new int[] {android.R.attr.textColorHighlight}, 0);
  try {
    highlightColor.set(a.getColor(0, 0));
  } finally {
    a.recycle();
  }
}
 
Example 5
Source File: EditTextSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnLoadStyle
static void onLoadStyle(
    ComponentContext c,
    Output<TextUtils.TruncateAt> ellipsize,
    Output<Float> spacingMultiplier,
    Output<Integer> minLines,
    Output<Integer> maxLines,
    Output<Boolean> isSingleLine,
    Output<CharSequence> text,
    Output<ColorStateList> textColorStateList,
    Output<Integer> linkColor,
    Output<Integer> highlightColor,
    Output<Integer> textSize,
    Output<Layout.Alignment> textAlignment,
    Output<Integer> textStyle,
    Output<Float> shadowRadius,
    Output<Float> shadowDx,
    Output<Float> shadowDy,
    Output<Integer> shadowColor,
    Output<Integer> gravity,
    Output<Integer> inputType,
    Output<Integer> imeOptions) {

  final TypedArray a = c.obtainStyledAttributes(R.styleable.Text, 0);

  for (int i = 0, size = a.getIndexCount(); i < size; i++) {
    final int attr = a.getIndex(i);

    if (attr == R.styleable.Text_android_text) {
      text.set(a.getString(attr));
    } else if (attr == R.styleable.Text_android_textColor) {
      textColorStateList.set(a.getColorStateList(attr));
    } else if (attr == R.styleable.Text_android_textSize) {
      textSize.set(a.getDimensionPixelSize(attr, 0));
    } else if (attr == R.styleable.Text_android_ellipsize) {
      final int index = a.getInteger(attr, 0);
      if (index > 0) {
        ellipsize.set(TRUNCATE_AT[index - 1]);
      }
    } else if (attr == R.styleable.Text_android_textAlignment) {
      if (SDK_INT >= JELLY_BEAN_MR1) {
        int viewTextAlignment = a.getInt(attr, -1);
        textAlignment.set(getAlignment(viewTextAlignment, Gravity.NO_GRAVITY));
      }
    } else if (attr == R.styleable.Text_android_minLines) {
      minLines.set(a.getInteger(attr, -1));
    } else if (attr == R.styleable.Text_android_maxLines) {
      maxLines.set(a.getInteger(attr, -1));
    } else if (attr == R.styleable.Text_android_singleLine) {
      isSingleLine.set(a.getBoolean(attr, false));
    } else if (attr == R.styleable.Text_android_textColorLink) {
      linkColor.set(a.getColor(attr, 0));
    } else if (attr == R.styleable.Text_android_textColorHighlight) {
      highlightColor.set(a.getColor(attr, 0));
    } else if (attr == R.styleable.Text_android_textStyle) {
      textStyle.set(a.getInteger(attr, 0));
    } else if (attr == R.styleable.Text_android_lineSpacingMultiplier) {
      spacingMultiplier.set(a.getFloat(attr, 0));
    } else if (attr == R.styleable.Text_android_shadowDx) {
      shadowDx.set(a.getFloat(attr, 0));
    } else if (attr == R.styleable.Text_android_shadowDy) {
      shadowDy.set(a.getFloat(attr, 0));
    } else if (attr == R.styleable.Text_android_shadowRadius) {
      shadowRadius.set(a.getFloat(attr, 0));
    } else if (attr == R.styleable.Text_android_shadowColor) {
      shadowColor.set(a.getColor(attr, 0));
    } else if (attr == R.styleable.Text_android_gravity) {
      gravity.set(a.getInteger(attr, 0));
    } else if (attr == R.styleable.Text_android_inputType) {
      inputType.set(a.getInteger(attr, 0));
    } else if (attr == R.styleable.Text_android_imeOptions) {
      imeOptions.set(a.getInteger(attr, 0));
    }
  }

  a.recycle();
}