Wednesday 28 October 2015

Applying User-defined Font Type in Android



                                                 
                                                      (Before Applying the Font type)

This post helps the Tyro’s who are trying to apply their user-defined font type or face to their android views i.e. (TextView, Button, EditText etc…) in android there are only four basic predefined fonts type are available to design but in order to design our rich UI we are suppose to go with our own font face (.ttf) so first we need to copy our font type file (.ttf) in to our assets folder showed in Package Explorer and use the code highlighted in the GodTest.java file

   
                                           
                                                  (After Applying the Font Face)

Sunday 20 September 2015

Get Android Device UDID


Get Your Android Device UDID:

1
2
3
public static String getDeviceUDID(Context ctx) {
return Secure.getString(ctx.getContentResolver(),Secure.ANDROID_ID);
}

Tuesday 25 August 2015

How to learn Android App development in 30 days?


You need to study core Java before starting to enter into the android platform.

Try to learn core Java by 5 days

Once you learned Code java

Try the below link and learn it by 5 days
In this link they have explained about android development from the very basic things.

They have many tutorial application with perfect explanation. And it will be easy to understand.

Try below link and learn it by 5 days
This blog will be useful to learn material designs and advanced technologies like chat application to do web service etc..

To know about all android application development and android versions update see the below link
Study and workout the below mentioned widgets by 10 days
  1. Should know completely about RelativeLayout, LinearLayout and should know where to use it.
  2. RecyclerView (For list and grid).
  3. Fragment.
  4. Navigation Drawer, and TabLayout.
  5. Database (Sqlite, Try to use third party libraries ex. Realm).
  6. Retrofit, Volley, Picasso, Glide, ,Fresco, Butterknife.
  7. Material Designs.
  8. Google maps.
  9. Thread, Handler, Asynchronous task.
  10. FrameLayout, Table Layout.
  11. Intent, Types of Intent.
  12. Broadcast Receiver, Service, Content Provider
  13. Gradle.
That’s it, you can learn Android with in 25 days.

Thanks :)

Sunday 21 June 2015

Tuesday 19 May 2015

Displaying Bitmaps Efficiently and Avoiding java.lang.OutofMemoryError

java.lang.OutofMemoryError: bitmap size exceeds VM budget. This is most common error for us when we are decoding Bitmap more than 4 MB. In generally before decoding bitmap we do not know how much bigger the size of a bitmap will be. So its very difficult problem for us to handle this error in proactive approach But good thing is that android provide a way to handle this problem.Before decoding bitmap we just decode it with options.inJustDecodeBounds = true. options is the instance of BitmapFactory. 
It does not load bitmap into memory but it help us to find the width and height of a bitmap so that we can reduce the height and width according to our device

As Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices.

So we will find actual height and width of a bitmap as follows...

 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
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

Now scale down the bitmap and load into memory.I use decodeResource() method here but you can use any method(decodeFile etc).So now using following function scale down bitmap

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;
}

Here inSampleSize will reduce the size and memory size of an Bitmap.To use this method, first decode withinJustDecodeBounds set to true, pass the options through and then decode again using the newinSampleSize value and inJustDecodeBounds set to false.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

Memory over flow error is gone…….

Sunday 17 May 2015

Make your android TextView Scrollable

In XML Design: - TextView
1
android:scrollbars="vertical"
In Java Code: - TextView 
1
txtScroll.setMovementMethod(new ScrollingMovementMethod());

Tuesday 10 February 2015

How to create the Customized Splash Screen in Android


1.      Create a customized splash screen layout

      a.       Layout having the TextView with the splash screen title
      b.      ImageView having the Loading image


 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
Layout : main.xml
<? xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:background="#fff"
    android:gravity="center"
    android:layout_height="fill_parent">
   
 <TextView
android:id="@+id/Title"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:padding="10dp"
               android:textSize="30sp"
               android:textColor="#cd6000"
               android:textStyle="bold"
               android:text="My Splash Page" />
              
 <ImageView
android:id="@+id/Loading"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@drawable/loading" />
</LinearLayout>

2.      Create a rotate.xml in a drawable folder for need of  loading animation

<?xml version="1.0" encoding="UTF-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:interpolator="@android:anim/linear_interpolator"   
    android:duration="1000" />

3.      Source SplashScreen.java

       a.  Referencing the xml widgets in to code.
 b.  Create a Animation and start load the animation to the ImageView.
       c.     Using the handler class and runnable thread make postdelay for 1000 milliseconds   and clear the animation then start the second activity.


 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
package com.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Rajendhiran. E
 * Feb 10, 2013 11:10:52 PM
 */

public class SplashScreen extends Activity
{
   TextView Title;
   ImageView Loading;
   Animation anim;
   Handler h;
  
   protected void onCreate(Bundle savedInstanceState)
   {    
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         init();
         process();
   }
  
/* @author Rajendhiran. E, Feb 10, 2013 11:14:24 PM  */ 

   private void init()
   {
         Title = (TextView) findViewById(R.id.Title);
         Loading = (ImageView) findViewById(R.id.Loading);
         anim = AnimationUtils.loadAnimation(SplashScreen.this, 
R.drawable.rotate);
         Loading.startAnimation(anim);
         h = new Handler();
   }    

   /* @author Rajendhiran. E, Feb 10, 2013 11:19:35 PM */        
   private void process()
   {
         h.postDelayed(new  Runnable()
         {
               public void run()
               {                      
                     Loading.clearAnimation();
                     finish();
startActivity(new Intent(SplashScreen.this,SecondActivity.class));
               }
         }, 1000);        
   }    
}
Source Code: SplashScreen.zip

Tuesday 3 February 2015

Customized Count down timer


      CountDownTimer is a predefined class, which help us in the timer related activities, such as showing up the  reducing of time seconds, that we have seen while playing games or doing some timer related stuffs to our apps etc… its easy to develop just passing the milliseconds as parameters and it throws as our customized output as (HH:MM:SS) etc… 

1.     Design a layout with a counter start Button and TextView to display the time

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3:    android:orientation="vertical"  
4:    android:layout_width="fill_parent"  
5:    android:layout_height="fill_parent"  
6:    >  
7:  <TextView   
8:    android:id="@+id/countdown"  
9:    android:layout_width="fill_parent"   
10:    android:layout_height="wrap_content"   
11:    android:text="00:00:00"  
12:    />  
13:   <Button  
14:     android:id="@+id/TimeBtn"  
15:     android:layout_width="fill_parent"  
16:     android:layout_height="wrap_content"  
17:     android:text="Count down Start!" />  
18:  </LinearLayout>  

2.      POCApp.java with counter down timer

1:  package com.app.poc;  
2:  import java.text.SimpleDateFormat;  
3:  import java.util.TimeZone;  
4:  import android.app.Activity;  
5:  import android.os.Bundle;  
6:  import android.os.CountDownTimer;  
7:  import android.util.Log;  
8:  import android.view.View;  
9:  import android.widget.Button;  
10:  import android.widget.TextView;  
11:  public class POCApp extends Activity   
12:  {  
13:    public Button TimeBtn;  
14:    public TextView Time;  
15:    MyCounter timer;  
16:    public void onCreate(Bundle savedInstanceState)   
17:    public void onCreate(Bundle savedInstanceState)   
18:    {  
19:      super.onCreate(savedInstanceState);  
20:      setContentView(R.layout.main);  
21:      init();  
22:      process();  
23:    }  
24:    private void process()   
25:    {  
26:      TimeBtn.setOnClickListener(new View.OnClickListener()  
27:      {  
28:        public void onClick(View arg0)   
29:        {      
30:          if(timer!=null)  
31:           timer.cancel();  
32:          timer = new MyCounter(3800*1000,1000);  
33:          timer.start();                          
34:          //Toast.makeText(POCApp.this, disHour+":"+disMinu+":"+disSec, Toast.LENGTH_LONG).show();  
35:        }  
36:      });  
37:    }  
38:    /**  
39:     *  @author Rajendhiran. E  
40:     *  Jan 16, 20134:46:24 PM  
41:     */  
42:    private void init()   
43:    {  
44:    TimeBtn = (Button) findViewById(R.id.TimeBtn);  
45:      Time = (TextView) findViewById(R.id.countdown);  
46:    }  
47:    /**  
48:     *  @author Rajendhiran. E  
49:     *  Jan 16, 20134:46:25 PM  
50:     */  
51:    class MyCounter extends CountDownTimer  
52:    {  
53:      SimpleDateFormat mSimpleDateFormat;  
54:      public MyCounter(long millisInFuture, long countDownInterval)   
55:      {  
56:        super(millisInFuture, countDownInterval);  
57:        mSimpleDateFormat= new SimpleDateFormat("HH:mm:ss");  
58:        mSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));  
59:      }  
60:      @Override  
61:      public void onFinish()   
62:      {  
63:       Log.d("Timer Completed: ","Completed!");  
64:        Time.setText("00:00:00");        
65:      }  
66:      @Override  
67:      public void onTick(long millisUntilFinished)  
68:      {  
69:       Time.setText(mSimpleDateFormat.format(millisUntilFinished));      
70:      }  
71:     }  

