Below is my Footer Layout for setting it for my ListView :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray_cell_bg">
<TextView
android:text="footer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
It's working fine. But, What if I want to set an emptyview with some background color in my ListView's footer ?
If I am doing it as below, ListView not showing footer view. :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray_cell_bg">
</RelativeLayout>
I am setting footer as below :
View footerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_for_gym_list, null);
listGymSuggestions.addFooterView(footerView);
Please guide me How can I set empty footer with some height and background color in ListView ?
The View is there but it has NO height so you must add your own height value may be 24dp or something and set it as a footer you can even add a width if you want. For instance here I am adding a height of 24dp:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="24dp"
android:background="#color/gray_cell_bg">
</RelativeLayout>
If there is no child in that layout, It will not be shown. If you have problem with setting specific height(when you are adding child dynamically), you can use padding like,
android:padding="20dp"
I added a button as a footer in one of my layout.
Here is my code. You may add anything you want.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/search_toolbar"
layout="#layout/search_toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ProgressBar
android:id="#+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/emp_button"
android:layout_below="#+id/search_toolbar" />
<TextView
android:id="#+id/tvsample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/search_toolbar"
android:layout_marginTop="200dp"
android:gravity="center_horizontal"
android:textAppearance="#style/TextAppearance.AppCompat.Medium" />
<android.support.v7.widget.AppCompatImageButton
android:id="#+id/emp_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/Red"
android:src="#drawable/ic_add_employee"
android:text="#string/emp_button"
android:textSize="14sp" />
Issue is solved by adding an ImageView in Footer view as below :
footer.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray_cell_bg">
<ImageView
android:layout_width="match_parent"
android:layout_height="#dimen/com_facebook_profilepictureview_preset_size_normal"
android:scaleType="fitXY"
android:src="#color/gray_cell_bg" />
</RelativeLayout>
Related
I want to Scroll ReyclerView inside a Fixed height/region.
Sample XML to demonstrate the use case
<?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:id="#+id/ele_parent_ll"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:id="#+id/election_parent_sv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/sample_id"
android:background="#color/white"
android:fitsSystemWindows="true">
<ImageView
android:contentDescription="#string/btn_youtube"
android:layout_width="match_parent"
android:layout_height="115dp"
android:id="#+id/election_banner"
android:background="#drawable/some_drawable"
android:adjustViewBounds="true"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mid_parent"
android:layout_marginLeft="#dimen/view_margin_10"
android:layout_marginRight="#dimen/view_margin_10"
android:layout_marginTop="#dimen/view_margin_10"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shaded_purple_new_local"
android:text="Layout heading"
android:textColor="#color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Choose one"
android:textColor="#color/icon_yellow"
android:textSize="13sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/candidate_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/view_margin_10"
android:layout_marginLeft="#dimen/view_margin_10"
android:layout_weight=".5"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!--Sample Layout to make the screen scrollable for the example-->
<RelativeLayout
android:id="#+id/samp_RL"
android:layout_width="match_parent"
android:layout_height="200dp">
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
In this scenario, the RecyclerView is taking it's required space and list is pretty much long so the total length of the scrollable area has gone long so probability of seeing elements below RCV has decreased.
To solve this I want to scroll RecyclerView inside a fixed area so that If the user is not interested in that he can easily look up to the other Elements present below the RecyclerView
When user scrolls within the region of RCV the RCV should scroll.
When the user scrolls outside the RCV the whole Layout should scroll
Image made at draw.io
What I have tried:
Making the height of RecyclerView constant: It is Jammed and is not scrolling at all.
Making Recyclerview Fixed Height and Scrollable : It's also the same. It is jammed and is not scrolling at all.
Scrollable NestedScrollViews inside RecyclerView: Tried this but could not establish a connection to my current situation.
Please tell what I am missing here.
you do not need to do anything. by default it works like this only !
Try the below code -
recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(false);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
mAdapter = new MyAdapterClass(getContext(), lst);
recyclerView.setAdapter(mAdapter);
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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="com.example.iosdteachingapp.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="100sp"
android:text="hello"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="200sp"
android:layout_height="300sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="end"
android:padding="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="end"
android:padding="20sp"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="end"
android:padding="20sp"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="end"
android:padding="20sp"/>
</LinearLayout>
</ScrollView>
Add android:nestedScrollingEnabled="false" to your RecyclerView.
Set Recycler view height dynamically thr java using param.
You can even set the height to show only particular no of elements by setting height = size of each element of recycler view * no of elements in the list.
Try this :
ViewGroup.LayoutParams
params=recyclerview.getLayoutParams();
params.height=100;
recyclerview.setLayoutParams(params);
Problem I want view-layout like the following photo, on top I want image view, in the middle a text view and bottom list of items. All this I want to be scrollable (black arrow in the picture). in this way, the user for first see image-text-and part of list items, if scroll down, see the list of item.
For the bottom I thought to use listView, but in this way I think I'll have 2 scrollable view (???) and then I want to make the list in 2 columns, and I don't know if it is possible with ListView
Question
I didn't write code, because I want to know what kind of view or somethings else I've to study.
I sketchy something like this, but the result is really horrible
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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">
<RelativeLayout
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="#drawable/nope"
android:id="#+id/imageView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<View
android:id="#+id/centerShim5"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:visibility="invisible" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView2"
android:textColor="#color/ame_default_cluster_circle_color_medium"
android:textSize="26dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</ScrollView>
I can't use listview in Scrollview, and inspired by the matryoshka I found the solution.
I define xml1 file in which I have only ScrollView and LinearLayout inside
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/linear">
</LinearLayout>
</ScrollView>
after, for each kind of items that I want to add inside, I define another xml2 and after I add element:
<?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:background="#color/ame_default_cluster_text_color">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="5"
android:textSize="32sp"
android:textColor="#color/ame_default_cluster_circle_shadow_color"
android:id="#+id/text1"
android:textAlignment="center" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/nope"
android:id="#+id/photo" />
<TextView
android:text=" "
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:id="#+id/descrtiption"
android:textColor="#color/ame_default_cluster_circle_shadow_color"
android:layout_margin="10sp" />
</LinearLayout>
with code:
layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view=layoutInflater.inflate(R.layout.xml1,container,false);
LinearLayout linearLayout=(LinearLayout)item_view.findViewById(R.id.linear);
LayoutInflater inflater= LayoutInflater.from(ctx);
View view1 = inflater.inflate(R.layout.xml2, linearLayout, false);
TextView text1=(TextView)view1.findViewById(R.id.text1);
text1.setText();
ImageView imageview=(ImageView)view1.findViewById(R.id.photo);
and obviously I can define XML for element to add like listview and add it with normal foreach ,Hope this will help someone.
In my MainActivity I'm using a FloatingActionButton to add CardViews to an existing container (a FrameLayout) programatically.
But when I add the CardView all LayoutParams (e.g. width, height, margin, etc) are ignored. Is there a way to use the params specified in the XML File?
Here is my code:
card_view.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
card_view:cardCornerRadius="2dp"
card_view:cardBackgroundColor="#color/cardViewBackground">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="example Text" />
</android.support.v7.widget.CardView>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<TextView
android:id="#+id/textViewDate"
android:text="#string/default_main_activity_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textAlignment="center"
android:padding="16dp" />
<FrameLayout
android:id="#+id/container_body"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="#drawable/ic_fab_add"
android:layout_gravity="end"
android:onClick="addCard"
android:visibility="gone"/>
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.life_hacks.liftnotes.activity.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
and finally the code I use to add the CardView:
public void addCard(View v){
View cardView = View.inflate(getApplicationContext(), R.layout.card_view, null);
FrameLayout container = (FrameLayout) findViewById(R.id.container_body);
if(container != null){
container.addView(card_view);
}
}
For the layout_* attributes of an XML-defined View to be applied correctly, the ViewGroup that will hold it must be supplied to the LayoutInflater. In this case, it means that you need to pass your container as the last argument in the View.inflate() call. Doing this will also cause the inflated View to be automatically added to the ViewGroup, so you must not call addView() with it, or you'll get an IllegalStateException.
Also, since you'll be adding multiple Views to your container, it would seem that a LinearLayout would be more appropriate than a FrameLayout. I would also mention that if you plan on adding a lot of additional Views, a ListView or a RecyclerView might end up being a better option.
I've some line of code in parent XML as below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/header_parent_layout"
layout="#layout/common_header_layout" />
<include
android:id="#+id/title_header_layout"
layout="#layout/title_header_layout" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</LinearLayout>
then i have an another xml that i want to add in in LinearLayout with id:content layout in first xml inflated by below xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customviews="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/agenday_account_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<!-- More Code -->
</FrameLayout>
I already set the center_vertical gravity in inflated xml layout but it's always displaying in top. Could anybody help how to treat with this.
Below code is how i add it:
View view = getLayoutInflater().inflate(R.layout.second, null);
if (view != null) {
mContentContainer.addView(view);
}
mContentContainer is parent one.
Just add a gravity attribute to your id:content LinearLayout,so the content inside it stays centered. Like this:
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" />
I have this Idea:
Change content LinearLayout attributes to:
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:gravity="center_vertical"
android:orientation="vertical" />
So I'm having some trouble with a RecyclerView in a layout file
Here it is:
<android.support.v7.widget.RecyclerView
android:id="#+id/chat_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="#dimen/abc_action_bar_default_padding_material"
android:paddingLeft="#dimen/abc_action_bar_default_padding_material"
/>
<LinearLayout
android:layout_below="#id/chat_listview"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_bar_height"
android:orientation="horizontal"
>
<EditText
android:id="#+id/chat_input_edittext"
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:inputType="textAutoCorrect"
/>
<Button
android:id="#+id/chat_send_button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/ic_action_send_now"
/>
</LinearLayout>
The RecyclerView is visible and scrollable but the LinearLayout and the Views inside are not visible... I tried quite a few things but nothing has worked so far.
Can any of you please point me into the right direction?
Many thanks in advance!
When RecyclerView is drawn, it calculates all the remaining size on screen to itself before drawing the next elements and don't recalculate after the other elements are drawn, leaving them outside the screen.
The trick is to draw all other elements first, and leave RecyclerView for last. FrameLayout does not work anymore so use a relative layout and put the RecyclerView last on the XML layout file.
Example to add a bar with buttons below the RecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity"
>
<LinearLayout
android:id="#+id/pagination_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"> //HERE YOU ALIGN THIS ELEMENT TO THE BOTTOM OF THE PARENT
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/previous_btn_label"/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/next_btn_label"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/items_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_above="#id/pagination_btns"/> //HERE YOU ALIGN THE RECYCLERVIEW ABOVE THE PAGINATION BAR
</RelativeLayout>
Change the root view to a FrameLayout. Set the gravity of the LinearLayout to bottom
<FrameLayout 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.support.v7.widget.RecyclerView
android:id="#+id/chat_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="#dimen/abc_action_bar_default_padding_material"
android:paddingLeft="#dimen/abc_action_bar_default_padding_material"
/>
<LinearLayout
android:layout_below="#id/chat_listview"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_bar_height"
android:layout_gravity="bottom"
android:orientation="horizontal">
<EditText
android:id="#+id/chat_input_edittext"
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:inputType="textAutoCorrect"/>
<Button
android:id="#+id/chat_send_button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/ic_action_send_now"/>
</LinearLayout>
</FrameLayout>
Alternatively, you can wrap the linear layout in a FrameLayout
and set it to align_parentBottom = true if it is a relative layout or a linear layout at your root and you have other things in your view.