I am trying to create a simple layout that would look similar to a TabLayout but I don't need to have actual tabs. It would just be acting like a plain button.
I am not sure if this should be done via background drawable (to handle change of state and color of line) or via just views.
Any suggestions on this?
You can make separate buttons / relative / image layouts as buttons. And change fragments in frameLayout. I can't say it is good practice but at the same time I don't know your situation. But Im sure it will work and you can easy custom each part of the process and view.
Use a customized TabLayout
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="40dp" // select your height
app:tabBackground="#drawable/buttons_selector" // this to customize tabs
app:tabSelectedTextColor="#color/your_tab_text_color"
app:tabTextAppearance="#style/TabTextAppearance"
app:tabIndicatorHeight="0dp" // hide the indicator line
app:tabGravity="fill" // or center
app:tabMode="fixed" // or scrollable
>
drawable buttons_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- drawable for selected tab -->
<item
android:drawable="#drawable/button_selected"
android:state_selected="true"/>
<!-- drawable for unselected tab -->
<item
android:drawable="#drawable/button_not_selected"
android:state_selected="false"/>
</selector>
drawable button_not_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- unselected tab background -->
<solid
android:color="#android:color/transparent" />
drawable button_selected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp"
android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" />
<!-- color of the selected tab -->
<solid
android:color="#color/your_color"/>
</shape>
You can play with colours, radius, etc.
Related
I am trying to create an ImageButton able to change the icon color after pressing. I want to do that in my XML file. I have the following code:
...
<ImageButton
android:id="#+id/stop_button"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="#drawable/stop_icon"
android:background="#drawable/stop_button"
android:tint="#color/ship_cove"
app:layout_constraintBottom_toTopOf="#id/bottom_icon_guideline"
app:layout_constraintLeft_toRightOf="#id/start_icon_guideline"
app:layout_constraintRight_toLeftOf="#id/end_icon_guideline"
app:layout_constraintTop_toBottomOf="#id/mediatop_icon_guideline" />
And here is my #drawable/stop_button.xml file content:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/cornflower_blue">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/oxford_blue" />
<corners android:radius="8dp" />
</shape>
</item>
</ripple>
As I stated before, my purpose is to change my stop_icon tint after pressing my stop_button, but I do not know how I can do that. Can anyone help me?
Note: if my button is not pressed, I want my stop_icon with the color #color/ship_cove. If it is, I want the color #color/mirage.
Instead of color create a color selector with a pressed state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="pressed_color" android:state_pressed="true" />
<item android:color="default_color" />
</selector>
And use this selector for the color of the icon. The rest should be taken by the system.
How do I change the color of the selected tab? I would like to each tab to have its own color attribute. So tab 1 selected would be Red. Tab 2 selected would be Blue. Tab 3 selected is Yellow. When not selected they go back to the tab original color.
Currently, I am using a selector to change the background of the selected tab. However, that only allows for one color. I would like to have multiple colors
This is the unselected_tab.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/colorPrimaryDark" />
</shape>
This is the selected_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/tabSelectedColor" />
</shape>
This is the selector I am using
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="50"
android:exitFadeDuration="50" >
<item android:drawable="#drawable/tab_selected"
android:state_selected="false" />
<item android:drawable="#drawable/tab_unselected"
android:state_selected="true"/>
</selector>
And I am applying it to the tablayout background
<com.google.android.material.tabs.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
android:elevation="5dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="#android:color/white"
app:tabMode="scrollable"
app:tabMaxWidth="100dp"
app:tabBackground="#drawable/tab_selector"
/>
Below here are some screen shots of how it looks currently.
Ideally, I would like for each tab to have its own separate color.
How can accomplish this?
EDIT: IDEALLY I WOULD LIKE TO CHANGE EACH TAB COLOR PROGRAMATICALLY
For anyone who is looking for a solution to this. I was able to find one. You can apply the color to the layout of the child of the tabLayout. For some reference code this is what I used a worked really well for me
final LinearLayout tabsContainerLayout = (LinearLayout)expenseTabs.getChildAt(0);
Linear LayouttempLinearLayout = (LinearLayout)tabsContainerLayout.getChildAt(selectedTabPosition);
tempLinearLayout.setBackgroundColor(Color.RED);
I have a question which I can't find an answer to and really have to solve.
I am trying to change the background for the tab layout indicator but JUST FOR IT, i.e. if the entire tablayout is green and the tablayoutindicator height is 4dp I want the ENTIRE bottom 4dp of the tablayoutview will be in another color (lets say red) is that possible?
Before you answer please note I'm not talking about app:tabIndicatorColor property but on the entire height of the tabIndicator (regardless of the tabIndicator itself).
For example, on the attached image the app:tabIndicatorColor is white but the background of it is black while the entire view is a gardient of blue.
Edit -
I managed to do it using a FrameLayout having the bottom view in black while the tablayout is above it with its properties, I'm still looking for a more "clean" way to achieve the same thing
To achieve what you are looking for you can try the following
Create a selector tab_layout_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:state_pressed="false">
<layer-list>
<!-- Stripe bottom indicator color -->
<item android:top="-5dp" android:left="-5dp" android:right="-5dp">
<shape android:shape="rectangle">
<stroke android:color="#color/red" android:width="4dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
Finally, in your TabLayout add
<android.support.design.widget.TabLayout
<!-- Whole tab background -->
android:background="#color/white"
<!-- Line color -->
android:tabBackground="#drawable/tab_layout_selector"
.... Others....
/>
I have created custom seekbar to make it work according to my need. There are two questions related to it.
Before it let me show you my code for seekbar
In android xml
<com.abc.projectname.customlayout.CustomHorizontalSeekBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:max="7650"
android:paddingLeft="30dp" // Padding given to show thumbnail properly
android:paddingRight="30dp"
android:clickable="false"
android:paddingTop="20dp"
android:tag="drawable/seek_bar_layout_two"
android:thumb="#drawable/dial" />
CustomHorizontalSeekBar Is a class which I have created to get the tag from seekbar and set progress drawable value as whatever tag have supplied to support multiple theme
seek_bar_layout_two xml file
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+android:id/background"
android:drawable="#drawable/deselect">
</item>
<item
android:id="#+android:id/progress"
android:top="20dp"
android:bottom="20dp"
android:drawable="#drawable/lt_grey_seek_bar">
</item>
<!-- android:drawable="#drawable/loadingbar"> -->
</layer-list>
Here deselect is and highlighted image which I want to show as background of seekbar permanently.
lt_grey_seek_bar xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal"
android:gravity="left" >
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke
android:width="1dp"
android:color="#color/lt_grey_border" />
<gradient
android:endColor="#color/lt_grey_end"
android:startColor="#color/lt_grey_start" />
</shape>
</clip>
Questions
1) Can I give same background image for custom seekbar for enable and disable state? If yes than how? It is a special need from my client and it is not necessary to show disable state.
What I have tried:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/seek_bar_layout_two xml" android:state_enabled="false"/>
<item android:drawable="#drawable/seek_bar_layout_two xml" android:state_pressed="true"/>
<item android:drawable="#drawable/seek_bar_layout_two xml" android:state_selected="true"/>
<item android:drawable="#drawable/seek_bar_layout_two xml"/>
</selector>
But Still no success.
2) As you can see in my seekbar implementation I have given padding to show proper thumbnail. But when I click outside the drawn state of seekbar where this padding is available seekbar automatically turns into non highlighted form. How should I stop it?
Seekbar and its touch area by which it gets non highlight
Non highlight seekbar look
I have been trying lots of ways to make it work successfully but unable to do so. Can anyone provide me a solution.
This is what I have done in my custom seekbar class
Rect bounds = getProgressDrawable().getBounds();
setProgressDrawable(getResources().getDrawable(idByTag));
getProgressDrawable().setBounds(bounds);
I am stuck in creating android top bar tabs and the search bar, as the following attached image.
I have already created simple tabs with text but the line of the selected is shown downside. Now the problem is to show the icons with and the selection line should be shown upside ? How would I do that ?
You could use a custom view containing a linear layout and an image . I am using a Textview and in the linear layout background attribute , i have added a drawable which is a selector for selected tab:-
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/tabsText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/tab_text_bg"
android:gravity="center"
android:paddingBottom="#dimen/dashboardtabpadding"
android:paddingTop="#dimen/dashboardtabpadding"
android:shadowColor="#android:color/white"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.5"
android:textColor="#color/dashboard_tab_selector"
android:textSize="#dimen/dashboardtab_heading_Text"
android:textStyle="bold" />
tab_bg_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item
android:state_selected="true"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/tab_bg_selected"
/>
<!-- Inactive tab -->
<item
android:state_selected="false"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/tab_bg_unselected"
/>
</selector>
and for that green part, you can use a layer list for selected and unselected tab. You can google on layer list. You can use below for selected tab :-
tab_bg_selected.xml
<!-- "background shadow" -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#C1C1C1" />
</shape>
</item>
<!-- background color -->
<item
android:top="5px">
<shape android:shape="rectangle" >
<solid android:color="#202222" />
</shape>
</item>
and in your main activity, you can inflate the above layout and use it in the setIndicator(View view) method.
I hope you got it.