Archive for the ‘Android’ Category

17
Feb

In the onCreate() you have to add following code:

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(dashboard.this, "" + position, Toast.LENGTH_SHORT).show();
}
});

Next is to include ImageAdapter class:

 

class ImageAdapter extends BaseAdapter {

private Context mContext;

public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return mThumbIds.length;
}

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

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

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {  // if it’s not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}

imageView.setImageResource(mThumbIds[position]);
return imageView;
}

// references to our images
private Integer[] mThumbIds = { R.drawable.img1, R.drawable.img2   };
}

** You have to copy img1.png and img2.png in the drawable folder **

15
Feb

Create a fonts folder in assets folder and put  TTF fonts in it. After that you have to add following two line to include that fonts:

TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
tv.setTypeface(face);

That’s it!

14
Feb

The WLAN MAC Address string, is a unique identifier that you can use as a device id. Before you read it, you will need to make sure that your project has the android.permission.ACCESS_WIFI_STATE permission or the WLAN MAC Address will come up as null.

 WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
String WLANMAC = wm.getConnectionInfo().getMacAddress();

14
Feb

it is possible to find two devices with the same Pseudo-Unique ID, but the chances in real world applications are negligible

String pseudoUniqueID =
        	Build.BOARD.length()%10+ Build.BRAND.length()%10 +
        	Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 +
        	Build.DISPLAY.length()%10 + Build.HOST.length()%10 +
        	Build.ID.length()%10 + Build.MANUFACTURER.length()%10 +
        	Build.MODEL.length()%10 + Build.PRODUCT.length()%10 +
        	Build.TAGS.length()%10 + Build.TYPE.length()%10 +
        	Build.USER.length()%10 ; //13 digits

14
Feb

The IMEI: only for Android devices with Phone use:

TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String szImei = TelephonyMgr.getDeviceId(); // Requires READ_PHONE_STATE

This requires adding a permission in AndroidManifest.xml, and users will be notified upon installing your software: android.permission.READ_PHONE_STATE. The IMEI is unique for your phone and it looks like this: 114985038630335 (unless you have a pre-production device with an invalid IMEI like 0000000000000).

10
Feb

It is possible to show google map in your android app without creating an xml layout for placing google map.

//Step1: extends MapActivity

public class MyMap extends MapActivity{

//Step2: create instance of MapView

private MapView mapView;

mapView = new MapView(this, "google map api key");

mapView.setEnabled(true);

mapView.setSatellite(false);

//Step3: Set mapView as content view

this.setContentView(mapView);

}

//Step 4:

** in the manifest file you must add following line:

<uses-permission android:name=”android.permission.INTERNET” />

That’s all.

09
Feb

It is very easy and simple to send SMS from your Android app. Copy the following code snippet and paste it in your code:

Intent intent = new Intent( Intent.ACTION_VIEW,  Uri.parse( "sms:" ) );

// or you can add phone here

//Intent intent = new Intent( Intent.ACTION_VIEW,  Uri.parse( "sms:" + your_phone_number) );

String smsMessage = "Hello World";

intent.putExtra("sms_body", smsMessage);

startActivity( intent );

02
Feb

String str = “one,two,three,four,five”;

String arr[] = str.split(“,”);

System.out.println(“Array Size: ” + arr.length);

The ‘|’ character means OR in regex, so you have to split it like following:

line.split("\\|");

 

That’s it!

31
Jan

//Usage from activity:

  //String verison = GlobalSettings.getVersionName(this,MyActivity.class)
  public static String getVersionName(Context context, Class cls) {
    try {
      ComponentName comp = new ComponentName(context, cls);
      PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
      return "Version: " + pinfo.versionName;
    } catch (android.content.pm.PackageManager.NameNotFoundException e) {
      return null;
    }
  }

28
Jan

To start Services automatically after the Android system starts you can register a BroadcastReceiver to the Android android.intent.action.BOOT_COMPLETED system event.

In the onReceive() method the corresponding BroadcastReceiver would then start the service.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Intent service = new Intent(context, WordService.class);
		context.startService(service);
	}
}

If you application is installed on the SD card, then it is not available after the android.intent.action.BOOT_COMPLETED event. Register yourself in this case for the android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE event.

Also note that as of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.