How to position and align the password toggle drawable to the right of TextInputEditText?
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/passwordTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="#style/DefaultTextInputLayout"
android:paddingStart="#dimen/activity_default_margin"
android:paddingEnd="#dimen/activity_default_margin"
app:passwordToggleEnabled="true"
app:passwordToggleDrawable="#drawable/select_show_password"
app:passwordToggleTint="#AFB5C0"
app:errorEnabled="true"
app:layout_constraintBottom_toTopOf="#+id/rememberPasswordCheckBox"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/emailTextInputLayout" >
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/login_password"
android:textSize="16sp"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
I need to position the password toggle to the right of TextInputEditText like on the image (where the red arrow are pointing).
You can try the following:
1) MainActivity.class:
public class MainActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();
private TextInputLayout textInputLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textInputLayout = (TextInputLayout) findViewById(R.id.passwordTextInputLayout);
/**
* By inspecting the source code of TextInputLayout, one can find that:
*
* - edit text (called editText) and show password (
* called startIconView) views are both placed inside
* a frame layout (called inputFrame).
* - edit text view is the first child of the frame layout
* and that the show password view is the third child of this layout.
*
*/
FrameLayout inputFrame = null;
TextInputEditText editText = null;
CheckableImageButton startIconView = null;
for (int i = 0; i < textInputLayout.getChildCount(); i++) {
View child = textInputLayout.getChildAt(i);
if (child instanceof FrameLayout) {
inputFrame = (FrameLayout) child;
for (int j = 0; j < inputFrame.getChildCount(); j++) {
View child1 = inputFrame.getChildAt(j);
if (child1 instanceof TextInputEditText) {
editText = (TextInputEditText) child1;
} else if (child1 instanceof CheckableImageButton) {
if (j == 2) {
startIconView = (CheckableImageButton) child1;
}
}
}
break;
}
}
/**
*
* - relocate editText and startIconView inside inputFrame.
* In our case we want startIconView to be located to the
* right of editText
*
**/
if (inputFrame != null) {
RelativeLayout rl = new RelativeLayout(MainActivity.this);
FrameLayout.LayoutParams params_rl = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
rl.setLayoutParams(params_rl);
if (editText != null &&
startIconView != null) {
inputFrame.addView(rl);
inputFrame.removeView(startIconView);
inputFrame.removeView(editText);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_END);
startIconView.setLayoutParams(params);
rl.addView(startIconView);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_START);
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params1.addRule(RelativeLayout.LEFT_OF, startIconView.getId());
params1.addRule(RelativeLayout.START_OF, startIconView.getId());
editText.setLayoutParams(params1);
rl.addView(editText);
}
}
}
}
2) activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark"
tools:context=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/passwordTextInputLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
android:layout_marginTop="100dp"
app:passwordToggleDrawable="#drawable/select_show_password"
app:passwordToggleTint="#AFB5C0"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="123"
android:text=""
android:background="#color/colorPrimary"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/passwordTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
android:layout_below="#id/passwordTextInputLayout2"
app:passwordToggleDrawable="#drawable/select_show_password"
app:passwordToggleTint="#AFB5C0"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="123"
android:text=""
android:background="#color/colorPrimary"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>
3) Result:
I think this will help
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/input_layout_password"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edtxt_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:imeOptions="actionNext"
android:paddingTop="10dp"
android:paddingBottom="20dp"
android:paddingStart="10dp"
android:inputType="textPassword"
android:drawableRight="#drawable/ic_visibility_off"
android:maxLines="1"/>
</com.google.android.material.textfield.TextInputLayout>
Below is working code for me
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/input_layout_password"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Password"
app:layout_constraintTop_toBottomOf="#id/input_layout_username"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edtxt_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="textPassword"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
Below is my output
I solved using negative padding, maybe it not a good pratic, but I can't think how to do it using good practics
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/passwordTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="#style/DefaultTextInputLayout"
android:paddingStart="#dimen/activity_default_margin"
android:paddingEnd="#dimen/activity_default_margin"
app:passwordToggleEnabled="true"
app:passwordToggleDrawable="#drawable/select_show_password"
app:passwordToggleTint="#AFB5C0"
app:errorEnabled="true"
app:layout_constraintBottom_toTopOf="#+id/rememberPasswordCheckBox"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/emailTextInputLayout" >
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/login_password"
android:textSize="16sp"
android:paddingEnd="-10dp"
app:setDebugText="#{password}"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
Related
i have a problem when trying to add a programmatically defined radio button to a radio group.
so i have a method that when is called inflates a dialog, this dialog contains within it a list of subjects displayed as a list of checkBox.
since i can't predefine how many subjects are needed in my radio group i decided to add them programmatically using the method mentioned earlier.
for some reason the app keep on crashing every time i open the dialog. with the break point being when the radio button gets added to the radio group.
here is a copy of the fragment responsible of calling the method (as well as the method called) :
public class addPaymentFragment extends Fragment {
private RadioGroup radioGroup;
private RadioButton radioButton;
private SchoolViewModel schoolViewModel;
private NumberPicker numberPicker;
private Button btn;
private Subject subject;
private Dialog dialog;
private RadioGroup radioGroup0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_add_payment, container, false);
btn = v.findViewById(R.id.btn_add_payment);
radioGroup0 = v.findViewById(R.id.rg_select_subject);
radioGroup = v.findViewById(R.id.rg_add_payment);
numberPicker = v.findViewById(R.id.numberPicker);
numberPicker.setMinValue(0);
numberPicker.setMaxValue(10);
schoolViewModel = new ViewModelProvider(this).get(SchoolViewModel.class);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
schoolViewModel.getSingleTeacherWithSubjects(i).observe(getViewLifecycleOwner(), new Observer<TeacherWithSubjects>() {
#Override
public void onChanged(TeacherWithSubjects teacherWithSubjects) {
if (teacherWithSubjects.subjects.size() > 1) {
openDialog(teacherWithSubjects.subjects,radioGroup0);
} else {
subject = teacherWithSubjects.subjects.get(0);
}
}
});
}
});
schoolViewModel.getAllTeachers().observe(getViewLifecycleOwner(), new Observer<List<Teacher>>() {
#Override
public void onChanged(List<Teacher> teachers) {
addRadioButtons(teachers, radioGroup);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), teachers.get(0).getTeacherName() + "", Toast.LENGTH_SHORT).show();
}
});
}
});
return v;
}
private void addRadioButtons(List<Teacher> teachers, RadioGroup radioGroup) {
for (Teacher i : teachers) {
//instantiate...
RadioButton radioButton = new RadioButton(getContext());
//set the values that you would otherwise hardcode in the xml...
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
params.bottomMargin = 25;
params.leftMargin = 20;
params.rightMargin = 20;
params.topMargin = 20;
radioButton.setLayoutParams(params);
//label the button...
radioButton.setBackground(new ColorDrawable(Color.TRANSPARENT));
radioButton.setMinWidth(250);
radioButton.setBackgroundResource(R.drawable.custom_checkbox);
radioButton.setElevation(16);
radioButton.setGravity(Gravity.CENTER);
radioButton.setText(i.getTeacherName());
radioButton.setPadding(50, 100, 50, 100);
radioButton.setButtonDrawable(R.drawable.custom_checkbox);
radioButton.setId(i.getTeacherId());
//add it to the group.
radioGroup.addView(radioButton);
}
}
void openDialog(List<Subject> subjects,RadioGroup radioGroup) {
dialog = new Dialog(getContext());
radioGroup = dialog.findViewById(R.id.rg_select_subject);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.choose_teached_subject_dialog);
dialog.setCancelable(false);
Button confirmSubject = dialog.findViewById(R.id.btn_choose_subject);
Button cancelSubject = dialog.findViewById(R.id.btn_dont_choose_subject);
//populate the checkBox
for (int i = 0; i<subjects.size(); i++){
RadioButton radioButton1 = new RadioButton(dialog.getContext());
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
params.bottomMargin = 25;
params.leftMargin = 20;
params.rightMargin = 20;
params.topMargin = 20;
radioButton1.setLayoutParams(params);
radioButton1.setBackground(new ColorDrawable(Color.TRANSPARENT));
radioButton1.setMinWidth(250);
radioButton1.setBackgroundResource(R.drawable.custom_checkbox);
radioButton1.setElevation(16);
radioButton1.setGravity(Gravity.CENTER);
radioButton1.setId(i);
radioButton1.setText(subjects.get(i).getSubjectName());
radioButton1.setPadding(50, 100, 50, 100);
radioButton1.setButtonDrawable(R.drawable.custom_checkbox);
//add it to the group.
radioGroup.addView(radioButton1);
}
confirmSubject.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
Toast.makeText(getContext(), "Select a subject!", Toast.LENGTH_SHORT).show();
}
});
cancelSubject.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
dialog.getWindow().setGravity(Gravity.CENTER);
dialog.show();
}
}
and here is the code to the corresponding XML file :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".addPaymentFragment">
<HorizontalScrollView
android:id="#+id/rv_add_payment"
android:layout_width="match_parent"
android:layout_height="146dp"
android:orientation="horizontal"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioGroup
android:id="#+id/rg_add_payment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></RadioGroup>
</HorizontalScrollView>
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/numberPickerTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heures Enseignées :"
android:textColor="#000"
android:textSize="28sp"
app:fontFamily="#font/k2d_semibold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rv_add_payment" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_baseline_play_arrow_24"
app:layout_constraintBottom_toBottomOf="#+id/numberPicker"
app:layout_constraintEnd_toStartOf="#+id/numberPicker"
app:layout_constraintTop_toTopOf="#+id/numberPicker" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/numberPicker">
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total: "
android:textColor="#000"
android:textSize="28sp"
app:fontFamily="#font/k2d_semibold" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0DA"
android:textColor="#000"
android:textSize="28sp"
app:fontFamily="#font/k2d_semibold" />
</LinearLayout>
<android.widget.Button
android:id="#+id/btn_add_payment"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="20dp"
android:background="#drawable/rounded_button"
android:text="add"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout" />
<android.widget.Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginEnd="20dp"
android:background="#drawable/rounded_button"
android:text="cancel"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
and here is a copy of the XML file of the dialog called :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="#drawable/dialog_background"
android:elevation="16dp">
<TextView
android:id="#+id/choose_subject_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:fontFamily="#font/k2d_semibold"
android:text="choose teached subject :"
android:textColor="#color/black"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="146dp"
android:orientation="horizontal"
android:scrollbars="none"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/choose_subject_tv">
<RadioGroup
android:id="#+id/rg_select_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></RadioGroup>
</HorizontalScrollView>
<android.widget.Button
android:id="#+id/btn_choose_subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="40dp"
android:background="#drawable/custom_button"
android:fontFamily="#font/k2d_semibold"
android:text="Confirm"
android:textAllCaps="false"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/horizontalScrollView" />
<android.widget.Button
android:id="#+id/btn_dont_choose_subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:layout_marginBottom="40dp"
android:layout_marginTop="20dp"
android:background="#drawable/custom_button"
android:fontFamily="#font/k2d_semibold"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/horizontalScrollView" />
</androidx.constraintlayout.widget.ConstraintLayout>
and here is the message shown in the logcat :
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.addView(android.view.View)' on a null object reference
at com.example.ecolemathphysique.addPaymentFragment.openDialog(addPaymentFragment.java:142)
at com.example.ecolemathphysique.addPaymentFragment$1$1.onChanged(addPaymentFragment.java:62)
at com.example.ecolemathphysique.addPaymentFragment$1$1.onChanged(addPaymentFragment.java:58)
i apologize in advance if there is any mistakes in my question, am a bit new to asking questions here so please take it easy on me and thanks in advance
so i have managed to find the problem which was i called
radioGroup = dialog.findViewById(R.id.rg_select_subject);
before finishing setting up the dialog, which mean that the radioGroup object was never assigned.
here is how the related bloc looks like after fixing it :
void openDialog(List<Subject> subjects,RadioGroup radioGroup) {
dialog = new Dialog(getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.choose_teached_subject_dialog);
dialog.setCancelable(false);
radioGroup = dialog.findViewById(R.id.rg_select_subject);
Button confirmSubject = dialog.findViewById(R.id.btn_choose_subject);
Button cancelSubject = dialog.findViewById(R.id.btn_dont_choose_subject);
I'm trying to implement a layout where user will input their phone number. The phone number will be 11 digits. And so I wanted to build a text input field with a fixed width that can accommodate 11 characters.
NOTE: I don't want the input field to be wider than 11 characters. Let's say each character is 10px wide. So the input field should be 11*10px = 110px wide (I'm not taking the drawable icon into account here).
Here's what I've tried in XML:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textField"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_48"
app:startIconDrawable="#drawable/ic_phone">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="11"
android:inputType="phone"
android:maxLength="11"
android:maxLines="1"
android:minEms="11"
android:text="01"
android:textSize="20sp"
android:typeface="monospace" />
</com.google.android.material.textfield.TextInputLayout>
But this produces an output like the following:
As you can see, despite using ems and minEms, the field is 2 characters wide (it's 0 character wide if I don't add android:text). But I want it to have a width of 11 characters (not more or less). Why is ems not effective here and what's the solution?
Just edit your layout width
from
android:layout_width="wrap_content"
to
android:layout_width="match_parent"
I just edited your code
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textField"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_48"
app:startIconDrawable="#drawable/ic_phone">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="11"
android:inputType="phone"
android:maxLength="11"
android:maxLines="1"
android:minEms="11"
android:text="01"
android:textSize="20sp"
android:typeface="monospace" />
</com.google.android.material.textfield.TextInputLayout>
You should set a 11 digits monospace text in your TextInputEditText widget (for example 12345678901), then define a ViewTreeObserver.OnGlobalLayoutListener in your fragment, that calculates the width when the layout is available to be measured.
You could instantiate the observer in onResume() of your fragment, and remove it in onPause(). You should also store the width during the rotation of the activity.
Here below a full working example:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
ConstraintLayout constraintLayout;
TextInputEditText textInputEditText;
int viewWidth;
ViewTreeObserver.OnGlobalLayoutListener viewTreeObserver = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
constraintLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
constraintLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
viewWidth = textInputEditText.getMeasuredWidth();
textInputEditText.setWidth(viewWidth);
textInputEditText.setText("");
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
viewWidth = savedInstanceState.getInt("viewWidth", 0);
}
constraintLayout = findViewById(R.id.id_constraintlayout);
textInputEditText = findViewById(R.id.phone);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("viewWidth", viewWidth);
}
#Override
public void onResume() {
super.onResume();
if (viewWidth == 0) {
textInputEditText.setText("12345678901");
ViewTreeObserver vto = constraintLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(viewTreeObserver);
} else textInputEditText.setWidth(viewWidth);
}
#Override
public void onPause() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
constraintLayout.getViewTreeObserver().removeGlobalOnLayoutListener(viewTreeObserver);
} else {
constraintLayout.getViewTreeObserver().removeOnGlobalLayoutListener(viewTreeObserver);
}
super.onPause();
}
}
activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/id_constraintlayout"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textField"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:startIconDrawable="#drawable/ic_phone">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="11"
android:inputType="phone"
android:maxLength="11"
android:maxLines="1"
android:minEms="11"
android:textSize="20sp"
android:typeface="monospace" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I enclose an image of the result here below, where I already typed 6 digits:
For me the given answers didn't work. But this did:
<com.google.android.material.textfield.TextInputLayout
...
android:maxEms="12"
minEms="12">
<com.google.android.material.textfield.TextInputEditText
...
singleLine="true">
</com.google.android.material.textfield.TextInputLayout>
I am facing a problem with the position of the error indicator of my EditText when calling editText.setError("...").
As you can see in the screenshot I am using a BottomSheetDialog with an EditText inside of it. When I display the error indicator, the text is completely out of place. It seems as if the dialog "thinks" that it is full-screen, while it is actually not.
This is my dialog layout file (phone_dialog.xml):
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:padding="#dimen/padding_layout_normal"
android:text="#string/dialog_title_edit_phone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<EditText
android:id="#+id/etPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:inputType="phone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvTitle"/>
<Button
android:id="#+id/btnSavePhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etPhone"/>
</android.support.constraint.ConstraintLayout>
My Activity layout file (activity_contacts.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvContacts"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
This is how I show the dialog from my Activity:
PhoneBottomDialog dialog = new PhoneBottomDialog(Context);
dialog.show();
This is my PhoneBottomDialog class:
public class PhoneBottomDialog extends BottomSheetDialog {
public PhoneBottomDialog(Context context) {
super(context);
View view = getLayoutInflater().inflate(R.layout.phone_dialog, null);
setContentView(view);
// additional setup below this...
}
// ...
}
I am not performing any other layouting inside my custom PhoneButtomDialog. Changing the root layout of my dialog to RelativeLayout or LinearLayout as well as adding a ScrollView did not change anything. It's also not a device or specific Android version related issue as the problem occurs on all of my testing devices ranging from Android 5.0 to 7.1, it also occurs on the emulator.
Does anyone have an idea why this is happening?
You can use TextInputLayout and inside that you can define Edit Text.
I have done some modification inside your phone_dialog.xml.
phone_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="bottom"
android:background="#android:color/holo_blue_light"
android:padding="10dp"
app:behavior_hideable="true"
app:behavior_peekHeight="60dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:gravity="center"
android:padding="10dp"
android:text="dialog_title_edit_phone" />
<android.support.design.widget.TextInputLayout
android:id="#+id/inputPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/TextInputLayoutLabel">
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter phone"
android:imeOptions="actionDone"
android:inputType="phone"
android:maxLines="1"
android:textSize="20sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btnSavePhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/etPhone"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:text="Save" />
</LinearLayout>
</FrameLayout>
Inside style.xml add this.
<style name="TextInputLayoutLabel" parent="Widget.Design.TextInputLayout">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#android:color/black</item>
<item name="android:textSize">15sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#color/colorPrimary</item>
<item name="colorControlNormal">#color/colorAccent</item>
<item name="colorControlActivated">#color/colorAccent</item>
</style>
PhoneBottomDialog.java
public class PhoneBottomDialog extends BottomSheetDialog {
TextInputLayout inputPhone;
EditText edtPhone;
Button btnSave;
public PhoneBottomDialog(Context context) {
super(context);
View view = getLayoutInflater().inflate(R.layout.phone_dialog, null);
setContentView(view);
// additional setup below this...
inputPhone = (TextInputLayout) view.findViewById(R.id.inputPhone);
edtPhone = (EditText) view.findViewById(R.id.etPhone);
btnSave = (Button) view.findViewById(R.id.btnSavePhone);
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!validatePhone())
return;
}
});
}
private boolean validatePhone() {
if (edtPhone.getText().toString().isEmpty()) {
inputPhone.setError("Please enter valid phone number with country code.");
requestFocus(edtPhone);
return false;
} else {
inputPhone.setErrorEnabled(false);
}
return true;
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
// ...
}
And inside Activity.
#SuppressLint("SetTextI18n")
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test24);
mContext = this;
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
PhoneBottomDialog dialog = new PhoneBottomDialog(mContext);
dialog.show();
}
Below you can see the output for this.
add compile 'com.android.support:design:24.2.0' in gradle dependencies
use TextInputLayout in xml.
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputServer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="36dp"
app:errorTextAppearance="#style/TextErrorAppearance">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
........./>
</android.support.design.widget.TextInputLayout>
From Lollipop(5.0) version android provide TextInputLayout to do this.
Use below xml and java code to show same type view.
abc.xml:
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
app:theme="#style/AppTheme">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:imeOptions="actionNext"
android:inputType="text"
android:singleLine="true"
android:textColor="#color/icons"
android:textSize="16sp" />
</android.support.design.widget.TextInputLayout>
Abc.java:
private TextInputLayout
textInputLayout_User = (TextInputLayout) findViewById(R.id.text_input_layout_user);
textInputLayout_User.setError(getString(R.string.valid_username));
I have a form where the user enter the client info. When I try to validate the fields I have a problem, I used editText.setError(errorMessage) and it's showing the error message, but it shown on the top of my EditText making it confusing to the user. Like the image below.
Any ideas of how I can fix this?
EditText on XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="br.com.intelecto.intesigmobile.activity.ClienteEditActivityFragment">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_nome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_nome"
android:imeActionId="#+id/cli_name"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="textPersonName"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_telefone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_telefone"
android:imeActionId="#+id/cli_cpf"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="phone"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_email"
android:imeActionId="#+id/cli_cpf"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_cpf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_cpf"
android:imeActionId="#+id/cli_cpf"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="number"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_endereco"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_endereco"
android:imeActionId="#+id/cli_address"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="textPostalAddress"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_bairro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_bairro"
android:imeActionId="#+id/cli_district"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<Spinner
android:id="#+id/sp_estados"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"/>
<Spinner
android:id="#+id/sp_cidade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:prompt="#string/cliente_cidade"/>
This ClienteEditActivity holds two fragments with different forms so I listen for a action menu click at the fragment, when menu is clicked I use this to call may validation:
if (!isValidEmail(etEmail)) {
cancel = true;
focusView = etEmail;
}
//after other validations
if (cancel){
focusView.reqeustFocus();
} else {
//save datas
}
The isValidEmail method is like this:
protected boolean isValidEmail(EditText editText) {
validate = new Validate(editText);
validate.addValidador(new ValidadorVazio(getContext()));
validate.addValidador(new ValidadorEmail(getContext()));
return validate.isValid();
}
My Validateclass looks like this:
public class Validate{
private EditText etFonte;
public Validate(EditText etFonte){
this.etFonte = etFonte;
}
public void addValidador(AbstractValidador validador){
lalidadores.add(validador);
}
public boolean isValid(){
for (AbstractValidador validator : lValidadores) {
try {
if (!validator.isValid(etFonte.getText().toString())) {
if(errorNotification != null) {
errorNotification.onInvalid(this);
} else {
setSourceViewError(validator.getMessage(), validator.getErrorDrawable());
}
return false;
} else {
if(errorNotification != null) errorNotification.onValid(this);
}
} catch (ValidadorException e) {
e.printStackTrace();
if(errorNotification != null) {
errorNotification.onInvalid(this);
} else {
setSourceViewError(e.getMessage(),validator.getErrorDrawable());
}
return false;
}
}
etFonte.setError(null);
return true;
}
private void setSourceViewError(String errorMessage, Drawable errorDrawable) {
if(errorDrawable != null) {
etFonte.setError(errorMessage, errorDrawable);
} else {
etFonte.setError(errorMessage);
}
}
}
Set error on TextInputLayout instead of EditText, Here's your code...
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_nome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cliente_nome"
android:imeActionId="#+id/cli_name"
android:imeActionLabel="#string/str_next"
android:imeOptions="actionUnspecified"
android:inputType="textPersonName"
android:maxLines="1"
android:singleLine="true"/>
Get your TextInputLayout at runtime and apply following code...
TextInputLayout textInputLayout = (TextInputLayout) findVeiwById(R.id.textInputLayout);
textInputLayout.setErrorEnabled(true);
textInputLayout.setError("Please enter a valid license number");
It will produce output something like this...
In your layout's root element, find the following line and remove it.
app:layout_behavior="#string/appbar_scrolling_view_behavior"
Everything will be fine!
I assume that you have set an error on wrong reference of edit text. You have to set error on the same edit email edit text and also have to request focus on same edit text.
Here is the code snippet for activity:-
private EditText mEmailET;
#Override
protected void onCreate(Bundle savedInstanceState) {
mEamilET = (EditText)findViewById(R.id.et_email);
}
private void setSourceViewError(String errorMessage, Drawable errorDrawable) {
if(errorDrawable != null) {
mEamilET.setError(errorMessage, errorDrawable);
mEamilET.requestFocus();
} else {
mEamilET.setError(errorMessage);
mEamilET.requestFocus();
}
}
If you still face the issue please send the activity code too for better understanding for us and appropriate answer.
Do not use "include" tag to show your layout. Just replace that tag with your layout and you good to go. And you would want to set marginTop="60dp" so the toolbar is accessible.
I am trying to add an edit text view dynamically to a layout from the java class. However from what I have implemented nothing changes on my action. This is what I have:
public final void onCreate(final Bundle i){
int value = 0;
isEdit = false;
try
{
super.onCreate(i);
this.setContentView(R.layout.contactedit);
ContactList.soundIndicator = 0;
etPhoneNumber = new ArrayList<EditText>();
this.etPhoneNumber.add((EditText) this.findViewById(R.id.etPhoneNumberInput));
this.addNumberBtn = (Button) this.findViewById(R.id.addNumberEditText);
addNumberBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//etPhoneNumber.add(new EditText(ContactEdit.this));
try{
LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll);
EditText temp = new EditText(ContactEdit.this);
temp.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
etPhoneNumber.add(temp);
layout.addView(temp);
}catch(Exception e){
Log.d(TAG, "Failed to create new edit text");
}
}
});
}
This is the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="vertical"
android:id="#+id/contacteditll"
android:background="#drawable/darkimg">
<TextView
android:id="#+id/titlePrint"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/titlePrompt"
/>
<Spinner
android:id="#+id/spContactTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/titlePrompt"
/>
<TextView
android:text="#string/namePrint"
android:id="#+id/namePrint"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:layout_width="match_parent"
android:hint="#string/returnName"
android:id="#+id/etFirstNameInput"
android:layout_height="wrap_content"
android:layout_toRightOf= "#+id/namePrint">
</EditText>
<TextView
android:text="#string/secondPrint"
android:id="#+id/secondPrint"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:layout_width="match_parent"
android:hint="#string/returnName"
android:id="#+id/etSecondNameInput"
android:layout_height="wrap_content"
android:layout_toRightOf= "#+id/namePrint">
</EditText>
<TextView android:id="#+id/numberPrint"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/numberPrint">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/etPhoneNumberInput"
android:hint="#string/returnNumber">
</EditText>
<Button android:id="#+id/addNumberEditText"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:freezesText="true"
android:editable="false"
android:text="ADD"
/>
<include
android:id="#+id/customedittext"
layout="#layout/myedittext"/>
<TextView android:id="#+id/emailPrint"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/emailPrint">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/etEmailAddressInput"
android:hint="#string/returnEmail">
</EditText>
<Button
android:freezesText="true"
android:editable="false"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:id="#+id/btnSaveButton"
android:layout_width="wrap_content"
android:text="#string/save"
android:layout_below="#+id/emailInput"
/>
</LinearLayout>
Whenever I click on my button the action is heard but nothing is done. The layout of the page does not change even though the new edit text is in my list of edit texts and I am adding the new edit text to the layout.
Any help would be appreciated thanks.
It sure gets added but you are not seeing it cause there is no space left. Two things to test:
Place the LinearLayout inside a ScrollView.
Use the weight parameter.
Using the weight parameter:
EditText temp = new EditText(ContactEdit.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1);
temp.setLayoutParams(params);