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.

Friday, June 14, 2013

Missing contentDescription attribute on ImageView in XML - Android



I've the below ImageView in my XML file,

<ImageView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:id="@+id/myImgView"
            android:src="@drawable/something"/>

I get a warning saying that [Accessibility]Missing contentDescription attribute on image in Eclipse IDE. Since it is a warning we used to ignore it.. However to overcome this warning, all you need to add is the below piece of code in ImageView,

android:contentDescription="@string/description" (static or dynamic) 

This is helpful for people using alternative input methods because of their disability (like TalkBack, Tecla Access Shield etc)

You can read more about this here http://android-developers.blogspot.in/2012/04/accessibility-are-you-serving-all-your.html

Thursday, June 13, 2013

How to change ImageView Source - Android?

Consider you have set the source for the ImageView in your xml as below,

<ImageView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:id="@+id/myImgView"
            android:src="@drawable/something"/>
To change the image source programatically,

myImgView.setImageResource(R.drawable.ball);
or,

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.ball));

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);

Monday, April 8, 2013

Capitalize every letter in EditText - Android

I had an interesting usecase in my application, where I need to make EditText uppercase by default.

After googling it for a while found the solution,


EditText someText = (EditText) findViewById(R.id.someText);
someText.setInputType(0x00001001);



This will solve the problem :) Let me know if there are any alternate ways