how to add line above tabs android tablayout - android

need help
I have searched for my requirement but didn't found so posting my own question.
I need to show line above tabs not below as all solution is for adding the line below the tabs text.
should I use custom tabs or android supports this scenario without making custom ??
links I have viewed are as follows
https://www.google.com.pk/search?q=fabric+documentation&rlz=1C1CHBD_enPK741PK753&oq=fabric+documentation+&aqs=chrome..69i57.5669j0j7&sourceid=chrome&ie=UTF-8#safe=active&q=how+to+adding+line+above+tabs+android
no code is posted as I want to know what should be used .. ur suggestion would be helpful .. thanks

Try this create a background in res/drawable folder and set app:tabBackground="#drawable/selector"
make your like this Tablayout
<android.support.design.widget.TabLayout
android:id="#+id/detail_tabs"
android:layout_width="match_parent"
app:tabSelectedTextColor="#color/colorPrimary"
app:tabIndicatorHeight="0dp"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabBackground="#drawable/selector" />
Here selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="3dp" android:left="-5dp" android:right="-5dp" android:bottom="-5dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorWhite"/>
<stroke android:color="#color/colorPrimary" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
<item android:state_selected="true" android:state_pressed="true">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="3dp" android:left="-5dp" android:right="-5dp" android:bottom="-5dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorWhite"/>
<stroke android:color="#color/colorPrimary" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
Hope it will works if not then add comment.

app:tabIndicatorGravity="top"
Its very simple now to show the indicator at the top with this.

Related

Recreate switches similar to Android 12 settings

Android 12's settings' switches now look like this,
which its code is here:
https://android.googlesource.com/platform/packages/apps/Settings/+/c4cc279a2a32e6b5dfac9af4a16a4d98def82a22/res/xml/configure_notification_settings.xml#133
but neither androidx.preference.SwitchPreferenceCompat nor androidx.preference.SwitchPreference look like this in my app with updated material library (1.5.0, Material 3),
How can I have the same looking (and feeling, animation, etc) switches at least in Android 12 or how Android is doing it for itself? Thanks!
You can easily customize your switches to look like these.
In styles.xml add this :
<style name="App.Switches" parent="Widget.Material3.CompoundButton.Switch">
<item name="track">#drawable/switch_track</item>
<item name="android:thumb">#drawable/switch_thumb</item>
</style>
switch_track.xml :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#000000" />
<corners android:radius="56dp" />
<size
android:width="64dp"
android:height="28dp" />
</shape>
</item>
</layer-list>
switch_thumb.xml :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp">
<shape android:shape="oval">
<solid android:color="#000000" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
</item>
</layer-list>
In the end add this line in your app's main theme to apply this style to all the switches in your app :
<item name="switchStyle">#style/App.Switches</item>
As Material 1.7.0 the suggestion is to use MaterialSwitch which more or less is like this by default,

How to set padding for selection tab in TabLayout on Android

In my application I should TabLayout and ViewPager.
I want when selected Item set padding for tabIndicatorColor in tabLayout.
By default show me such as this image :
Bu t I want when selected item, show me such as this :
I want set Padding for tabIndicatorColor.
My TabLayout XML code:
<android.support.design.widget.TabLayout
android:id="#+id/serialDetail_tabLayout"
android:layout_width="match_parent"
android:layout_height="#dimen/size50"
android:layout_gravity="bottom"
android:background="#color/colorPrimary"
app:tabIndicatorColor="#color/colorAccent"
app:tabSelectedTextColor="#color/colorAccent"
app:tabTextAppearance="#style/allCapsTabLayout"
app:tabTextColor="#color/white" />
How can I it? please help me. Thanks all <3
Yes it is possible without custom view and external library..
you may create one selector file
tab_indicator_line.xml add into drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-5dp" android:left="-5dp" android:right="-5dp" android:bottom="5dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<stroke android:color="#color/colorPrimary" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
<item android:state_selected="true" android:state_pressed="true">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-5dp" android:left="-5dp" android:right="-5dp" android:bottom="5dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<stroke android:color="#color/colorPrimary" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
Now apply in tabLayout using
app:tabBackground="#drawable/tab_indicator_line"
app:tabIndicatorHeight="0dp"
Tell me if any query..
There are two easy ways to do this. Either you use a custom layout for tab or just add a view below the tab of same color.The second option is more easier.

