Archive for 'Label String'

Android manifest permissions

 

Declares a security permission that can be used to limit access to specific components or features of this or other applications.

<permission android:description="string resource"
            android:icon="drawable resource"
            android:label="string resource"
            android:name="string"
            android:permissionGroup="string"
            android:protectionLevel=["normal" | "dangerous" | 
                                     "signature" | "signatureOrSystem"] />

Android application versionCode and versionName

 

android:versionCode — An integer value that represents the version of the application code, relative to other versions.

android:versionName — A string value that represents the release version of the application code, as it should be shown to users.

As with android:versionCode, the system does not use this value for any internal purpose, other than to enable applications to display it to users. Publishing services may also extract the android:versionName value for display to users.

 

Here’s an example



   
        ...
   

How to use Activity as Dialog in Android

If you want more control over the content and life cycle of your dialog box, conventional Dialog class wont help much. In such cases the solution is to implement Dialog as a full Activity. You can implement any screen you want as Dialog and you will be able to have full access to the Activity life-cycle event handlers.

The easiest way to make an Activity look like a dialog is to apply the android:style/Theme.Dialog theme when you add it to your manifest. Refer the following XML snippet

 
 

<activity android:name=”.ActivityDialogExample”

android:label=”@string/app_name”

android:theme=”@android:style/Theme.Dialog”>

<intent-filter>

<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

 
 

This will cause Activity to behave as a Dialog, floating on top of, and partially obscuring the Activity beneath it.

 
 

Try this out!!

 
 

Android String resources

Strings are a sequence of characters that are used in several scenarios. Android enables developers to use three types of

string resources, namely

  • Plain Strings.
  • String formats.
  • Styled texts.

Plain Strings:

Plain string resources are declared within res/values/strings.xml These are normal text strings with no

codes or styles.
When a new project is created in Eclipse, the ADT automatically generates a sample strings.xml file containing the appname

and hello.
The ADT also adds this to the string name in layout, main.xml and in AndroidManifest.xml.

strings.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources> 

<string name="hello">Hello World, SampleAPP!</string> 

<string name="app_name">SampleAPP</string>
</resources>

main.xml:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

Strings referenced from the res/layout/main.xml.
Strings can also be referenced from the java code.
The first string is the text of a textview defined in res/layout/main.xml file.

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.schogini.SampleAPP" android:versionCode="1" android:versionName="1.0"> 
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SampleAPP" android:label="@string/app_name"> 
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Referencing Strings:

@string/Resource_Name in xml files.

R.string.Resource_Name in java files.
R.java:

package com.schogini.SampleAPP;  
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon =0x7f020000;
}

public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}

strings.xml is never accessed or referenced directly in Java. Android converts them to R.Java and we access those stringsvia the R.java file.

The main advantage is that there is no need to parse xml file, android does this.String arrays:

String arrays can also be created.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>

Custom String Resources:
Our own custom resource strings file can also be created.

To create go to res/values/ directory in Package Explorer in Eclipse IDE,Right click and select,

New–>File and Enter a name for the file with Extension. (See Image Attached).
Resources can be added manually by choosing the myStrings.xml tab or by clicking “Add” button and adding the name and the

value of the resource.

custom String Resources  in Android

Custom String Resources in Android

Formatted strings:
The Dalvik Virtual Machine offers string formats, which provide placeholders representing data to be replaced at runtimeby variables.
Escaping apostrophes and quotes
If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the othertype of enclosing quotes. For example, here are some stings that do work and those don’t work:

<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This doesn't work</string>
<string name="bad_example_2">XML encodings don&apos;t work</string>

Formatting strings
Android allows strings to be formatted as in String.format(String, Object…) in java.lang.string Package. This can beaccomplished easily. The following are a few examples of exceptions.
<string name=”welcome_messages”>Hello, %1$s! You have %2$d new messages.</string>In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the

string with arguments from your application like this:

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages),username, mailCount);

