Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

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)

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 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 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 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"
   >



Wednesday, June 26, 2013

How to get Selected Index or text of RadioGroup - Android?


The following code will get the selected Index of the RadioGroup and also get the text of the selected RadioButton,

int radioButtonID = someRadioGroup.getCheckedRadioButtonId();
View radioButton = someRadioGroup.findViewById(radioButtonID);
int idx = someRadioGroup.indexOfChild(radioButton);
RadioButton clickedRadioBut = (RadioButton) someRadioGroup.getChildAt(idx);
String selection = (String) clickedRadioBut.getText();

Monday, June 24, 2013

How to get the context in a fragment? - Android Apps

Use the following code to get the context in Fragment

public static class SomeFragment extends Fragment{

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity.getApplicationContext();
    }
}

Sunday, June 16, 2013

Google Play Store policies?

Its always better to be clear on Google Play's policies. Before publishing your app on Google Play Store make sure to check below guide, which will take you through some important areas.

http://developer.android.com/distribute/googleplay/policies/index.html#side-nav

Publishing Android app?

If you're going to publish your app in Google Play store you need to have the below checklist.. Thanks to Ellie Powers for the list.

1. Boost your developer account security

  • If you take just one step today to protect your Google Play apps, enabletwo-step authentication for your Google account, and encourage the rest of your team to do the same.
  • Next, many developers first set up their Google Play account with their personal gmail account, but it’s actually a good idea to transfer your apps to a separate account. All of your installations and reviews remain intact. If you haven’t done this already, transfer your apps to a new account today.
  • Don’t share passwords. Instead, add each individual who needs access and only grant the minimum level of access they need — and encourage them to enable two-step authentication.
  • Review the list of people with access regularly, and when people leave your project, make it a standard practice to remove their access. Learn more about developer account security.

2. Protect your keystore

In order to publish an update to an existing app, you’ll need to sign it with the same keystore every time. If you lose your keystore, you’ll lose your history and reviews, so you’ll need to start over with new apps with new package name and a new key, so you’ll want to make sure you protect it. First, choose a secure password, and don’t use the same password that you use for your Google account. Next, back up your keystore somewhere, but don’t upload it to Google Drive with an account you use to publish on Google Play.

3. Check your email addresses

As a developer, you are responsible for checking two important email addresses:
  • Account owner email address: Google uses the address used to register your Developer Console to contact you about your apps and account, so it is extremely important that someone is responsible for checking it regularly. If necessary, you can forward messages from this account via Gmail, or transfer your apps to another account.
  • Customer support email address: For each individual application, you can specify the best way for users to contact you for customer support. Ensure that a valid support email address for your product is specified. As a best practice, this should probably be a designated support account that is checked regularly and not the same email as the address used to login to the Developer Console.

4. Familiarize yourself with the policies

We recently launched some new guides and examples for Google Play’s Developer Program Policies and Developer Distribution Agreement. Note that once you publish an app as free, you can’t change it to a paid app later, though you can add in-app products.

5. Set up team processes

You may have many people involved with your Google Play apps. Make sure roles are clear in terms of whose job it is to publish updates, check statistics and optimization tips, read and reply to user reviews, and track revenue. Make sure all of these people have the right access to the Developer Console. Many developers who are part of larger organizations also report to their larger teams about their apps’ performance. Designate someone to make sure your app description, graphics (including localized and tablet screenshots), and pricing are up to date.

6. Configure your Developer Console UI languages

To change the language you want to see the Developer Console in, set your primary language. If you speak additional languages, configure those, too — user reviews in those languages won’t be translated automatically in the Developer Console. That was a popular request from developers.

7. Refresh your app’s marketing materials

8. Stay on top of developer news

To make sure you’re aware of the latest Google Play updates for developers, make sure you check the Android Developers blog regularly, follow +Android Developers, and check the Developer Console regularly for announcements.

Thursday, June 13, 2013

GridView scroll - Android


I've a list of images in my app that needs to be shown in a Grid layout.. I've tried many ways to set height in Gridview layout, but all went in vain.. 

After too much of research, I stumbled on the excellent answer of Neil Traft.  I just copy-pasted his code in my app and it worked like a charm :)

ExpandableHeightGridView.java:

public class ExpandableHeightGridView extends GridView
{

    boolean expanded = false;

    public ExpandableHeightGridView(Context context)
    {
        super(context);
    }

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

    public ExpandableHeightGridView(Context context, AttributeSet attrs,
            int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

embed the GridView inside ScrollView as below,

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <com.droid.gview.ExpandableHeightGridView
       android:id="@+id/dashboard_grid"
       android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:horizontalSpacing="2dp"
    android:isScrollContainer="false"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:verticalSpacing="20dp" />
</RelativeLayout>
</ScrollView>    

and lastly set the following,


ExpandableHeightGridView gridview = (ExpandableHeightGridView)  findViewById(R.id.dashboard_grid);
gridview.setExpanded(true);