Thursday, 29 May 2014

Avoid blinking imageViews in android - example nostra confuguration

Here is short code describing configuration for imageLoader to avoid blinking images when scrolling.

 imageLoader = ImageLoader.getInstance();  
 imageLoader.init(configBuilder);  
   
 imageLoaderOptions = new DisplayImageOptions.Builder()  
           .cacheInMemory(true)   
           .cacheOnDisc(true)   
           .imageScaleType(ImageScaleType.EXACTLY)   
           .bitmapConfig(Bitmap.Config.ARGB_8888)   
           .handler(new Handler())   
           .build();  
             
 imageLoader.displayImage("https://i.imgur.com/1od0gIFl.jpg", myImageView, imageLoaderOptions);  

Sunday, 25 May 2014

How to hide keyboard softinputmode in android? By view or without - example

Okay, here is another short code describing how to hide soft keyboard in android. I usually use this second code because I don't have to bother with specifying view. It is very important to call hide keyboard when all elements were initialized.

//1 method - you specify view with focus
public void hideKeyboard(View view) {  
      getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);  
      InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);  
   
      if (view != null) {  
           inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);  
      }  
 }  
 
//  2 method - gets any view which has focus
public void hideKeyboard(Activity activity) {  
      activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);  
      InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);  
            View cur_focus = activity.getCurrentFocus();  
      if (cur_focus != null) {  
           inputMethodManager.hideSoftInputFromWindow(cur_focus.getWindowToken(), 0);  
      }  
}  
   

Thursday, 15 May 2014

Regex - Intellij - examples

 search android:text= without @string refference  
   
 expression:  
 android\:text\=\"[^@].+  
   
 Text  
 android:text="balloon_item_title"<--  
 android:text="@balloon_item_title"  
 android:text="1234"<--  

 select <TextView...> or </TextView...>  
   
 expression:  
 \<(\/|)TextView   

 select just TextView word in <TextView...> or </TextView...>  
   
 Expression  
 (?!=\<(\/|))TextView   

Wednesday, 14 May 2014

Example of dialog fragment with listview (coded)

Here is short example how create dialog fragment with list in code.

 public class DialogMyListFragment extends DialogFragment {  
   
   private android.widget.AdapterView.OnItemClickListener onItemClickListener;  
   private Core application;  
   public static String TAG = "DialogMyListFragment";  
   
   public static DialogMyListFragment getInstance() {  
     DialogMyListFragment df = new DialogMyListFragment();  
     return df;  
   }  
   
   public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {  
     this.onItemClickListener = onItemClickListener;  
   }  
   
   @Override  
   public Dialog onCreateDialog(Bundle savedInstanceState) {  
   
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());  
     ListView list = new ListView(getActivity());  
   
     application = (Core) getActivity().getApplication();  
   
     List<String> elements = new ArrayList<String>();  
           for(int i=0;i<10;i++)  
                elements.add("item " + i);  
             
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, elements);  
       
           list.setAdapter(adapter);  
     list.setOnItemClickListener(onItemClickListener);  
   
     builder.setView(list);  
   
     return builder.create();  
   
   }  
   
 }  

And then you call

 DialogMyListFragment df = new DialogMyListFragment().getInstance();  
 df.setOnItemClickListener(listener); 
 Fragment fr = getSupportFragmentManager().findFragmentByTag(DialogMyListFragment.TAG);  
 if (fr == null) {  
      df.show(getSupportFragmentManager(), DialogMyListFragment.TAG);  
 }   

Friday, 9 May 2014

Playing system notification sound with soundpool, converting uri to path

Here is short example, I think there is no need to explain. This works from 2.3+

 //take an uri and convert to path  
 Uri uriSound = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION;  
 String soundPath = getRealPathFromURI(uriSound);  
   
 //get natification system volume  
 AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);  
 final int currentVolume = audio.getStreamVolume(AudioManager.STREAM_NOTIFICATION);  
   
 //create pool, load system notification sound, when load - set volume and play  
 SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);  
 final int loadedNotification = soundPool.load(soundPath, 1);  
 soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {  
      @Override  
      public void onLoadComplete(SoundPool soundPool, int i, int i2) {  
           soundPool.setVolume(loadedNotification, currentVolume, currentVolume);  
           soundPool.play(loadedNotification, 1, 1, 1, 0, 1);  
      }  
 });  
   
 //somewhere created method...  
   
 private String getRealPathFromURI(Uri contentUri) {  
      String[] proj = { MediaStore.Images.Media.DATA };  
      CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null);  
      Cursor cursor = loader.loadInBackground();  
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);  
      cursor.moveToFirst();  
      return cursor.getString(column_index);  
 }  

Monday, 5 May 2014

Avoiding double executing fragment element - i.e DialogFragment

In short example:

 //somewhere I create instance of my fragment  
 df = new DialogAllSectionsFragment().getInstance();  
   
 //when calling the fragment  
 Fragment fr = getSupportFragmentManager().findFragmentByTag(DialogAllSectionsFragment.TAG);  
 if (fr == null) {  
      df.show(getSupportFragmentManager(), DialogAllSectionsFragment.TAG);  
 }  

Every fragment element has own TAG with own name, so when fragmentManager finds the current tag exists it just doesn't call it again. It helps when user doubleclik one of our buttons.

Friday, 2 May 2014

Libgdx - custom label text style declared in fonts.json

Here is short tutorial how set custom layout with custom font from json file.

What we need:
  1. create json file describing font style
  2. load json file to skin
  3. create own label with custom style
So let's start:

First I create font.json file to describe font style.

 {   
   com.badlogic.gdx.graphics.g2d.BitmapFont:  
   {  
     normaltext: { file: fonts64.fnt },  
     smalltext: { file: fonts32.fnt },  
   },  
   com.badlogic.gdx.graphics.Color:  
   {  
     white: { hex: "FFFFFF" },  
     green: { hex: "99CC00" },  
   },  
   com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle:  
   {  
     default: { font: normaltext, fontColor: green},  
     smalltext: { font: smalltext, fontColor: white },  
   }  
 }  

In

 com.badlogic.gdx.graphics.g2d.BitmapFont:   
 {   
        ...  
 },   

I declare font files, as you see I use two types of fonts. You can use just one. I assume you created own fonts by program i.e bmfont and stored in "your-project/assets/data/" folder.

In

 com.badlogic.gdx.graphics.Color:   
 {   
  white: { hex: "FFFFFF" },   
  green: { hex: "99CC00" },   
 },   

I declare color which will be used.

In

 com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle:   
 {   
  default: { font: normaltext, fontColor: green},   
  smalltext: { font: smalltext, fontColor: white },   
 }  

I declare here syles. That names will be used in java code to describe which style to use for label. I didn't add here "normaltext" because I won't use it in this example, so don't worry. When you finish your file, place it in "your-project/assets/data/" - the same place where the your .fnt file is stored.

OK, so we created file and stored in specific path. Now I need to go back to Java code. I must create skin object to load this json file.

 Skin skin = new Skin(Gdx.files.internal("data/fonts.json"));

Next step is to create label in code and use own style.

 Label instructionLabel = new Label("My instruction", skin, "smalltext");  

The third argument is the my custom style of font. If I didn't add this third element, label would load "default" style. But I want own style so I added "smalltext".

So, that's it! Don't forget add this label as "addActor(instructionLabel)" to your stage.