Posts

Get the Local IP address of your device

Have a great day.


Set the permission in Manifest.xml

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

Then just use below code:

 public String getLocalIpAddress() {
        String firstIP = "192.168.0.1";
        ArrayList<String> ipList = new ArrayList<String>();
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        Log.d(TAG, inetAddress.getHostAddress().toString());
                        ipList.add(inetAddress.getHostAddress().toString()…

Check the service on several time gaps.

Now three...

Hope this will be very useful for those who doing time intervals and web services.

ServiceChecker.Java 

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class ServiceChecker extends BroadcastReceiver {
    ShukranDbHelper dbHelper;
    @Override
    public void onReceive(Context context, Intent arg1) {
        // Just calling your method
Log.d("hey", "I'm alive");
    }
}


In AndroidManifest.xml

<receiver android:name=".service.DataChecker"></receiver>

In your Activity, Just call this.

Intent alarmIntent = new Intent(this, ServiceChecker.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
startAlarm();


public void startAlarm() {
    manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    int interval =600000; // 10 minute //10000
    manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
}

public void c…

Check Network Using BroadCastReceiver

Hello friends,

Hope you already see this example in lot of websites and blogs. I'm also doing these, because hope my example might be easy and understandable.

Let's go.

Create the class:

ConnectivityReceiver.Java

public class ConnectivityReceiver extends BroadcastReceiver {

    public static ConnectivityReceiverListener connectivityReceiverListener;

    public ConnectivityReceiver() {
        super();
    }

    @Override
    public void onReceive(Context context, Intent arg1) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null
                && activeNetwork.isConnectedOrConnecting();

        if (connectivityReceiverListener != null) {
            connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
        }
    }

    public static boolean isConnected() {
        ConnectivityManage…

Touch Events in Fragment

Hi friends, Have a great day. Today we're going to look for touch events in fragment. Taps - If you gonna multiple tap to view or do something, this will be more helpful.

Just implement TouchListener in your fragment like this,

public class FirstFragment extends Fragment implements View.OnTouchListener

Then

  @Override
    public boolean onTouch(View v, MotionEvent event) {
        int eventaction = event.getAction();
        if (eventaction == MotionEvent.ACTION_UP) {

            //get system current milliseconds
            long time = System.currentTimeMillis();


            //if it is the first time, or if it has been more than 3 seconds since the first tap ( so it is like a new try), we reset everything
            if (startMillis == 0 || (time - startMillis > 3000)) {
                startMillis = time;
                count = 1;
            }
            //it is not the first, and it has been  less than 3 seconds since the first
            else { //  time-startMillis< 3000
         …

Add Application Name and Version Name in Generated APK

Hi Everyone, Have a great day. Happy Feb.

How to add app name, version name in the generated apk? 

- It's very interesting question asked by my junior developer.


In app.gradle

Under the android

just add following line:

project.archivesBaseName = "IndAppz";


more?

add the version information:

Put the following code under the buildTypes.


applicationVariants.all { variant ->
                variant.outputs.each{ output ->
                    output.outputFile = new File(output.outputFile.parent,output.outputFile.name.replace(".apk","-" + defaultConfig.versionName+".apk" ))
                }
            }

even more?

add the app with date information:

define the date:
def getDate() {
    new Date().format('ddMMMyyyy')
}

And then add the date info into the file name.

applicationVariants.all { variant ->
                variant.outputs.each{ output ->
                    output.outputFile = new File(output.outputFile.parent,output.outputFile.nam…

Using TypeFace in Android

In android, when you using the custom fonts. You need to create the assets folder in the main, then paste your .ttf or .otf format fonts.

Here some methods will be very useful for you,


Set the typeface

    public Typeface setTypeFace(Context context) {
        Typeface typeface = Typeface.createFromAsset(context.getAssets(),
                "fonts/MyriadPro_Regular.otf");
        return typeface;
    }

txt_username.setTypeface(setTypeFace(getActivity()));

Like you can set the EditText, Spinner and other widgets.


Using the spannable text view in android,

Spannable span = new SpannableString("Hello Android, Have a great day!");        
span.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    span.setSpan(new ForegroundColorSpan(Color.RED), 13, 25, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv.setText(span);

Using Custom TypeFace:
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Typeface;
import android…

Date conversion into different data type

Most of times, we've busy with converting one data type into another data type and validations. That's a developer's life. Actually it's makes life interesting.

This is old, anyhow I post here. Just try it.

Convert current time to long Timestamp.
    public static long getCurrentTimeStamp(Context context) {
        long timestamp = 0;
        try {
            Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMMM-yyyy hh:mm ss a");
            timestamp = c.getTime().getTime();
        } catch (Exception e) {
            //handle exception
        }
        return timestamp;
    }


Convert Date into String

    public static String convertDate(Date date) {
        String format = "dd-MMM-yyyy hh:mm a";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String stringDate = "";
        try {
            stringDate = sdf.format(date)…