TextInputEditText don't work on Android 5.1 - java

I'm hoping you would help me to get to the problem I'm facing right now.
I created a custom search view using a TextInputEditText, it's really simple actually and works fine on all the Android version with the exception of 5.1(Lollipop), the EditText never hide the hint when write or even show the cursor, also the text is not visible.
I t I have tried everything I know, but I can't figure what could it be.
Thank you so much for your time.
This is my custom class view :
public class CustomSearchView extends RelativeLayout {
#BindView(R.id.csvIcon) ImageView csvIcon;
public #BindView(R.id.csvEditText) TextInputEditText editText;
public CustomSearchView(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_search_view, this, true);
ButterKnife.bind(this);
}
public CustomSearchView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_search_view, this, true);
ButterKnife.bind(this);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.CustomSearchView,
0, 0);
String hintText = typedArray.getString(R.styleable.CustomSearchView_hintText);
try {
int max = typedArray.getInteger(R.styleable.CustomSearchView_csv_length, 17);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
editText.setInputType(typedArray.getInt(R.styleable.CustomSearchView_android_inputType, InputType.TYPE_CLASS_TEXT));
editText.setHint(hintText);
csvIcon.setImageDrawable(typedArray.getDrawable(R.styleable.CustomSearchView_csv_icon));
} catch (Exception e) {
e.printStackTrace();
}
}
public CustomSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
This are the attribute specification :
<declare-styleable name="CustomSearchView">
<attr name="csv_icon" format="reference"/>
<attr name="hintText" format="string"/>
<attr name="csv_length" format="integer"/>
<attr name="android:inputType"/>
<attr name="android:digits"/>
</declare-styleable>
This is my XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/charcoal_grey_two">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/csvIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:src="#drawable/icon_search"/>
<android.support.design.widget.TextInputEditText
android:id="#+id/csvEditText"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center"
android:cursorVisible="true"
android:focusableInTouchMode="true"
android:layout_marginLeft="10dp"
android:imeOptions="actionSearch"
android:inputType="textAutoComplete"
android:textSize="20dp"
android:textColorHint="#color/slate_grey"
android:textColor="#color/slate_grey"
android:background="#android:color/transparent"/>
</LinearLayout>
</RelativeLayout>
And this is my implementation in a fragment :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#color/gunmetal">
<mx.com.segurosbancomer.gpsajustadores.view.custom.TopBarLayout
android:id="#+id/topBar"
android:layout_width="match_parent"
android:layout_height="45dp"
app:tb_color="#color/gunmetal"
app:tb_text="#string/bf_title"
app:tb_text_color="#color/white"
app:tb_btnleft_visibility="0"
app:tb_btnright_visibility="8"
app:tb_btnleft="#drawable/icon_prev_white"
app:tb_btnright="#drawable/icon_dot_gray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomTextView
android:id="#+id/tituloPoliza"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/bf_search"
android:textColor="#color/white"
android:textSize="18dp"
android:layout_margin="10dp"
app:ctv_customFont="stagsansbook.otf" />
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomSearchView
android:id="#+id/searchPoliza"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="10dp"
android:cursorVisible="true"
android:focusableInTouchMode="true"
android:inputType="textAutoComplete"
android:digits="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890"
app:csv_length="30"
app:csv_icon="#drawable/icon_search"
app:hintText="#string/bf_poliza" />
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomSearchView
android:id="#+id/searchInciso"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="10dp"
android:cursorVisible="true"
android:focusableInTouchMode="true"
android:inputType="textAutoComplete"
android:digits="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890"
app:csv_length="30"
app:csv_icon="#drawable/icon_search"
app:hintText="#string/bf_inciso" />
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#color/charcoal_grey_two"/>
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomTextView
android:id="#+id/tituloSerie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/bf_search_serie"
android:textColor="#color/white"
android:textSize="18dp"
android:layout_margin="10dp"
app:ctv_customFont="stagsansbook.otf" />
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomSearchView
android:id="#+id/searchSerie"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="10dp"
android:cursorVisible="true"
android:focusableInTouchMode="true"
android:inputType="textAutoComplete"
android:digits="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890"
app:csv_length="17"
app:csv_icon="#drawable/icon_search"
app:hintText="#string/bf_serie"/>
</LinearLayout>
</LinearLayout>

Finally I find the solution. If some else have a similar problem just like me, the issue was resolved by removing this line from the AndroidManifest.xml
android:hardwareAccelerated="false"
This line allow to the fragment and all the components to work perfectly.

