Android custom AutoCompleteTextView doesn't appear after hiding - android

I create an autoCompleteTextView as below Layout and Java class :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/autoCompleteTextViewGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:minHeight="#dimen/component_height">
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:animateLayoutChanges="true">
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<android.support.v4.widget.ContentLoadingProgressBar
android:id="#+id/progressBar"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:indeterminate="true"
android:indeterminateDrawable="#anim/anim_indeterminate_mini"
android:interpolator="#anim/anim_progress_interpolator"
android:visibility="visible" />
</FrameLayout>
and Java class is :
public class MAutoCompleteText extends LinearLayout {
AutoCompleteTextView autoCompleteTextView;
ContentLoadingProgressBar progressBar;
TextInputLayout textInputLayout;
ViewGroup autoCompleteTextViewGroup;
public MAutoCompleteText(Context context) {
super(context);
init(context);
}
public MAutoCompleteText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MAutoCompleteText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
#TargetApi(21)
public MAutoCompleteText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
private void init(Context context) {
inflate(context, R.layout.layout_auto_complete_text, this);
autoCompleteTextViewGroup = (ViewGroup) findViewById(R.id.autoCompleteTextViewGroup);
textInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout);
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
progressBar = (ContentLoadingProgressBar) findViewById(R.id.progressBar);
}
public void setAdapter(ArrayAdapter<String> adapter) {
autoCompleteTextView.setAdapter(adapter);
}
public String getText() {
return autoCompleteTextView.getText().toString();
}
public void setErrorEnabled(boolean status) {
textInputLayout.setErrorEnabled(status);
}
public void setError(int errorId) {
textInputLayout.setError(getContext().getString(errorId));
}
public int getVisibility() {
return autoCompleteTextViewGroup.getVisibility();
}
public void setVisibility(int visibility) {
autoCompleteTextViewGroup.setVisibility(visibility);
autoCompleteTextViewGroup.setMinimumHeight(R.dimen.component_height);
}
public void setProgressBarVisibility(int visibility) {
progressBar.setVisibility(visibility);
}
public MAutoCompleteText setHint(int hintId) {
autoCompleteTextView.setHint(hintId);
return this;
}
public void setImeOption(int imeOption) {
autoCompleteTextView.setImeOptions(imeOption);
}
}
Everything work OK. I setVisibility(GONE) and my component hide from UI,
but when I use setVisibility(VISIBLE) doesn't happen any thing !!!!
How can I display my component programmatically?

