I am trying to hide a tab dynamically using databinding.
the code is
<com.google.android.material.tabs.TabItem
android:id="#+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab1" />
<com.google.android.material.tabs.TabItem
android:id="#+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab2" />
<com.google.android.material.tabs.TabItem
android:id="#+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="#{vm.flag ? View.VISIBLE : View.GONE}"
android:text="#string/tab3" />
but this is not working and all three tabs are always visible.
I tried setting it in the code as tablayout.getindex(2).visibility but this gives me NPE.
How do I do this?
You need to modify the adaterList that you pass to the viewpager. Delete the third element and it will disappear
When you need the third tab to appear, simply update the adapterList/viewPager. Check out this post
Related
As the topic shown.
I made a TabbedActivity and implement an ListView under it.
I also made an string array to display inside the Listview.
The problem is : Same text will display on different tabbed
For example: There are two tabbed (number_decimal, number_roman),
It should display (1,2,3) (I,II,III)
But after I executed, both two pages are displayed 1,2,3.
What should I do to solve it, Thanks.
Also,
Which Java file should I provide?
SectionPagerAdaptor ? PlaceHokderFragment ? PageVIewModel
If there are similar questions, please let me know.
Belows are the code (XML) .
Here are the code (TabbedActivity and Listview):
<androidx.constraintlayout.widget.ConstraintLayout 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/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.PlaceholderFragment">
<!--
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="#+id/constraintLayout"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1" />
-->
<!-- 結帳 -->
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="#array/vegetable">
</ListView>
<Button
android:id="#+id/button_jumptocash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:layout_marginBottom="20dp"
android:text="#string/jumptocash"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
you need to use one fragment inside each tab, then instead of list view inside of each fragment use always recyclerview whit its adapter,
list view is stupid and most of time create strange problem, especially when user scrolls
I want to have seven tabs in a tabLayout. They won't be scrollable. So, I want them to be in multiple lines. But, in fixed mode, they are all arranged in a single row. I have found some answers which are not a direct answer rather a workaround. Is there a way to do this?
A snapshot of my XML:
<com.google.android.material.tabs.TabLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/colorGrayStandard"
app:tabTextColor="#color/colorWhite">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/device" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/wikipedia" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/wikia" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/youtube" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/goodreads" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/imdb" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/yelp" />
</com.google.android.material.tabs.TabLayout>
Here is the documentation for TabLayout: https://developer.android.com/reference/com/google/android/material/tabs/TabLayout
TabLayout provides a horizontal layout to display tabs.
That is the first line written in the documentation and it is your answer to this question.
This is not possible with TabLayout. You can make them scrollable and that's it. There is a lot of workarounds you can create and use without problems.
In the documentation above, you can find anything that's actually possible with TabLayout. If you want to create workarounds just create fixed tabs by yourself, create listeners, and use fragments to change views you want when the tab changes.
I'm trying to change the background color of a TabLayout, but so far everything I've tried doesn't work.
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#FF0000"
app:tabSelectedTextColor="#FF0000"
app:tabTextAppearance="#style/TabTextAppearance"
app:tabTextColor="#FFFFFF">
<com.google.android.material.tabs.TabItem
android:id="#+id/tab_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 1" />
<com.google.android.material.tabs.TabItem
android:id="#+id/tab_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 2" />
I've tried using a drawable (selector) as advised here: https://stackoverflow.com/a/33230289/12221284
I've tried using a custom style extending the TabLayout style.
I've tried setting the XML attributes in my layout file directly.
Maybe I did something wrong, but nothing has worked so far and I don't understand why.
This question already has answers here:
How can I disable click on TabLayout in Android
(7 answers)
Closed 2 years ago.
I have tabs inside TabLayout and sometimes I need disable some tab, for example tab1 (I don´t need disable all of them, only some of them). Disable means it is not possible tap on it. How can I do that?
<com.google.android.material.tabs.TabLayout
android:id="#+id/switchLayout"
android:layout_width="match_parent">
<com.google.android.material.tabs.TabItem
android:id="#+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#layout/custom" />
<com.google.android.material.tabs.TabItem
android:id="#+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#layout/custom" />
<com.google.android.material.tabs.TabItem
android:id="#+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#layout/custom" />
</com.google.android.material.tabs.TabLayout>
Try this if it works:
touchableList = tabLayout?.touchables
touchableList?.forEach { it.isEnabled = false }
Or you could refer to this:
How can I disable click on TabLayout in Android
I am trying to copy a previously worked version of a tab layout, however since the library dependencies changed, I get unexpected errors. In my xml, I have:
<com.google.android.material.tabs.TabLayout
android:id="#id/tabLayout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/nextButton"
android:layout_marginEnd="15dp"
android:layout_marginStart="20dp"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1st" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2nd" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3rd" />
</com.google.android.material.tabs.TabLayout>
and this is inside a constraint layout. When I try to find this view by id:
TabLayout tabLayout = findViewById(R.id.tabLayout);
I get the error of WrongViewCast. import com.google.android.material.tabs.TabLayout; I import the correct library. What is the correct way of calling findViewById in TabLayout context?
Change android:id="#id/tabLayout" to android:id="#+id/tabLayout" . . .