How to disable click on TabItem inside TabLayout? [duplicate] - android

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

Related

How to arrange tabitems of tabLaout in multiple rows in fixed mode?

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.

How to change the TabLayout background color using XML?

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.

Android : setting visibilty of a tab in a tablayout not working

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

How to set toolbar title and subtitle in centre [duplicate]

This question already has answers here:
Is there an official API for centered title on Toolbar like on popular Android apps, in the new material design?
(5 answers)
Closed 3 years ago.
In my case I want to set toolbar title and subtitle in centre. I have already one solution to set custom layout in toolbar. Is that any other solution to set toolbar title and subtitle without using custom layout?
Solution 1:
<android.support.v7.widget.Toolbar
android:id="#+id/custom_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#android:color/holo_red_dark">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:textColor="#android:color/white"
android:textSize="20sp"
android:layout_marginRight="?android:attr/actionBarSize"
android:gravity="center"/>
</android.support.v7.widget.Toolbar>
I don't want to use custom layout to set toolbar title and subtitle.I appreciate your effort, in advance.
<android.support.v7.widget.Toolbar
android:id="#+id/custom_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#android:color/holo_red_dark">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:textColor="#android:color/white"
android:textSize="20sp"
android:layout_marginRight="?android:attr/actionBarSize"
android:gravity="center"/>
</android.support.v7.widget.Toolbar>

Customization Search View Toolbar Android [duplicate]

This question already has answers here:
Searchview with back button
(2 answers)
Android SearchView customisation
(4 answers)
Custom search implementation with SearchView
(2 answers)
Android ActionBar Customize Search View
(4 answers)
Closed 4 years ago.
I want to make custom search bar layout in android. I have to attach screenshot of my design what I want to require. Check action bar design. On click action-bar search icon open custom edit-text in toolbar.
I want to make action bar layout like this.
Try this way make sure you have search icon..
<LinearLayout
android:id="#+id/llSearchContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/_5sdp"
android:background="#drawable/drawer_gredient"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="#dimen/_10sdp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.80"
android:background="#drawable/edittext_search_box"
android:gravity="center_vertical"
android:paddingBottom="#dimen/_2sdp"
android:paddingLeft="#dimen/_5sdp"
android:paddingRight="#dimen/_5sdp"
android:paddingTop="#dimen/_2sdp">
<ImageView
android:layout_width="#dimen/_24sdp"
android:layout_height="#dimen/_24sdp"
android:padding="#dimen/_5sdp"
android:scaleType="centerCrop"
android:src="#drawable/ic_seach" />
<EditText
android:id="#+id/lsfEtSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_10sdp"
android:layout_weight="0.80"
android:background="#android:color/transparent"
android:drawablePadding="#dimen/_10sdp"
android:hint="Search"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLength="50"
android:maxLines="1"
android:textSize="#dimen/_14sdp" />
</LinearLayout>
</LinearLayout>

Categories

Resources