Difference between a clickable ImageView and ImageButton - android

I'm just wondering if there is any significant difference between an ImageView that's set to be clickable, compared with an ImageButton?
Is there any reason for using one over the other? Is there any restriction on the drawable for an ImageButton that leaves ImageView as the only possible option?
Will I possibly lose any functionality of a button if I opt for a clickable ImageView over ImageButton?

There's no differences, except default style. ImageButton has a non-null background by default.
EDIT: Also, ImageButton.onSetAlpha() method always returns false, scaleType is set to center and it's always inflated as focusable.
Here's ImageButton's default style:
<style name="Widget.ImageButton">
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:scaleType">center</item>
<item name="android:background">#android:drawable/btn_default</item>
</style>

ImageButton is inherited from ImageView
public class ImageButton extends ImageView {
public ImageButton(Context context) {
this(context, null);
}
public ImageButton(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.imageButtonStyle);
}
public ImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true);
}
#Override
protected boolean onSetAlpha(int alpha) {
return false;
}
#Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event);
event.setClassName(ImageButton.class.getName());
}
#Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(ImageButton.class.getName());
}
as #Micheal describe i just add details to his answer

The effect of a button click when I click is there for imagebutton but not for imageView.

If you want, you can give the button clic effect as a background. ImageButton has an effect by default.

Related

Android Custom Icon Font only works if I call setTypeface after View is inflated

I have a custom font file, say myfont.ttf in assets/fonts/
I have created a custom View like this
public class IconFontView extends AppCompatTextView {
public IconFontView(Context context) {
super(context);
applyIconFonts(context);
}
public IconFontView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
applyIconFonts(context);
}
public IconFontView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
applyIconFonts(context);
}
private void applyIconFonts(final Context context) {
final Typeface iconFont = Typeface.createFromAsset(context.getAssets(), "fonts/myfont.ttf");
setTypeface(iconFont);
}
}
in XML:
<com.smule.singandroid.customviews.IconFontView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="400dp"
android:text="#string/icontext"/> <!-- my unicode for the specific icon -->
strings.xml:
<string name="icontext"></string>
This way, I can see the preview perfectly fine.
But, if I enter this fragment, I see nothing but an empty view. (using "Show layout bounds" to tell you that this view exists there, just not drawing)
HOWEVER, if I add this font to my styles.xml like this
<style name="IconFont">
<item name="fontPath">fonts/myfont.ttf</item>
</style>
and apply it in layout xml like this
<com.smule.singandroid.customviews.IconFontView
style="#style/IconFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="400dp"
android:text="#string/icontext"/> <!-- my unicode for the specific icon -->
This not only displays correct preview, but also shows the icon perfectly fine.
This has same effect as doing something like
mIconFontview.setText(R.string.icontext);
mIconFontView.setTypeFace(...);
Why would this work this way?
Then there would be no reason to create IconFontView at all. Might as well just use TextView.

Change multiple views background color

I have a lot of views using one and the same color as a background. I want to change the color of all views when I receive a call from the server programmatically. I don't want to call for every view
view.setBackgroundColor(new color);
Is there a way to change a color code that is in colors.xml.
Short answer: No, you can't. The resources are defined at compile time.
See this question for a similar case: How can I programmatically change the value of a color in colors.xml?
You can't replace the value of the color in the xml file. But you
can create different themes which are used in your application and
change the theme dynamically
See this tutorial:
http://www.developer.com/ws/android/changing-your-android-apps-theme-dynamically.html
What I end up doing is create a custom class that sets the color form preference. And use this class everywhere I want to change the color. And next time the view is drawn it gets the new color. Something like this:
public class ColoredToolbar extends android.support.v7.widget.Toolbar {
public ColoredToolbar(Context context) {
super(context);
setBackgroundColor(context);
}
public ColoredToolbar(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(context);
}
public ColoredToolbar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setBackgroundColor(context);
}
private void setBackgroundColor(Context context) {
int color = PreferenceHelper.getToolBarColor(context, Preferences.PREF_TITLE_BAR_COLOR_KEY);
this.setBackgroundColor(color);
}
}

