I am trying to add an empty view with height 30dp below my gridview but unfortunately, I am not able to see the expected result.
The only thing visible is gridview.
How to display the empty view after all contents of gridview are rendered?
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary">
<GridView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:background="#color/colorAccent"
/>
<View
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/list"
android:background="#color/colorPrimaryDark"/>
</RelativeLayout>
This is my current output.
EDIT:
There are many workarounds you could use to achieve what you're looking for.
In my opinion, using a dedicated library could be the best options for lightness.
Have a look at this: GridViewWithHeaderAndFooter
From the docs:
GridViewWithHeaderAndFooter gridView = (GridViewWithHeaderAndFooter) v.findViewById(R.id.ly_image_list_grid);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View headerView = layoutInflater.inflate(R.layout.test_header_view, null);
View footerView = layoutInflater.inflate(R.layout.test_footer_view, null);
gridView.addHeaderView(headerView);
gridView.addFooterView(footerView);
Related
I have ScrollView with LinearLayout and button inside:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="#+id/layout_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#color/card_color"
android:orientation="vertical"
android:padding="10dp">
</LinearLayout>
<Button
android:id="#+id/button_leave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/leave" />
</LinearLayout>
</ScrollView>
Then in code I add view to layout_list:
View view = LayoutInflater.from(this)
.inflate(R.layout.item_member, mLayoutMemberList, true);
If I add not many items, then button_leave is visible. But if I add a lot of items (more then showing on the screen) then button is not visible on the screen even if I scroll down.
Update:
Looks like it is bug of android.support.design.widget.CoordinatorLayout. I change it to simple linear layout and all work good.
Try like this:
I think layout view is working fine and try below code,
LayoutInflater inflater = context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.layout_list, null, true);
I want to make to the listView with an increase fell button:
My attempt at implementing this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#214075"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listbascet"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<Button
android:id="#+id/sendtoEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Заказать услуги" />
</LinearLayout>
You would better remove the button from the XML and add it to another one.
Then you can use the inflate to get the button and add it as footer view to the original list view.
This way it will be at the end of the list and scroll will it as well!
How to inflate:
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylayout, null);
Button item = (Button) view.findViewById(R.id.item); // how to reference your button
How to add footer:
listView.addFooterView(view); // view is the inflated layout.
More details about footer view contained in this answer as well.
I am trying to design a 3D page curl.
In my mainActivity, I have a viewPager which contains pages to be curled. For each page I have a separate layout file.
In the layout file if i just add a text view like below it is showing up fine:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/PageTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green"
android:text="VIEW 1" /> -->
But if I wrap the textview inside relative layout, it just shows a white blank screen and textview is not shown.
<RelativeLayout 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"
tools:context=".DemoActivity" >
<TextView
style="#style/PageTitle"
android:id="#+id/sampletextview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="VIEW 1" />
</RelativeLayout>
Can someone please help me to know why i am not able to use relative layout in my view layout file:
I inflate the view like this:
LayoutInflater inflater = (LayoutInflater) myAppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view1 = inflater.inflate(mViewIds[0], null);
Try changing the second parameter of the inflate() call from null to the ViewGroup that view1 is added to. Or post the code where you add it (and include the part where you define the LayoutParams).
I have implemented a ListView in my application using a custom implementation of CursorAdapter. My problem is, that whenever I fling to scroll quickly to the bottom of the list (right after launching the application), I sometimes end up with all the ListView items drawn overlapping each other. If I scroll back up or touch one of the items, they become properly arranged.
Here is how it looks after I quickly scroll down :
http://i.stack.imgur.com/cTcfD.png
Here is how it looks when I am select-ing one of the items :
http://i.stack.imgur.com/ZTRSt.png
Here is the XML for my ListView :
<ListView
android:id="#+id/all_reminders_list"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:clickable="true"
android:dividerHeight="1.0sp"
android:animateLayoutChanges="true">
Here's the newView(..) method of my custom CursorAdapter :
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.view_list_item, parent, false);
return view;
}
And this is the bindView(..) method :
public void bindView(View view, Context context, Cursor cursor) {
TextView whatTextView = (TextView) view.findViewById(R.id.item_what_text);
whatTextView.setText(cursor.getString(1));
TextView whenTextView = (TextView) view.findViewById(R.id.item_when_text);
if(cursor.getInt(9) != 0) // DONE_FLAG = 1 (completed)
{
//Arrow visibility
ImageView arrow = (ImageView)view.findViewById(R.id.list_item_arrow);
arrow.setVisibility(View.INVISIBLE);
//Text color
whatTextView.setTextColor(Color.LTGRAY);
whenTextView.setTextColor(Color.LTGRAY);
//WHEN text
whenTextView.setText(TimeCalculationHelper.getCompletedTimeString(cursor.getLong(2)));
}
else // DONE_FLAG = 0
{
//Arrow visibility
ImageView arrow = (ImageView)view.findViewById(R.id.list_item_arrow);
arrow.setVisibility(View.VISIBLE);
//Text color
whatTextView.setTextColor(Color.BLACK);
whenTextView.setTextColor(Color.BLACK);
//WHEN text
whenTextView.setText(TimeCalculationHelper.getTimeRemainingString(cursor.getLong(2)));
}
}
I've also noticed that I have been able to replicate it only when my device (Galaxy S2) is in power saving mode. Is there something I should be doing differently here? Thanks in advance!
EDIT : Including the list item's layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="2dp">
<TextView android:id="#+id/item_what_text"
android:lines="1"
android:maxLines="2"
android:textSize="22dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"/>
<TextView android:id="#+id/item_when_text"
android:lines="1"
android:maxLines="1"
android:textSize="14dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="13 minutes"
android:paddingBottom="2dp"
android:layout_below="#+id/item_what_text"/>
<ImageView
android:id="#+id/list_item_arrow"
android:src="#drawable/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
I also faced the same problem, with List items in my ListView overlapping each other whilst scrolling.
My fix:
I just specified a background for the parent layout that contained the ListView in question. Previously it was transparent.
I had the same problem and found it was caused by setting the animateLayoutChanges attribute to true on the listview.
Unfortunately I lose the animation of the listview by removing it but at least it draws properly when scrolling fast.
Giving the parent layout a background also appears to fix the issue, as mentioned by Pavan. Will experiment and change my answer if I discover issues with the background change.
For solving this ,i have one more solution:
I faced problem by using this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:paddingTop="4dip"
android:dividerHeight="0dp"
android:divider="#null"
android:cacheColorHint="#00000000"
android:listSelector="#color/transprent_editbox"
android:focusableInTouchMode="false" />
</RelativeLayout>
In order to solve that Problem,i just did few modifications on Above layout,those are:
1)Changed top layout from Relative to Linear Layout.
2)Put my ListView in other Relative Layout ..
i did like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:paddingTop="4dip"
android:dividerHeight="0dp"
android:divider="#null"
android:cacheColorHint="#00000000"
android:listSelector="#color/transprent_editbox"
android:focusableInTouchMode="false" />
</RelativeLayout>
</LinearLayout>
After using this,my problem is solved.
1.
in my opinion, height is wrap_content
because listview is restore row
2.
other opinion is write this code android:cacheColorHint="#00000000" in ListView
I also faced the same problem and found that animateLayoutChanges attribute causing the problems.
So i changed
android:animateLayoutChanges="true"
to
android:animateLayoutChanges="false"
and it worked.
In my application I'm adding a view dynamically to the layout using addView.
The added view was inflated beforehand using
andorid:layout_width="match_parent"
however, after the adding the view using
parentView.addView(view, index);
The view is addded but dosen't fill the parent's width
I guess this is because the calculation to the "match_parent" size is done beforehand,
when the view is inflated, and therefore it has no reference of how to set the corect width
is this the case?
is there a way for me to tell the view to recalculate the layout params?
my Code:
activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue_bg">
<ImageView android:id="#+id/myImg"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:src="#drawable/myImg"
android:scaleType="fitXY"/>
<LinearLayout android:id="#+id/addressItemContainer"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_below="#id/myImg" android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<View android:id="#+id/placeHolder"
android:layout_width="match_parent" android:layout_height="match_parent"/>
</LinearLayout>
</RelativeLayout>
inserted_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="#drawable/blue_selector">
.... internal views
</LinearLayout>
java code:
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.inserted_view, null);
... fill inserted view fields ...
View aiv = findViewById(R.id.placeHolder);
ViewGroup parent = (ViewGroup) aiv.getParent();
int index = parent.indexOfChild(aiv);
parent.removeView(aiv);
parent.addView(view, index);
Tx for the help :)
Ok, just add this code
View aiv = findViewById(R.id.placeHolder);
aiv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Mark as resolved, please.
View view = findViewById(R.id.placeHolder);
view .setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//addView(view)
Thanks Dmytro Danylyk. Now its work prefectly.