Combining text & image on a Button on Android

Just use a LinearLayout and pretend it’s a Button – setting background and clickable is the key:

<LinearLayout
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:drawable/btn_default"
    android:clickable="true"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:src="@drawable/image" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_margin="5dp"
        android:text="Do stuff" />
</LinearLayout>

References
https://stackoverflow.com/questions/1532876/android-combining-text-image-on-a-button-or-imagebutton

Set android shape color programmatically

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid android:color="#666666" />

    <size
        android:width="8dp"
        android:height="8dp" />
</shape>
            <ImageView
                android:id="@+id/imageViewStep2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="4dp"
                android:layout_marginRight="4dp"
                android:background="@drawable/ic_stepper"
                android:contentDescription="@string/step_2" />
        Drawable background = imageViewStep1.getBackground();

        if (background instanceof ShapeDrawable) {
            // cast to 'ShapeDrawable'
            ShapeDrawable shapeDrawable = (ShapeDrawable) background;
            shapeDrawable.getPaint().setColor(ContextCompat.getColor(this, R.color.md_blue_500));
        } else if (background instanceof GradientDrawable) {
            // cast to 'GradientDrawable'
            GradientDrawable gradientDrawable = (GradientDrawable) background;
            gradientDrawable.setColor(ContextCompat.getColor(this, R.color.md_blue_500));
        } else if (background instanceof ColorDrawable) {
            // alpha value may need to be set again after this call
            ColorDrawable colorDrawable = (ColorDrawable) background;
            colorDrawable.setColor(ContextCompat.getColor(this, R.color.md_blue_500));
        }

References
https://stackoverflow.com/questions/17823451/set-android-shape-color-programmatically

Detect when there is an Internet connection available on Android

In this method just current activity receive this event
Add to android Manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

In your activity, create a Broadcast Receiver:

private BroadcastReceiver networkStateReceiver=new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = manager.getActiveNetworkInfo();
        doSomethingOnNetworkChange(ni);
    }
};
@Override
public void onResume() {
    super.onResume();
    registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
}

@Override
public void onPause() {
    unregisterReceiver(networkStateReceiver);
    super.onPause();
}

References
https://stackoverflow.com/questions/6169059/android-event-for-internet-connectivity-state-change

How to root Android Huawei mate 7 gold ( MT7-TL10 )

download TWRP image then go to download folder

adb reboot bootloader

fastboot devices # just for check
fastboot flash recovery twrp_3.0.2_mate7_6.0.img
fastboot reboot

download zip file of SuperSu and put it on root folder of Phone SD card

adb reboot recovery

install zip

References
http://forum.supersu.com/topic/908/huawei-phone-general-guide-huawei-root
http://www.droidviews.com/how-to-boot-android-devices-in-fastboot-download-bootloader-or-recovery-mode/
https://android.gadgethacks.com/how-to/know-your-android-tools-what-is-fastboot-do-you-use-it-0155640/

Word wrap text with break word in Android EditText

class WordBreakTransformationMethod extends ReplacementTransformationMethod
{
    private static WordBreakTransformationMethod instance;

    private WordBreakTransformationMethod() {}

    public static WordBreakTransformationMethod getInstance()
    {
        if (instance == null)
        {
            instance = new WordBreakTransformationMethod();
        }

        return instance;
    }

    private static char[] dash = new char[] {'-', '\u2011'};
    private static char[] space = new char[] {' ', '\u00A0'};

    private static char[] original = new char[] {dash[0], space[0]};
    private static char[] replacement = new char[] {dash[1], space[1]};

    @Override
    protected char[] getOriginal()
    {
        return original;
    }

    @Override
    protected char[] getReplacement()
    {
        return replacement;
    }
}
myEditText.setTransformationMethod(WordBreakTransformationMethod.getInstance());

References
https://stackoverflow.com/questions/22289161/word-wrap-break-word-in-edittext

Set a timer in Android using Handler

Runnable:

private final int interval = 1000; // 1 Second
private Handler handler = new Handler();
private Runnable runnable = new Runnable(){
    public void run() {
        Toast.makeText(MyActivity.this, "C'Mom no hands!", Toast.LENGTH_SHORT).show();
    }
};
...
handler.postAtTime(runnable, System.currentTimeMillis()+interval);
handler.postDelayed(runnable, interval);

Message:

private final int EVENT1 = 1; 
private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {         
        case Event1:
            Toast.makeText(MyActivity.this, "Event 1", Toast.LENGTH_SHORT).show();
            break;

        default:
            Toast.makeText(MyActivity.this, "Unhandled", Toast.LENGTH_SHORT).show();
            break;
        }
    }
};

...

Message msg = handler.obtainMessage(EVENT1);
handler.sendMessageAtTime(msg, System.currentTimeMillis()+interval);
handler.sendMessageDelayed(msg, interval);

References
https://stackoverflow.com/questions/1877417/how-to-set-a-timer-in-android