You should use TextInputEditText with TextInputLayout wrapped. Just use EditText instead.
See this: https://stackoverflow.com/a/37834022/5558150

Related

How to manage width of a custom view with layout using ConstraintLayout?

I have a fragment and I'm using an accordion view in it, which I downloaded form GitHub - https://github.com/riyagayasen/Android_accordion_view, and in its body (which is a RelativeLayout) I need a custom view named AccordionItem like the second one on the pic, which I simply made in fragment's xml:
https://pp.userapi.com/c846123/v846123865/1d925d/CaEhr8uSLFA.jpg
But my custom view (the first one) has a wrap_content width behavior for some reason.
The code follows:
Here is the body of an AccordionView in my fragment's xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- This is my custom view -->
<!-- Notice that the width is set to match_constraint -->
<com.app.myapp.views.AccordionItem
android:id="#+id/item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:isStarred="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="Another title" />
<!-- This is an example of how my view should look like -->
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/accordion_item"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/item">
<CheckBox
android:id="#+id/favourite_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:button="#drawable/star_checkbox"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_t" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Title"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/imageView2"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="#+id/imageView2" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
The xml of the custom view:
<?xml version="1.0" encoding="utf-8"?>
<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:background="#drawable/accordion_item">
<CheckBox
android:id="#+id/favourite_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:button="#drawable/star_checkbox"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/title"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_t" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Title"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/item_icon"
app:layout_constraintStart_toEndOf="#+id/item_icon"
app:layout_constraintTop_toTopOf="#+id/item_icon" />
</android.support.constraint.ConstraintLayout>
Java class of the custom view:
public class AccordionItem extends ConstraintLayout {
private String titleString;
private TextView title;
private boolean isStarred;
public AccordionItem(Context context) {
super(context);
initView();
}
public AccordionItem(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.AccordionItem,
0, 0);
try {
titleString = a.getString(R.styleable.AccordionItem_title);
isStarred = a.getBoolean(R.styleable.AccordionItem_isStarred, false);
} finally {
a.recycle();
}
initView();
}
private void initView() {
View view = inflate(getContext(), R.layout.accordion_item, null);
addView(view);
}
public void setTitleString(String titleString) {
this.titleString = titleString;
invalidate();
requestLayout();
}
public String getTitleString() {
return titleString;
}
public void setStarred(boolean starred) {
this.isStarred = starred;
invalidate();
requestLayout();
}
public boolean isStarred() {
return isStarred;
}
}
And finally the attributes of my view:
<declare-styleable name="AccordionItem">
<attr name="title" format="string"/>
<attr name="isStarred" format="boolean"/>
<attr name="background_drawable" format="reference"/>
</declare-styleable>
I expect my view to take the whole width it is given, but it shrinks instead.
Am I using the wrong way of creating views, or the wrong layout that my view extends?
Thank you.
inflate method with no parent will lost layoutParams(height,width)
private void initView() {
inflate(getContext(), R.layout.accordion_item, this);
// or
// View view = LayoutInflater.from(getContext()).inflate(R.layout.accordion_item, this, false);
// addView(view);
}
You don't really need library for this simple layout, have a look at this example :
<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=".Fragments.CheckoutScreen">
<!-- This is an example of how my view should look like -->
<CheckBox
android:id="#+id/favourite_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="#+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView4" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="T"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/textView4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView4" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Title"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.383" />

Auto Size TextView for TextSwitcher