How to change the textColor if button press Android

I have a specific need to texcolorlist put the drawable with the background button.
My textColor:
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#android:color/black" />
<item android:state_checked="true" android:color="#android:color/black" />
<item android:color="#8e000000"/>
</selector>
My Button State:
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#005b7f"
android:startColor="#051725"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#e6e6e6"
android:startColor="#b3b3b3"/>
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
</selector>
I know that in this way I can put the button color: android:textColor="#drawable/button_text_color"
I wonder if the button text color can be put directly into My Button State.
This is something, but it does not work:
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle"
android:textColor="#000000">
<gradient
android:angle="90"
android:endColor="#e6e6e6"
android:startColor="#b3b3b3"/>
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
</selector>
You need to define selector also to the text color:
android:textColor="#drawable/yourselector"
Here example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#color/blue" />
<item android:state_focused="true" android:state_pressed="true" android:color="#color/red" />
<item android:state_focused="false" android:state_pressed="true" android:color="#color/green" />
</selector>
Edit: I didn't saw I refereed to that possibility. I don't think what you want is possible, because what you define in your selector refer to the android attribute you assign it. Since the attribute is background there is no meaning to text color there.
I wonder if the button text color can be put directly into My Button State.
No, it cannot. Drawable XML resources can not set properties on the elements they are applied to. What you might be interested in, is using a Style.
values/styles.xml
<style name="MyButtonStyle">
<item name="android:textColor">#drawable/button_text_color</item>
<item name="android:background">#drawable/button_background</item>
</style>
MyLayout.xml
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/MyButtonStyle"/>
This question was answered before:
Android customized button; changing text color
Hope it will help.

How Do Create This Type Of CheckBox In Android

How to create this type of checkbox in android when it pressed. I tried . May I know what is the correct way to achieve my objective?Maybe this question too basic, but i did't find any suitable solution.Please Help me out.
I want to create Like this
When it unchecked it will looks like :
Here is my XML code for checkbox:
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:button="#drawable/custom_checkbox"/>
Here is custom_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/checked" />
<item android:state_pressed="true"
android:drawable="#drawable/checked" />
<item android:drawable="#drawable/unchecked" />
</selector>
Here is checked.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#54d66A" android:endColor="#54d66A" android:angle="270"/>
<stroke android:width="4px" android:color="#ffc0c0c0" />
<size android:height="20dp" android:width="20dp"/>
</shape>
Here is my output:
You need to create a drawable with layer-list with two items as rectangle shape . one for the background and other as stroke with some margin.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient android:startColor="#54d66A" android:endColor="#54d66A" android:angle="270"/>
<size android:height="20dp" android:width="20dp"/>
</shape>
</item>
<item android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp">
<shape android:shape="rectangle" >
<stroke android:width="2dp" android:color="#ffc0c0c0" />
</shape>
</item>
</layer-list>
And , set this drawable as state_checked background.

How to create custom button in Android using XML Styles

