Why does the imageView get doubled in android? - android

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!

Related

Create Custom class for TextInputLayout

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" />

Android: Text in custom textView not centering

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!!

Custom LinearLayout with an Icon

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.

Custom compound view and .findViewById()

I am really worried about the .findViewById() method and its use in a custom compound view creation. I am not sure about the exact place that it is guaranteed that it will never return null.
Let's say I have this custom compound view and that it is added to some .xml like this:
<com.app.path.TwoButtonsView
android:id="#+id/ok_cancel_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
two_buttons_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge>
<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="horizontal">
<TextView
android:id="#+id/first_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:textAllCaps="true"/>
<TextView
android:id="#+id/second_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:textAllCaps="true"/>
</LinearLayout>
</merge>
TwoButtonsView.java
public class TwoButtonsView extends LinearLayout {
// views
private TextView mFirstButtonView;
private TextView mSecondButtonView;
public TwoButtonsView(Context context) {
super(context);
init(context, null);
}
public TwoButtonsView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public TwoButtonsView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public TwoButtonsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.two_buttons_view, this);
// retrieve views
mFirstButtonView = (TextView) findViewById(R.id.first_button); // is there a chance it will return null?
mSecondButtonView = (TextView) findViewById(R.id.second_button); // is there a chance it will return null?
}
}
My question is: is there any chance that .findViewById(RESOURCE_ID) will return null right after calling inflater.inflate(R.layout.two_buttons_view, this);, or should I call my init() method on onFinishInflate() callback?
My question is: is there any chance that .findViewById(RESOURCE_ID)
will return null right after calling
inflater.inflate(R.layout.two_buttons_view, this)
No, there is not, as long as the views you are looking for are in the layout and you explicitly added to the view's hierarchy. In the end, findViewById loops on View[], filled up by addView.
A small note about
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.two_buttons_view, this);
ViewGroup has the static method inflate, so you don't need to retrieve the inflater calling getSystemService

textView.setText is not working in compound view

textView.setText is not working properly in a compound view.
Means Both hello and jack are appearing...
and jack is displayed below layout not appearing if I put some background color,...
Could somebody help... Thanks and regards...
Here is my code...
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/chatLayout"
android:layout_width="300dp"
android:layout_height="400dp"
>
<TextView
android:id="#+id/chatNamed"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Hello"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
/>
</RelativeLayout>
CompoundView.java
public class ChatCompoundView extends RelativeLayout {
private static TextView zoneName;
public ChatCompoundView(Context context) {
this(context, null);
setView();
}
public ChatCompoundView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
setView();
}
public ChatCompoundView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setView();
}
public void setView()
{
inflate(getContext(), R.layout.chat_layout, this);
zoneName = (TextView) findViewById(R.id.chatNamed);
}
zoneName.setText("jack");
}
try this..
LayoutInflater mInflater;
public ChatCompoundView (Context context) {
super(context);
mInflater = LayoutInflater.from(context);
setView();
}
setView():
public void setView(){
mInflater.inflate(R.layout.custom_view, this, true);
TextView zoneName = (TextView) findViewById(R.id.chatNamed);
zoneName .setText("jack");
}
Yeah Now I got the Answer... Actually my constructors are called with this(context),.. and I need to call super(context)...
Code Changed to
public ChatCompoundView(Context context) {
super(context);
setView();
}
public ChatCompoundView(Context context, AttributeSet attrs) {
super(context, attrs);
setView();
}
public ChatCompoundView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setView();
}

Categories

Resources