In TextInputLayout I want to add TextInputEditText via Custom class
Here is the code for custom class I have created :
public class CustomTextInputEditText extends TextInputLayout {
private TextInputEditText editText;
public CustomTextInputEditText(#NonNull Context context) {
this(context, null);
}
public CustomTextInputEditText(#NonNull Context context, #Nullable AttributeSet attrs) {
this(context, attrs, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
}
public CustomTextInputEditText(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
removeAllViews();
setWillNotDraw(false);
editText = new TextInputEditText(getContext());
createEditBox(editText);
}
private void createEditBox(TextInputEditText editText) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editText.setPadding(0,10,0,0);
editText.setLayoutParams(layoutParams);
}
}
Here is the Xml code :
<com.example.myapplication.CustomTextInputEditText
android:id="#+id/custom_view"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:boxStrokeColor="#color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"/>
It doesn't show any preview of the custom view
public class CustomTextInputEditText extends TextInputLayout {
private TextInputEditText editText;
public CustomTextInputEditText(#NonNull Context context) {
this(context, null);
}
public CustomTextInputEditText(#NonNull Context context, #Nullable AttributeSet attrs) {
this(context, attrs, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
}
public CustomTextInputEditText(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
//removeAllViews();
setWillNotDraw(false);
editText = new TextInputEditText(getContext());
createEditBox(editText);
}
private void createEditBox(TextInputEditText editText) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editText.setPadding(0,10,0,0);
editText.setLayoutParams(layoutParams);
addView(editText);
}
}
Instead of extending it with TextInputLayout try extending it with constraint layout and add your custom view with editText and InputField then inflate view
custom_material_edit_text.xml
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
style="#style/contentInputLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_8dp"
android:paddingStart="5dp"
android:paddingLeft="5dp"
android:layout_marginLeft="#dimen/_8dp"
android:layout_marginEnd="#dimen/_8dp"
android:layout_marginRight="#dimen/_8dp"
app:errorEnabled="false"
app:layout_constraintBottom_toTopOf="#+id/errorTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:hint="#string/EnterMobileNumber">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/textInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:gravity="start"
android:textDirection="anyRtl"
tools:text="03-xxxxxxx"
android:background="#null"
android:paddingEnd="#dimen/inputFieldPadding_36dp"
android:paddingRight="#dimen/inputFieldPadding_36dp" />
</com.google.android.material.textfield.TextInputLayout>
<ImageView
android:id="#+id/cancelButton"
android:layout_width="#dimen/inputIcon_20dp"
android:layout_height="#dimen/inputIcon_20dp"
android:layout_marginEnd="#dimen/iconMargin_16dp"
android:layout_marginRight="#dimen/iconMargin_16dp"
android:src="#mipmap/close"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#+id/textInputLayout"
app:layout_constraintEnd_toEndOf="#+id/textInputLayout"
app:layout_constraintTop_toTopOf="#+id/textInputLayout"
app:layout_constraintVertical_bias=".48" />
<ImageView
android:id="#+id/keyboardButton"
android:layout_width="#dimen/inputIcon_20dp"
android:layout_height="#dimen/inputIcon_20dp"
android:layout_marginEnd="#dimen/iconMargin_16dp"
android:layout_marginRight="#dimen/iconMargin_16dp"
android:src="#mipmap/label_keyboard"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="#+id/textInputLayout"
app:layout_constraintEnd_toEndOf="#+id/textInputLayout"
app:layout_constraintRight_toRightOf="#+id/textInputLayout"
app:layout_constraintTop_toTopOf="#+id/textInputLayout"
app:layout_constraintVertical_bias=".48" />
<TextView
android:id="#+id/errorTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_8dp"
android:layout_marginLeft="#dimen/_8dp"
android:layout_marginEnd="#dimen/_8dp"
android:layout_marginRight="#dimen/_8dp"
android:visibility="gone"
android:textColor="#color/red"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout"
tools:text="Invalid data" />
inflate(context, R.layout.custom_material_edit_text, this);
<com.example.widget.MaterialInputView
android:id="#+id/materialEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
app:floatingLabel="Amount (Rs.)"
android:inputType="number"
android:hint="00000-XXXXXX" />
Related
I have problem in android layout,that I don't know what is the problem.
Actually I want to change the font of the text beside the image.
When I don't put any text inside the textViews in xml code and I set the text in java code and also set the typeface,I got this problem.
This is my
group_view_two_4_first_activity.xml file:
<android.support.v7.widget.CardView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:cardCornerRadius="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/sport_complex_name"
app:layout_constraintTop_toTopOf="#+id/sport_complex_name">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/field" />
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/sport_complex_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textColor="#565657"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/hallNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="#7E7E7F"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="#+id/sport_complex_name"
app:layout_constraintTop_toBottomOf="#+id/sport_complex_name" />
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7E7E7F"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/hallNumber"
app:layout_constraintEnd_toStartOf="#+id/hallNumber"
app:layout_constraintTop_toTopOf="#+id/hallNumber" />
</android.support.constraint.ConstraintLayout>
And this is its java file:GroupViewTwo4FirstActivity
public class GroupViewTwo4FirstActivity extends ConstraintLayout {
View rootView;
TextView sportComplexName;
TextView hallNumber;
TextView number;
String txtNo;
String complexName;
public GroupViewTwo4FirstActivity(Context context) {
super(context);
init(context);
}
public GroupViewTwo4FirstActivity(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init(context);
}
public GroupViewTwo4FirstActivity(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.GroupViewTwo4FirstActivity, defStyleAttr, 0);
txtNo = ta.getString(R.styleable.GroupViewTwo4FirstActivity_hallNo);
complexName = ta.getString(R.styleable.GroupViewTwo4FirstActivity_complex_name);
ta.recycle();
init(context);
}
private void init(Context context) {
rootView = inflate(context, R.layout.group_view_two_4_first_activity, this);
Typeface typeface = EnglishToPersian.createTypeFace1(context);
sportComplexName = rootView.findViewById(R.id.sport_complex_name);
hallNumber = rootView.findViewById(R.id.hallNumber);
number = rootView.findViewById(R.id.number);
sportComplexName.setTypeface(typeface);
hallNumber.setTypeface(typeface);
number.setTypeface(typeface);
number.setText(EnglishToPersian.englishToPersian(txtNo));
hallNumber.setText("سالن شماره ");
sportComplexName.setText("مجتمع ورزشی افق لاله");
}
}
Everything is OK,But when I run the program,instead of getting this picture:
I got this one:
The constructor being called while inflating from xml is this one:
public GroupViewTwo4FirstActivity(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init(context);
}
this(context,attrs,0) calls your third constructor, which after while calls init(context) and then you call init(context) again, so you're doing that twice.
Defining your constructors as below guarantee that your init method gets called exactly once each time.
public GroupViewTwo4FirstActivity(Context context) {
this(context, null);
}
public GroupViewTwo4FirstActivity(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public GroupViewTwo4FirstActivity(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.GroupViewTwo4FirstActivity, defStyleAttr, 0);
txtNo = ta.getString(R.styleable.GroupViewTwo4FirstActivity_hallNo);
complexName = ta.getString(R.styleable.GroupViewTwo4FirstActivity_complex_name);
ta.recycle();
init(context);
}
Hope that helps!
I have the following layout file. I want to center the text in the second textView. In Android Studio design preview the text is centered. However, on running the app it is off the center.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="74dp"
android:background="#color/white">
<com.my.app.MyCustomTextView
android:id="#+id/custom_text_1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="15dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="15dp"
android:background="#drawable/rounded_corner_green"
android:gravity="center"
android:text="Some text here"
android:textColor="#color/white"
android:textSize="16sp" />
<com.my.app.MyCustomTextView
android:id="#+id/custome_text_2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:background="#drawable/circular_red"
android:gravity="center"
android:layout_gravity="end"
android:visibility="gone"
android:includeFontPadding="false"
android:textColor="#color/white"
android:textSize="16sp" />
</FrameLayout>
</layout>
This is how it looks (0 is not in the center)
Strangely though, if I replace my custom textView with just TextView the text is perfectly centered now.
This is the MyCustomTextView
public class MyCustomTextView extends TextView {
public MyCustomTextView (Context context) {
super(context);
initView();
}
public MyCustomTextView (Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public MyCustomTextView (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MyCustomTextView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initView();
}
//here the typeface is set on the TextView after creating typeface from font asset
private void initView() {
CommonUtils.setCustomFontForText(MyApplication.getAppContext(), MyCustomTextView.this);
}
}
Can someone help me? What is going wrong here? Thanks!!
I got a bunch of form controls that all show an Icon to the left, like so:
For this I have this code:
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="#dimen/icon_edittext_height"
android:layout_gravity="top"
android:layout_marginRight="#dimen/icon_edittext_marginRight"
android:layout_marginTop="#dimen/icon_edittext_margintop"
android:src="#drawable/ic_location_city_black_24dp"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/create.data.city.layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<AutoCompleteTextView
android:id="#+id/create.data.city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/create_event_city"
android:imeOptions="actionNext"
android:inputType="textCapSentences"
android:maxLines="1"
/>
</android.support.design.widget.TextInputLayout>
</FrameLayout>
</LinearLayout>
Now because I have around 20 controls, I dont want to repeat the:
<LinearLayout>
<ImageView/>
<FrameLayout>
...content....
</FrameLayout>
</LinearLayout>
schema 20 times, but I would like to do something like this:
<MycustomLayout>
<EditText .../>
</MycustomLayout>
<MycustomLayout>
<Spinner ... />
</MycustomLayout>
So basically I want to create a custom Layout that extends Linear, and I want to be able to use it in the same way that I use LinearLayout, while having an icon prepended at all times.
You can try using include tag in your layout to include the part which is repeating. Or if that doesn't work you can try something like this:
public class CustomControl extends LinearLayout {
public CustomControl (Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_layout, this);
}
}
Where custom_layout is the layout for you custom view.
Then you can just do in your xml:
<CustomControl />
UPD:
In order to add child views in xml just override onFinishInflate and add views like this:
protected void onFinishInflate() {
View[] children = detachChildren();
for (int i = 0; i < children.length; i++)
this.addView(children[i]);
}
No need to add too much layouts. Just try to use this...
<android.support.design.widget.TextInputLayout
android:id="#+id/create.data.city.layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/create.data.city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="City"
android:drawableLeft="#drawable/ic_location_city_black_24dp"
android:drawablePadding="5dp"
android:imeOptions="actionNext"
android:inputType="textCapSentences"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
Solved it myself like so:
public class IconFieldLinearLayout extends LinearLayout {
private Drawable mDefaultDrawable;
public IconFieldLinearLayout(Context context) {
super(context);
init(context, null);
}
public IconFieldLinearLayout(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public IconFieldLinearLayout(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public IconFieldLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
this.setOrientation(HORIZONTAL);
mDefaultDrawable = getResources().getDrawable(R.drawable.tv_avatar_default);
initAttr(context, attrs);
}
private void initAttr(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.IconFieldLinearLayout, 0, 0);
Drawable icon = a.getDrawable(R.styleable.IconFieldLinearLayout_icon);
a.recycle();
initIcon(context, icon);
}
else {
initIcon(context, mDefaultDrawable);
}
}
private void initIcon(Context context, Drawable icon) {
ImageView imageView = new ImageView(context);
imageView.setColorFilter(ContextCompat.getColor(context, R.color.colorAccent));
int dimension = getResources().getDimensionPixelSize(R.dimen.icon_edittext_height);
int marginRight = getResources().getDimensionPixelSize(R.dimen.icon_edittext_marginRight);
int marginTop = getResources().getDimensionPixelSize(R.dimen.icon_edittext_margintop);
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dimension);
layoutParams.gravity = Gravity.TOP;
layoutParams.setMargins(0, marginTop, marginRight, 0);
imageView.setImageDrawable(icon);
addView(imageView, layoutParams);
}
}
Did not really need to put the FrameLayout in there.
I'm trying to change different EditText properties using data binding but it doesn't seem to be working.
custom_edittext.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="textVar"
type="String"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/descriptionTextEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:ems="12"
android:inputType="textMultiLine"
android:text="#{textVar}"/>
</RelativeLayout>
</layout>
My class extends RelativeLayout
public class CustomEditText extends RelativeLayout {
public CustomEditText(Context context) {
super(context);
init(context, null);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
}
and my init:
private void init(Context context, AttributeSet attrs){
View.inflate(context, R.layout.custom_edittext, this);
CustomEdittextBinding stomEdittextBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.custom_edittext, null, false);
Drawable drawableButton = context.getResources().getDrawable(R.drawable.ic_action_name);//context.getResources().getDrawable(R.drawable.ic_action_name);
customEdittextBinding.setTextVar("new text"); //it should change the text, shouldn't it?
}
my main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customEditText = findViewById(R.id.custom);
}
and inside the activity_main.xml I've added my CustomEditText like this but there is an error - The following classes could not be found.
<com.example.xxx.app.CustomEditText
android:id="#+id/custom"
android:layout_width="344dp"
android:layout_height="50dp"
android:paddingLeft="100dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"></com.example.xxx.app.CustomEditText >
Ok, I've solved this problem by adding addView(customEdittextBinding.getRoot()); after instantiating CustomEdittextBinding.
I try to align spinner and edit text by baseline, but it doesn't work:
It start happen after update support library dependency from 24.1.1 to 24.2.1 (support-v4, appcompat-v7, design).
This is my xml code:
<RelativeLayout
android:id="#+id/email_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spinner"
style="#style/MailSpinner"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:dropDownWidth="wrap_content" />
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_alignBaseline="#id/spinner"
android:layout_toStartOf="#id/spinner"
android:baselineAlignedChildIndex="0">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="E-mail"
android:ellipsize="end"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:textSize="16sp"/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
MailSpinner style:
<style name="MailSpinner" parent="Widget.AppCompat.Spinner.Underlined">
<item name="android:background">#drawable/spinner_textfield_background</item>
<item name="backgroundTint">#color/spinner_tint</item>
<item name="backgroundTintMode">src_atop</item>
</style>
Essentially the TextInputLayout isn't providing a baseline value to it's parent. We need to pipe the correct baseline of the EditText by extending TextInputLayout. This works for me, however, I'm not sure if the baseline would change due to other events from the TextInputLayout.
public class CTextInputLayout extends TextInputLayout {
public CTextInputLayout(Context context) {
super(context);
}
public CTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public int getBaseline()
{
EditText editText = getEditText();
return editText.getPaddingTop() + editText.getBaseline();
}
}
I think this version is better when hint label is floating
public class CTextInputLayout extends TextInputLayout {
public CTextInputLayout(Context context) {
super(context);
}
public CTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public int getBaseline() {
EditText editText = getEditText();
return getMeasuredHeight() - (editText.getMeasuredHeight() - editText.getBaseline());
}
}
Here's a version which aligns correctly when the TextInputLayout error text is set:
public class BaselineTextInputLayout extends TextInputLayout {
public BaselineTextInputLayout(Context context) {
super(context);
}
public BaselineTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BaselineTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public int getBaseline() {
EditText editText = getEditText();
if (editText == null) { return 0; }
return editText.getTop() + editText.getBaseline();
}
}