I want to arrange buttons evenly on the layout using styles to make my code simple. So why buttons have different distance between each other and ViewGroup? Am I right that attributes defined in the xml file overrides style's attributes?
// xml code for the activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
style="#style/style1"
android:id="#+id/b1"
android:text="button1"
app:layout_constraintBottom_toTopOf="#+id/b2" />
<Button
style="#style/style1"
android:id="#+id/b2"
android:text="button2"
app:layout_constraintTop_toBottomOf="#+id/b1"
app:layout_constraintBottom_toTopOf="#+id/b3" />
<Button
style="#style/style1"
android:id="#+id/b3"
android:text="button3"
app:layout_constraintTop_toBottomOf="#+id/b2"/>
</androidx.constraintlayout.widget.ConstraintLayout>
// used style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="style1">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">10dp</item>
<item name="android:textColor">#color/teal_200</item>
<item name="layout_constraintTop_toTopOf">parent</item>
<item name="layout_constraintLeft_toLeftOf">parent</item>
<item name="layout_constraintRight_toRightOf">parent</item>
<item name="layout_constraintBottom_toBottomOf">parent</item>
</style>
</resources>
What the three buttons have in common is layout_constraintEnd_toEndOf and layout_constraintStart_toStartOf.
So in style you will only define these two attributes of layout_constraint.
STYLE
<style name="style1" parent="Widget.AppCompat.Button.Colored">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">10sp</item>
<item name="android:textColor">#color/teal_200</item>
<!-- layout_constraint attributes -->
<item name="layout_constraintEnd_toEndOf">parent</item>
<item name="layout_constraintStart_toStartOf">parent</item>
</style>
your_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".YourActivity">
<Button
android:id="#+id/b1"
style="#style/style1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/b2"
/>
<Button
android:id="#+id/b2"
style="#style/style1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
app:layout_constraintTop_toBottomOf="#+id/b1"
app:layout_constraintBottom_toTopOf="#+id/b3"
/>
<Button
android:id="#+id/b3"
style="#style/style1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3"
app:layout_constraintTop_toBottomOf="#+id/b2"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
With this code, AndroidStudio will point out an error in the xml file, after all the ConstraintLayout was not made to define its attributes from style. But your app will run normally.
Don't forget to put your your ActivityClass in tools:context=".ActivityClass".
Tip: when you want to set a text size in any android xml, you should use "sp" instead of "dp".
Example
Incorrect:
<item name="android:textSize">10dp</item>
Correct
<item name="android:textSize">10sp</item>
Related
I have a group of radio buttons displaying fine in my XML design window, but in the emulator (and on my actual device that I installed the app on) it only shows the button text and not the button.
Here is the XML file for this activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Question1">
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp"
android:layout_marginEnd="25dp"
android:layout_marginRight="25dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<TextView
android:id="#+id/errorView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="#string/error_msg"
android:textColor="#android:color/holo_red_light"
android:visibility="invisible" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="#string/my_gender_is"
android:textSize="20sp"
android:textStyle="bold" />
<RadioGroup
android:id="#+id/gender_group"
android:layout_width="match_parent"
android:layout_height="100dp">
<RadioButton
android:id="#+id/radio_male"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="#string/male"
android:textColor="#636363"
android:textSize="18sp" />
<RadioButton
android:id="#+id/radio_female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/female"
android:textColor="#636363"
android:textSize="18sp" />
</RadioGroup>
</LinearLayout>
<Button
android:id="#+id/nextPage"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginBottom="15dp"
android:layout_weight="1"
android:background="#color/colorPrimary"
android:fontFamily="#font/segoeuil"
android:gravity="center"
android:includeFontPadding="false"
android:paddingTop="-9dp"
android:paddingBottom="5dp"
android:text="#string/next"
android:textAlignment="gravity"
android:textAllCaps="false"
android:textSize="26sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorBackground">#color/background</item>
</style>
<!-- No title bar theme -->
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:colorBackground">#color/background</item>
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:textColorPrimaryInverse">#color/primary_text_material_dark</item>
</style>
</resources>
It looks fine in the preview but on my device and in the emulator is seems to exclude the radio buttons.
You are using in a wrong way I guess use it as following:
First declare your custom styles in styles.xml file like this:
<style name="MyRaidoButton" parent="Widget.AppCompat.CompoundButton.RadioButton">
<item name="colorAccent">#0091ea</item>
<item name="android:textColorPrimaryDisableOnly">#f44336</item>
<item name="android:textAppearance">#style/TextAppearance.AppCompat</item>
</style>
Once the style is done now all you need is to assign that particular style to your radio button or group like the following:
<RadioButton
android:id="#+id/products"
. . .
android:theme="#style/MyRaidoButton"/>
For more info take a look at the following links:
http://www.zoftino.com/android-ui-control-radiobutton
How to customize default theme of radio button in android?
I added a style for a checkbox like below
<style name="SurveyCheckBox" >
<item name="colorControlNormal">#color/colorPrimaryLight</item>
<item name="colorControlActivated">#color/colorSecondary</item>
<item name="colorControlHighlight">#color/colorSecondaryDark</item>
</style>
It works perfectly but I also added this style for checkboxes created dynamically in a recycler view. In this case, style does not affect checkboxes. To solve this problem, I added "android:" before the item name.
<style name="SurveyCheckBox" >
<item name="android:colorControlNormal">#color/colorPrimaryLight</item>
<item name="android:colorControlActivated">#color/colorSecondary</item>
<item name="android:colorControlHighlight">#color/colorSecondaryDark</item>
</style>
This property works in API >=21 but does not work <21. Is there any solution for this problem?
recycler_view_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="#+id/survey_check_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:layout_weight="8"
android:textColor="#color/colorOnBackground"
android:textSize="16sp" />
<CheckBox
android:id="#+id/survey_check_box"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:theme="#style/SurveyCheckBox" /> </LinearLayout>
Have you tried with android.support.v7.widget.AppCompatCheckBox instead of CheckBox ?
I'm new to Stackoverflow but not that new to coding.
I have this simple flyer I made in Android Studio.
I'm trying to apply different styles to different TextViews.
The first two styles shown work on all TextViews but the third style named "address_style" won't apply for some reason.
The TextView goes away completely when I apply it as a style.
I've tried a couple of different things.
I just don't get why the third style won't work.
Can someone help me out?
<?xml version="1.0" encoding="UTF-8" ?>
<style name="Title">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#000000</item>
<item name="android:text">#string/Title</item>
<item name="android:textColor">#2196F3</item>
<item name="android:textSize">18sp</item>
</style>
<style name="textView">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#CCBBDEFB</item>
<item name="android:paddingTop">12dp</item>
<item name="android:textColor">#F44336</item>
<item name="android:textSize">15sp</item>
</style>
<style name="address_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:paddingTop">14dp</item>
<item name="android:text">#string/address</item>
<item name="android:background">#CCBBDEFB</item>
<item name="android:textColor">#000000</item>
<item name="android:textSize">16sp</item>
</style>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<?xml version="1.0" encoding ="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/soccer" />
<TextView
android:id="#+id/title"
style="#style/Title"
android:fontFamily="#font/hanalei_fill"
android:text="#string/Title" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:background="#000000"
android:fontFamily="#font/trade_winds"
android:paddingTop="10dp"
android:text="#string/textUnderTitle"
android:textColor="#03A9F4"
android:textSize="20sp" />
<TextView
android:id="#+id/textView3"
style="#style/textView"
android:layout_below="#id/textView2"
android:fontFamily="#font/carter_one"
android:text="#string/price" />
<TextView
android:id="#+id/textView4"
style="#style/textView"
android:layout_below="#id/textView3"
android:fontFamily="#font/carter_one"
android:text="#string/seasons" />
<TextView
android:id="#+id/textView5"
style="#style/textView"
android:layout_below="#id/textView4"
android:fontFamily="#font/carter_one"
android:text="#string/whatYouLearn" />
<TextView
android:id="#+id/textView6"
style="#style/textView"
android:layout_below="#id/textView5"
android:fontFamily="#font/carter_one"
android:text="#string/allAges" />
<TextView
style="#style/textView"
android:layout_above="#id/register"
android:fontFamily="#font/trade_winds"
android:text="#string/address" />
<TextView
android:id="#+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:autoLink="all"
android:background="#000000"
android:fontFamily="#font/carter_one"
android:gravity="center_horizontal"
android:text="#string/web_address"
android:textColor="#2196F3"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
So, I feel stupid. I finally saw the little red exclamation point in the red stop sign in my preview of the activity_main box. It told me one of the styles is not registering(address_style) and gave me an option to refresh the styles.xml page. And now it works. I didn't know that could cause an error because there were no problems with the other two styles I created.
I want to change my default Rating bar Star(*) icon to another icon.
Can anybody help me out..
My Code: XML
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
style="#style/foodRatingBar"
android:isIndicator="false"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"
android:numStars="4" />
Styles.xml:
<style name="foodRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/selector</item>
<item name="android:minHeight">23dip</item>
<item name="android:maxHeight">25dip</item>
</style>
Selector.xml:
<?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/ic_fuel_gray"/>
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/ic_fuel_gray"/>
<item
android:id="#android:id/progress"
android:drawable="#drawable/ic_fuel_color"/>
</layer-list>
But outPut is:
Just add below two snippets and you are done:
<style name="customRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/custom_image1</item>
<item name="android:indeterminateDrawable">#drawable/custom_image2</item>
</style>
and Use above style in your xml where rating bar is used:
<RatingBar style="#style/customRatingBar" ... />
Add replace your Rating bar code with this and check if it is showing in preview?
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/customRatingBar"
/>
<RatingBar
android:id="#+id/ratingBar"
style="#style/foodRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:numStars="5"
android:rating="5"
android:transformPivotX="0dp"
android:transformPivotY="0dp"
android:isIndicator="false"/>
try this
I need to change checkbox from
to
I found that to change color of tick mark you have to put selectors:
<?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_checked="false" android:drawable="#drawable/unchecked" />
</selector>
In xml:
<CheckBox
android:id="#+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/your_selector"/>
create xml file
coustom_check_box.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_checked="false" android:drawable="#drawable/unchecked" />
</selector>
In checkbox set android:button property to the custom_checkbox_design.xml drawable like
android:button="#drawable/coustom_check_box"
<CheckBox
android:id="#+id/checkBoxCustomized1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Coustom Checkbox"
android:checked="true"
android:button="#drawable/coustom_check_box"
android:textSize="20dp" />
icons
Create a drawable custom_cheackbox:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/checkbox_not_selected"android:state_checked="false"/>
<item android:drawable="#drawable/checkbox_selected" android:state_checked="true"/>
<item android:drawable="#drawable/checkbox_not_selected"/>
</selector>
add your custom background inside custom_checkbox android:button="#drawable/custom_cheackbox"
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector"
android:text="CheckBox"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/Black" />