Monthly Archives: December 2011

Permission to enable read/write access to SD card

If you are looking for the permission to add to your AndroidManifest file, you are looking for the WRITE_EXTERNAL_STORAGE setting. To do this programmatically, use this code:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

How to get default preferences

You can get the default preferences from your preferences.xml file by using the following code:

// Get the xml/preferences.xml preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String settingValue = preferences.getString("setting", "");

PHP Arrays in JAVA

Going from PHP to Java is a step backwards when dealing with arrays (imo).  One of the more beneficial things that you can do with a PHP array is define keys.  For example I can do something like:

$myList = array("oranges"=>15, "apples"=>25);

Then I can access how many oranges I have by going echo $myList[“oranges”];

To do something like this in JAVA is a bit different than just defining a normal array. In JAVA you need to use a Map:

HashMap myList = new HashMap();
myList.put("oranges", 15);
myList.put("apples", 25);

If you need to store two strings, then change the object type (I.e. HashMap).
To get the value, simply use the get method.

myList.get("oranges");

How to use Unix Timestamp with Java

You can use a unix timestamp in java wit the following code:

import java.util.Date;
String TimeStamp = "1324716371";
Date time=new Date((long)TimeStamp*1000);

Now, timestamp doesn’t have to be a string, however in many of your projects you may have the value in that format. In this case, we convert it to a long (integer is too small), and then multiply it by 1000 since Java uses milliseconds.

How to get the version name from the AndroidManifest.xml file

Use the following code snip in your application:

try
{
    String VersionName = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
}
catch (NameNotFoundException e)
{
    Log.v(tag, e.getMessage());
}

How to scroll a workspace/activity/layout

You need to add the ScrollView FrameLayout.  A great example on how to do this can be found here:

http://android-pro.blogspot.com/2010/02/android-scrollview.html

How to remove the title or “Application Name” bar

To remove the bar that appears at the top of your activity, modify your AndroidManifest.xml document.  Foreach activity, add the line:

android:theme="@android:style/ThemeNoTitleBar.Fullscreen"

Making the result look like:

<activity android:name=".MainActivity"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

How to save application settings/preferences

Here is the answer! 🙂

http://stackoverflow.com/questions/2154438/how-to-implement-remember-me-function-in-login-of-android-activity