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