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



Wednesday, June 26, 2013

How to put an Image at the end of RadioButton - Android?


use the following code to add an image to top,right, left, bottom of a RadioButton (or any Button)


setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.tick), null);

someRadioButton.setCompoundDrawablesWithIntrinsicBounds(nullnull, getResources().getDrawable(R.drawable.tick), null);
If you pass null as a paramater for anything that are not needed.

How to set OnClickListener for a RadioButton - Android?


It is better to set OnClickListener for a RadioGroup and update the View accordingly,

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.someRadioGroup);        
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
            // Do something here..

        }
    });

How to disable a button - Android?


Programatically you can use the below code to disable a button

myButton.setEnabled(false);

In XML,

android:clickable="false"

or


android:enabled="false"

Setting background color through java code? - Android


It is quite easy to set the background color programatically of an Android Textview,

Option1:

TextView et = (TextView)findViewById(R.id.someTextView); someTextView.setText("Some text With Color"); someTextView.setBackgroundColor(Color.white);

Option2:

someTextView.setBackgroundColor(Color.parseColor("#ffffff"));

Option3:

someTextView.setBackgroundResource(R.color.white);


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.