How to make ListView resizable? - java

Picture 1
Picture 2
How to make ListView resizable?
This ListView is OK.
OK ListView 1 Picture
OK ListView 2 Picture
I have tried all the layouts. That doesn't help.
I have a fragment container:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="#color/yellow"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fragmentsContainer"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
app:menu="#menu/menu"
android:id="#+id/bottomNavigation"
android:layout_alignParentBottom="true" />
</RelativeLayout>
OK Layout is Search Layout:
<?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="#color/yellow"
android:id="#+id/searchAndListViewContainer"
android:orientation="vertical">
<SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorOfSearchView" />
<ListView
android:id="#+id/downloadedMusicListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
unadabtable Layout :
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/darkGreen">
<ListView
android:id="#+id/musicListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
As you see Java Code will change Fragments container's inner content.
Two fragments are almost identical.
What's the problem?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ListView
android:id="#+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/tv_footer"
android:layout_below="#+id/tv_header" />
<TextView
android:id="#+id/tv_footer"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#ff0000"
android:gravity="center"
android:text="Footer" />
<TextView
android:id="#+id/tv_header"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#00ff00"
android:gravity="center"
android:orientation="vertical"
android:text="Header" />
</RelativeLayout>
you can do that in multiple ways. here you can set your own view as header and footer.

Related

Hide SearchView in Fragment

I want to hide my Searchview when I scroll in my Fragment. I use a RecyclerView to retrieve my item and scroll them.
My fragment are actually like that:
When I scroll I want it to hide and then when I return to the top, I want it to show.
This is the xml of my 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".stock.StockFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.SearchView
android:id="#+id/search_view"
android:layout_width="341dp"
android:layout_height="34dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/search_layout"
android:gravity="center"
app:iconifiedByDefault="false"
app:queryHint="Cerca Articolo">
</androidx.appcompat.widget.SearchView>
</LinearLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycleViewStock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
I tried to put it inside the Recycler but doesn't work, help please!
you can use NestedScrollView for scrolling all items.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".stock.StockFragment">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<androidx.appcompat.widget.SearchView
android:id="#+id/search_view"
android:layout_width="341dp"
android:layout_height="34dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/search_layout"
android:gravity="center"
app:iconifiedByDefault="false"
app:queryHint="Cerca Articolo">
</androidx.appcompat.widget.SearchView>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycleViewStock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
After that set recyclerView.setNestedScrollingEnabled(false); in java for proper scrolling.

How to include layout at the bottom of another layout

