Programmer XR Just someone who explains Android code!

13Feb/120

Android quick win: Hide the soft-keyboard on button press ( or from AndroidManifest.xml )

I just wanted to quickly put something up again ( it has been to toooo long since i posted something )...

Sometimes you just want to do a simple task, such as hide the on-screen keyboard. In Android there is an easy way to do that. You just need a View thats currently inside the Activity and you can utilize the InputMethodManager to hide it.

It can also be nice to hide the keyboard from the Android manifest. I'll explain that as well.

Here is a quick way to hide the keyboard from Java:

Hide from Java

1. Find a View thats currently in the Activity. (Lets give it id: myView)

View v = findViewById(R.id.myView);

2. Then you can use this view to find the corresponding WindowToken. And then we can close the keyboard with these 2 lines

InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(fEmail.getWindowToken(), 0);

3. You can also use this in any OnClickListener but you will have to make a reference to the Context of the Activity:

this.getSystemService will become YourActivityClassName.this.getSystemService

 Hide from AndroidManifest.xml

And this can be used to hide your keyboard on launch of your Activity.

<activity
android:name=".activities.MyActivity"
android:configChanges="keyboardHidden"  />

Pretty easy, aint it :)

1Jun/1113

Android tutorial: How to set a custom wallpaper

Android wallpapers

Android wallpapers

Wallpapers are a nice way to customize your homescreen. There are many applications that allow you to change your wallpaper and they have huge databases with alot of wallpapers. An example is Zedge.

But the main question is:
How to they do it?

If we take a look at how this is done, it's pretty simple. In this tutorial i'll explain the basics ( really the basics :-) ) of how you can make your own Wallpaper-changer-application-thingy!

This tutorial will cover the following subjects:

  • A GridView to  show the wallpapers
  • Implement a custom Adapter to show our images
  • The code required to set a new wallpaper

Follow me after the break to learn how I put this together.

27May/110

Android tip: Emulator key bindings

Whots that ancient thing?

I woke up today and i had the need to display the emulator in landscape mode. Didn't get the right answer just by looking at the emulator so after a quick Google search i found a nice table with all Emulator key bindings! Just wanted to share the knowledge :-) Most of them have a normal button on the emulator itself but i didnt find one for swichting the oriëntation.

 

Emulated Device Key Keyboard Key
Home HOME
Menu (left softkey) F2 or Page-up button
Star (right softkey) Shift-F2 or Page Down
Back ESC
Call/dial button F3
Hangup/end call button F4
Search F5
Power button F7
Audio volume up button KEYPAD_PLUS, Ctrl-5
Audio volume down button KEYPAD_MINUS, Ctrl-F6
Camera button Ctrl-KEYPAD_5, Ctrl-F3
Previous orientation (e.g. portrait, landscape) KEYPAD_7, Ctrl-F11
Next orientation (e.g. portrait, landscape) KEYPAD_9, Ctrl-F12
Toggle cell networking on/off F8
Toggle code profiling F9 (only with -trace startup option)
Toggle fullscreen mode Alt-Enter
Toggle trackball mode F6
Enter trackball mode temporarily (while key is pressed) Delete
DPad left/up/right/down KEYPAD_4/8/6/2
DPad center click KEYPAD_5
Onion alpha increase/decrease KEYPAD_MULTIPLY(*) / KEYPAD_DIVIDE(/)

 

27May/1111

Android tutorial: Game menu with a custom Font

Android game development!

Game dev FTW!

Android game development is HOT! I really get a lot of mails / requests for more game oriented tutorial. So here is one just for all you starting Android game developers out there! This post will explain how to make a simple game menu in Android. As always the source code is in the bottom! Want more about game development? There is a tag that holds all posts about game development

Here is a list with features that the finished menu will have:

  • a nice background
  • a custom font
  • a style for making clean menu buttons
  • a sub-menu with settings for your game
  • settings will be saved in persistent storage ( with SharedPreferences )

Here is a picture of the complete menu.

Custom menu for your Android game

Finished project: Your custom game menu

Screen 2

Finished project: The settings menu

 

 

 

 

 

 

 

 

Jump over to the entire post to see some code!

24May/113

Android snippet: Check if the device is connected to the internet

Peeking Android

Boo!

Just a quick function to make your live easier :-)

Bart added a comment to the XML + ProgressBar post from yesterday that checks is the device that runs your application has an active internet connection. To make sure its properly searchable i chose to repost it as an Android snippet.

If you guys come across more useful little pieces of code. Just contact me or leave a comment below.

Here is the function

private Boolean isOnline()	{
	ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo ni = cm.getActiveNetworkInfo();
	if(ni != null && ni.isConnected())
		return true;

	return false;
}

Also don't forget to add the INTERNET (and ACCESS_NETWORK_STATE) permissions to you application :-)

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Tagged as: , 3 Comments
23May/119

Merging 2 tutorials: XML data & ProgressDialog using ASyncTask

Make 1 from 2 :-)

Make 1 from 2 :-)

This post is just to for fill a request of one of the commentors / commentators ( you get the meaning :-) ). He asked if i could show an example that combines these 2 tutorials:

  1. Android tutorial: How to parse XML to a Android ListView
  2. Android tutorial: How to implement a ProgressDialog

So i started off with the XMLtest code from the 1st tutorial. ( you can download the Eclipse project at the bottom of the XML tutorial ). Its pretty simple to implement a ProgressDialog. Apposed to the original tutorial i will be implementing a ASyncTask.

Quote from official docs:

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.