Posts

Android how to keep the white space inside of string.xml file

Quick solution for maintaining white space inside of string.xml file \u0020

solve one issue because of thread safe httpclient

I was using HttpClient to handle the http request and response. Previously I always debug on latest phones which are upper 4.0 everything works fine. One day I tried on my older phone Moto Defy running 2.3.7 from Cyanogenmod . There was one issue when I tried to post an image . I got the log : Invalid use of SingleClientConnManager: connection still allocated. or Caused by: java.lang.IllegalStateException: No wrapped connection. or Caused by: java.lang.IllegalStateException: Adapter is detached. Searched in stackoverflow normally it is because of accessing single instance of HttpClient in different thread or did not close InputStream of httpresponse.  Mine issue is the first one.  So I learnt one solution to get a thread safe httpclient. public static DefaultHttpClient getThreadSafeClient () { DefaultHttpClient client = new DefaultHttpClient (); ClientConnectionManager mgr = client . getConnectionManager (); HttpParams para...

Android Trick - AutoCompleteTextView & MultiAutoCompleteTextView & Spannable & EditText

Story came from I tried implement an EditText View which supports "Mention feature" like Facebook and Twitter. After user type in "@" symbol there is a dropdown list come out and let user select some item to replace that word started with "@". The first I try to use EditText and popup window to customize this component. Which I need to listen EditText text change and using popup window to show the dropdown list with suggestions. But when I tried to handle the focus between EditText and PopupWindow I gave up this way . Could not find better solution for that means when EditText get focus all the time my dropdown list can not be clicked . Because popup window needs focusable to make views inside can be clicked. But if I give popup window focus user can not type in anymore. Then I realized how stupid I worked. There supposed to be a solution from Android . Coz Contacts app support autocompletion. Then I found AutoCompleteTextView . Followed docume...

Problem when using Jackson json parser in Android.

I really like to use Jackson library to parse json string. Just now met a problem org.codehaus.jackson.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value The reason is my string contains some newline symbols. Then actually the string is not "valid" or "formatted" Json .  Finally I found one quick solution . try { JSONObject b = new JSONObject ( decodedPayload ); decodedPayload = b . toString (); } catch ( JSONException e ) { e . printStackTrace (); } That JSONObject can formatted that unformatted json string . Might be there are some other better solution. 

Android how to listen ListView scrolling offset problem

Today I wanted to implement the UI looks like Google plus app in both Android and IOS. One thing came out is that ListView and bottom menu. From Google Plus that bottom menu will slidedown when User scrolling the listview down and slideup when user scrolling the listview up. So the question is how to listen the listview scrolling up and down. First , there is no this api to listen how much listview scrolled by. Second, I tried the onInterceptTouchEvent in my customized ListView. It only works at first several touchevents. Then I checked the source codes of ListView and AbsListView. in function : private boolean startScrollIfNeeded ( int y ){ ... if ( overscroll || distance > mTouchSlop ) { ... if ( parent != null ) { parent . requestDisallowInterceptTouchEvent ( true ); } ... } ... } ok, thats why our interception not work anymore after the scrolling offset bigger t...

Android Trick - Pick photo from Gallery and 3rd party app

In Android we can using Intent to share data between applications. Simple usage for this part is Picking images or photos from the other applications. Normally the codes is like the following: private void pickPhotoFromGallery() {     Intent intent = new Intent();     intent.setType( "image/*" );     intent.setAction(Intent.ACTION_PICK);     //following line code will specify the dialog title of choosing different content source app     startActivityForResult(Intent.createChooser(intent, "Complete action using" ),SELECT_PHOTO ); } Noticed that if I set Intent Action as Intent.ACTION_PICK it will only select images from System Gallery App. if I called  intent.setAction(Intent. ACTION_GET_CONTENT );  It will get from the other apps. And the result is not same. From Gallery app the uri format is like : content://xxx/xx/xx but the other app will return uri like : file:///xxx/xx/xx ...

Android Trick - select Gridview item and make the cell highlighted

As Title said, I am making a customized calendar view . For date part I am using Gridview. But after I set item background and using setSelection(int) I could not highlighted the item I selected. this is the drawable for cell view background. <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:drawable="@drawable/bg_purple_round_corner" android:state_pressed="true">     <item android:drawable="@drawable/bg_purple_round_corner" android:state_checked="true">     <item android:drawable="@drawable/bg_purple_round_corner" android:state_selected="true">     <item android:drawable="@drawable/bg_white_round_corner" android:state_pressed="false"> </item></item></item></item></selector> This is the gridview codes:                  gvDates.setOnItemClickListener(new OnItemClickListener() { ...