Friday 22 April 2016

Splas Screen - Doing it right way



Step 1: First create an XML Drwable in res/drwable folder.

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

    <item
        android:drawable="@color/gray"/>

    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

Step 2: Next, you will set this as your splash activity’s background in the theme. Navigate to your styles.xml file and add a new theme for your splash activity:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/background_splash</item>
    </style>

</resources>

Step 3: In your new SplashTheme, set the window background attribute to your XML drawable. Configure this as your splash activity’s theme in your AndroidManifest.xml:


<activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Step 4: Finally, your SplashActivity class should just forward you along to your main activity:

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}



Doing it Right


With these steps completed, you will have a splash screen implemented the right way:


Thursday 23 January 2014

Check Sensor availablity on android Phone

private void senserAvailablityCheck() {
       
        PackageManager PM = this.getPackageManager();

        boolean acc = PM
                .hasSystemFeature(PackageManager.FEATURE_SENSOR_ACCELEROMETER);
        boolean gyroscope = PM
                .hasSystemFeature(PackageManager.FEATURE_SENSOR_GYROSCOPE);
        if (gyroscope) {
            AlertUtility.showToast(MainActivity.this, "Gyroscope available");
        }
        if (acc) {
            AlertUtility.showToast(MainActivity.this, "ACC available");
        }
    }

Tuesday 13 August 2013

Read file from SD card in Android


Use this function to read files from SD card. Pass the path of the file with file name 
  -  ("/TestFolder/TestFile.text")

public static BufferedReader readFileFromSdCard(String fileName){
        File sdcard = Environment.getExternalStorageDirectory();
        File file=new File(sdcard,fileName);
              BufferedReader myReader = null;
        try {
            if (file.exists()){
            FileInputStream iStream =  new FileInputStream(file);
             myReader = new BufferedReader(
                    new InputStreamReader(iStream));
             Log.i("File", fileName+" Exists ");
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            Log.e("File", fileName+" Not found");
            e.printStackTrace();
        }
        return myReader;

    }

Monday 12 August 2013

Random Number generator

/*
* Takes Max and Min value and returns a random no
*/
 public static int getRandomValue(int min, int max) {
        Random foo = new Random();
        int randomNumber = foo.nextInt(max - min) + min;
        if (randomNumber == min) {
           
            return min + 1;
        } else {
            return randomNumber;
        }
    }

Wednesday 26 September 2012

Android Connectivity test


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class Connectivity {
/*
 * HACKISH: These constants aren't yet available in my API level (7), but I need to handle these cases if they come up, on newer versions
 */
public static final int NETWORK_TYPE_EHRPD=14; // Level 11
public static final int NETWORK_TYPE_EVDO_B=12; // Level 9
public static final int NETWORK_TYPE_HSPAP=15; // Level 13
public static final int NETWORK_TYPE_IDEN=11; // Level 8
public static final int NETWORK_TYPE_LTE=13; // Level 11

/**
 * Check if there is any connectivity
 * @param context
 * @return
 */
public static boolean isConnected(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    return (info != null && info.isConnected());
}

public static boolean isRoming(Context context){
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if(telephony.isNetworkRoaming()){
     

    Toast.makeText(context, "Is Roaming", Toast.LENGTH_LONG).show();
}
return true;

}


/**
 * Check if there is fast connectivity
 * @param context
 * @return
 */
public static boolean isConnectedFast(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(),info.getSubtype()));
}

/**
 * Check if the connection is fast
 * @param type
 * @param subType
 * @return
 */
