Posts

Showing posts from April, 2010

ExpandableListActivity in Android

Image
I want to use expandablelist activity, when I want to show two lists in a single screen like a main list and a sub-list. For example, a list of countries are there(Main list) and I want to show the main cities(sub-list) of theses countries. In that case, expandable list activity will be very useful. This is just one use of expandable list. Like that it can use for hundreds of purposes.

The complete code is,


public class CountryStateList extends ExpandableListActivity {

private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
private ExpandableListAdapter mAdapter;
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
InputStream is;
boolean addState = true;
boolean addCountry;
HashMap<String, String> countryMap = new HashMap<String, String>();
HashMap<…

AlarmManager and Notification in Android

Image
In this, I am going to explain AlarmManager and Notification. For Notifying Users in Android, we can use mainly 3 methods as,

Dialog
Toast
Notification

The full picture of this can seen here

AlarmManager, we can use in two ways, as

-To start some action, if a particular event happens,
-To do some particular actions in every time interval.

As specified in Android developer site, we will use AlarmManager, when we need to do some actions, even if our application is not running. For normal timing operations use handlers.

For using AlaramManager, first we need to declare the BroadcastReceiver in Manifest file as,

<receiver android:name=".TimeAlarm" />
We need to get the object of the AlarmManager using getSystemService(), then define an intent with parameters as the current context and the BoradcastReceiver class(TimeAlarm). Then define a pendingIntent, we will get the object of it using PendingIntent.getBroadcast(). Now we can call the set() of AlarmManager for doing some ac…

AsyncTask in Android

AsyncTask allows us to do proper background operations without creating threads and handlers. AsyncTask will run in the background thread and whose results we can pass to the UI thread. To implement AsyncTask, what we need to do is, create a class which extends AsyncTask and need to override some methods in it like doInBackground(), onPostExecute(), onProgressUpdate().

Sample program is,


private class BackgroundTask extends AsyncTask {

@Override
protected Void doInBackground(Void... params) {
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
protected void onProgressUpdate(Integer... progress) {
}

}


We can call AsyncTask using, new BackgroundTask().execute();

Fetching IMEI number in Android

We can fetch the IMEI number of android phones using,


TelephonyManager mTelephonyMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
String imei = mTelephonyMgr.getDeviceId();

ContentProvider in Android

I already told about the built-in content-providers in Android, in one of my tutorial

In this, I am creating a new custom content provider. For this, first we will declare the content provider in Manifest file as,

<provider android:name="Provider" android:authorities="simple.provider"/>

So,Provider is my content provider name. Created a class called Provider which extends ContentProvider. The CONTENT_URI, which I am going to use is, "content://simple.provider/mytable"

The complete source code is,

Provider.java


public class Provider extends ContentProvider {

public static final String AUTHORITY = "simple.provider";
// public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
// + "/table1");
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/mytable");
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.provider.mytable";
public static …

ViewStub in Android

Image
In this, I just going to show how to use ViewStub in Android. ViewStub we can achieve through <merge> attribute. ViewStub is like a place holder and dynamically we can add layout to our main layout. In viewstub, we have to you the "andorid:layout" attribute and add any layout trough this.
Once again, I am telling this is just a very basic tutorial for ViewStub.

The complete code is,

SampleViewStub.java

public class SampleViewStub extends Activity { ViewStub stub; boolean click = true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.openstub).setOnClickListener(new OnClickListener() { public void onClick(View v) { if (click) { stub = (ViewStub) findViewById(R.id.stub1); stub.inflate(); click = false; } } }); } }

main.xml

<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android…

Swipe Action and ViewFlipper in Android

Last tutorial I talked about ViewFlipper, in this I am just extending the functonality of ViewFlipper with swipe action. ViewFlipper is mainly used to animate views. I have two or three views added to the ViewFlipper and using the API showNext() and showPrevious(), we can show the next and previous view in the ViewFlipper. At the time of switching, we can create some animation and it looks great.

Foe switching between views, we can either use time interval or through user interaction. Last tutorial I used button for switching between Views. In this tutorial I am going to use swipe action for switching views.

class MyGestureDetector extends SimpleOnGestureListener { private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { System.out.println(" in onFling() :: "); if (Ma…

Changing layout height dynamically

We can change the layout height dynamically to say, x by using

getLayoutParams().height= x;
requestLayout(); or invalidate();

ViewFlipper in Android

ViewFlipper is used to do animation when switching between views. There are lot of options for doing animations between views like ViewAnimator, LayoutAnimationController, applying animation to the view itself. But I feel ViewFlipper is the better option for doing animation for switching views.

In this I am controlling the switching between views using user interaction(Button click). We can set duration also for switching between views.

The complete code for showing ViewFlipper is,


public class ShowFlipper extends Activity {

ViewFlipper flipper;
Button button1;
Button button2;
LinearLayout l1;
LinearLayout l2;
TextView tv1, tv2, tv3, tv4, tv5, tv6;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);

flipper = new ViewFlipper(this);
l1 = new LinearLayout(this);
l2 = new LinearLayout(this);
button1 = new Button(this);
button2 = new Button(thi…

PopupWindow in Android

This tutorial is about PopUpWindow in Android. Actually popupwindow came to my mind, when I need a dialog, to position at the bottom of the screen. PopupWindow is like "android.app.Dialog" only, but the main difference is in this we can position, where it need to be displayed. Not only that, now using the setElevation() (from API level 21) we can use it as a context menu. For example when long click on an view, pop up a window using PopupWindow and give the options as a list

In popupwindow also we can add a layout, much like in dialog.

The complete code is,
public class ShowPopUp extends Activity { PopupWindow popUp; LinearLayout layout; TextView tv; LayoutParams params; LinearLayout mainLayout; Button but; boolean click = true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); popUp = new PopupWindow(this); layout = new LinearLayout(this); mainLayout = new LinearLayout(this); tv = new TextView(this); but…

Activity Launch Mode in Android

Android activity launchMode

4 modes...

"standard"
"singleTop"
"singleTask"
"singleInstance"

The default mode is "standard".

The modes fall into two groups. standard and singleTop comes in one side and singleTask and singleInstance comes in another side.

The main difference between standard and singleTop is in standard, every time a new intent for standard activity, a new instance is created. In case of singleTop too, a new instance is created but an instance of the activity is already in top of the stack, it wont create a new instance.

Actually, the issue comes , when we download an application from a server and launch it and open it from there itself. After launching the application, press home button. Then click the all programs and select the icon of the application from home screen. Then another activity will be created in the case of standard, but in singleTop , no new instance will be created.

The "singleTask" and "singleI…

Failed to initialize Monitor Thread in Android

When I am working on Eclipse suddenly, I got this error

Failed to initialize Monitor Thread: Unable to establish loopback connection android

I have no idea, why this error occurs. After some mind grilling, I came to know, its because of the settings in firewall, so reset the firewall and my eclipse and adb work normally.