Month: July 2014

Android Testing Device in Ubuntu

Hi Everyone,

Lately, I’ve tried to install my app onto an android device for testing. I’m making some server calls in it. Server is running in my ubuntu pc. I was not able to connect to my pc. I have checked every network issue that can popup, wasted lot of time only to finally know that Firewall was blocking the device. You can disable firewall using this command-

sudo ufw disable

Hope this helps!

Advertisements

Immutable Android App

Hi Guys,

In this post I would like to explain about Immutable Android App.

It is a great app to send special messages. Messages can also be locked setting a timer to unlock it. You can share it with any of your contact. Messages sent are not mutable. You can share it with new people but cannot change what was done in past. It’s like a diary, a proof.

You can also send surprise messages by locking them up and setting the exact time to unlock.

While wiriting new message which they call Immutable, we have a provision to set meta text where one can give glimpse of the message, realting to time or just as a subject to the message.

There is a limit on number of messages that on can write in a period of time. There is a nice cool down timer in settings menu. If someone is desperate to write more, they can purchase.

Overall it is very beatiful app.

Android Floating Context Menu for ListView/GridView

Hi,

While trying to implement floating context menu, I’ve faced few problems and finally reading through many posts I was able to get it working properly. Here is a sample for implementing it properly-

Suppose lView is your ListView/GridView.

In your onCreate method of activity, do this-

registerForContextMenu(lView);

As given below, you have to override few methods-

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    // below variable info contains clicked item info and it can be null; scroll down to see a fix for it
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch(item.getItemId()){
    case R.id.hide:
        hideItem(info.position);
        return true;
    case R.id.info:
        showItemInfo(info.position);
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

Here is the xml for my_context_menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/hide" android:title="Hide" android:showAsAction="always" android:visible="true"></item>
    <item android:id="@+id/info" android:title="Info" android:showAsAction="always" android:visible="true"></item>
</menu>

If you do not want to use xml, you can use below method instead of above on-

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Choose an option");
    menu.add(0, v.getId(), 0, "Hide");
    menu.add(0, v.getId(), 0, "Info");
}

If item.getMenuInfo() is null in onContextItemSelected(MenuItem item) method, I guess you are using custom ListView or GridView instead of android default ones. In such case, your custom View is not implementing getContextMenuInfo() method. Don’t worry we can fix that if you have its source. Open the view file and override the method getContextMenuInfo(). It should look something like this –

    @Override
    protected ContextMenuInfo getContextMenuInfo(){
    	ContextMenuInfo menuInfo = super.getContextMenuInfo();
    	if(menuInfo==null){
    		final ListAdapter adapter = mAdapter;
		final int motionPosition = mPerformClick.mClickMotionPosition;
            if (adapter != null && mItemCount > 0 &&
                    motionPosition != INVALID_POSITION &&
                    motionPosition < adapter.getCount()) {
                final View view = getChildAt(motionPosition);

                if (view != null) {
                    final int clickPosition = motionPosition + mFirstPosition;
                    menuInfo = new AdapterContextMenuInfo(view, clickPosition, adapter.getItemId(clickPosition));
                }
            }
    	}
    	return menuInfo;
    }

Hope this helps!
Cheers!

Android notifyDataSetChanged() Fix

Hi,

I was not able to refresh AdapterView data after modifying it. I have searched through some posts to find that I was doing it wrongly. I thought it must be the case with many people. So I decided to write it down to help others 🙂

After modifying data in AdapterView, calling notifyDataSetChanged() won’t refresh the data. Problem is When you use ArrayList Data to set for AdapterView it creates a seperate copy.

Fix is to clear the adapter completely and add data again.

It might be possible there is some other alternative. But as of now I find it useful. Please comment if you have something better.

Cheers!

Android AsyncTaskLoader

Hi Guys,

Lately I ran into a small problem with AsyncTaskLoader, I thought it would help you if I share 🙂

I was using AsycTaskLoader class to implement background loading for my app. I have written the code
in LoadInBackground() Method. I’ve started the loader using –

getSupportLoader().initLoader(0,null,null);

But my loadInBackground() method was never getting called. After searching for some time, I came across the solution-

forceLoad();

has to be called in onStartLoading() method of AsyncTaskLoader without which your loadInBackground() method never gets called. I’m not sure about the reason but it works !!