Saturday, 13 February 2016

Background transparent by using Hexadecimal Color


How to make background color as a transparent color with Hexadecimal color.

If you provide 6 hex digits, that means RGB (2 hex digits for each value of red, green and blue). Each 2-digit hex value is one byte, representing values from 0-255.
Ex. #424242
#RRGGBB

If you provide 8 hex digits, it's an ARGB (2 hex digits for each value of alpha, red, green and blue respectively).
First two digit is Alpha value.
Ex. #66424242
#AARRGGBB



100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00

Saturday, 6 February 2016

Create common dialog for whole application using DialogFragment

As per the standrad recomendation we should use DialogFragment instead of common dialog for showing message on dialog window.
A fragment display dialog window on top of it's activity window.
For more information read DialogFragment.

Here I am going to create dialog which will be the common for whole application.


MainActivity.java
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity implements CustomDialogFragment.DialogCallBack {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btnOnClick).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }

    void showDialog() {
        DialogFragment newFragment = CustomDialogFragment.newInstance(
                R.string.title, R.string.message);
        newFragment.onAttach(this);
        newFragment.show(getFragmentManager(), "dialog");
    }

    @Override
    public void dialogPositiveCallBack() {
        Log.i("FragmentAlertDialog", "Positive click!");
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="activity.tcp.com.dialogfragment.MainActivity">

   <Button
       android:layout_width="wrap_content"
       android:id="@+id/btnOnClick"
       android:text="Click Me"
       android:onClick="submit"
       android:layout_height="wrap_content" />
</RelativeLayout>

CustomDialogFragment.java
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

public class CustomDialogFragment extends DialogFragment {
    private DialogCallBack mDialogCallBack;

    public CustomDialogFragment() {
    }

    public static CustomDialogFragment newInstance(int title, int message) {
        CustomDialogFragment frag = new CustomDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        args.putInt("message", message);
        frag.setArguments(args);
        return frag;
    }

    public interface DialogCallBack {
        void dialogPositiveCallBack();

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mDialogCallBack = (DialogCallBack) activity;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Bundle args = getArguments();
        int title = args.getInt("title", 0);
        int message = args.getInt("message", 0);

        return new AlertDialog.Builder(getActivity())
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (mDialogCallBack != null) {
                            mDialogCallBack.dialogPositiveCallBack();

                        }
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .create();
    }


}

In the same way you can create negative callback, interface given below.

public interface DialogCallBack {
        void dialogPositiveCallBack();
        void dialogNegativeCallBack();

    }


please comment if anything need to support.