Tuesday, 1 May 2018

Radio Times, Radio Measures.


There are times when you find yourself in a state where things are out of control just like a RadioButton.

Weird behaviours crop out while you try to add a radio button to your layout dynamically or inflated from layout.

Some times the 
button's drawables doesn't appear at all, sometimes it appears unexpected. 

So for such a scenario, there is a cure in few steps as follows:

1. Try to use AppCompatRadioButton from support library. Set a custom style to it which extends from "@android:style/Widget.Holo.CompoundButton.RadioButton"

<style name="RadioButtonStyle" parent="@android:style/Widget.Holo.CompoundButton.RadioButton">
        <item name="buttonTint">@color/coke_red</item>
        <item name="android:textSize">@dimen/dialog_common_transaction_item_details_text_view</item>
        <item name="android:typeface">normal</item>
        <item name="android:button">@drawable/custom_radio_selector</item>
</style>

2. Add two image drawables, one for checked and other for unchecked state.

3. Create a custom drawable selector "custom_radio_selector.xml"


<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:state_window_focused="false"

        android:drawable="@drawable/ic_radio_on" />

    <item android:state_checked="false" android:state_window_focused="false"

        android:drawable="@drawable/ic_radio_off" />


    <item android:state_checked="true" android:state_pressed="true"

        android:drawable="@drawable/ic_radio_on" />

    <item android:state_checked="false" android:state_pressed="true"

        android:drawable="@drawable/ic_radio_off" />


    <item android:state_checked="true" android:state_focused="true"

        android:drawable="@drawable/ic_radio_on" />

    <item android:state_checked="false" android:state_focused="true"

        android:drawable="@drawable/ic_radio_off" />


    <item android:state_checked="false" android:drawable="@drawable/ic_radio_off" />

    <item android:state_checked="true" android:drawable="@drawable/ic_radio_on" />

</selector>



where ic_radio_off.png and ic_radio_on.png are the image drawbles added as required.

Demo:




Tuesday, 6 February 2018

Android EditText : Input Filter with Restrictions

Note : This post is not about InputFilter but about Restriction Filter for an Android EditText.
If visited page by mistake, one could follow Ctrl+W (on Firefox).


InputFilter interface's following method is origin of all such rules.

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend){
  // Governs what to input and what not to
}

So first let's get to know some use cases of InputFilter before we actually show how we created a restriction filter out of it.

InputFilter is very easy in cases where we have to use it to
1. restrict characters from input at certain positions.
2. restrict characters from deletion from certain positions.

Above cases can be easily handled by checking only three parameters of the  interface's implemented filter() method as :
1. source -> input content going to be added .
2. dstart -> starting position where it is to be placed.
3. dend -> ending position where it is to be placed.


Please follow this simple example demoing a non-editable country code for PhoneNumber EditText  (Note : World knows about TextWatcher as well :))


class CountryCodeInputFilter implements InputFilter {
        
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            //Back Key Press conditions
            boolean deleteCond0 = dstart == 0 && dend == 1; // +
            boolean deleteCond1 = dstart == 1 && dend == 2; // 9
            boolean deleteCond2 = dstart == 2 && dend == 3; // 5

            boolean insertCond0 = dstart == 0 && dend == 0; // ahead of +
            boolean insertCond1 = dstart == 1 && dend == 1; // ahead of 9
            boolean insertCond2 = dstart == 2 && dend == 2; // ahead of 5

            // Handling Back Key Presses
            if ((deleteCond0 || deleteCond1 || deleteCond2) && (source.equals(""))) {
                // BackPress
                if (deleteCond0) {
                    return "+";
                } else if (deleteCond1) {
                    return "9";
                } else {
                    return "5";
                }
            }

            // Handling insertions at invalid positions
            if ((insertCond0 || insertCond1 || insertCond2) && (!source.equals(""))) {
                return "";
            }

            return null;
        }
}

Got updates ?
Welcome.


Android UI and Inflate Exception

Error : Inflate Exception : Binary xml .....


I can't say how frequently you guys face this error but when it occurs surely takes quite time.

Whenever one face such an exception, the greatest possibility is with the child view not being able to initialize correctly which could be due to the following reasons :



1. The layout file must be including some CustomView which is not being correctly referenced or sometimes if it is referenced, there could be some error with in the implementation (one reason of which could be the absence of constructor with attribute set, theme etc.)


2. If you think that case one is checked, then please check once for the view that causes the inflation failure. Need to check if the drawing is successful for that view.

You will get the easiest reasons quickly but one of the most elusive causal reason is the error in background drawable which may or may not give error at compile time.

So little digging is required here.