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(null, null, 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();