how to change number picker style in android?

I want to use the NumberPicker component-widget but Instead in the default Holo theme I need to replace the blue color with orange since that is the default color in my styling.
How can I replace the blue color and the color of the numbers,and keep all of the functionality of the component?
thanks
Unfortunately, you can't style it. The styles and styling attributes for NumberPicker are not present in the public API, therefore you can't set them and change the default look. You can only select between light and dark theme.
As a solution I would suggest to use android-numberpicker library instead. The library is basically a port of NumberPicker extracted from Android source codes. But it's better than that, it also backports NumberPicker to Android 2.x. The library can be easily styled.
To style the divider adjust NPWidget.Holo.NumberPicker style and its selectionDivider and selectionDividerHeight attributes.
To style the text adjust NPWidget.Holo.EditText.NumberPickerInputText style.
Preview of a customized number picker:
<NumberPicker
android:id="#+id/np"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:background="#drawable/drawablenp"
android:layout_centerVertical="true"/>
Create the background xml in the drawable folder named "drawablenp.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#707070"
android:centerColor="#f8f8f8"
android:endColor="#707070"
android:angle="270"/>
</shape>
Make copy of library/res/drawable-*/numberpicker_selection_divider.9.png and name then, for example, custom_np_sd.9.png.
Override default NumberPicker style via activity theme:
<style name="AppTheme" parent="#style/Holo.Theme">
<item name="numberPickerStyle">#style/CustomNPStyle</item>
</style>
<style name="CustomNPStyle" parent="#style/Holo.NumberPicker">
<item name="selectionDivider">#drawable/custom_np_sd</item>
</style>
And apply #style/AppTheme as activity theme.
I faced this problem too. I really want to have nice NumberPicker UI. All answer in this question worked but very limited. I almost create my own RecylerView to create the NumberPicker I want. Apparently I found neat library which is very robust. Here is the link https://github.com/Carbs0126/NumberPickerView
Not trying to answer the question here. Just want to help someone with the same problem as I am.
I face this problem too, I use reflect to change the style
public class MyNumberPicker extends NumberPicker {
public MyNumberPicker(Context context) {
super(context);
setNumberPickerDivider();
}
public MyNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
setNumberPickerDivider();
}
public MyNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setNumberPickerDivider();
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyNumberPicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setNumberPickerDivider();
}
#Override
public void addView(View child) {
super.addView(child);
updateView(child);
}
#Override
public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
updateView(child);
}
#Override
public void addView(View child, android.view.ViewGroup.LayoutParams params) {
super.addView(child, params);
updateView(child);
}
public void updateView(View view) {
if (view instanceof EditText) {
EditText et = (EditText) view;
et.setTextColor(ContextCompat.getColor(getContext(), R.color.font_content));
et.setTextSize(16);
}
}
private void setNumberPickerDivider() {
try {
{
Field field = NumberPicker.class.getDeclaredField("mSelectionDivider");
field.setAccessible(true);
field.set(this, ContextCompat.getDrawable(getContext(), R.drawable.horizontal_divider));
}
{
Field field = NumberPicker.class.getDeclaredField("mSelectionDividerHeight");
field.setAccessible(true);
field.set(this, 1);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

Custom Preference Category won't show Text title

I'm trying to get my Preference category header to be consistent with the theme of my app, so after searching around both on SO and some blogs I follow..I found the best(and easiest) way was just to create a custom Preference Category class that overrides onCreateView() or onBindView() and programmatically set the color of the category TextView and it's background. Sounds easy enough.
My only two caveats is that I'm using a SherlockPreferenceActivity, and therefore I'm not able to simply apply a custom theme to my Activity since I have to use a Sherlock theme or a derivative. Also, my other thing(and I think this may be the cause) is that I'm trying to set the TextView's background to a Shape Drawable, which is a gradient with a stroke in xml. The background of the preference category changes just fine, but I don't see any TextView at all, which leads to me to think that maybe the background is being drawn on top of it's textView? I'm not sure
Here's the code for my CustomPreferenceCategory class. As you can see I tried it both using onBindView() and onCreateView().
public class CustomPreferenceCategory extends PreferenceCategory {
public CustomPreferenceCategory(Context context) {
super(context);
}
public CustomPreferenceCategory(Context context, AttributeSet attrs) {
super(context);
}
public CustomPreferenceCategory(Context context, AttributeSet attrs,
int defStyle) {
super(context);
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
TextView titleView = (TextView) view.findViewById(android.R.id.title);
titleView.setBackgroundResource(R.drawable.header_non_rounded);
titleView.setTextColor(Color.WHITE);
}
//#Override
/*protected View onCreateView(ViewGroup parent) {
// And it's just a TextView!
TextView categoryTitle = (TextView) super.onCreateView(parent);
categoryTitle.setBackgroundResource(R.drawable.header_non_rounded);
categoryTitle.setTextColor(Color.WHITE);
return categoryTitle;
}*/
}
Here's how I'm setting it via my preferences.xml file
<com.brightr.weathermate.views.CustomPreferenceCategory android:title="Weather" >
<CheckBoxPreference
android:defaultValue="true"
android:key="degreesC"
android:summary="Display the weather degrees in Celsius units"
android:title="Show Celsius" />
<CheckBoxPreference
android:defaultValue="true"
android:key="degreesF"
android:summary="Display the weather in Farenheit units"
android:title="Show Farenheit" />
<ListPreference
android:entries="#array/textColors"
android:entryValues="#array/textColor_values"
android:summary="Change the color of the temerature text"
android:title="Temperature Text Color" />
</com.brightr.weathermate.views.CustomPreferenceCategory>
Any thoughts as to why only the background is being changed but the TextView is not visible? Also, even when I use setBackgroundResource() instead of setBackgroundDrawable(), it still doesn't set the text. Any help would be greatly appreciate guys.
Got it. Silly me, I forgot to pass in the other two params in each of the constructors. I feel like kicking myself.
public CustomPreferenceCategory(Context context) {
super(context);
}
public CustomPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomPreferenceCategory(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}

How does the CheckBox obtain it's respective drawables?

The CheckBox class extends the CompoundButton, but add nothing to it. But some how it obtains it's respective look. I found some declarations in Android sources, but wonder how they are mapped to CheckBox class?
public class CheckBox extends CompoundButton {
public CheckBox(Context context) {
this(context, null);
}
public CheckBox(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.checkboxStyle);
}
public CheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
Styles
<style name="Theme">
<item name="checkboxStyle">#android:style/Widget.CompoundButton.CheckBox</item>
</style>
<style name="Widget.CompoundButton.CheckBox">
<item name="android:background">#android:drawable/btn_check_label_background</item>
<item name="android:button">#android:drawable/btn_check</item>
</style>
EDIT:
Probably I was not clear... I understand how the drawable assigned to Widget.CompoundButton.CheckBox style, but how this style assigned to CheckBox class? I see the ".CheckBox" in the style name, but is this naming convention really what makes the trick? If so, what are the rules? If I derive MyCheckBox from CompoundButton, can I just define the Widget.CompoundButton.MyCheckBox style and it will work?
There's your answer: <item name="android:button">#android:drawable/btn_check</item>
A drawable can have multiple states (whether or not the element is focused, pressed, etc), and that's where the different states of the checkbox are.
EDIT: After you revised your question, I see that you're looking for something different. Again, it's all in what you posted:
<style name="Widget.CompoundButton.CheckBox">
This is the style.
<item name="checkboxStyle">#android:style/Widget.CompoundButton.CheckBox</item>
The main theme (android.internal.R.attr) has an attribute "checkboxStyle" that points to this style.
public CheckBox(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.checkboxStyle);
}
The constructor assigns that attribute to the view. And that's it.
Right here
<item name="android:button">#android:drawable/btn_check</item>

Categories

Resources