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.
Related
Why is this not working?
It's a problem with the listview.
I've tried in many ways and ending up with the button not showing
<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"
tools:context=".fragments.notes.NotesFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_baseline_add_24" />
</LinearLayout>
</ScrollView>
</FrameLayout>```
The first problem is that you cannot put a ListView inside a ScrollView, ListView itself is scrollable and the second problem it's with the margin if you use a bottom menu, I also encountered this problem. Try this one
<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"
tools:context=".fragments.notes.NotesFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|end"
android:layout_marginRight="20dp"
android:layout_marginBottom="70dp"
android:src="#drawable/ic_baseline_add_24" />
</RelativeLayout>
</FrameLayout>```
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.
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.
My app has a banner ad at the bottom.It takes some time load.While it loads a white space appears in its place as shown below:
How do i slove this problem.Should improve the loading speed of the banner ad or do something with my xml layout.
Here is the xml:
<?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"
tools:context="com.example.android.gametalks.MainActivity"
>
<LinearLayout
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/no_internet_view"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Internet Connection!"/>
<Button
android:id="#+id/retry_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retry"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_above="#id/adView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
</LinearLayout>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
You can remove android:layout_above="#id/adView from list container and add to your ListView paddingBottom = height_of_your_banner (or about it) and add android:clipToPadding="false". Now your listview will fill whole screen and if you scroll to bottom - you will be able to see al items without overlap from banner. Also you can remove odd containers from ListView so instead of:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_above="#id/adView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
</LinearLayout>
Will be just:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
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.