Saturday, 26 March 2016

How to convert UTC time stamp to PST time.

Here I am going to write how to convert UTC time zone to PST time zone.

private void convertUtcTimeToPstTimeZone() {
        String utcTime = "2016-08-14T22:56:02.038Z";
        SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
        utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            Date date = utcFormat.parse(utcTime);
            DateFormat pstFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
            pstFormat.setTimeZone(TimeZone.getTimeZone("PST"));
            System.out.println(pstFormat.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

Result is: 14-08-2016 22:56:02

How to install google play services on Genymotion and Android Studio Plugin

Genymotion emulator provides Google services easily like GCM push notification and Google maps etc.
Follow the below steps easily you can make available Google services on Genymotion with the help of CyanogenMod's gapps. In genymotion there are so many features available you can use easily like GPS sensor , camera and Gyroscope etc..
For more information follow Genymotion details.


InstallationGenymotion operation depends upon use of Oracle VM Virtual Box in the background.
To install Genymotion, follow the steps corresponding to your operating system.


To download Genymotion for Windows:


1. Go to the Genymotion download page.
2. Save and run the .exe file.
3. Click Install and Finish.
4. Start Genymotion now you will be able to see below screen.


5. Click + Add button now you will be able to see this screen and select virtual devices tap on Next Button.

6. Retrieve and Deploy the new virtual devices.

7. Select downloaded device and tap on start button now you will be able to see Genymotion Emulator.

8. To install download Genymotion-ARM-Translation_v1.1zip and then drag and drop the downloaded file to opened Genymotion virtual device. When installation is done then you will be able to see confirmation dialog like given below. Click on OK button.

Then again you will see dialog File installation result is done.

9. You need to reboot the virtual device using below command.

adb reboot

10. Now Install gapps. Here most of gapps link given below according to your device android version you can download it.

Android 5.0.x
Android 4.4.x
Android 4.3.x
Android 4.2.x

Drag and drop the downloaded file to a virtual device and wait till installation process.

Reboot device once again with the same command. Once device is booted up you will be able to see all the Google play services available in your virtual device.
Now you will be able to run all the application to Genymotion virtual device which is using google services.


Install Genymotion Plugin:

1. Open Android Studio.
2. Go to File => Setting=> Plugin
3. Search Genymotion if not found click on Search repository.
4. Tap on Install Button and Restart Android Studio.

5. Now you will be able to see Genymotion icon in android studio.

6. You can open Genymotion directly from android studio.


if any thing you find gap please mark me correct.

Thanks,



Tuesday, 22 March 2016

How to design EditText Border


A EditText is an overlay over TextView that configures itself to be editable. It is the predefined subclass of TextView that includes rich editing capabilities.




MainActivity.java

package com.test.border;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.test.border.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:background="@drawable/first_name_edit_text_border"
        android:hint="@string/fName"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:layout_height="40dp" />


    <EditText
    android:layout_width="match_parent"
    android:background="@drawable/last_name_edit_text_border"
    android:hint="@string/lName"
    android:gravity="center_vertical"
    android:layout_marginTop="10dp"
    android:paddingLeft="10dp"
    android:layout_height="40dp" />

    <EditText
        android:layout_width="match_parent"
        android:background="@drawable/age_edit_text_border"
        android:hint="@string/age"
        android:gravity="center_vertical"
        android:layout_marginTop="10dp"
        android:paddingLeft="10dp"
        android:layout_height="40dp" />

    <EditText
        android:layout_width="match_parent"
        android:hint="@string/without_border"
        android:gravity="center_vertical"
        android:layout_marginTop="10dp"
        android:paddingLeft="10dp"
        android:layout_height="40dp" />

</LinearLayout>

Create selector xml file and keep inside drawable directory which will show you as a border of the edittext. Use below xml file as a background of the edittext.

first_name_edit_text_border.xml

First Name EditText Border result given below.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#fffae5"/>
            <stroke
                android:color="@color/colorAccent"
                android:width="1dp"
                />
            <corners android:radius="7dp"/>
        </shape>
    </item>
</selector>


last_name_edit_text_border.xml

Last Name EditText Border result given below.

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

            <solid android:color="@color/last_name_background_white" />
            <stroke android:width="2dp" android:color="@color/colorAccent" />
            <corners android:radius="7dp" />
        </shape>
    </item>
</selector>


age_edit_text_border.xml

Age EditText Border result given below.

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

            <solid android:color="@color/age_edit_text_background_color" />
            <stroke android:width="2dp" android:color="@color/age_border_color" />
            <corners android:radius="0dp" />
        </shape>
    </item>
</selector>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="age_border_color">#FF00FF</color>
    <color name="last_name_background_white">#FFFFFF</color>
    <color name="age_edit_text_background_color">#ffffffff</color>
</resources>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

Please comment in case of any issues.


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.

Saturday, 23 January 2016

HTML formatted string resource in XML

I am going to write HTML formatted string resource in XML file and will show you how to bind in TextView.

You can add raw html like that
<![CDATA[ ...raw html... ]]>

I have some fixed text in string XML file.

string.xml
<resources>
    <string name="app_name">HtmlTextResource</string>
  <string name="html_data_text"><![CDATA[
<p>This is a html-formatted string with <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph of the same string.</p>
 <a href=\'https://github.com/'>Github</a>&nbsp;&nbsp;
       <a href=\'https://www.google.co.in/?gfe_rd=cr&ei=uGOjVrC1C4LC8gfDtqngDA&gws_rd=ssl\'>Google</a>&nbsp;&nbsp;
       <a href=\'http://mail.google.com/mail\'>Gmail</a>

]]></string>
</resources>


Binding string data in TextView

TextView textView= (TextView) findViewById(R.id.tvHtmlText);
        Spanned aboutBody = Html.fromHtml(getResources().getString(R.string.html_data_text));
        textView.setText(aboutBody);
        textView.setMovementMethod(LinkMovementMethod.getInstance());