I see that Android released in Oreo a new attribute for textView:
android:autoSizeTextType
This adapts the layout of the textView based on the string of text that it is showing.
How would I be able to use this with a textSwitcher?
I don't know about the ones above but here's an solution with minimal coding. You can change the main layout height to see the autosizing in action, or you can tap the upper switcher. A cool feature is that the styling of the second text field carries over when tapped (i.e. text is white).
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun switchText(v: View) {
(v as? TextSwitcher)?.let{
it.tag = (((it.tag as? Int) ?: 0) + 1) % it.childCount
it.setText((it.children.toList()[it.tag as Int] as TextView).text)
}
}
}
activity_main.xml
<?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="350dp"
tools:context=".MainActivity">
<TextSwitcher
android:id="#+id/a"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/purple_200"
app:layout_constraintBottom_toTopOf="#id/b"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread"
android:onClick="switchText"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<TextView
android:id="#+id/a1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="999 / 999"
android:gravity="center"
android:letterSpacing="-0.05"
app:autoSizeMaxTextSize="936sp"
app:autoSizeMinTextSize="28sp"
app:autoSizeTextType="uniform"
android:lines="1"
android:includeFontPadding="false" />
<TextView
android:id="#+id/a2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1000000 / 10000000"
android:textColor="#color/white"
android:gravity="center"
android:letterSpacing="-0.05"
app:autoSizeMaxTextSize="136sp"
app:autoSizeMinTextSize="28sp"
app:autoSizeTextType="uniform"
android:lines="1"
android:includeFontPadding="false" />
</TextSwitcher>
<TextSwitcher
android:id="#+id/b"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/teal_200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/a"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_chainStyle="spread"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<TextView
android:id="#+id/b1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="999 / 999"
android:gravity="center"
android:letterSpacing="-0.05"
app:autoSizeMaxTextSize="136sp"
app:autoSizeMinTextSize="28sp"
app:autoSizeTextType="uniform"
android:lines="1"
android:includeFontPadding="false" />
</TextSwitcher>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello" />
private ViewFactory viewFactory = new ViewFactory() {
public View makeView() {
LayoutInflater inflater = LayoutInflater.from(TextSwitcherTest.this);
TextView textView = (TextView) inflater.inflate(R.layout.textView, null);
return textView;
}
};
altitudeSwitcher = (TextSwitcher) findViewById(R.id.altitude);
altitudeSwitcher.setFactory(new ViewFactory() {
#Override
public View makeView() {
TextView t = new TextView(getApplicationContext());
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 50);
return t;
}
});

Custom button that extends AppCompatButton

I'm trying to create custom button that will contain multiple TextViews. I read that I should have it extend android.support.v7.widget.AppCompatButton rather than android.widget.Button
Here is the code for the custom button class:
public class CustomButton extends AppCompatButton {
public CustomButton(Context context) {
this(context, null);
}
public CustomButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
This works as a simple button, but as soon as I add a textview in the activity xml I get the following error, where the error line is the line of the text view
java.lang.RuntimeException: Unable to start activity ComponentInfo{me.app/me.app.MainActivity}: android.view.InflateException: Binary XML file line #21: me.app.CustomButton cannot be cast to android.view.ViewGroup
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MainActivity">
<me.app.CustomButton
android:id="#+id/button7"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="100dp"
android:text="Button"
android:theme="#style/Widget.AppCompat.Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/increment_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="00:00:00"
android:textColor="#android:color/black"
android:textSize="22sp" />
</me.app.CustomButton>
</android.support.constraint.ConstraintLayout>
Do I need to write the activity.xml file in a different way if I extend AppCompatButton instead of Button ?
You can use a CardView for the same effect...
<android.support.v7.widget.CardView
android:id="#+id/card0"
android:focusable="true"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:foreground="?android:selectableItemBackground"
app:cardCornerRadius="4dp"
app:cardElevation="4dp"
app:cardPreventCornerOverlap="false">
<LinearLayout
android:id="#+id/ll1"
android:padding="10.0dip"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="How to Refresh!?"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="serif"
android:gravity="center"/>
<TextView
android:id="#+id/t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="How to Refresh!?"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="serif"
android:gravity="center"/>
</LinearLayout>
</android.support.v7.widget.CardView>
This way you can add as many TextView you want!

Design lib - CoordinatorLayout/CollapsingToolbarLayout with GridView/listView

This might be silly question but I didn't understand Design lib well. I am following this reference to create below layout. The Blue area should work as parallax when I scroll the GridView.
But when I scroll grid View nothing happens in AppBarLayout.
But This works with NestedScrollView and RecyclerView
Below is My Layout file-
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:background="#500403"
android:layout_height="#dimen/detail_backdrop_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#122453"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<ImageView
android:id="#+id/backdrop1"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:fitsSystemWindows="true"
android:layout_gravity="center"
android:src="#drawable/bar_offline"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#982223"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<GridView
android:id="#+id/grid"
android:numColumns="4"
android:background="#367723"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:src="#drawable/bar_offline"
android:layout_margin="#dimen/fab_margin"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
Any help would be appreciated.
With ListView/GridView, it works only on Lollipop by following code-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
listView.setNestedScrollingEnabled(true);
}
I think for Now CoordinatorLayout works only with RecyclerView and NestedScrollView
EDIT :
use -
ViewCompat.setNestedScrollingEnabled(listView/gridview,true); (add Android Support v4 Library 23.1 or +)
A simple solution was added to the support lib:
ViewCompat.setNestedScrollingEnabled(listView,true);
I've tested it with Android Support v4 Library 23.1 and it works well.
Currently the ListView and the GridView have the expected behavior with the CoordinatorLayout only with API>21.
To obtain this behavior you have to set:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setNestedScrollingEnabled(true);
}
It is not enough to implement the NestedScrollingChild.
The AbsListView isn't deployed with support libraries , then it depends by the SO running in the device.
You have to override some methods in the AbsListView. For example you can check the onInterceptTouchEvent method.
Inside this code you can see:
case MotionEvent.ACTION_DOWN: {
//......
startNestedScroll(SCROLL_AXIS_VERTICAL);
//....
}
case MotionEvent.ACTION_MOVE: {
//.....
if (startScrollIfNeeded((int) ev.getX(pointerIndex), y, null)) {
//....
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: {
//..
stopNestedScroll();
break;
}
This code is only in the implementation of AbsListView v21+.
If you check the AbsListView with API 20 or lower, you will not find any nested scroll reference.
You have to put gridview inside NestedScrollview as usual then you have to add gridview height dynamically. then everything would work good.!!!
<android.support.v4.widget.NestedScrollView
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="match_parent"
android:layout_gravity="fill_vertical"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="#+id/camp_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolbar"
android:layout_margin="10dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
android:visibility="visible" >
</GridView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
This is working for me.
https://gist.github.com/sakurabird/6868765
I use GridView inside NestedScrollView
For convenience i've created a subclass with the new ViewCompat solution:
public class CoordinatedListView extends ListView {
public CoordinatedListView(Context context) {
super(context);
init();
}
public CoordinatedListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CoordinatedListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CoordinatedListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
ViewCompat.setNestedScrollingEnabled(this, true);
}
}
Enjoy :)

