Android examples for User Interface:Layout
create EditText and set layout
//package com.java2s; import android.content.Context; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.TableRow; public class Main { /**/*from w w w.j a va 2 s .c o m*/ * @fn public static EditText createEditText(Context context,String message,int size,int textColor,int backgroundColor) * @brief Creates a textview object. * @param context * @param message Message to be displayed in the text view * @param size Text size * @param textColor Numerical representation of color. Use android.graphics.Color.rgb(red,green,blue) * @param backgroundColor Numerical representation of color. Use android.graphics.Color.rgb(red,green,blue) * @return Created EditText Object. */ public static EditText createEditText(final Context context, String message, int size, int textColor, int backgroundColor) { /// http://stackoverflow.com/questions/11504635/layout-margin-for-text-view-programmatically EditText editText = new EditText(context); TableRow.LayoutParams tvlp = new TableRow.LayoutParams( TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT); editText.setLayoutParams(tvlp); tvlp.setMargins(2, 2, 2, 2); editText.setText(message); editText.setTextSize(size); editText.setBackgroundColor(backgroundColor); editText.setTextColor(textColor); /// http://stackoverflow.com/questions/432037/how-do-i-center-text-horizontally-and-vertical-in-a-textview-in-android editText.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); //Send click event to the parent if the focus changed. /// http://stackoverflow.com/questions/8397609/onclicklistener-listens-only-on-the-second-time editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { ViewGroup vp = (ViewGroup) v.getParent(); vp.performClick(); } } }); return editText; } }