Thursday, 19 June 2014

Collection.sort Comparator and Collator - compare special chars or just polish words

Here is the short example code.

 Collections.sort(parse, new Comparator<HashMap<String, String>>() {  
           @Override  
           public int compare(HashMap<String, String> lhs, HashMap<String, String> rhs) {  
             String station1 = null;  
             String station2 = null;  
   
             try {  
               station1 = new String(lhs.get("station").getBytes(), "utf-8");  
               station2 = new String(rhs.get("station").getBytes(), "utf-8");  
             } catch (UnsupportedEncodingException e) {  
               e.printStackTrace();  
             }  
   
             Locale locale = new Locale("pl_PL");  
             Collator collator = Collator.getInstance(locale);  
             return collator.compare(station1, station2);  
           }  
         });  

Tuesday, 3 June 2014

Example creating DialogFragment with onClickListener

Okay, another post with example DialogFragment with onClickListener.
This a my CustomDialogFragment class:

 public class CustomDialogFragment extends DialogFragment {  
   
   public static final String TAG = CustomDialogFragment.class.getSimpleName();  
   private DialogInterface.OnClickListener onItemClickListener;  
   private ImageView icon;  
   private TextView msg;  
   private Button btn;  
   private int iconRes;  
   private String msgRes;  
   private String btnRes;  
   private Context context;  
   
   public static CustomDialogFragment getInstance() {  
     CustomDialogFragment f = new CustomDialogFragment();  
     return f;  
   }  
   
   public void setOnClickListener(DialogInterface.OnClickListener onItemClickListener) {  
     this.onItemClickListener = onItemClickListener;  
   }  
   
   @Override  
   public Dialog onCreateDialog(Bundle savedInstanceState) {  
   
     final Dialog dialog = new Dialog(getActivity());  
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);  
   
     View view = LayoutInflater.from(getActivity()).inflate(R.layout.custom_dialog_fragment, null);  
   
     icon = (ImageView) view.findViewById(R.id.image1);  
     msg = (TextView) view.findViewById(R.id.text1);  
     btn = (Button) view.findViewById(R.id.button1);  
   
     if (iconRes != 0)  
       icon.setImageResource(iconRes);  
   
     if (msgRes != null)  
       msg.setText(msgRes);  
   
     if (btnRes != null)  
       btn.setText(btnRes);  
   
     if (onItemClickListener != null) {  
       btn.setOnClickListener(new View.OnClickListener() {  
         @Override  
         public void onClick(View v) {  
           onItemClickListener.onClick(dialog, 0);  
         }  
       });  
     }  
   
     dialog.setContentView(view);  
   
     return dialog;  
   }  
   
   public void setIconImageResource(int res) {  
     iconRes = res;  
   }  
   
   public void setMessageTextResource(int res) {  
     msgRes = context.getString(res);  
   }  
   
   public void setMessageText(String res) {  
     msgRes = res;  
   }  
   
   public void setButtonTextResource(int res) {  
     btnRes = context.getString(res);  
   }  
   
   public void setButtonTextResource(String res) {  
     btnRes = res;  
   }  
   
   public void setContext(Context context) {  
     this.context = context;  
   }  
   
 }  

And here is dialog layout:

 <?xml version="1.0" encoding="utf-8"?>  
   
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:padding="50dp"  
   android:background="#ff0000"  
   >  
   
   <ImageView  
     android:id="@+id/image1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:src="@drawable/ic_launcher" />  
   
   <TextView  
     android:layout_below="@+id/image1"  
     android:id="@+id/text1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:text="This is message" />  
   
   <View  
     android:layout_below="@+id/text1"  
     android:id="@+id/separator"  
     android:layout_width="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:layout_height="10dp" />  
   
   <Button  
     android:layout_below="@id/separator"  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:text="OK" />  
 </RelativeLayout>  

And here is initalization in any FragmentActivity class:

 public class MainActivity extends ActionBarActivity {  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
   
     initDialog();  
   }  
   
   private void initDialog() {  
     CustomDialogFragment cdf = CustomDialogFragment.getInstance();  
     cdf.setContext(this);  
     cdf.setIconImageResource(android.R.drawable.btn_star);  
     cdf.setMessageText("Hello world!");  
     cdf.setButtonTextResource(android.R.string.ok);  
     cdf.setOnClickListener(new DialogInterface.OnClickListener() {  
       @Override  
       public void onClick(DialogInterface dialog, int which) {  
         dialog.dismiss();  
       }  
     });  
   
     Fragment fragmentByTag = getSupportFragmentManager().findFragmentByTag(CustomDialogFragment.TAG);//avoid opening twice dialog  
     if (fragmentByTag == null)  
       cdf.show(getSupportFragmentManager(), CustomDialogFragment.TAG);  
   }  
   
 }  

Sample pic:

Uri from local drawables for universal image loader

Here is short example how display local drawables by universal image loader.

We have somewhere xml, array with images:

 <array name="avatars_male">  
      <item>@drawable/pic0</item>  
      <item>@drawable/pic1</item>  
      <item>@drawable/pic2</item>  
      <item>@drawable/pic3</item>  
      <item>@drawable/pic4</item>  
      <item>@drawable/pic5</item>  
      <item>@drawable/pic6</item>  
      <item>@drawable/pic7</item>  
      <item>@drawable/pic8</item>  
      <item>@drawable/pic9</item>  
      <item>@drawable/pic10</item>  
      <item>@drawable/pic11</item>  
      <item>@drawable/pic12</item>  
      <item>@drawable/pic13</item>  
 </array>  

We can load them to list:

 ArrayList<Integer> array = new ArrayList<Integer>();  
 TypedArray typedArray = getResources().obtainTypedArray(R.id.avatars_male);  
 for (int i = 0; i < 15; i++) {  
      array.add(typedArray.getResourceId(i, 0));  
 }  

And now we can take any pic by uri:

 int num = 10;//<according to value from dice :)  
   
 Uri uri = Uri.parse("drawable://" + array.get(num));  

Few configs how to prepare universal image loader:

 ImageLoaderConfiguration configBuilder = new ImageLoaderConfiguration.Builder(context)  
           .threadPriority(Thread.NORM_PRIORITY - 2)  
           .discCacheExtraOptions(250, 250, Bitmap.CompressFormat.JPEG, 75, null).build();  
   
 ImageLoader imageLoader = ImageLoader.getInstance();  
 imageLoader.init(configBuilder);  
   
 DisplayImageOptions imageLoaderOptions = new DisplayImageOptions.Builder()  
           .cacheInMemory(true) // default  
           .cacheOnDisc(true) // default  
           .imageScaleType(ImageScaleType.EXACTLY) // default  
           .bitmapConfig(Bitmap.Config.ARGB_8888) // default  
           .handler(new Handler()) // default  
           .build();  

And displaying:

 imageLoader.displayImage(String.valueOf(uri), holder.iv, imageLoaderOptions);