public void HideNavbar() {
if(Build.VERSION.SDK_INT < 19) //API 18 or below
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else { //API 19 or above
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
}
}
Saturday, September 5, 2015
How to hide the TSB(nav bar - back, home, recent) no matter what version of Android you are using
Thursday, April 17, 2014
How to start an Activity from within a Service
The trick is to set the intent's flag for FLAG_ACTIVITY_NEW_TASK. Also, as you probably already know, all life cycle methods like onCreate are executed on the UI thread which is required to start an Activity.
Here's an example:
public void onCreate(){
super.onCreate();
Intent intent = new Intent("com.example.email.compose");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //This is necessary!
intent.putExtra("Contacts_ID", 0);
startActivity(intent);
}
Tuesday, July 27, 2010
How to add leading and trailing spaces in string.xml
You must use the UTF character \u0020 for white spaces. Just type this succession of characters and it will be interpreted as a space. The \u part tells android that the following number represents the character in the UTF table.
Wednesday, July 8, 2009
How to protect part of a public method from being executed by others
To solve this problem I used enforceCallingOrSelfPermission.
Anything you do in your code until you call that method will be runnable by anyone who can access your method but passed this point it will through a SecurityException If neither you nor the calling process has been granted a particular permission.
Here's what it looks like:
void myMethod() {
// my code every one has access to...
myContext.enforceCallingOrSelfPermission(myPermission, myMessage);
// my super code that noone else can execute
}
Wednesday, June 17, 2009
How to have a few layout elements wrap around
Here is a problem people might encounter when dealing with layouts on android.
Say you want to place an image at the middle of a sentence. you may add a couple TextView and an ImageView but if the horizontal space gets too small the two TextViews are going to start wrapping around independently wich is going to look weird. You'd want the image to be part of your TextView and the entire sentence to wrap around nicely.
To do so:* replace the three objects by one Spannable TextView
* use a SpannableStringBuilder to append all your strings (don't forget to add an extra character (a space for example) that will be replaced by your image)
* and finally use setSpan(ImageSpan, begin, end, flag) on that builder where ImageSpan is an ImageSpan containing the drawable of your picture, begin is the index of the character you've inserted
So instead of having something that looks like this:
| I really really really really like | ![]() | Picasa because it's a very very very very nice free image editing software |
You get something that looks more like that:
I really really really really like Picasa because it's a very very very very nice free image editing software |
And here's what the code would look like:
add android:bufferType="spannable" to your TextView
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(mContext.getText(R.string.part1));
int lengthOfPart1 = builder.length();
builder.append(" ");
builder.append(mContext.getText(R.string.part2));
Drawable d = mContext.getResources().getDrawable(R.drawable.picasaIcon);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); // <---- Very important otherwise your image won't appear
ImageSpan myImage = new ImageSpan(d);
builder.setSpan(myImage, lengthOfPart1, lengthOfPart1 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(builder);
Tuesday, June 16, 2009
How to create a custom progress bar
<layer-list xmlns:android="http://schemas.
</shape>
</clip>
</item>
</layer-list>
Then you would add that custom progress bar to the layout you want that bar to appear:
And finally you would access it from your code like you would with any regular progress bar:
Special Thx to Mikael for this one.
Using resources
When using resources the name used behind the @ sign is not the name of the resource file you will find that value in but the type of resource you are refering to. here xxxx is the type and yyyy is the name of the variable you're using. So your layout file would have something like this:
android:background="@xxxx/yyyy"
and your resource file (say mySuperRes.xml) would look something like that:
<xxxx name="yyyy">zzzz</xxxx>