Styled strings:
Android support the following HTML elements:

  • <b> for bold text.
  • <i> for italic text.
  • <u> for underline text.

To create a styled text resource that is also used as a format string, HTML tags can be used. Normally, this won’t workbecause the String.format(String, Object…) method will strip all the style information from the string. The work-around

to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place.

For example:

  • Store your styled text resource as an HTML-escaped string:
  • <resources>
      <string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>
    </resources>

    In this formatted string, a <b> element is added. Notice that the opening bracket is HTML-escaped, using the

    &lt; notation.

  • Then format the string as usual, but also call fromHtml(String) to convert the HTML text into styledtext:
  • Resources res = getResources();
    String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
    CharSequence styledText = Html.fromHtml(text);

    Because the fromHtml(String) method will format all HTML entities, be sure to escape any possible HTML characters in the

    strings you use with the formatted text, using htmlEncode(String). For instance, if you’ll be passing a string argument

    to String.format() that may contain characters such as “<” or “&”, then they must be escaped before formatting, so

    that when the formatted string is passed through fromHtml(String), the characters come out the way they were originally

    written.
    For example:

    String escapedUsername = TextUtil.htmlEncode(username);
    Resources res = getResources();
    String text = String.format(res.getString(R.string.welcome_messages), escapedUsername, mailCount);
    CharSequence styledText = Html.fromHtml(text);

    Executing application at Bootime on Android

    In Android OS, we can start an application at boot time. BroadcastReaceivers are used for implementing this. They can listen for defined system or custom defined intents. They are executed when the intent is fired, even when the application is not running at the moment. You can define custom actions and fire them manually to control custom receivers, but there are several predefined broadcast within the operating system that are fired automatically on certain events.One among them is android.intent.action.BOOT_COMPLETED action. To receive this action one has to extend the Broadcastreceiver class accordingly.

    A sample code snippet is Shown Below:

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.schogini.bootAPP"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".LaunchActivity" android:label="@string/app_name">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    </intent-filter>
    </activity>
    <receiver android:name="MyIntentReceiver">
    <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <category android:name="android.intent.category.HOME" />
    </intent-filter>
    </receiver>
    </application>
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    </manifest>
    

    Java Coding:

    package com.schogini.bootAPP;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class MyIntentReceiver extends BroadcastReceiver {
    //This is called when boot completes
    @Override
    public void onReceive(Context context, Intent intent) {
    //Set what activity should launch after boot completes
    Intent startupBootIntent = new Intent(context, LaunchActivity.class);//new class to be launched
    startupBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(startupBootIntent);
    }
    }
    

    Usage Scenario:
    An anti-virus/anti-malware app
    A security app
    Mobile Anti-theft App
    Mail Client…etc

    Playing the video in SD card of Android

    Here iam considering a 3gp video(http://www.free-3gp-video.com).

     Steps to insert video in SD card

    1) In eclipse open perspective DDMS(Window –> Open Perspective –> DDMS) .

    2)In DDMS perspective open view File Explorer(Window –> Show View –> File Explorer).

    3)open Device view (Window –> Show View –> Device) and click the emulator you want to use(if emulator is not running then run a emulator with a sdcard option).

    4)In File Explorer view navigate the tree shown and click sdcard node. There are two buttons on the top right hand side of File Explorer as shown below

    Click the button “Push a file onto the device” and in the file chooser navigate and choose the 3gp video.It should appear in the list of sdcard.
    Code to play video is as below

    AndroidManifest.xml
    ——————–
    <?xml version=”1.0″ encoding=”utf-8″?>
    < manifest xmlns:android=”http://schemas.android.com/apk/res/android”
          package=”sample.video” android:versionCode=”1″ android:versionName=”1.0.0″>
          < uses-permission android:name=”android.permission.MOUNT_UNMOUNT_FILESYSTEMS” />
          < application android:icon=”@drawable/icon” android:label=”@string/app_name”>
                < activity android:name=”.VideoActivity” android:label=”@string/app_name”>
                      < intent-filter>
                            < action android:name=”android.intent.action.MAIN” />
                            < category android:name=”android.intent.category.LAUNCHER”/>
                      < /intent-filter>
                </activity>
                < activity android:name=”.ViewVideo”>
                      < intent-filter>
                            < action android:name=”android.intent.action.VIEW” />
                            < category android:name=”android.intent.category.DEFAULT” />
                      </intent-filter>
                </activity>
          </application>
    </manifest>

    main.xml
    ——–
    <?xml version=”1.0″ encoding=”utf-8″?>
    < LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
          android:orientation=”vertical” android:layout_width=”fill_parent”
          android:layout_height=”fill_parent”>
          < ListView
                android:id=”@+id/PhoneVideoList”
                android:layout_width=”fill_parent”
                android:layout_height=”fill_parent”/>
    </LinearLayout>

    VideoActivity.java
    ——————-
    package sample.video;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.AdapterView.OnItemClickListener;

    public class VideoActivity extends Activity {
          private Cursor videocursor;
          private int video_column_index;
          ListView videolist;
          int count;

          /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                init_phone_video_grid();
          }

          private void init_phone_video_grid() {
                System.gc();
                String[] proj = { MediaStore.Video.Media._ID,
    MediaStore.Video.Media.DATA,
    MediaStore.Video.Media.DISPLAY_NAME,
    MediaStore.Video.Media.SIZE };
                videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
    proj, null, null, null);
                count = videocursor.getCount();
                videolist = (ListView) findViewById(R.id.PhoneVideoList);
                videolist.setAdapter(new VideoAdapter(getApplicationContext()));
                videolist.setOnItemClickListener(videogridlistener);
          }

          private OnItemClickListener videogridlistener = new OnItemClickListener() {
                public void onItemClick(AdapterView parent, View v, int position,
    long id) {
                      System.gc();
                      video_column_index = videocursor
    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                      videocursor.moveToPosition(position);
                      String filename = videocursor.getString(video_column_index);
                      Intent intent = new Intent(VideoActivity.this, ViewVideo.class);
                      intent.putExtra(“videofilename”, filename);
                      startActivity(intent);
                }
          };

          public class VideoAdapter extends BaseAdapter {
                private Context vContext;

                public VideoAdapter(Context c) {
                      vContext = c;
                }

                public int getCount() {
                      return count;
                }

                public Object getItem(int position) {
                      return position;
                }

                public long getItemId(int position) {
                      return position;
                }

                public View getView(int position, View convertView, ViewGroup parent) {
                      System.gc();
                      TextView tv = new TextView(vContext.getApplicationContext());
                      String id = null;
                      if (convertView == null) {
                            video_column_index = videocursor
    .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
                            videocursor.moveToPosition(position);
                            id = videocursor.getString(video_column_index);
                            video_column_index = videocursor
    .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
                            videocursor.moveToPosition(position);
                            id += ” Size(KB):” + videocursor.getString(video_column_index);
                            tv.setText(id);
                      } else
                            tv = (TextView) convertView;
                      return tv;
                }
          }
    }

    ViewVideo.java
    —————
    package sample.video;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.MediaController;
    import android.widget.VideoView;

    public class ViewVideo extends Activity {
          private String filename;
          @Override
          public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                System.gc();
                Intent i = getIntent();
                Bundle extras = i.getExtras();
                filename = extras.getString(“videofilename”);
                VideoView vv = new VideoView(getApplicationContext());
                setContentView(vv);
                vv.setVideoPath(filename);
                vv.setMediaController(new MediaController(this));
                vv.requestFocus();
                vv.start();
          }
    }

     The output will be as shown below.

    Incoming search terms:

    • android video plaslit code
    • what is sd card listener
    We Can Build Your App for You. Lets Get Started !