public static boolean isConnectionFast(int type, int subType){
    if(type==ConnectivityManager.TYPE_WIFI){
        System.out.println("CONNECTED VIA WIFI");
        return true;
    }else if(type==ConnectivityManager.TYPE_MOBILE){
        switch(subType){
        case TelephonyManager.NETWORK_TYPE_1xRTT:
            return false; // ~ 50-100 kbps
        case TelephonyManager.NETWORK_TYPE_CDMA:
            return false; // ~ 14-64 kbps
        case TelephonyManager.NETWORK_TYPE_EDGE:
            return false; // ~ 50-100 kbps
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
            return true; // ~ 400-1000 kbps
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
            return true; // ~ 600-1400 kbps
        case TelephonyManager.NETWORK_TYPE_GPRS:
            return false; // ~ 100 kbps
        case TelephonyManager.NETWORK_TYPE_HSDPA:
            return true; // ~ 2-14 Mbps
        case TelephonyManager.NETWORK_TYPE_HSPA:
            return true; // ~ 700-1700 kbps
        case TelephonyManager.NETWORK_TYPE_HSUPA:
            return true; // ~ 1-23 Mbps
        case TelephonyManager.NETWORK_TYPE_UMTS:
            return true; // ~ 400-7000 kbps
        // NOT AVAILABLE YET IN API LEVEL 7
        case Connectivity.NETWORK_TYPE_EHRPD:
            return true; // ~ 1-2 Mbps
        case Connectivity.NETWORK_TYPE_EVDO_B:
            return true; // ~ 5 Mbps
        case Connectivity.NETWORK_TYPE_HSPAP:
            return true; // ~ 10-20 Mbps
        case Connectivity.NETWORK_TYPE_IDEN:
            return false; // ~25 kbps
        case Connectivity.NETWORK_TYPE_LTE:
            return true; // ~ 10+ Mbps
        // Unknown
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            return false;
        default:
            return false;
        }
    }else{
        return false;
    }
}

}

Friday 3 August 2012

Creating Custom Dialog in Android

In this tutorial, we show you how to create a custom dialog in Android. See following steps :

  1. Create a custom dialog layout (XML file).
  2. Attach the layout to Dialog.
  3. Display the Dialog.
  4. Done.
dialog.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
</LinearLayout>


activity_caustom_dialog_main.xml

<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".CaustomDialogMainActivity" />

    <Button
        android:id="@+id/btn_show_dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="Button" />

</RelativeLayout>

CaustomDialogMainActivity.java

package com.savtech.android.caustomdialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class CaustomDialogMainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_caustom_dialog_main);

Button btn_ShowDialog = (Button) findViewById(R.id.btn_show_dialog);

btn_ShowDialog.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

final Dialog dialog = new Dialog(CaustomDialogMainActivity.this);

// to set dialog a custom layout.......
dialog.setContentView(R.layout.dialog);

// to set dialog title .......
dialog.setTitle("This is Dialog Title ...");

TextView text = (TextView) dialog.findViewById(R.id.textView1);
text.setText("Android custom dialog example!");

ImageView image = (ImageView) dialog
.findViewById(R.id.imageView1);
image.setImageResource(R.drawable.ic_launcher);

Button dialogButton1 = (Button) dialog
.findViewById(R.id.button1);
Button dialogButton2 = (Button) dialog
.findViewById(R.id.button2);
Button dialogButton3 = (Button) dialog
.findViewById(R.id.button3);

dialogButton1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button 1",
Toast.LENGTH_SHORT).show();
}
});

dialogButton2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button 2",
Toast.LENGTH_SHORT).show();
}
});
dialogButton3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button 3",
Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
});
}
}


Now run this application ......



Now click on the Button and you will see this Dialog...with Image View ,Text View and 3 Buttons.


Now click on Button 2 ......see the Toast Message .....


Enjoy...........

Thursday 2 August 2012

To change Airplane mode on android programmatically

 public static boolean isAirplaneModeOn(Context context) 
                { 
                        return Settings.System.getInt(context.getContentResolver 
(),Settings.System.AIRPLANE_MODE_ON, 0) != 0; 
                } 
                /** 
                 * 
                 * @param status 
                 */ 
                public static void setAirplaneMode(Context context,boolean status) 
                { 
                        boolean isAirplaneModeOn = isAirplaneModeOn(context); 

                        if(isAirplaneModeOn && status) 
                        { 
                                return; 
                        } 
                        if(!isAirplaneModeOn && !status) 
                        { 
                                return; 
                        } 
                        if(isAirplaneModeOn && !status) 
                        { 
                                Settings.System.putInt(AppContext.getInstance().getContext 
().getContentResolver(), 
                                Settings.System.AIRPLANE_MODE_ON, 0); 
                        Intent intent = new Intent
(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
                        intent.putExtra("state", 0); 
                        AppContext.getInstance().getContext().sendBroadcast 
(intent); 
                        return; 
                        } 
                        if(!isAirplaneModeOn && status) 
                        { 
                                Settings.System.putInt(AppContext.getInstance().getContext 
().getContentResolver(), 
                                Settings.System.AIRPLANE_MODE_ON, 1); 
                        Intent intent = new Intent
(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
                        intent.putExtra("state", 1); 
                        AppContext.getInstance().getContext().sendBroadcast 
(intent); 
                        return; 
                        } 
                }