Tag Archives: android

[Android] Mimic press event of Back button

Must dispatch both ACTION_DOWN and ACTION_UPDATE KeyEvent

// trigger button back programmatically 

this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));

this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));

Advertisements

[Android] Webview on android 2.x does not show content properly with loadData() function

Fix: 

mWebView.loadDataWithBaseURL(null, htmlContent, “text/html”, “UTF8”, null);


[android] Full-screen mode on ICS and above not working on 2.3

That is when you got error like :

java.lang.NoSuchMethodError: android.view.View.setSystemUiVisibility

when you try to hide the task bar on your system with “

getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_HIDDEN);

And here is how I fixed it:

if(SDK_INT >= 11 && SDK_INT < 14) {
getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_HIDDEN);
}else if(SDK_INT >= 14){
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
}

 

Or you can find the well-formated snippet here:

https://gist.github.com/tttoan/5428334

 

 

 

 


[android] Setting up project with Sliding Menu — got some errors

1/ For the error

“The method getSupportActionBar() is undefined for the type BaseActivity”

Inside the SlidingMenu library, edit the class SlidingFragmentActivity to extend SherlockFragmentActivity (like below). Then clean and rebuild, this method should now be found.

public class SlidingFragmentActivity extends SherlockFragmentActivity implements SlidingActivityBase {


2/  http://boroniatechnologies.com/installing-slidingmenu-android-library-and-example/ 

[Android] Solving problem when setup ActionBarSherlock libs and sample project.

The hierarchy of the type ‘Class name’ is inconsistent

My Eclipse, together with Android SDK has settings for java compiler:

– Compiler compliance level : 1.6  ( Ubuntu + openjdk) ( Window –> Pref –> Java –> Compiler)

when import ActionBarSherlock project, I must change it back to this compliance level.
–> Enable Project Specific Setting.
–> Set Compiler Compliance level of this project to be 1.6

Done !

Clear and rebuild project.


[Instagram] How to get more than 60 images from Instagram service

Hi,

Recently, I have created an app that uses instagram images ,.( I made this app for other guy, so i can not reveal it here ).
The problem is that , Instagram only returned first 60 images per returned pack. So if you have more than 60 images, and you want to get these older images, you must find out some way to deal with that. And here is what I did in my app.

It turned out  Instagram provided us a pagination mechanism to get each 60 images, The first request will return 60 newest images, then you then the oldest id of images returned from previous pack.
Just add token “max_id” with the token’s value is the oldest image id:
For example, this is how I create my JSONObject for request more images.
JSONObject feedJson = executeHttpGet("https://api.instagram.com/v1/users/"
+ userId
+ "/media/recent/"
+ "?access_token="
+ token
+ "&count=" + mediaCount
+ "&max_id=" + mOldestId);

The excuteHttpGet is my private method based on HttpClient and HttpResponse to send get read the response from server.

That’s it.


[ANDROID] Set title of app to be centered

Use this:

((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);


Comment: This will help your app crash in ICS ( >= 4.0)


Change appearance of button/imageview/imagebutton in android for different states

“States” here, I mean the press/focus states of control. We have normal state, on-focusing state and pressed-state. ( I used these, and don’t care about others ).
I find an interesting post at http://www.mkyong.com/android/android-imagebutton-selector-example/

Just re-post in my blog for later use.
Step 1 : Add images to resource.
button_normal_green.png – Default image button.
button_focused_orange.png – Display when button is focused, for example, when phone’s keypad is move (focus) on this button.
button_pressed_yellow.png – Display when button is pressed.
Step 2: Now, create a new XML file in “res/drawable/” folder, in whatever name you want, in this case, we just give a name as “new_button.xml“. This file defined which button state is belong to which image.

Now, you can refer to this button via this Id : @drawable/new_button.

Step 3: Add Button
Open “res/layout/main.xml” file, add a normal button, and attach the background image to above “new_button” via “android:background=”@drawable/new_button”
File : res/layout/main.xml

Step 4: Code the main activity
A normal button with a simple click listener.

File : MyAndroidAppActivity.java



import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class MyAndroidAppActivity extends Activity {

Button imageButton;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

addListenerOnButton();

}

public void addListenerOnButton() {

imageButton = (Button) findViewById(R.id.imageButtonSelector);

imageButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

Toast.makeText(MyAndroidAppActivity.this,
"ImageButton (selector) is clicked!",
Toast.LENGTH_SHORT).show();

}

});

}

}

Reference :
1/ http://www.mkyong.com/android/android-imagebutton-example/
2/ http://developer.android.com/resources/tutorials/views/hello-formstuff.html

3/ Sample source code can be found here http://www.mediafire.com/?h1gvjdagmkupuiu