Design lib - CoordinatorLayout/CollapsingToolbarLayout with GridView/listView - java

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 :)

Related

RecyclerView on Bottom sheet can not scroll?

I want to recyclerView on Bottom sheet can scroll but i cant . I try use OntouchListenner in RecyclerView but which can not scroll. Pls help me....
Main layout
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efefef"
tools:context=".MainActivity">
<include layout="#layout/conten_main" />
<include layout="#layout/layout_bottom_sheet" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#drawable/air" />
</androidx.constraintlayout.widget.ConstraintLayout>
I want to know how to Scroll in RecyclerView
Try this custom view
package com.avatus.util
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.LinearLayout
class DisallowInterceptView : LinearLayout {
constructor(context: Context?) : super(context) {
requestDisallowInterceptTouchEvent(true)
}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
requestDisallowInterceptTouchEvent(true)
}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
requestDisallowInterceptTouchEvent(true)
}
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
parent.requestDisallowInterceptTouchEvent(true)
return super.dispatchTouchEvent(ev)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_MOVE -> requestDisallowInterceptTouchEvent(true)
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> requestDisallowInterceptTouchEvent(
false
)
}
return super.onTouchEvent(event)
}
}
<com.avatus.util.DisallowInterceptView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:padding="#dimen/_16sdp"
android:background="#17141A"
android:layout_height="match_parent">
// Your view here
</androidx.constraintlayout.widget.ConstraintLayout>
</com.avatus.util.DisallowInterceptView>
I had the same requirement and solved it like below
Make BottomSheetDialog object attribute set cancelable false like below
viewIndentItemBottomSheetDialog.setCancelable(false)

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!

TextInputEditText don't work on Android 5.1

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

Giant white space in between Toolbar and ViewPager while using the Android Design Support Library

I started using the Android Design Support Library in my app. I started using the CoordinatorLayout with an AppBarLayout and Toolbar with a ViewPager to create the animation where the Toolbar leaves the screen on scroll.
So far this works fine. The part that's the problem is whenever the orientation is changed to landscape from portrait, and then back to portrait from landscape. The result of this is a large white space in between the Toolbar and the ViewPager. Does anyone know how to fix this? Thanks! The code is below and I would've added screenshots of the problem, but apparently I can't do so until I reach level 10 or something.
Here's the layout file (activity_main.xml):
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!--Content Container-->
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--App Bar Container-->
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--ActionBar-->
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
<!--Prevents a bug in the App Bar Layout-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="1px"/>
<!--Tabs-->
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:tabMode="scrollable"
app:tabGravity="fill"
app:tabIndicatorHeight="#dimen/toolbar_tab_indicator_height"
app:tabTextColor="#color/colorPrimaryLight"
app:tabSelectedTextColor="#color/textColor"/>
</android.support.design.widget.AppBarLayout>
<!--Body Content-->
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
<!--Navigation Drawer-->
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer_items"/>
</android.support.v4.widget.DrawerLayout>
I believe the content holder, your ViewPager, belongs inside the CoordinatorLayout. The only thing that I see outside CoordinatorLayout is the nav view or drawer.
While messing around with the layout behavior, I figured out the problem. So I created a custom layout behavior class to handle the height inconstancy:
import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import java.util.List;
public class CustomBehavior extends AppBarLayout.ScrollingViewBehavior
{
public CustomBehavior()
{
super();
}
public CustomBehavior(Context context, AttributeSet attrs)
{
super(context, attrs);
}
#Override
public boolean onMeasureChild(CoordinatorLayout parent, View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)
{
if (child.getLayoutParams().height == -1)
{
List dependencies = parent.getDependencies(child);
if (dependencies.isEmpty())
{
return false;
}
AppBarLayout appBar = getOriginalLayout(dependencies);
if (appBar != null && ViewCompat.isLaidOut(appBar))
{
if (ViewCompat.getFitsSystemWindows(appBar))
{
ViewCompat.setFitsSystemWindows(child, true);
}
int scrollRange = appBar.getTotalScrollRange();
int parentHeight = View.MeasureSpec.getSize(parentHeightMeasureSpec);
int height = parentHeight - appBar.getMeasuredHeight() + scrollRange;
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);
parent.onMeasureChild(child, parentWidthMeasureSpec, widthUsed, heightMeasureSpec, heightUsed);
return true;
}
}
return false;
}
private static AppBarLayout getOriginalLayout(List<View> views)
{
int i = 0;
for (int z = views.size(); i < z; ++i)
{
View view = (View)views.get(i);
if (view instanceof AppBarLayout)
{
return (AppBarLayout)view;
}
}
return null;
}
}

