Wednesday, July 4, 2012

Android Tricks

How to Disable Home Button
    @Override
    public void onAttachedToWindow()
    {  
        this.getWindow().setType(WindowManager.
          LayoutParams.TYPE_KEYGUARD_DIALOG);     
        super.onAttachedToWindow();  
    }

How to Disable Back Button
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

How to Disable Soft Keypad
final EditText txtName = (EditText) findViewById(R.id.txtName);
txtName.setInputType(InputType.TYPE_NULL);

How to Make Static Rotation/orientation in Android
        //if you want to lock screen for always Portrait mode
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                                     or
        //if you want to lock screen for always Landscape mode
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);



How to Disable screen Rotation/orientation in Android (better way is java code)
        
//put this code in Manifest file, activity tag
android:screenOrientation="nosensor"
android:configChanges="keyboardHidden|orientation|screenSize"
/* or even you can do it by programmatically -- just put 
Configuration code in onResume method before calling super 
like this */
@Override
protected void onResume() {
    int currentOrientation = getResources().getConfiguration()
                               .orientation;
    if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) 
    {
        setRequestedOrientation(ActivityInfo
                    .SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    }
    else {
        setRequestedOrientation(ActivityInfo
                    .SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }
    super.onResume();
}
How to Disable Title Bar and Make Full Screen View
        
//1. put this line to manifest file in Application tag
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

//2. put below code in your activity onCreate method

//to disable notification bar (Top Bar)
        requestWindowFeature(Window.FEATURE_NO_TITLE);        
                         
        
        //to set full screen view
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                     WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        //make sure code should be before calling below method
        setContentView(R.layout.main);

How to Create Alert Dialog Box in Android
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("App has been started..")
               .setCancelable(false)
               .setTitle("Alert Box")           
               .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                   }
               });
        AlertDialog alert = builder.create();
        alert.show();


How to Create Toast message in Android
        Toast.makeText(getApplicationContext(), "I am splash message..", 
          Toast.LENGTH_LONG).show();

How to create Progress Dialog in Android
        ProgressDialog dialog = ProgressDialog.show(this, "", 
                "Loading. Please wait...", true);

Load Home Screen Programmatically in Android
  //to load home screen put this code in onClick method of desired button
  Intent startMain = new Intent(Intent.ACTION_MAIN);
  startMain.addCategory(Intent.CATEGORY_HOME);
  startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(startMain)

Start/Load Activity from Activity
//put this code where you want to load another Activity
Intent intent = new Intent(FirstActivty.this, SecondActivity.class);

//below 2 lines (Flags) are optional
// 1. if set, it will clear the back stack
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

// 2. If set, this activity will become the start of a new task
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

Make a Phone Call
String mobileNo = "+919741817902";
String uri = "tel:" + mobileNo.trim() ;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);

//add permission to Manifest file

How to check WiFi is Connected or Not
public void chekcWifiConnectDisconnect() {
  ConnectivityManager connManager = (ConnectivityManager) 
    getSystemService(CONNECTIVITY_SERVICE);
  NetworkInfo mWifi = connManager
    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

  if (mWifi.isConnected()) {
   Log.v("Debug", "Wifi is connectd..");   
  } else {
   Log.v("Debug", "Wifi is not connectd..");   
  }

}

//dont forget to put Wifi permission in manifest file

2 comments: