Monday, December 30, 2013

ToDo's before publishing Android app!!


I found this stackoverflow thread a very useful one - tips or todo's before publishing Android app in Google's Play Store,

  • Launch your app at the end of the week ( thursday afternoon is usually a good time ) why so ? well, no companies would like to publish an app only 1.5 day before the week end -> too dangerous ( in case there is a problem that needs a quick reaction time )
  • Use proguard on your app ( usually, you just have to add this line :proguard.config=proguard.cfg in the default.properties file) this will optmize, shrink and obfuscate your code, very usefull for preventing from code thieves. You don't have to delete any comments, there are automatically deleted at compile time
  • Optimize your images ( using Paint.NETPNGCrush or OptiPNG )
  • Optimize your layouts for most of screen sizes. You can doing this by simply changing the screen size while editing a layout in eclipse
  • Try/Catch all exceptions on the UI and display a simple toast wich indicate to the user that something wrong happened. In the meantime, retrieve the error with the help the ACRA
  • Don't use too much .jar libraries, prefer library projects ( optimize the code size )
  • Don't use the Android preferences windows -> that's not really beautiful, even if it's in the Android guidelines, prefer making your own settings windows
  • Never show the title of your app (this.requestWindowFeature(Window.FEATURE_NO_TITLE);), and consider using the fullscreen mode (this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);)
  • Use either Flurry or Google analytics for future analytics -< try to get as much informations as possible, but don't grab anything that violate the anonymous state of the customer. Don't forget to retrieve exceptions that happen on the user side
  • Ask your friends to do monkey test, learning from users usually brings many good things
  • Consider publishing your app before having finished all features, you don't already know what your users will want to have in the app
  • Add a section "More apps" in your app, that's free ads
  • Add a section "send feedback" wich let the user ask for feature or specify some bugs
  • Ask your users to translate your app by providing the strings.xml somewhere on the web
  • Try your app on each android version with the emulator -> many bugs appear at that moment
  • Think about the name of the app -> what keywords would you use to search for your app ? these keywords are the name of your app
  • Consider including keywords in the app description
  • Be the first to rate your app with 5 stars -> that influence the future users ratings
  • Consider using Google to translate your app either for the description, either for the strings.xml or both
  • Consider adding an advertising feature in your app such as AdMob
  • Instead of providing a paid version, consider doing in-app billing -> user are more likely to in-app pay than pay for a paid version
  • Add a change log in the app -> users like to see what changes since the last version
  • Add a Thanks section for the users that helped you -> this will influence the users to help you
  • Add a "If you like this app, please rate it" link ( to your Android Market's description ) in your app -> you will get more 5 stars
  • Consider including a "Tips" or "instructions" section in your app
  • Unless it's not possible, Prefer external installation (android:installLocation="preferExternal" in the AndroidManifest.xml)

Sunday, December 29, 2013

Eclipse doesn't render layout


After installing the new Android SDK, the graphical layout in Eclipse is blank.. It shows

This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in.

If you get this message, it means that your ADT is out of date and needs to be updated. Follow the below steps to update it,

  1. Click Help > Install New Software.
  2. In the Work with field, enter: https://dl-ssl.google.com/android/eclipse/
  3. Select Developer Tools / Android Development Tools.
  4. Click Next and complete the wizard.

Tuesday, October 22, 2013

Preventing Android device from going to sleep - programmatically?


To prevent an Android device from going to sleep programmatically,

Call setKeepScreenOn(true) on that View or set the keepScreenOn property to true. This will prevent the screen from going off while the View is on the screen. No special permission required for this.

Sunday, October 20, 2013

Margin vs Padding - difference??


Knowing the difference between margins and padding in Android is quite important.

Padding is the space inside the border, between the border and the actual view's content. Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent).
Basically this is relative to your border, if you want to add the space between the elements inside the border (i.e. have the borders touching), you should use the padding property.

Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object. Note that, like the padding, the margin goes completely around the content: there are margins on the top, bottom, right, and left sides.
If you want to add the space outside the borders (have the borders apart) you should use the margin property.
An image says more than 1000 words:
alt text 

Saturday, August 17, 2013

ClassCastException in FrameLayout - changing


I'd spent quite a lot of time to find out the root cause of ClasscastException when we change the order of views in layout xml..

And found that if we do "Project --> Clean" in Eclipse the problem will be resolved.

Eclipse - is the culprit :)

Tuesday, July 9, 2013

Admob error in Eclipse? - Android


Eclipse is showing an error in android:configChanges line in my AndroidManifest.xml:

<activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"></activity>
and the error is,
error: Error: String types not allowed (at 'configChanges' with value 'keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize').

I resolved it by setting target=android-13 in project.properties file.. After changing it don't forget to do Project-> Clean 

If you would like to know how to make use of Admob in your Android app, you can take a look at this tutorial..

http://www.monkeycoder.co.nz/Community/posts.php?topic=1121

APK signed with a certificate that is not yet valid - Android?

While publishing the app I got the below error,

"You uploaded an APK signed with a certificate that is not yet valid. You need to sign your APK with a certificate that is currently valid."

After several Google searches, I resolved this issue by changing the system date of the computer.. I changed my system date to some earlier date, generated a new keystore and uploaded the app.. it worked like a charm :)


Tuesday, July 2, 2013

How to start an activity if the user clicks on OK button in AlertDialog - Android?

Make use of this code to start an activity if the user clicks on OK button in AlertDialog,

AlertDialog.Builder alertDialog = new AlertDialog.Builder(Someclass.this);
    builder.setMessage("Are you sure you want to quit?")    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int which) { 
        Intent myIntent = new Intent(((Dialog) dialog).getContext(), Someclass2.class);        startActivity(myIntent);    
        return;  
        }        
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int which) {
             // Do Nothing
     }

    alertDialog.show();

Saturday, June 29, 2013

How to add common OnClick event for ImageView for every activity? - Android

If your layout contains one common header (with an ImageView) across multiple activities.. you may need to write an OnClick event for the imageView in every single activity..

Instead of writing the same code in every activity you can write a class the extends OnClickListener and the OnClick method like below,

public class ImageViewOnclickListener implements OnClickListener {
  private Context context;
  public ImageViewOnclickListener(Context context) {    this.context = context;
  }

  @Override
  public void onClick(View arg0) {
    Intent intent = new Intent(context, SomeActivity.class);
    context.startActivity(intent);
  }
}

In your activities:
protected void onCreate(...) {
  setContentView(...);
  ((ImageView) findViewById(R.id.mybutton)).setOnClickListener(new ImageViewOnclickListener(this));
}
Hope this helps :)

Draw dotted/dashed line in Android?


You can make use of the below code to draw a dotted or dashed line in Android.
drawable/dotted.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
    <stroke
       android:color="#C7B299"
       android:dashWidth="10px"
       android:dashGap="10px" />
</shape>
somefile.xml:
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/dotted" />

How to specify percentage width for TextView in Linearlayout - Android?

I was able to achieve this with the following XML,

   <LinearLayout
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">

        <TextView
            android:text="left" 
            android:layout_width="0dp" 
            android:layout_height="wrap_content" 
            android:layout_weight=".60" /> 

        <TextView
            android:text="right" 
            android:layout_width="0dp" 
            android:layout_height="wrap_content" 
            android:layout_weight=".40" />

    </LinearLayout>
You can replace the TextView with EditText or Button or whatever elements that fit your needs..
Note: Make sure to set the  layout_width to 0dpor your views may not be scaled properly.

Thursday, June 27, 2013

How to stop EditText from gaining focus at Activity startup? - Android


I have an activity with EditText at the top and a ListView under it.. EditText takes focus as soon as the Activity starts.. and also the soft keyboard comes up..

To fix this problem, add the following code in your enclosing layout of EditText, ListView,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mainLayout"
  android:descendantFocusability="beforeDescendants"
  android:focusableInTouchMode="true" >
and also remove

 <requestFocus />
from EditText.

How to use custom fonts in Android XML file?


Add the .ttf file in your assets/fonts folder. To use it in TextView the best way is to extend the TextView and have to set the font.  Here is the code of the extended TextView,

package naveendroid.fontstest;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class RobotTextView extends TextView {
    public RobotTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);
        init();
    }

    public RobotTextView(Context context, AttributeSet attrs) {        super(context, attrs);
        init();
    }

    public RobotTextView(Context context) {        super(context);
        init();
    }

    private void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                                               "Roboto-Regular.ttf");
        setTypeface(tf);
    }

}

Then use this in your .xml file as shown below,

<naveendroid.fontstest.MyTextView
    android:id="@+id/sometext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="TextView with the font that you had set in RobotTextView class "
    android:textSize="20dip"
    android:textColor="#000"
   >