In my current project I have about 10 buttons that are almost the same. I'm looking for a way to reuse the code for these buttons.
I've tried to move part of code to separate layout and reuse it via <include... layout=...>, but no success.
I use binding in the project and the compiler does not allow me to cast this View to TextView for calling it from the code. I've tried to move part of this code and set it as a style. No way :-( I have a few selectors here (for different states including enabled and disabled). However, they don't work.
<TextView
android:id="#+id/tv_next_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="40dp"
style="#style/Myfont_Bold"
android:layout_gravity="center_horizontal"
android:background="#drawable/selector_button_next_bg"
android:drawablePadding="36dp"
android:letterSpacing="-0.01"
android:lineSpacingExtra="3sp"
android:padding="#dimen/padding_15"
android:text="#string/next"
android:textColor="#drawable/selector_button_next_font"
android:textSize="#dimen/text_16"
app:drawableEndCompat="#drawable/selector_button_next_arrow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/ll_switchers"/>
Any ideas?
I don't want to copy-paste this code 10 times.
Create a style for reused styled Views and apply it in every layout it used
<style name="MyCustomStyle" parent="DesiredParentStyleFromLibrary">
<item name="attribute1">value</item>
<item name="attribute2">value</item>
<item name="attribute2">value</item>
// and so on
</style>
Related
I have a checkBox and set gravity center but the box doesn't come center
this is my XML file
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:checked="false"
android:clickable="false"
app:buttonTint="#color/colorPrimary"
android:scaleX="1.4"
android:scaleY="1.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
CheckBox image:
I myself solved this problem with style
first set the style attribute
android:theme="#style/checkBoxStyle"
Create a style
<style name="checkBoxStyle" parent="Base.Theme.AppCompat">
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:textColorSecondary">#color/colorPrimary</item>
</style>
ConstraintLayout Chains
Android developers recently released a new version of ConstraintLayout (1.0.2 as of today). This version contains a new major feature - Chains, which allows us to group views in ConstraintLayout.
Chains provide group-like behavior in a single axis (horizontally or vertically).
A set of widgets are considered a chain if they a linked together via a bi-directional connection
Once a chain is created, there are two possibilities:
Spread the elements in the available space
A chain can also be "packed", in that case the elements are grouped together
Maybe I'm mistaken, but I tried the same with radio buttons and it only centered the text associated with the radiobutton. But I think you might find your answer over here: https://stackoverflow.com/questions/47386980/align-checkbox-to-center-of-the-listview-item/47387075#:~:text=Create%20a%20LinearLayout%20as%20PARENT.&text=If%20you%20check%20the%20device,It%20should%20work.
I have a chip with 100dp of width but the text is not centered how I can center the text.
I use androidx with material library, I've tried put android:textAlignment="center" and android:gravity="center" but not work
<com.google.android.material.chip.Chip
android:id="#+id/chip"
style="#style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="7:00" />
I have this
I want this
just now I faced with the same problem, and I solved it by set a chip property: android: textAlignment = "center". I tested your example and it works fine too, here the code that I tested:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.chip.Chip
android:id="#+id/chip"
style="#style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="7:00"
android:textAlignment="center"/>
</FrameLayout>
Also make sure that you don't set or change a chip's text alignment somewhere in your code.
The short answer:
Chips aren't meant to be used the way you are trying to use them. They are supposed to wrap your content. Therefore there isn't a clean way to align the text in the center.
There is a workaround tho, you can use Chip_textEndPadding and Chip_textStartPadding attributes, which will be kinda awkward I guess.
I don't really know what you are trying to achieve, I mean, what is your why? Is it a button? Is it suppose just to show some text?
Please describe the feature, or at least, part of it.
Anyway:
According to the material design guidelines
Chips allow users to enter information, make selections, filter content, or trigger actions. Chips should appear dynamically as a group of multiple interactive elements. Unlike buttons, which should be a consistent and familiar call to action, one that a user expects to appear as the same action in the same general area.
Does your feature as anything to do with this?
In case you want a clickable, circular component you can simply use material button.
There is a similar question that was asked at github.
as others said you can use textAlignment ...but i wanted to tell you that if your using a custom font it wont be perfectly vertically aligned. you can check here for explanation.
so i would make a custom style that inherits from chip styles and set the font padding for usage like this:
<style name="customStyle" parent="#style/Widget.MaterialComponents.Chip.Choice">
<item name="chipBackgroundColor">#color/white</item>
******* <item name="android:includeFontPadding">true</item> *************
</style>
then for the text appearance you can make another style:
<style name="CustomChipTextAppearance" parent="TextAppearance.MaterialComponents.Chip">
<item name="android:fontFamily">?attr/myFont</item>
<item name="android:textAlignment">center</item>
</style>
dont forget to force a bridge theme in xml:
<com.google.android.material.chip.Chip 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"
style="#style/customStyle"
**** android:theme="#style/Theme.MaterialComponents.Bridge" ****
***** android:textAppearance="#style/CustomChipTextAppearance" *******
app:chipMinHeight="38dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:chipStrokeWidth="2dp"
app:rippleColor="#android:color/transparent"
tools:chipText="my chip" />
Use isTextAlignmentResolved,
For example, chipname.isTextAlignmentResolved() to do this programmatically.
I have this button:
<Button
android:id="#+id/btn_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:theme="#style/AppTheme.Button.Toolbar"/>
And this style:
<style name="AppTheme.Button.Toolbar" parent="Widget.AppCompat.Button.Borderless">
<item name="colorButtonNormal">#color/main</item>
<item name="android:textColor">#color/secondary</item>
</style>
Even though the style inherits from Widget.AppCompat.Button.Borderless, the button still has a border.
Changing Button to android.support.v7.widget.AppCompatButton did not help.
How to remove the border then?
Edit:
Setting background of the button is not an option - by doing so the animation of ripple effect is lost.
Edit 2:
Things become even more interesting.
Tried to change android:theme to style as #cadet suggested.
When button is defined this way:
<Button
android:id="#+id/btn_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:theme="#style/ToolbarButton"/>
That's what I get:
The colors apply, but there is distinct border.
If I just change theme to style:
<Button
android:id="#+id/btn_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
style="#style/ToolbarButton"/>
I get this:
There is no border, and the style is applied only partially (text is colored, button is not)
Edit 3:
Friends, I'm looking for a way to get borderless, styled button with ripple effects using styling approach. Hacking each and every button separately in layout files might work, but that's not the point.
Try this, hope out of this one may help you
<Button
android:id="#+id/btn_photo_lib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startPhotoLibAction"
android:src="#drawable/library_blau_2"
style="?android:attr/borderlessButtonStyle"/>
or
android:background="#null"
set background #null. or set own created background
android:background="#null"
You can use a different View instead of Button
<LinearLayout
android:id="#+id/btn_action_alternative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:clickable="true" />
I found a better solution, you'll wanna create a custom drawable and depending on the min version your app supports, you'll need to create two, one for Android versions pre-21(Lollipop) and another for post 21(Lollipop). The two files will need to be named identically so Android can find them and match them appropriatly based on the API level. But in the file drawable file for API 21 and above your file should look like such:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:drawable="#drawable/button_normal"/>
</ripple>
This Drawable file is wrapping another Drawable that is your preferred background image or color with a ripple whose color is defined using "?android:colorControlHighlight", which is simple a reference to a default color from what ever theme the current activity is using.
If you need to support pre-21(Lollipop), your drawable file would simply be a selector, with the preferred drawable. Your preferred drawable should be the same background color, or even a transparent color to make sure you can see your parent layouts background color. Similar to this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_normal"/>
</selector>
You can combine this with a style in order to apply the borderless style to all buttons it to all buttons in a layout... I recommend you use a transparent drawable so you can use this style with all buttons regardless if their parent has a different color background. This will prevent you from making several themes with different backgrounds.
To handle versioning support, or even config support if you'd like custom drawables based on various device configurations, you would just create several drawable folders with a configuration specific suffix. So, for example, drawables only for version 21 and above you'd create a folder called 'drawable-21'.
I found a website that better explains what I'm talking about.
Background
I'm adding a theme chooser of "Holo" and "Holo Light" (and maybe the dark action bar too) for my app "App Manager". For now I first try them out, via XML files.
The problem
I'm using the support library by Google and try out the themes, yet no matter what attributes and themes I use, the textViews' color on my listView have the wrong color.
The listView contains 2 TextViews that are defined as such:
<TextView
android:id="#+id/appLabelTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/isSystemAppImageView"
android:layout_toRightOf="#+id/appIconImageView"
android:ellipsize="marquee"
android:text="label"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/appDescriptionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/appLabelTextView"
android:layout_below="#+id/appLabelTextView"
android:layout_toLeftOf="#+id/isSystemAppImageView"
android:ellipsize="marquee"
android:text="description"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:ignore="HardcodedText" />
Theme.AppCompat.Light and Theme.AppCompat:
Same happens when I do it all programmatically, and when I use ActionBarSherlock library.
Here's the themes configurations I use (tried ActionBarSherlock this time, but the same occurs for the normal support library) :
<style name="BaseTheme" parent="#style/Theme.Sherlock.Light"></style>
<style name="AppTheme" parent="#style/BaseTheme">
<item name="android:listSeparatorTextViewStyle">#style/PreferenceListHeader</item>
<item name="android:windowBackground">#color/normal_window_background</item>
</style>
and the manifest:
<application
android:name="com.lb.app_manager.utils.App"
android:allowBackup="true"
android:description="#string/app_description"
android:hardwareAccelerated="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
The activity I've shown here doesn't have any code or XML that sets the theme to be different from what is written above.
What I've tried
I've tried to check out the samples and saw no apparent difference between the XMLs and code. They work just fine...
Also, for some reason, the preferences activities are shown just fine.
Same issue exists on ActionBarShelock library.
The question
How could it be ? what should be done to fix it?
I've found the cause to this problem.
It wasn't related to any XML or setting styles .
It was because of the LayourInflater of the adapter. I've created it via the application context instead of the one of the activity.
It also explains why my tests on sample projects worked fine, as I've checked the wrong stuff...
In Holo light theme and other Light themes we need to set Text color the only it will work..
<TextView
android:id="#+id/appLabelTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/isSystemAppImageView"
android:layout_toRightOf="#+id/appIconImageView"
android:ellipsize="marquee"
android:textcolor="#000"
android:text="label"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/appDescriptionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/appLabelTextView"
android:layout_below="#+id/appLabelTextView"
android:layout_toLeftOf="#+id/isSystemAppImageView"
android:ellipsize="marquee"
android:textcolor="#000"
android:text="description"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:ignore="HardcodedText" />
Hope this will work.. Try this and let me know the result.
I tried the sample you provided with Theme.AppCompat and Theme.AppCompat.Light, and they both work correctly as expected, on both API level 10 and 18. Since you didn't provide full code, only snippets, it must be the case that there are additional bits of XML or code in your application somewhere that cause the problem.
A couple of things for you to check:
Remove these entries from your AppTheme definition to make sure they are not interfering and causing the problem:
<item name="android:listSeparatorTextViewStyle">#style/PreferenceListHeader</item>
<item name="android:windowBackground">#color/normal_window_background</item>
You may have different style definitions for different API levels. Check the contents of res/values-v11/styles.xml, res/values-v14/styles.xml etc. to make sure they are not interfering
I have a custom EditText declared in an XML file and I'm including it like so:
<include layout="#layout/my_edit_text"
android:id="#+id/passwordField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/passwordHint"
android:inputType="textPassword" />
and here is my_edit_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:textColor="#color/gray"
android:textSize="#dimen/editTextFontSize"
android:padding="#dimen/editTextPadding"
android:background="#drawable/edit_text_background"
android:ellipsize="end" />
However, I can't set the hint or inputType this way, for some reason. If I set it in my_edit_text.xml, it works fine, but I would like to be able to set each reference individually.
The reason that I have the custom EditText is to avoid having to rewrite all of the common values in every one of my EditTexts.
Do I have to do something similar to what this person has? If I do, will I need to actually build a .java subclass and extract the attributes that way? That just seems excessive.
The reason that I have the custom EditText is to avoid having to rewrite all of the common values in every one of my EditTexts.
Step #1: Delete my_edit_text.xml.
Step #2: Delete all references to my_edit_text.xml.
Step #3: Define a style resource (e.g., in res/values/styles.xml) akin to:
<style name="rileyText">
<item name="android:textColor">#color/gray</item>
<item name="android:textSize">#dimen/editTextFontSize</item>
<item name="android:padding">#dimen/editTextPadding</item>
<item name="android:background">#drawable/edit_text_background</item>
<item name="android:ellipsize">end</item>
</style>
Step #4: Add style="#style/rileyText" to all EditText widgets that you want to have those particular attributes applied to.