I'm trying to set 'Sub-title' of a CollapsingToolbarLayout in my app using this example here.
Here's the code from onCreate() of Profile.java:
CollapsingToolbarLayout collapsingToolbarLayout;
Toolbar toolbar;
HeaderView toolbarHeaderView;
HeaderView floatHeaderView;
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapse_toolbar);
// error on the line below
toolbarHeaderView = (HeaderView) findViewById(R.id.toolbar_header_view);
floatHeaderView = (HeaderView) findViewById(R.id.float_header_view);
toolbarHeaderView.bindTo("title", "subtitle");
floatHeaderView.bindTo("title", "subtitle");
Here's activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/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:fitsSystemWindows="true"
tools:context="com.abc.zzz.Profile">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="256dp"
android:theme="#style/AppTheme.AppBarOverlay"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapse_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#color/colorPrimary"
android:fitsSystemWindows="true"
app:popupTheme="#style/AppTheme.PopupOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="parallax">
<include
android:id="#+id/toolbar_header_view"
layout="#layout/header_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginRight="#dimen/header_view_end_margin_right"
android:layout_marginEnd="#dimen/header_view_end_margin_right"
android:visibility="gone"
/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include
android:id="#+id/float_header_view"
layout="#layout/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.abc.zzz.ViewBehavior"/>
</android.support.design.widget.CoordinatorLayout>
Here's header_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Title -->
<TextView
android:id="#+id/header_view_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="18sp"
/>
<!-- Subtitle -->
<TextView
android:id="#+id/header_view_sub_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="16sp"
/>
</LinearLayout>
Here's HeaderView.java:
public class HeaderView extends LinearLayout {
TextView title;
TextView subTitle;
public HeaderView(Context context) {
super(context);
}
public HeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public HeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
title = (TextView) findViewById(R.id.header_view_title);
subTitle = (TextView) findViewById(R.id.header_view_sub_title);
}
public void bindTo(String title) {
bindTo(title, "");
}
public void bindTo(String title, String subTitle) {
hideOrSetText(this.title, title);
hideOrSetText(this.subTitle, subTitle);
}
private void hideOrSetText(TextView tv, String text) {
if (text == null || text.equals(""))
tv.setVisibility(GONE);
else
tv.setText(text);
}
}
Here's ViewBehavior.java:
public class ViewBehavior extends CoordinatorLayout.Behavior<HeaderView> {
private Context mContext;
private int mStartMarginLeft;
private int mEndMargintLeft;
private int mMarginRight;
private int mStartMarginBottom;
private boolean isHide;
public ViewBehavior(Context context, AttributeSet attrs) {
mContext = context;
}
#Override
public boolean layoutDependsOn(CoordinatorLayout parent, HeaderView child, View dependency) {
return dependency instanceof AppBarLayout;
}
#Override
public boolean onDependentViewChanged(CoordinatorLayout parent, HeaderView child, View dependency) {
shouldInitProperties(child, dependency);
int maxScroll = ((AppBarLayout) dependency).getTotalScrollRange();
float percentage = Math.abs(dependency.getY()) / (float) maxScroll;
float childPosition = dependency.getHeight()
+ dependency.getY()
- child.getHeight()
- (getToolbarHeight() - child.getHeight()) * percentage / 2;
childPosition = childPosition - mStartMarginBottom * (1f - percentage);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
lp.leftMargin = (int) (percentage * mEndMargintLeft) + mStartMarginLeft;
lp.rightMargin = mMarginRight;
child.setLayoutParams(lp);
child.setY(childPosition);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
if (isHide && percentage < 1) {
child.setVisibility(View.VISIBLE);
isHide = false;
} else if (!isHide && percentage == 1) {
child.setVisibility(View.GONE);
isHide = true;
}
}
return true;
}
private void shouldInitProperties(HeaderView child, View dependency) {
if (mStartMarginLeft == 0)
mStartMarginLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_start_margin_left);
if (mEndMargintLeft == 0)
mEndMargintLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_end_margin_left);
if (mStartMarginBottom == 0)
mStartMarginBottom = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_start_margin_bottom);
if (mMarginRight == 0)
mMarginRight = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_end_margin_right);
}
public int getToolbarHeight() {
int result = 0;
TypedValue tv = new TypedValue();
if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
result = TypedValue.complexToDimensionPixelSize(tv.data, mContext.getResources().getDisplayMetrics());
}
return result;
}
}
The problem is that I'm getting this error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.zzz/com.abc.zzz.Profile}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to com.abc.zzz.HeaderView on the line specified above.
Why am I getting this error and how to resolve it?
Please let me know.
You didn't show on your code but I bet that your header_view.xml have a LinearLayout as the root view.
So basically what happens is: the <include code "gets replaced" by the LinearLayout at the root of header_view.xml and then you call findViewById(R.id.toolbar_header_view) which returns that LinearLayout and then with the (HeaderView) you're telling the VM this is a HeaderView, but it's, it's a LinearLayout. So it crashes!
The best option without seeing piece of code you didn't show it is one of the following:
put <HeaderView> at the root of header_view.xml,
or if that is not possible because there's more stuff inside header_view.xml
change your code to find the include and then inside the include, to find the actual HeaderView.
Something like:
toolbarHeaderView = (HeaderView) findViewById(R.id.toolbar_header_view).findViewById(R.id.header_view_id);
floatHeaderView = (HeaderView) findViewById(R.id.float_header_view).findViewById(R.id.header_view_id);
note that it calls findViewById two times. One for the include and another for the HeaderView inside it
Related
I have surveyed in other sites and got different responses about how I can solve my problem. In fact the problem is that I am trying to make a responsive gridView layout to display 25 textviews with numbers. I had hard time in constructing it the way I want but here is what I got:
<LinearLayout 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:orientation="vertical"
android:weightSum="10"
android:layout_gravity="center"
android:gravity="center"
tools:context="com.example.hristodraganov.bingo.MainActivity"
tools:showIn="#layout/activity_main">
<RelativeLayout
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
</RelativeLayout>
<GridView
android:paddingTop="8dp"
android:id="#+id/gridview"
android:background="#000"
android:layout_width="match_parent"
android:layout_weight="6"
android:layout_height="0dp"
android:gravity="center"
android:columnWidth="70dp"
android:numColumns="5"
android:verticalSpacing="10dp"
android:horizontalSpacing="1dp"
android:stretchMode="spacingWidth"
/>
This is the layout that is used on the MainActivity. I inflate it with textview item with this code:
<?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:background="#d3d3d3"
android:gravity="center">
<com.example.****.****.SquareView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cell"
android:textColor="#D0583B"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp">
</com.example.****.****.SquareView>
There is the extension of the BaseAdapter class that is used to inflate the grid with the textviews:
public class GridAdapter extends BaseAdapter {
private final Context mContext;
private String[] numbers;
public GridAdapter(Context context, String[] numbers) {
this.mContext = context;
this.numbers = numbers;
}
#Override
public int getCount() {
return numbers.length;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(mContext);
gridView = inflater.inflate(R.layout.textview_layout, null);
TextView textView = (TextView) gridView.findViewById(R.id.cell);
textView.setText(numbers[position]);
} else {
gridView = (View) convertView;
}
return gridView;
}
}
Also, I am forcing the textView to be squared in this SquareView class:
public class SquareView extends android.support.v7.widget.AppCompatTextView {
public SquareView(Context context) {
super(context);
}
public SquareView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}}
The layout is doing fine by now, here is an image of the size I used to construct it Nexus 5X. It looks good but when I change to 4" -Nexus S the whole last row is somewhere below the screen. Also this happens to 5"-Nexus 5 where the last row is slightly visible. Above 5.2" to 6.0" the layout fits perfectly. So my question is what should I do in this scenario to make the layout fit for small-sized screens without making a duplicate layout for them. (Note: I was told that the BaseAdapter implementation would fix the responsiveness of the layout). Any ideas what could I do? (Sorry for the long texts.)
Because there are a lot of different Android devices out there your best choice would be to encapsulate your layout in a ScrollView.
Another option is to get rid of the weights and let the top RelativeLayout grow/shrink after the GridView has taken all the space it needs. This still might not work on smaller screens or in landscape.
```
<RelativeLayout
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</RelativeLayout>
<GridView
android:paddingTop="8dp"
android:id="#+id/gridview"
android:background="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:columnWidth="70dp"
android:numColumns="5"
android:verticalSpacing="10dp"
android:horizontalSpacing="1dp"
android:stretchMode="spacingWidth"/>
```
I'm trying to figure out why an Android custom view which works when no background is set suddenly stops working when the background is set. It seems the background covers the items added to the view when it is set. I've simplified the view code to the bare minimum which reproduces the problem and to be able to post the code here. The custom view inherits from RelativeLayout and the code is as follow:
public class TestView extends RelativeLayout {
private LayoutInflater mInflater;
private ViewTreeObserver mViewTreeObserber;
private boolean mInitialized = false;
public TestView(Context ctx) {
super(ctx, null);
initialize(ctx, null, 0);
}
public TestView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
initialize(ctx, attrs, 0);
}
public TestView(Context ctx, AttributeSet attrs, int defStyle) {
super(ctx, attrs, defStyle);
initialize(ctx, attrs, defStyle);
}
private void initialize(Context ctx, AttributeSet attrs, int defStyle) {
mInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewTreeObserber = getViewTreeObserver();
mViewTreeObserber.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (!mInitialized) {
mInitialized = true;
drawItem();
}
}
});
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawItem();
}
private void drawItem() {
if (!mInitialized) return;
removeAllViews();
View item = mInflater.inflate(R.layout.testview_item, null);
TextView txt = (TextView)item.findViewById(R.id.test_view_item);
txt.setText("Test View Text");
txt.setTextColor(Color.BLACK);
txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
addView(item);
}
}
The item layout is simple:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/test_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:ellipsize="end"
android:maxLines="1" />
</LinearLayout>
And the sample app simply declares two instances of the custom view in XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<TextView android:text="With Background set"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.machado.felipe.TestView
android:background="#android:color/holo_blue_light"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.machado.felipe.TestView>
<TextView android:text="WithOUT Background set"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.machado.felipe.TestView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.machado.felipe.TestView>
</LinearLayout>
The result looks like the picture bellow:
This is driving me crazy! I'm not used to write custom views in android and this is someone else's code which I'm trying to fix! I don't even know if this is the way it should be done, since I'm inflating views and adding them to the RelativeLayout I don't think I should be adding them in the onDraw, but since the complete code is doing more complex stuff, as laying out the items in multiple rows with wrapping, it is possibly a valid approach... But, anyway, I can't figure out how to fix this!
Past few days I've been working on fixed footer for my recyclerview but so far I didn't get the result that I expected. I've already taken a look existing questions but got same result as before.It seems I 'm still missing a point but I can't figure out... Therefore I'm waiting for your suggestions. Thanks!
Here is the current view of layout, I want that FOOTER part to be sticky at bottom of recyclerview.
DrawerAdapter.java
public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.DrawerViewHolder> {
// Class for view types.
private class VIEW_TYPES {
public static final int Header = 0;
public static final int Normal = 1;
public static final int Footer = 2;
}
private ArrayList<DrawerItem> drawerMenuList;
private String userProfileName;
private String userProfileMail;
private Drawable userProfilePic;
private OnItemSelecteListener mListener;
public DrawerAdapter(ArrayList<DrawerItem> drawerMenuList, String name, String mail, Drawable profileImage) {
this.drawerMenuList = drawerMenuList;
this.userProfileMail = mail;
this.userProfileName = name;
this.userProfilePic = profileImage;
}
#Override
public DrawerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == VIEW_TYPES.Header) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
} else if (viewType == VIEW_TYPES.Footer) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer, parent, false);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
}
return new DrawerViewHolder(view, viewType);
}
#Override
public void onBindViewHolder(DrawerViewHolder holder, int position) {
// If item's position 0 it's a header.
if (position == 0) {
holder.headerName.setText(userProfileName);
holder.headerMail.setText(userProfileMail);
holder.headerProfile.setImageDrawable(userProfilePic);
} else if (position == 5) { // If position is 5 then it's footer.
holder.headerName.setText("FOOTER");
} else { // it's a menu item.
holder.title.setText(drawerMenuList.get(position - 1).getTitle());
holder.icon.setImageResource(drawerMenuList.get(position - 1).getIcon());
}
}
#Override
public int getItemCount() {
return drawerMenuList.size() + 1;
}
#Override
public int getItemViewType(int position) {
if (position == 0) {
return VIEW_TYPES.Header;
} else if (position == 5) {
return VIEW_TYPES.Footer;
} else {
return VIEW_TYPES.Normal;
}
}
class DrawerViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView headerName;
ImageView icon;
TextView headerMail;
ImageView headerProfile;
public DrawerViewHolder(View itemView, int viewType) {
super(itemView);
if (viewType == VIEW_TYPES.Header) {
headerName = (TextView) itemView.findViewById(R.id.header_name);
headerMail = (TextView) itemView.findViewById(R.id.header_email);
headerProfile = (ImageView) itemView.findViewById(R.id.circleView);
} else if (viewType == VIEW_TYPES.Footer) {
headerName = (TextView) itemView.findViewById(R.id.textView3);
} else {
title = (TextView) itemView.findViewById(R.id.title);
icon = (ImageView) itemView.findViewById(R.id.icon);
}
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mListener.onItemSelected(view, getAdapterPosition());
}
});
}
}
// To set click listener.
public interface OnItemSelecteListener {
void onItemSelected(View v, int position);
}
public void setOnItemClickLister(OnItemSelecteListener mListener) {
this.mListener = mListener;
}
}
MainActivity.java
for (int i = 0; i < navMenuTitles.length; i++) {
Log.e("I", String.valueOf(i));
mDrawerItemList.add(new DrawerItem(navMenuTitles[i], navMenuIcons.getResourceId(i, -1)));
}
mDrawerItemList.add(new DrawerItem());
mRecyclerView = (RecyclerView) findViewById(R.id.drawerRecyclerView);
mDrawerAdapter = new DrawerAdapter(mDrawerItemList, logged_user.name, logged_user.email, draw_userImage);
mLinearLayoutManager = new CustomGridLayoutManager(HomeScreenActivity.this){
#Override
public boolean canScrollVertically() {
return false;
}
};
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAdapter(mDrawerAdapter);
mDrawerLayout = (DrawerLayout) findViewById(R.id.DrawerLayout);
mDrawerToggle = new ActionBarDrawerToggle(HomeScreenActivity.this, mDrawerLayout, mToolbar, R.string.openDrawer, R.string.closeDrawer) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// code here will execute once the drawer is opened( As I dont want anything happened whe drawer is
// open I am not going to put anything here)
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
// Code here will execute once drawer is closed
}
}; // Drawer Toggle Object Made
mDrawerLayout.addDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggle
mDrawerToggle.syncState(); // Finally we set the drawer toggle sync State
mDrawerAdapter.setOnItemClickLister(new DrawerAdapter.OnItemSelecteListener() {
#Override
public void onItemSelected(View v, int position) {
Log.e("CLICK", "You clicked at position: " + position);
}
});
drawer_layout.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:elevation="7dp">
<FrameLayout
android:id="#+id/containerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_gravity="left"
android:background="#ffffff"
app:reverseLayout="false"
app:stackFromEnd="false">
</android.support.v7.widget.RecyclerView>
footer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:background="#ffffff"
android:layout_alignBottom="#id/drawerRecyclerView"
android:foregroundGravity="bottom">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="true"
android:background="#f16666"
android:foregroundGravity="bottom"
android:gravity="center_horizontal|bottom"
android:paddingTop="#dimen/_15sdp"
android:paddingBottom="#dimen/_15sdp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="TEST"
android:id="#+id/textView3"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal|bottom" />
</LinearLayout>
</RelativeLayout>
In your drawer_layout.xml put RecyclerView inside a RelativeLayout and set alignParentTop to true. Then remove your footer view from your RecyclerView and put that inside the RelativeLayout below RecyclerView like this:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:elevation="7dp">
<FrameLayout
android:id="#+id/containerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_gravity="left"
android:background="#ffffff"
app:reverseLayout="false"
app:stackFromEnd="false" />
<include layout="#layout/footer" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
You could also take a look at NavigationView so you don't have to customize your drawer list. Here is a tutorial on how to use this. It is pretty easy to use and requires less code. You could then take a look at this answer on how to put a footer in NavigationView.
I am using LinearLayout with weight-ing (created multiple dimens files with different values). For me it works as excepted:
<?xml version="1.0" encoding="utf-8"?>
<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:background="#color/white"
android:orientation="vertical"
<include layout="#layout/header" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
tools:layout_height="0dp"
tools:listitem="#layout/row" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="#dimen/footer_weight"
android:padding="#dimen/extra_padding"
android:paddingEnd="#dimen/small_padding"
android:paddingLeft="#dimen/small_padding"
android:paddingRight="#dimen/small_padding"
android:paddingStart="#dimen/small_padding"
android:text="#string/contact"
android:textColor="#color/grey" />
</LinearLayout>
I am Using a list view inside a listview and both listview share same list item.
It is working fine but if height of the item increases in inside list view it doesn't wrap the content and put a scrollview which is not as per requirement.
Please see the codes below
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<ListView
android:id="#+id/list_comments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:divider="#android:color/transparent"
android:dividerHeight="5dp" />
</RelativeLayout>
comments_for_topic.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="#fafafa">
<RelativeLayout
android:id="#+id/commentsRelLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:paddingLeft="7dp"
android:background="#d5d5d5"
android:orientation="vertical">
<TextView
android:id="#+id/UserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="User name"/>
<TextView
android:id="#+id/Comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/UserName"
android:textSize="12sp"
android:padding="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/showRep"
android:text="Comment test 1"/>
<TextView
android:id="#+id/showRep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2Replies"
android:layout_toLeftOf="#+id/Reply"
android:layout_marginRight="3dp"
android:textSize="12dp"
android:textColor="#android:color/holo_blue_dark"
android:layout_below="#+id/UserName"/>
<TextView
android:id="#+id/Reply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reply"
android:textSize="12dp"
android:textColor="#android:color/holo_blue_dark"
android:layout_below="#+id/UserName"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<ListView
android:id="#+id/list_commentsLevel1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/commentsRelLayout"
android:layout_alignLeft="#+id/commentsRelLayout"
android:layout_marginLeft="34dp"
android:background="#d5d5d5"
android:layout_marginTop="2dp"
android:scrollbars="none"
android:divider="#android:color/white"
android:dividerHeight="1dp"
></ListView>
<TextView
android:id="#+id/showmorebtn"
android:layout_alignParentBottom="true"
android:layout_marginBottom="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/show_more"
android:layout_below="#+id/list_commentsLevel1"
android:layout_alignLeft="#+id/list_commentsLevel1"
android:textColor="#android:color/holo_blue_dark"
/>
</RelativeLayout>
AdapterComments.java
public class AdapterComments extends BaseAdapter {
private Activity activity;
private ArrayList<CommentListModel> itemsArrayList = new ArrayList<CommentListModel>();
private ArrayList<CommentListModel> Comments = new ArrayList<CommentListModel>();
CommentListModel tempValues=null;
private static LayoutInflater inflater=null;
ArrayList<AdapterComments> adapterCommentsArrayList = new ArrayList<AdapterComments>();
ViewHolder holder = new ViewHolder();
ViewHolder viewHolder;
ArrayList<String> list =new ArrayList<String>();
private int currentID=0;
boolean isChild =false;
FragmentDiscussinTopic fragmentDiscussinTopic;
public AdapterComments() {
}
public AdapterComments(Activity context, FragmentDiscussinTopic fragmentDiscussinTopic, ArrayList<CommentListModel> itemsArrayList) {
this.activity = context;
this.itemsArrayList = itemsArrayList;
this.isChild = false;
this.fragmentDiscussinTopic = fragmentDiscussinTopic;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0 ; i<itemsArrayList.size();i++){
AdapterComments adapterComments1 = new AdapterComments();
adapterCommentsArrayList.add(adapterComments1);
}
}
public AdapterComments(Activity context, ArrayList<CommentListModel> itemsArrayList) {
this.activity = context;
this.itemsArrayList = itemsArrayList;
this.isChild = true;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0 ; i<itemsArrayList.size();i++){
AdapterComments adapterComments1 = new AdapterComments();
adapterCommentsArrayList.add(adapterComments1);
}
}
#Override
public int getCount() {
if(itemsArrayList.size()<=0)
return 1;
return itemsArrayList.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public Object getItem(int position) {
return position;
}
public static class ViewHolder{
public TextView UserName;
public TextView Comment;
public TextView showRep;
public TextView Reply;
public TextView showmorebtn;
public ListView list_commentsLevel1;
}
#Override
public View getView(final int position, View rowView, ViewGroup parent) {
View v1 = rowView;
if(rowView==null){
v1 = inflater.inflate(R.layout.comments_for_topic,null);
viewHolder =new ViewHolder();
viewHolder.UserName=(TextView)v1.findViewById(R.id.UserName);
viewHolder.Comment=(TextView)v1.findViewById(R.id.Comment);
viewHolder.showRep=(TextView)v1.findViewById(R.id.showRep);
viewHolder.showmorebtn=(TextView)v1.findViewById(R.id.showmorebtn);
viewHolder.Reply=(TextView)v1.findViewById(R.id.Reply);
viewHolder.list_commentsLevel1=(ListView)v1.findViewById(R.id.list_commentsLevel1);
v1.setTag( viewHolder );
}
else
viewHolder=(ViewHolder)v1.getTag();
if(itemsArrayList.size()<=0)
{
Log.e("data", " no data found");
}
else {
tempValues = itemsArrayList.get(position);
if (tempValues.isVisible()){
viewHolder.list_commentsLevel1.setAdapter(adapterCommentsArrayList.get(position));
viewHolder.list_commentsLevel1.setVisibility(View.VISIBLE);
ObjectAnimator anim = ObjectAnimator.ofFloat(viewHolder.list_commentsLevel1, "alpha", 0f, 1f);
anim.setDuration(1000);
anim.start();
setListViewHeightBasedOnChildren(viewHolder.list_commentsLevel1);
// scrollMyListViewToBottom();
}else {
Animation animation = new TranslateAnimation(0,0,0,1000);
animation.setDuration(1000);
viewHolder.list_commentsLevel1.startAnimation(animation);
viewHolder.list_commentsLevel1.setVisibility(View.GONE);
}
viewHolder.UserName.setText(tempValues.getUserName());
viewHolder.Comment.setText(tempValues.getComment());
int subcmntcount = tempValues.getSubCommentCount();
if (subcmntcount>=1){
viewHolder.showRep.setVisibility(View.VISIBLE);
if (subcmntcount==1){
String repText=new String("1Reply");
SpannableString content = new SpannableString(repText);
content.setSpan(new UnderlineSpan(), 0, repText.length(), 0);
viewHolder.showRep.setText(content);
}else {
String repText=new String(subcmntcount+"Replies");
SpannableString content = new SpannableString(repText);
content.setSpan(new UnderlineSpan(), 0, repText.length(), 0);
viewHolder.showRep.setText(content);
}
viewHolder.showmorebtn.setVisibility(View.GONE);
}else {
viewHolder.showRep.setVisibility(View.GONE);
viewHolder.list_commentsLevel1.setVisibility(View.GONE);
viewHolder.showmorebtn.setVisibility(View.GONE);
}
if (tempValues.isChild()){
viewHolder.Reply.setVisibility(View.GONE);
viewHolder.showRep.setVisibility(View.GONE);
}else {
viewHolder.Reply.setVisibility(View.VISIBLE);
String repText=new String("Reply");
SpannableString content = new SpannableString(repText);
content.setSpan(new UnderlineSpan(), 0, repText.length(), 0);
viewHolder.Reply.setText(content);
}
v1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0;i<itemsArrayList.size();i++){
CommentListModel commentListModel = ( CommentListModel ) itemsArrayList.get( i );
if (i==position){
isChild =true;
if (commentListModel.getSubCommentCount()>0){
commentListModel.setVisible(true);
itemsArrayList.remove(i);
itemsArrayList.add(i,commentListModel);
new getDiscussionForumComments(commentListModel.getDiscussionCommentID(),position).execute();
}
}else {
commentListModel.setVisible(false);
itemsArrayList.remove(i);
itemsArrayList.add(i,commentListModel);
}
}
}
});
viewHolder.Reply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Configuration.currentDiscussID = itemsArrayList.get(position).getDiscussionCommentID();
fragmentDiscussinTopic.requestEditPop();
}
});
}
return v1;
}
public static void setListViewHeightBasedOnChildren(ListView listView)
{
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight=0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++)
{
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
ViewGroup.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
please see the highlighted area of image listview height is not proper to wrap the content
any help would be appericiated
The problem is you can't get the scroll of both listview at a time. if you what you both at the same time. you need to create a custom list view try something like this instead of your list view (inside list).
public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
Their are some points you need to fix.
Give a proper height to your ListView so that it can scroll within that boundry, wrap_content is never recommended for ListView. So better to put some height to your ListView.
Listview inside a ListView will not give the user experience good. Try a work around or better to Use RecyclerView. RecyclerView is better in performance in case of nested list.
Hope it will help :)
Compound view xml code:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dayRelativeLayout"
android:layout_width="30dip"
android:layout_height="45dip"
android:background="#drawable/selectable_day_layout_border">
<TextView android:id="#+id/eventTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:textSize="12dip"
android:visibility="invisible"
android:layout_margin="2.1dip"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="#+id/dayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_below="#+id/eventTextView"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</RelativeLayout>
Below is the java code for the above compound control:-
public class CalendarGridView extends RelativeLayout
{
private Context context = null;
private TextView eventTextView = null;
private TextView dayTextView = null;
public CalendarGridView(Context context)
{
super(context);
this.context = context;
init();
}
public CalendarGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
this.context = context;
init();
}
public CalendarGridView(Context context, AttributeSet attrs,int defStyle)
{
super(context, attrs, defStyle);
this.context = context;
init();
}
private void init()
{
int id = (int)Math.abs(Math.random()*100000);
super.setId(id);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addView(inflater.inflate(R.layout.day_view, null));
eventTextView = (TextView) findViewById(R.id.eventTextView);
dayTextView = (TextView) findViewById(R.id.dayTextView);
eventTextView.setOnClickListener(new GridClickListener());
dayTextView.setOnClickListener(new GridClickListener());
}
public TextView getEventTextView()
{
return eventTextView;
}
public TextView getDayTextView()
{
return dayTextView;
}
class GridClickListener implements OnClickListener
{
#Override
public void onClick(View v)
{
calculateClickedGrid(v);
}
}
private void calculateClickedGrid(View v)
{
int id = ((ViewGroup)v.getParent()).getId();
GridCordinates cordinates = GridViewUtil.getGridCordinate(id);
if(cordinates != null)
{
}
}
}
Below is the gridlayout xml code in which i am using above compound control:-
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:rowCount="8"
android:columnCount="7"
android:background="#drawable/selectable_day_layout_border">
<!-- Row 0-->
<ImageButton
android:id="#+id/previousMonthImageButton"
android:layout_row="0"
android:layout_columnSpan="1"
android:layout_column="0"
android:layout_gravity="center"
android:layout_margin="3dip"
android:padding="3dip"
android:background="#7F7CD9"
android:src="#drawable/navigation_previous_item"/>
<TextView
android:id="#+id/monthNameTextView"
android:text="September"
android:layout_row="0"
android:layout_columnSpan="5"
android:layout_column="1"
android:layout_gravity="center"
android:layout_margin="3dip"
android:padding="3dip"
android:textSize="25dip"
android:background="#7F7CD9"/>
<ImageButton
android:id="#+id/nextMonthImageButton"
android:layout_row="0"
android:layout_columnSpan="1"
android:layout_column="6"
android:layout_gravity="center"
android:layout_margin="3dip"
android:padding="3dip"
android:background="#7F7CD9"
android:src="#drawable/navigation_next_item"/>
<!-- Row 1-->
<com.gp.app.minote.calendar.ui.CalendarGridView
android:id="#+id/day1"
android:layout_row="2"
android:layout_column="0"
android:layout_width="30dip"
android:layout_height="40dip"
android:layout_margin="2dip"
android:padding="3dip"
android:layout_gravity="center"
android:background="#drawable/selectable_day_layout_border"/>
<com.gp.app.minote.calendar.ui.CalendarGridView
android:layout_width="30dip"
android:layout_height="40dip"
android:id="#+id/day2"
android:layout_row="2"
android:layout_column="1"
android:layout_margin="2dip"
android:padding="3dip"
android:layout_gravity="center"
android:background="#drawable/selectable_day_layout_border"/>
<com.gp.app.minote.calendar.ui.CalendarGridView
android:layout_width="30dip"
android:layout_height="40dip"
android:id="#+id/day3"
android:layout_row="2"
android:layout_column="2"
android:layout_margin="2dip"
android:layout_gravity="center"
android:padding="3dip"
android:background="#drawable/selectable_day_layout_border"/>
<!-- More CalendarGridViews but deleted for clarity -->
</GridLayout>
Below mentioned is the code from the class which extends GridLayout and uses CalendarGridViews:-
public class ProfessionalPACalendarView extends GridLayout
{
private Context context;
private Calendar cal;
private ImageButton previousMonthImageButton = null;
private ImageButton nextMonthImageButton = null;
private TextView monthNameTextView = null;
private List<DateInformation> dateList = new ArrayList<>();
private List<CalendarGridView> calendarGrids = new ArrayList<>();
private RelativeLayout base;
private ImageView next,prev;
private boolean isWithoutEvents = false;
private int id = 0;
public ProfessionalPACalendarView(Context context)
{
super(context);
this.context = context;
cal= Calendar.getInstance();
if(!isWithoutEvents)
{
CalendarDBManager.getInstance().addDataChangeListener(this);
}
initView();
this.isWithoutEvents = isWithoutEvents;
}
public ProfessionalPACalendarView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initView();
}
public ProfessionalPACalendarView(Context context, AttributeSet attrs,int defStyle)
{
super(context, attrs, defStyle);
this.context = context;
initView();
}
private void initView()
{
init();
Calendar calendar = Calendar.getInstance();
refreshDays(calendar.get(Calendar.DATE), calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR));
fillCalendarViewWithDates();
}
private void init()
{
int id = (int)Math.abs(Math.random() * 100000);
super.setId(id);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.calendar_grid_layout, null);
addView(view);
previousMonthImageButton = (ImageButton) findViewById(R.id.previousMonthImageButton);
previousMonthImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
previousMonth();
}
});
monthNameTextView = (TextView) findViewById(R.id.monthNameTextView);
nextMonthImageButton = (ImageButton) findViewById(R.id.nextMonthImageButton);
nextMonthImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
nextMonth();
}
});
calendarGrids.add((CalendarGridView) findViewById(R.id.day2));
calendarGrids.add((CalendarGridView) findViewById(R.id.day3));
}
Problem :- findViewById(id) in the lines below :-
previousMonthImageButton =(ImageButton)findViewById(R.id.previousMonthImageButton);
monthNameTextView = (TextView) findViewById(R.id.monthNameTextView);
nextMonthImageButton = (ImageButton) findViewById(R.id.nextMonthImageButton);
returns non null value whereas findViewById(id) in the below lines:-
calendarGrids.add((CalendarGridView) findViewById(R.id.day2));
calendarGrids.add((CalendarGridView) findViewById(R.id.day3));
return null value.
I have trying to solve this problem for long but not getting the difference why same method returns null and non null value for different views. I think this has something to do with composite views. Please help.
You initialize View view = inflater.inflate(R.layout.calendar_grid_layout, null);
Please pass View Object .
previousMonthImageButton =(ImageButton)view.findViewById(R.id.previousMonthImageButton);
monthNameTextView = (TextView)view. findViewById(R.id.monthNameTextView);
nextMonthImageButton = (ImageButton)view.findViewById(R.id.nextMonthImageButton);
Try this way .I hope it will helps you .