Unusual Android UI Issue: android:scaleType="fitXY and match_parent will not increase the size of an ImageView -

I have a thumbnail which is acting VERY strangely. I've set it's paramaters to match_parent as well as android:scaleType="fitXY" and neither seems to have any effect on the thumbnail (the desired behavior is filling to the edge of the screen).
XML: list_item_user_video.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.project.ui.widget.UrlImageView
android:id="#+id/userVideoThumbImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="false"
android:clickable="false"
android:contentDescription="YouTube video thumbnail"
android:focusable="false"
android:scaleType="fitXY"
android:focusableInTouchMode="false"
android:gravity="center"
android:src="#drawable/ic_launcher" />
Home.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/content_frame"
android:focusable="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/darkgrey"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AAFFFFFF" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.idg.omv.ui.widget.VideosListView
android:id="#+id/videosListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentBottom="true" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#color/darkgrey"
android:scaleType="centerCrop"
android:src="#drawable/home_up_btn" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_alignParentBottom="true" >
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageButton
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:focusable="false"
android:src="#drawable/scroll_lt_arrow" />
<ImageButton
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:focusable="false"
android:src="#drawable/scroll_rt_arrow" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
JAVA:
public class UrlImageView extends LinearLayout {
private Context mContext;
private Drawable mDrawable;
private ProgressBar mSpinner;
private ImageView mImage;
public UrlImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public UrlImageView(Context context) {
super(context);
init(context);
}
/**
* First time loading of the LoaderImageView
* Sets up the LayoutParams of the view, you can change these to
* get the required effects you want
*/
private void init(final Context context) {
mContext = context;
mImage = new ImageView(mContext);
mImage.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mImage.setVisibility(View.GONE);
mImage.setScaleType(ScaleType.FIT_XY);
mSpinner = new ProgressBar(mContext);
mSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mSpinner.setIndeterminate(true);
addView(mSpinner);
addView(mImage);
}
/**
* Set's the view's drawable, this uses the internet to retrieve the image
* don't forget to add the correct permissions to your manifest
*
* #param imageUrl the url of the image you wish to load
*/
public void setImageDrawable(final String imageUrl) {
mDrawable = null;
mSpinner.setVisibility(View.VISIBLE);
mImage.setVisibility(View.GONE);
mImage.setScaleType(ScaleType.FIT_XY);
new Thread() {
public void run() {
try {
mDrawable = getDrawableFromUrl(imageUrl);
imageLoadedHandler.sendEmptyMessage(RESULT_OK);
} catch (MalformedURLException e) {
imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
} catch (IOException e) {
imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
}
};
}.start();
}
/**
* Callback that is received once the image has been downloaded
*/
private final Handler imageLoadedHandler = new Handler(new Callback() {
#Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case RESULT_OK:
mImage.setImageDrawable(mDrawable);
mImage.setVisibility(View.VISIBLE);
mImage.setScaleType(ScaleType.FIT_XY);
mSpinner.setVisibility(View.GONE);
break;
case RESULT_CANCELED:
default:
// Could change image here to a 'failed' image
// otherwise will just keep on spinning
break;
}
return true;
}
});
/**
* Pass in an image url to get a drawable object
*
* #return a drawable object
* #throws IOException
* #throws MalformedURLException
*/
private static Drawable getDrawableFromUrl(final String url) throws IOException, MalformedURLException {
return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(url).getContent()), "name");
}
}
Have you tried using WRAP_CONTENT instead of MATCH_PARENT? And why would you stretch the images all the way to the edge? That's going to look awful.

Categories

Resources