Posts

Showing posts from 2013

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() { ...

Android Trick - make gridview unscrollable

Damon How to make Android Gridview unscrollable. I found one solution is : GridView .setOnTouchListener( new OnTouchListener(){     public boolean onTouch(View v, MotionEvent event) {         return event.getAction() == MotionEvent. ACTION_MOVE ;     } }); the code means consume the gridview's ACTION_MOVE touch event.

Android Trick - Using intent to Send SMS and come back

Damon In Android we can use Intent to send SMS or calling phone number and something else. System will start default ( Or show a list of apps who can accept this intent ) app. Then if you want to come back to your own application after that you will need this . Intent sendIntent = new Intent(Intent.ACTION_SENDTO);  sendIntent.setData(Uri.parse("sms:" + number)); sendIntent.putExtra("sms_body", smsContent);  sendIntent.putExtra("exit_on_sent", true); startActivity(sendIntent); Not sure whether it also works for the other Intents. 

customize a button with click effect on Android

Damon Usually in Android when we need button click effect we will define different state images and create a drawable as button background. But sometime it is really troublesome. coz you have to create different drawable files for different button. I am so lazy so try to find some generic ways. So I just extends the Button and when get touch event change the colors. Following codes. public class EffectButton extends Button { int textColor ; public EffectButton(Context context, AttributeSet attrs) { super(context, attrs); int[] attrsArray = new int[] { android.R.attr.textColor }; TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray); textColor = ta.getColor(0, Color.TRANSPARENT); ta.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { getBackground(...

How to customize Pie Chart View in Android

Image
Damon How to customize Pie Chart View in Android My app needs customize pie chart view. Found some open source projects. Either too complex or not so good. Spent several hours to customize own simple one. Learned how to use path and  Path.arcTo . Code first. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 public   class  PieChartView  extends  ...