I found answer of my problem , at least it works now ... :)
I change setVisibility to below code :
public void setVisibility(int visibility) {
autoCompleteTextViewGroup.setVisibility(visibility);
if (visibility == View.VISIBLE) {
textInputLayout.setVisibility(visibility);
autoCompleteTextView.setVisibility(visibility);
progressBar.setVisibility(visibility);
requestLayout();
}
}
In fact requestLayout(); reDraw layout of my component on UI again and it`s appear correctly .

Related

CustomView don't show in design mode

I have a CustomView extend from RelativeLayout.
I Use this CustomView in layout file but Android Studio in design mode not showing the CustomView. what is problem?
koala_appbar.xml:
<RelativeLayout 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="top"
tools:context=".activity.basic.BaseActivity">
<ImageView
android:id="#+id/appbar_imgLogo"
android:layout_width="#dimen/appBar_iconWidth"
android:layout_height="#dimen/appBar_iconHeight"
android:src="#drawable/logo"
android:layout_marginTop="#dimen/appBar_iconTopMargin"
android:layout_marginLeft="#dimen/appBar_iconLeftMargin"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/appbar_height"
android:orientation="horizontal"
android:layoutDirection="ltr"
>
<koala.android.customer.views.widget.CustomTextView
android:id="#+id/appBar_txtTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|right"
android:text="#string/app_name_persian"
android:textColor="#color/colorPrimary"
android:textSize="#dimen/TextSize_large"
app:tvCustomFont="#string/baseFont"
/>
<koala.android.customer.views.widget.CustomImageView
android:id="#+id/appBar_imgBack"
android:layout_width="#dimen/appbar_height"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_arrow_right"
app:ivDrawableTint="#color/colorPrimary"
android:padding="#dimen/appBar_backPadding"
/>
</LinearLayout>
</RelativeLayout>
KoalaAppBar.java:
public class KoalaAppBar extends RelativeLayout {
private static final String DEFAULT_TITLE = "default";
private static final int DEFAULT_TITLE_COLOR = ResourcesCompat.getColor(AppController.getContext().getResources(), R.color.colorPrimary , null);
private static final float DEFAULT_TITLE_FONT_SIZE = AppController.getContext().getResources().getDimension(R.dimen.TextSize_small);
protected CustomTextView txtTitle;
protected CustomImageView imgBack;
protected ImageView imgLogo;
public KoalaAppBar(Context context) {
super(context);
init(context , null);
}
public KoalaAppBar(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init(context , attrs);
}
public KoalaAppBar(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context , attrs);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public KoalaAppBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
if(context != null){
findViews(context);
}
if(attrs != null){
initAttribute(context,attrs);
}
}
private void initAttribute(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.KoalaAppBar);
String title = a.getString(R.styleable.KoalaAppBar_appTitle);
if(title == null || title.isEmpty()){
title = DEFAULT_TITLE;
}
int titleColor = a.getColor(R.styleable.KoalaAppBar_appTitleColor, DEFAULT_TITLE_COLOR);
float fontSize = a.getDimension(R.styleable.KoalaAppBar_appTitleFontSize, DEFAULT_TITLE_FONT_SIZE);
boolean logoVisibility = a.getBoolean(R.styleable.KoalaAppBar_appLogoVisibility , true);
initTitle(title , titleColor , fontSize);
initLogo(logoVisibility);
a.recycle();
}
private void initLogo(boolean logoVisibility) {
if(logoVisibility){
this.imgLogo.setVisibility(VISIBLE);
}else{
this.imgLogo.setVisibility(GONE);
}
}
private void initTitle(String title, int titleColor, float fontSize) {
this.txtTitle.setText(title);
this.txtTitle.setTextColor(titleColor);
this.txtTitle.setTextSize(fontSize);
}
private void findViews(Context context) {
View rootView =
LayoutInflater.from(context).inflate(
R.layout.koala_appbar,
this,
true
);
this.txtTitle = rootView.findViewById(R.id.appBar_txtTitle);
this.imgBack = rootView.findViewById(R.id.appBar_imgBack);
this.imgLogo = rootView.findViewById(R.id.appbar_imgLogo);
}
}

IconGenerator with custom view, remove padding or margin

I am trying to create this:
But unfortunately I'm only being able to get this
This question is not about the shadow or the font, I can easily adjust those I guess. What is driving me crazy is to remove the margins (or padding) between the blue line and the edges of the marker.
The relevant code is:
On ClusterRenderer that extends DefaultClusterRenderer
#Override
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
markerOptions.
icon(BitmapDescriptorFactory.fromBitmap(prepareIcon(item))).
position(new LatLng(item.getPosition().latitude, item.getPosition().longitude)).
anchor(iconGenerator.getAnchorU(), iconGenerator.getAnchorV());
super.onBeforeClusterItemRendered(item, markerOptions);
}
private Bitmap prepareIcon(MyItem item) {
markerDecoratorView.setPrice(getPriceSpannedText(item.getPrice()));
iconGenerator.setContentView(markerDecoratorView);
iconGenerator.setContentPadding(-2, -2, -2, 0); // This does not make any difference
return iconGenerator.makeIcon();
}
The custom view
public class MarkerDecoratorView extends LinearLayout {
#Bind(R.id.marker_item_price)
TextView markerItemPrice;
public MarkerDecoratorView(Context context) {
super(context);
init(context);
}
public MarkerDecoratorView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MarkerDecoratorView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MarkerDecoratorView(Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
private void init(Context context) {
inflate(context, R.layout.map_marker_view, this);
this.setOrientation(VERTICAL);
if (isInEditMode()) return;
ButterKnife.bind(this);
}
public void setPrice(SpannableString text) {
markerItemPrice.setText(text);
}
public void setPrice(String text) {
markerItemPrice.setText(text);
} }
}
The view layout is:
<?xml version="1.0" encoding="utf-8"?>
<merge 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">
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/tab_indicator" />
<TextView
android:id="#+id/marker_item_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/MapMarker"
android:gravity="center"
android:padding="4dp"
tools:text="SAR 300" />
</merge>
EDIT: I tried also with a 9patch in the custom view (xml and class). It works in Android Studio preview, but not inside the marker.
EDIT 2: I worked this out by adding a regular icon, without 9patch. I keep this question open because this topic might be relevant to others. So if anyone has the solution please post! :)

Baseline aligned TextInputLayout and Spinner

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();
}
}

Custom Linear Layout Child not showing

i tried to create custom linear layout that have dynamic height and width = height. The custom layout is fine, but the child view not showing at my custom linear layout. i tried some way to fix it, but still cant solve it.
public class CustomLinearLayout extends LinearLayout {
public CustomLinearLayout(Context context) {
super(context);
}
public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(height, height);
}
}
this is the xml that contain my custom linear layout. the child view not showing.
<com.example.admin.antriclient.CustomLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:autofit="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/bg_nomor_antrian"
android:padding="10dp"
>
<me.grantland.widget.AutofitTextView
android:id="#+id/service_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Teller"
android:textColor="#color/white"
android:textStyle="bold"
android:gravity="center_horizontal"
android:singleLine="false"
autofit:minTextSize="12sp"
/>
<me.grantland.widget.AutofitTextView
android:id="#+id/nomor_antrian"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="T21"
android:textSize="50sp"
android:textColor="#color/white"
android:textStyle="bold"
android:gravity="center_horizontal"
android:singleLine="true"
autofit:minTextSize="12sp"
/>
</com.example.admin.antriclient.CustomLinearLayout>
thanks in advance
My problem is because i didnt call the super.onMeasure() method. Thanks to Mike M.
my class should be like this
public class CustomLinearLayout extends LinearLayout {
public CustomLinearLayout(Context context) {
super(context);
}
public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec,heightMeasureSpec);
//int height = MeasureSpec.getSize(heightMeasureSpec);
//setMeasuredDimension(height, height);
}
}
For me I had to add a LayoutInflater for it to show
public class SidebarLayout extends LinearLayout {
private static final String TAG = "SidebarLayout";
public SidebarLayout(Context context) {
super(context);
}
public SidebarLayout(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.sidebar, this);
}
public SidebarLayout(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
<com.example.a3danimationtester.SidebarLayout
android:id="#+id/layout_sidebar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/layout_topbar" />

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