Hi Guys!
Today I share the code that are mostly used in the android. I collected the all the small part of the code that are used most of the cases. I hope you also enjoyed with this.
Today I share the code that are mostly used in the android. I collected the all the small part of the code that are used most of the cases. I hope you also enjoyed with this.
1. How to Disable Home Button
@Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.
LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
2. How to Disable Back Button
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return false;
}
3. How to Disable Soft Keypad
final EditText txtName = (EditText) findViewById(R.id.txtName);
txtName.setInputType(InputType.TYPE_NULL);
4. 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);
5. 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();
}
6. 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);
7. 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();
8. How to Create Toast message in Android
Toast.makeText(getApplicationContext(), "I am splash message..",
Toast.LENGTH_LONG).show();
9. How to create Progress Dialog in Android
ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
10. Load Home Screen Programmatically in Android
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain)
11. 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);
12. Make a Phone Call
String mobileNo = "+9189000000";
String uri = "tel:" + mobileNo.trim() ;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
//add permission to Manifest file
13. 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