step.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.stepstone.stepper.StepperLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/stepperLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:ms_stepperType="dots" />
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:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="3dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="3dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/firstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="11"
android:maxLines="1"
android:hint="First name"
android:textColor="#000000"
android:maxLength="15"
android:tag="#+id/first_name"
android:textCursorDrawable="#null" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
<include
android:id="#+id/bottom_Menu"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/step"/>
</ScrollView>
This is the design of step.xml
This is the design of activity_main. In this I have included the step.xml
I need the output like this.
When I am trying to include step.xml in activity_main, I get the exception "Scroll view can only host one child view." Please help me.
Change your StepperLayout hight to wrap content.
step.xml :
<?xml version="1.0" encoding="utf-8"?>
<com.stepstone.stepper.StepperLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/stepperLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ms_stepperType="dots" />
And change activity_main layout as following :
activity_main.xml:
<?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:id="#+id/profile_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:windowSoftInputMode="adjustResize|adjustPan">
<LinearLayout
android:id="#+id/profile_layout_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:id="#+id/layout_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="70dp"
android:paddingTop="3dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/firstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="11"
android:maxLines="1"
android:hint="First name"
android:textColor="#000000"
android:maxLength="15"
android:tag="#+id/first_name"
android:textCursorDrawable="#null" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
<RelativeLayout
android:id="#+id/btn_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true">
<include
android:id="#+id/bottom_Menu"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/step"/>
</RelativeLayout>
</RelativeLayout>
Use this code: Once the scroll view tag has ended add the step xml code like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Button2"/>
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/buttons">
<!--Scrollable content here-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="test text"
android:textSize="40dp"/>
</LinearLayout>
</ScrollView>
<com.stepstone.stepper.StepperLayout
android:id="#+id/stepperLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:ms_stepperType="dots" />
</RelativeLayout>
You should make a single child inside a scrollview (just as the error says). Wrap all the children inside of another (let's say) LinearLayout or RelativeLayout with wrap_content for both the width and the height as well as the vertical orientation.
From the Docs:
A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.
EDIT
Simplest example can be like this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
// all the views currently in your ScrollView
</LinearLayout>
</ScrollView>
StepperLayout hight is match parent that's why it is in top position, Change you StepperLayout hight to wrap content
step.xml
<?xml version="1.0" encoding="utf-8"?>
<com.stepstone.stepper.StepperLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/stepperLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ms_stepperType="dots" />
And ScrollView having only one child so move your include layout inside relative layout and add android:fillViewport="true" to ScrollView
<?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:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:fillViewport="true"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="3dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="3dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/firstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="11"
android:maxLines="1"
android:hint="First name"
android:textColor="#000000"
android:maxLength="15"
android:tag="#+id/first_name"
android:textCursorDrawable="#null" />
</android.support.design.widget.TextInputLayout>
<include
android:id="#+id/bottom_Menu"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/step"/>
</RelativeLayout>
</ScrollView>

Footer not responding in PreferenceScreen

I've tried to add my footer in my preference screen, but my buttons in it are not responding. Also, when I call the EditTextPreference, my footer moves too.. Any idea?
Here's my xml file
<?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">
<include layout="#layout/footer"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/footer">
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_weight="1" >
</ListView>
</RelativeLayout>
</RelativeLayout>
Use your layout like this..
<?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">
<include
android:id="#+id/your_footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="#layout/footer" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/your_footer">
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_weight="1" >
</ListView>
</RelativeLayout>
</RelativeLayout>

Android Java ViewStub for Empty Listview Adapter

I am trying to get the empty.xml layout display when the adapter listview is empty.
I tried with http://cyrilmottier.com/2011/06/20/listview-tips-tricks-1-handle-emptiness/
But I can't do it.
empty.xml
<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:orientation="vertical"
tools:context=".Principal"
>
<ListView
android:id="#+id/listview_items"
tools:listitem="#layout/item_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1px"
android:divider="#ebebeb"
android:scrollbarStyle="insideOverlay"
>
</ListView>
<ViewStub
android:id="#+id/stub_import"
android:layout="#layout/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>
main.xml
<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:orientation="vertical"
tools:context=".Principal"
>
<ListView
android:id="#+id/listview_items"
tools:listitem="#layout/item_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1px"
android:divider="#ebebeb"
android:scrollbarStyle="insideOverlay"
>
</ListView>
<ViewStub
android:id="#+id/stub_import"
android:layout="#layout/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>
I know the command to inflate and deflate is something.
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
Where should I put the command to inflate when my listview is empty?
Thanks in advance.
Personally i do these in either of onCreate or onResume, by first checking if my listview is empty using the listview's adapter.getCount() method to check if the adapter is empty , then i inflate the viewStub.
My code looks like this :
if(adapter.getCount()==0){
ViewStub stub=(ViewStub) viewRoot.findViewById(R.id.viewstub);
stub.inflate();
}
My XML file looks Like
<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"
tools:context="com.younickacademy.younickmobile.Fragment_AlertsList"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ListView
android:id="#+id/listview_alerts"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
<ViewStub
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout="#layout/empty_alert_list"
android:id="#+id/viewstub"/>
</FrameLayout>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="bottom|right"
android:layout_margin="10dp"
android:id="#+id/btn_new_alert"
android:elevation="6dp"
android:gravity="center"
android:text="+"
android:textColor="#color/textColorPrimary"
android:background="#drawable/circular_button_selector"/>
</LinearLayout>
And My empty_alert_list.xml layout looks like :
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="No Emergency Alerts have been received Yet."
android:textStyle="bold"
android:gravity="center_horizontal"
/>
</RelativeLayout>
However this may not be the best implementation, but does solve my problem momentarily until a better hack is found.

Extend fragment container with header line

I have an activity with a fragment container for showing different details of an object in my app.
<?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="match_parent"
android:layout_gravity="center">
<LinearLayout
android:orientation="horizontal"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
</LinearLayout>
This works fine. Tabs are displayed properly.
Now I want to add an header line to this actitivy which shows "static" content regardless of the current fragment for the details. The header line shall be on top with the fragment-container below. So I changed the layout to this:
<?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="match_parent"
android:layout_gravity="center">
<LinearLayout
android:orientation="horizontal"
android:id="#+id/LabeledObjectHeaderInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txtHeaderID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".10"
android:gravity="right"
android:padding="10dip"
android:text="#string/txtHeaderID"
android:textSize="20sp" />
<TextView
android:id="#+id/txtHeaderIDValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:gravity="left"
android:padding="10dip"
android:text="test"
android:textSize="20sp" />
<TextView
android:id="#+id/txtHeaderName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:gravity="left"
android:padding="10dip"
android:text="#string/txtHeaderName"
android:textSize="20sp" />
<TextView
android:id="#+id/txtHeaderNameValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:gravity="left"
android:padding="10dip"
android:text="test"
android:textSize="20sp" />
</LinearLayout >
<LinearLayout
android:orientation="horizontal"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
>
</LinearLayout>
</LinearLayout>
My problem is now, that only the header is shown, but not the content of the fragments. I only changed the layout file up to now (as you can see by the "test"-texts), but no other code.
What am I doing wrong and where is my mistake?
Try this
In your container LinearLayout you have android:orientation="horizontal" which shows its first child LinearLayout i.e the Header, but hides its second child LinearLayout i.e the content to the right.
<?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="match_parent"
android:layout_gravity="center">
<!-- Inner Layouts -->
</LinearLayout>
Change it to android:orientation="vertical".
<?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"
android:layout_gravity="center">
<!-- Inner Layouts -->
</LinearLayout>
just make the orientation in your main layout vertical and it will work fine.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
Because you are making its orientation horizontal, the fragments layout goes to the right.

Categories

Resources