I want to make this kind of button [same background & text] colors by using XML Styles
that's just for an example, i want to write some other texts, like: About Me
Still i am using button created by designer in Photoshop
<ImageButton
android:id="#+id/imageButton5"
android:contentDescription="AboutUs"
android:layout_width="wrap_content"
android:layout_marginTop="8dp"
android:layout_height="wrap_content"
android:layout_below="#+id/view_pager"
android:layout_centerHorizontal="true"
android:background="#drawable/aboutus" />
Note: I need this kind of button in every size and shape
I don't want to use any image in my Android App i want to make it using XML only
Copy-pasted from a recipe written by "Adrián Santalla" on androidcookbook.com:
https://www.androidcookbook.com/Recipe.seam?recipeId=3307
1. Create an XML file that represents the button states
Create an xml into drawable called 'button.xml' to name the button states:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/button_disabled" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/button_focused" />
<item
android:state_enabled="true"
android:drawable="#drawable/button_enabled" />
</selector>
2. Create an XML file that represents each button state
Create one xml file for each of the four button states. All of them should be under drawables folder. Let's follow the names set in the button.xml file.
button_enabled.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#00CCFF"
android:centerColor="#0000CC"
android:endColor="#00CCFF"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
button_focused.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#F7D358"
android:centerColor="#DF7401"
android:endColor="#F7D358"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
button_pressed.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#0000CC"
android:centerColor="#00CCFF"
android:endColor="#0000CC"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
button_disabled.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#F2F2F2"
android:centerColor="#A4A4A4"
android:endColor="#F2F2F2"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
3. Create an XML file that represents the button style
Once you have created the files mentioned above, it's time to create your application button style. Now, you need to create a new XML file, called styles.xml (if you don't have it yet) where you can include more custom styles, into de values directory.
This file will contain the new button style of your application. You need to set your new button style features in it. Note that one of those features, the background of your new style, should be set with a reference to the button (button.xml) drawable that was created in the first step. To refer to the new button style we use the name attribute.
The example below show the content of the styles.xml file:
<resources>
<style name="button" parent="#android:style/Widget.Button">
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:textColor">#FFFFFFFF</item>
<item name="android:shadowColor">#FF000000</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">-1</item>
<item name="android:shadowRadius">0.2</item>
<item name="android:textSize">16dip</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#drawable/button</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
</resources>
4. Create an XML with your own custom application theme
Finally, you need to override the default Android button style. For that, you need to create a new XML file, called themes.xml (if you don't have it yet), into the values directory and override the default Android button style.
The example below show the content of the themes.xml:
<resources>
<style name="YourApplicationTheme" parent="android:style/Theme.NoTitleBar">
<item name="android:buttonStyle">#style/button</item>
</style>
</resources>
Hope you guys can have the same luck as I had with this, when I was looking for custom buttons. Enjoy.
Have you ever tried to create the background shape for any buttons?
Check this out below:
Below is the separated image from your image of a button.
Now, put that in your ImageButton for android:src "source" like so:
android:src="#drawable/twitter"
Now, just create shape of the ImageButton to have a black shader background.
android:background="#drawable/button_shape"
and the button_shape is the xml file in drawable resource:
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#505050"/>
<corners
android:radius="7dp" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp"
android:bottom="1dp"/>
<solid android:color="#505050"/>
</shape>
Just try to implement it with this. You might need to change the color value as per your requirement.
Let me know if it doesn't work.
Have a look at Styled Button it will surely help you.
There are lots examples please search on INTERNET.
eg:style
<style name="Widget.Button" parent="android:Widget">
<item name="android:background">#drawable/red_dot</item>
</style>
you can use your selector instead of red_dot
red_dot:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#f00"/>
<size android:width="55dip"
android:height="55dip"/>
</shape>
Button:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp"
style="#style/Widget.Button"
android:text="Button" />
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#ffffffff"/>
<size
android:width="#dimen/shape_circle_width"
android:height="#dimen/shape_circle_height"/>
</shape>
1.add this in your drawable
2.set as background to your button
<gradient android:startColor="#ffdd00"
android:endColor="#color/colorPrimary"
android:centerColor="#ffff" />
<corners android:radius="33dp"/>
<padding
android:bottom="7dp"
android:left="7dp"
android:right="7dp"
android:top="7dp"
/>
Two things you need to do, if you want to make a custom button design.
1st is:
create a xml resource file in drawable folder (Example: btn_shape_rectangle.xml)
then copy and paste the code there.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="16dp"
android:shape="rectangle">
<solid
android:color="#fff"/>
<stroke
android:width="1dp"
android:color="#000000"
/>
<corners android:radius="10dp" />
</shape>
2nd is go to your layout button where you want to implement this design. just link up it.
Example:
android:background="#drawable/btn_shape_rectangle"
You can change shape color radius what design you want can do.
Hope it will works and help you. Happy Coding

Categories

Resources