Dec 23, 2014

get android device language



Sometime we need device's language.

Android provide class 'Locale'

Example
  1.         Locale mLocale = getResources().getConfiguration().locale;
  2.         String language = mLocale.getLanguage();
  3.         Log.e("lng",language);
  4.         if(language.contains("ko"))
  5.             in korean
  6.         else{
  7.             another language
  8.         }

Line 1 : Get country and language

Line 2 : Get language

Line 3 : show log

Line 4 : 'ko' mean korean. (english is 'en')

Line 6 : another language.

Android sound ( SoundPool)



Class' SoundPool' is for playing sound in Android.

Use SoundPool.Builder instead of SoundPool in API 21.

But, I use SoundPool in this post.

Example
  1. SoundPool pool=new SoundPool(1, AudioManager.STREAM_MUSIC,0);
  2. int hahaha=pool.load(this,R.raw.hahaha,1);
  3. pool.play(hahaha,1,1,00,1);

Line 1 : Initialize


First parameter : Default 1

Second parameter : Default STREAM_MUSIC

last parameter : default 0

Line 3 : Load sound file.


First parameter : Context

Second parameter : sound file name(path :  res/raw/ )

Third parameter : priority, default 1

P.S return value is integer

Line 5 : play the sound


First parameter : Sound id, integer

Second,Third parameter : Volume

Forth parameter : priority default 0

Fifth parameter : Repeat, no repeat is 0

Last parameter : Speed of play, normal is 1

P.S Your mp3 file copy to raw folder(res/raw/ )

Dec 9, 2014

How to use back key in webview of fragment



We sometimes implement webview in fragment.

But 'back key' not working in webview of fragment.

Because webview is not activity but fragment.

So we add back button.

But possible to using back key in webview of fragment.

Add this code to MainActivity.java 

  1. @Override
  2. public void onBackPressed() {
  3.     if (webfragment.webview.canGoBack()) {          
  4.         try{
  5.         webfragment.webview.goBack();  //only webview back-key code
  6.         }catch(Exception e){
  7.              //back-key code of another fragment
  8.         }
  9.     }else {
  10.             // back-key code of another fragment
  11.         }

Add onBackPressed method in MainActivity.java

line 3 : Method canGoBack() means possible to go back in webview.

line 5 : Method goBack() means go back in webview.


Dec 1, 2014

Android Web browser


Generally installed web browser in smartphone.

But sometimes we need web browser our application.

Recently I added web browser in my application Labor pains Timer V3. 

 
Labor pains Timer V3

Okay let's go~

 Layout

  1.     <LinearLayout
  2.        android:layout_width="match_parent"
  3.        android:layout_height="0dp"
  4.        android:layout_weight="1"
  5.        android:orientation="horizontal">
  6.        
  7.         <ImageButton
  8.            android:layout_width="0dp"
  9.            android:layout_height="match_parent"
  10.            android:layout_weight="1"
  11.            android:id="@+id/btnBack"
  12.            android:scaleType="centerCrop"
  13.            android:src="@drawable/back"/>
  14.         <ImageButton
  15.            android:layout_width="0dp"
  16.            android:layout_height="match_parent"
  17.            android:layout_weight="1"
  18.            android:id="@+id/btnForward"
  19.            android:scaleType="centerCrop"
  20.            android:src="@drawable/forward"/>
  21.         <EditText
  22.            android:layout_width="0dp"
  23.            android:layout_height="match_parent"
  24.            android:layout_weight="7"
  25.            android:id="@+id/address"
  26.            android:singleLine="true"           
  27.            /> 
  28.         <ImageButton
  29.            android:id="@+id/btnRefresh"
  30.            android:layout_width="0dp"
  31.            android:layout_height="match_parent"
  32.            android:layout_weight="1"
  33.            android:scaleType="centerCrop"
  34.            android:src="@drawable/refresh"/>
  35.     </LinearLayout>
  36.     <WebView
  37.        android:layout_width="match_parent"
  38.        android:layout_height="0dp"
  39.        android:layout_weight="10"
  40.        android:id="@+id/internet"
  41.        />

Line 7 : imagebutton is back 

Line 14 : imagebutton is forward

Line 21 :  EditText is address

Line 28 :  imagebutton is refresh

Line 37 : webview is web browser

layout is simple!!

Now JAVA code

First,  this setting is enable javascript by web browser.

  1.         final WebSettings webSetting = internet.getSettings();
  2.         webSetting.setJavaScriptEnabled(true);
  3.         webSetting.setLoadWithOverviewMode(true);
  4.         webSetting.setUseWideViewPort(true);

button setting(on click listener)

  1.         @Override
  2.     public void onClick(View v) {
  3.         switch(v.getId()){
  4.        
  5.         case R.id.btnBack:
  6.              
  7.             internet.goBack();
  8.                
  9.             break;
  10.         case R.id.btnForward:
  11.             internet.goForward();
  12.                
  13.             break;
  14.         case R.id.btnRefresh:
  15.             String add=address.getText().toString();
  16.             if(!add.contains("http://"))
  17.                 add="http://"+add;
  18.             internet.loadUrl(add);
  19.             break;
  20.         }
  21.        
  22.     }

internet means  WebView.
Line 7 : goBack() method is go to last back page.

Line 11: goForward() method is go to last forward page.

Line 15~17 : Checking EditText. If not include 'http://' in edit text ,automatically add.


Add function 1

  1.         internet.setWebViewClient(new WebViewClient(){
  2.             @Override
  3.             public void onPageFinished(WebView view, String url) {
  4.                 address.setText(url);
  5.                 super.onPageFinished(view, url);
  6.             }
  7.            
  8.         });
This is page changed, display new page's address in EditText.

Add function 2

Anti change display.

If you use display keyboard, your layout is changing!!

But you use this code, not changed your layout!!

  1.     @Override
  2.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  3.         if((actionId==EditorInfo.IME_ACTION_NEXT)||(event !=null &&event.getKeyCode()==KeyEvent.KEYCODE_ENTER)){
  4.             String add=address.getText().toString();
  5.             if(!add.contains("http://"))
  6.                 add="http://"+add;
  7.             internet.loadUrl(add);
  8.         }
  9.         return false;
  10.     }