Friday 2 January 2015

Plurals in Android

Plurals are a type of resource, we use it very often to deal with plurality of Strings, what does plurality of Strings means? Suppose you wants to denote the stock items and you want to print out the result, you will have to write at least 2 Strings like:

1
2
3
4
5
<resources>
    <string name="zero"> No Items on the Stall!</string>
    <string name="one">Only One Item on the Stall</string>
    <string name="moreThanOne">There are %d Items on the Stall</string>
</resources>

and then you will add some logic to your code

1
2
3
4
5
6
7
if (ItemsCount ==0)
    result= getString(R.string.zero);
else if (ItemsCount ==1)
    result= getString(R.string.one);
else if (ItemsCount >1)
    result= getString(R.string.moreThanOne, ItemsCount);
textView.setText(result);

It is not a best practice to keep languages in logic inside our code, imagine you have many internationalized text and every languages have its own kind of formatting rules !

here plurals comes to solve this problem:

We are going to use “Plurals” tag rather than “String” tag

1
2
3
4
5
6
<resources>
   <plurals name="numberOfItems">
        <item quantity="one">Only %d Item</item>
        <item quantity= "other">%d Items!</item>
   </plurals>
</resources>

Note that each language have limited quantity values based on grammar of each language, for English there are only two values of quantity “one”

and “other”,  “zero”  is not allowed in english, The plurals have the following types but it get differs to the languages.

Zero When the language requires special treatment of the number 0 (as in Arabic).

One   When the language requires special treatment of numbers like one (as with the number 1 in                  English and most other languages; in Russian, any number ending in 1 but not ending in 11                  is in this class).

Two   When the language requires special treatment of numbers like two (as with 2 in Welsh, or                   102 in Slovenian).

Few          When the language requires special treatment of "small" numbers (as with 2, 3, and 4 in
                 Czech; or numbers ending 2, 3, or 4 but not 12, 13, or 14 in Polish).

Many       When the language requires special treatment of "large" numbers (as with numbers ending                   11-99 in Maltese).

Other       When the language does not require special treatment of the given quantity (as with all
                  numbers in Chinese, or 42 in English).

List of Languages for Plurals Rules: 
http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html

We are suppose to use getQuantityString method:


1
2
3
int ItemsCount= 20;
String result = getResources().getQuantityString(R.plurals.numberOfItems, ItemsCount,ItemsCount);
textView.setText(result);

Output:
“20 Items!”

When using the getQuantityString() method, you need to pass the ItemsCount twice if your string includes string formatting with a number. For example, for the string %d Items found, the first ItemsCount parameter selects the appropriate plural string and the second ItemsCount parameter is inserted into the %d placeholder. If your plural strings do not include string formatting, you don't need to pass the third parameter to

getQuantityString.

Android Plurals Documentation:
http://developer.android.com/guide/topics/resources/string-resource.html#Plurals

Enjoy!!!!!!!!

Show or Hide soft keyboard in Android

Show or Hide soft keyboard on opening a dialog or activity in android

For showing the soft keyboard on entering into the activity or to the dialog we can use this coding
Code Snippet:
1
2
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

and for hiding the keyboard
Code Snippet:
1
2
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(singleedittext.getWindowToken(),0);

Enjoy!!!!!!!!!!!