Custom View not appearing

Here is the XML for the layout in which I want my custom view to appear.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rs="http://schemas.android.com/apk/res/com.bookcessed.booksearch"
android:id="#+id/widget273"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/csp_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Your Source"
android:textSize="40sp"
android:textColor="#ffc83200"
android:gravity="center"
android:paddingTop="15dip"
android:paddingBottom="75dip"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<com.bookcessed.booksearch.SearchProviderButton
android:id="#+id/csp_spb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/csp_tv_title"
rs:providerName="BOOKP" />
</RelativeLayout>
Here is the custom view's XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rs="http://schemas.android.com/apk/res/com.bookcessed.booksearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/pv_rl_strings"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/pv_tv_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_toRightOf="#+id/pv_tv_and"
android:textSize="18sp"
android:textColor="#ff11ab37"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/pv_tv_and"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and "
android:textSize="18sp"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#ff000000"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/pv_tv_browse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Browse"
android:textSize="18sp"
android:textColor="#ff0077bb"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/pv_tv_and" />
<ImageView
android:id="#+id/pv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pv_tv_search"
android:layout_centerInParent="true"
android:layout_alignTop="#+id/pv_tv_and"
android:src="#drawable/bookp_logo" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/pv_rl_genres"
android:layout_width="wrap_content"
android:layout_below="#+id/pv_rl_strings"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
<ImageView
android:id="#+id/pv_genre1"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<ImageView
android:id="#+id/pv_genre2"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre1" />
<ImageView
android:id="#+id/pv_genre3"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre2" />
<ImageView
android:id="#+id/pv_genre4"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre3" />
<ImageView
android:id="#+id/pv_genre5"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre4" />
<ImageView
android:id="#+id/pv_genre6"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre5" />
<ImageView
android:id="#+id/pv_genre7"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre6" />
<ImageView
android:id="#+id/pv_genre8"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre7" />
<ImageView
android:id="#+id/pv_genre9"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/pv_genre8" />
</RelativeLayout>
</RelativeLayout>
Here is the class SearchProviderButton:
public class SearchProviderButton extends LinearLayout {
private Connector connector;
private Context context;
private View inflatedView;
public SearchProviderButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.SearchProviderButton);
connector = SearchProvider.valueOf(a.getString(R.styleable.SearchProviderButton_providerName)).getConnector();
this.context = context;
setFocusable(true);
setBackgroundColor(Color.WHITE);
setVisibility(VISIBLE);
//setOnClickListener(listenerAdapter);
setClickable(true);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
LayoutInflater li = LayoutInflater.from(context);
inflatedView = li.inflate(R.layout.provider_view, null);
ImageView logo = (ImageView)inflatedView.findViewById(R.id.pv_logo);
logo.setImageResource(connector.getLogoDrawableID());
TextView searchTV = (TextView)inflatedView.findViewById(R.id.pv_tv_search);
TextView andTV = (TextView)inflatedView.findViewById(R.id.pv_tv_and);
if(!connector.isSearchSupported()){
andTV.setText("");
searchTV.setVisibility(GONE);
}
setgenreIcons();
}
public Connector getConnector(){
return connector;
}
public void setConnector(Connector connector){
this.connector = connector;
}
private void setgenreIcons(){
int[] genreIconDrawables = {R.id.pv_genre1,R.id.pv_genre2, R.id.pv_genre3,
R.id.pv_genre4, R.id.pv_genre5, R.id.pv_genre6, R.id.pv_genre7,
R.id.pv_genre8, R.id.pv_genre9};
ArrayList<Genre> availgenre = connector.getAvailablegenres();
availgenre.remove(Genre.ALL);
int counter = 0;
for(int genreIVid : genreIconDrawables){
ImageView curgenreImageView = (ImageView)inflatedView.findViewById(genreIVid);
if(counter < availgenre.size() - 1){
curgenreImageView.setImageResource(availgenre.get(counter).getDrawable());
} else {
curgenreImageView.setVisibility(GONE);
}
counter++;
}
}
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
if (gainFocus == true){
this.setBackgroundColor(Color.rgb(255, 165, 0));
} else {
this.setBackgroundColor(Color.WHITE);
}
}
}
Here is the code for the class loading the xml that contains my custom component:
bookPServiceProviderButton = (SearchProviderButton)findViewById(R.id.csp_spb_1);
bookPServiceProviderButton.setOnClickListener(SPBOnClickListener);
bookPServiceProviderButton.setConnector(new bookPConnector());
bookPServiceProviderButton.setVisibility(View.VISIBLE);
EDIT: After the first comment, I added this code:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpec = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int heightSpec = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
}
Now it has a width and a height, but nothing is showing up inside of it!
Your custom layout view is not appearing because you're not putting anything into it. In your onFinishInflate you have the line inflatedView = li.inflate(R.layout.provider_view, null); But you don't add that to your view. You have two options to add the views to your custom layout view.
Change your custom view to extend RelativeLayout, change the enclosing RelativeLayout to <merge> in your provider_view.xml, and fix your findViewId lines in to this.findViewId(...) since the views will be inflated into your layout.
In your layout xml do:
<com.bookcessed.booksearch.SearchProviderButton
android:id="#+id/csp_spb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/csp_tv_title"
rs:providerName="BOOKP">
<!-- include this so it's added to your custom layout view -->
<include layout="#layout/provider_view" />
</com.bookcessed.booksearch.SearchProviderButton>
provider_view becomes:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rs="http://schemas.android.com/apk/res/com.bookcessed.booksearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/pv_rl_strings"
.
.
.
</merge>
SearchProviderButton:
public class SearchProviderButton extends RelativeLayout{
.
.
.
#Override
protected void onFinishInflate() {
super.onFinishInflate();
//the <include> in the layout file has added these views to this
//so search this for the views
ImageView logo = this.findViewById(R.id.pv_logo);
logo.setImageResource(connector.getLogoDrawableID());
TextView searchTV = (TextView)this.findViewById(R.id.pv_tv_search);
TextView andTV = (TextView)this.findViewById(R.id.pv_tv_and);
if(!connector.isSearchSupported()){
andTV.setText("");
searchTV.setVisibility(GONE);
}
setgenreIcons();
}
.
.
.
Or you could properly inflate the view in onCreate by layoutInflater.inflate(R.layout.provider_view, this, true). That call will inflate the referenced layout into the passed ViewGroup, in this case your custom view, and add the inflated Views to it. Then you can fix up the findViewId calls in your onFinishInflate.
TC... Sometimes it wont show in the preview unless you run it.
For wrap_content to work, you need to properly implement the measure and layout functions for your custom View. See How Android Draws Views for details on what these methods mean.
My guess would be that getMeasureWidth() and getMeasureHeight() functions for your view are always returning 0.

Categories

Resources