How will you programatically enable or disable WIFI in android?

Hello all..
This is a simple code snippet to enable WI-FI in android programatically in android.

public boolean enableWIFI()
{
        WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        if(wifiManager.isWifiEnabled()){
             if(wifiManager.setWifiEnabled(false))
                return true;
          }else{
            if(wifiManager.setWifiEnabled(true))
                 return true;
        }
        return false;
}

Note : Make sure to add the permissions in the manifest file.
These are the permissions.

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

Please leave your valuable comments on this post.

How to remove a view in your xml layout file using program?

Hello everyone,
this is a simple example showing how to remove a view in android that is created using your xml file.

This is the xml file that contains a TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="CoderzHeaven"
    android:id="@+id/tv"
    />
</LinearLayout>

Here is the java code for removing this view

package pack.coderzheaven;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

public class RemoveViewDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View tv= (View)findViewById(R.id.tv);
        ((LinearLayout)tv.getParent()).removeView(tv);
    }
}

After running this program you will not see the TextView that was in your layout file.
Please leave your valuable comments on this post.

How to open wifi settings in android?

Hello all..

In today’s tutorial I will show you how to open the wifisettings in android programatically.
This code comes handy when the user have not enabled the wifi and you want the user to enable it for your application to work.

Here is the code for that.

package com.coderzheaven;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class OpenWifiSettingsDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button open = (Button)findViewById(R.id.open);
        open.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				openWifiSettings();
			}
		});
    }

    public void openWifiSettings(){

    	final Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings");
        intent.setComponent(cn);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity( intent);
    }
}

Here is the layout xml- main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="How to open Wifi Settings Demo"
    />

  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Open Wifi settings"
    android:id="@+id/open"
    />
</LinearLayout>

Wifi settings in android

How to create a splash screen in android?

Hello everyone today i will show you how to create a splash screen in android.
This is one of the simplest ways to create a splash screen however there are another ways to create the splash screen.
Lets look at the code.

We need two layouts one for the splash screen and another for the first screen that comes after splash screen.

The splash screen layout will look like this.
splashscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <ImageView android:src="@drawable/android"
    android:layout_width="fill_parent"
     android:id="@+id/imageView1"
     android:layout_height="fill_parent"></ImageView>
</LinearLayout>

Now the main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Splash screen Demo from CoderzHeaven"
    />
</LinearLayout>

Now the main java file.

package pack.coderzheaven;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;

public class SplashScreenDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);
        creatingSplashScreen();
    }

	private void createFirstScreen()
	{
	    	   setContentView(R.layout.main);
	}

	private void creatingSplashScreen()
	{
		 new CountDownTimer(5000, 1000) {
                   public void onTick(long millisUntilFinished)
		     {
		     }

		     public void onFinish() {
		    	 createFirstScreen();
		     }
		  }.start();
	}
}

Make sure you have an image named “android.png” or “android.jpg” in your res/drawable folder.

How to create a MD5 hash of a word in android?

This is one of the most useful things in android.
We can use the MD5 algorithm to create a hash of any string we want.
Here is the code for doing that.
Check the LogCat for output.

package pack.coderzheaven;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.app.Activity;
import android.os.Bundle;

public class MD5Demo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        System.out.println("Hash Word : " + createHash("CoderzHeaven"));
    }

    static public String createHash(String word) {
    	   String created_hash = null;
    	   try {
    	      MessageDigest md5 = MessageDigest.getInstance("MD5");
    	      md5.update(word.getBytes());
    	      BigInteger hash = new BigInteger(1, md5.digest());
    	      created_hash = hash.toString(16);
    	   } catch (NoSuchAlgorithmException e) {
    	      System.out.println("Exception : " + e.getMessage());
    	   }
    	   return created_hash;
    	}
}

Please leave your valuable comments on this post.

How to keep your screen-on through XML in android?

Hello everyone,

Today I will give you a simple tip on how to keep your screen on While your application is running in android.
In Real devices the problem is you will loose some of the values in your variable when your device goes to sleep mode.
To prevent this what we can do is to prevent the phone from going to sleep mode.
To do this a simple thing needs to be done in your xml.

paste this in the root of your layout to keep the screen on.

android:keepScreenOn=”true”

Its really simple as that.