Android check box button style for API < 21 - java

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 ?

Related

Sophisticated button layout

Im trying to create a large button in the center of the layout, with an image on the left,
with padding on all sides, followed by another two different textviews centered vertically
This is what im trying to achieve:
Im kind of new and any help would be greatly appreciated :)
This is how I do it currently:
<FrameLayout
android:id="#+id/buttonLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="#color/white">
<Button
android:id="#+id/requestButton"
style="#style/request_button"
android:drawableStart="#drawable/card_icon" />
<Button
android:id="#+id/requestTextView"
style="#style/request_title_text_view"
android:text="#string/cards_request"/>
<Button
android:id="#+id/bodyTextView"
style="#style/request_body_text_view"
android:text="#string/cards_request_card" />
</FrameLayout>
And styles:
<style name="request_title_text_view" parent="request_body_text_view">
<item name="android:textSize">26sp</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_marginTop">10dp</item>
</style>
<style name="request_body_text_view" parent="android:Widget.TextView">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#color/white</item>
<item name="android:textSize">18sp</item>
<item name="android:textAllCaps">true</item>
<item name="android:paddingRight">20dp</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:layout_marginTop">45dp</item>
<item name="android:layout_marginLeft">80dp</item>
<item name="android:elevation">10dp</item>
</style>
Its just a bad practice to put those margins there hard-coded and was wondering if there is another better way?
You can try this:
<RelativeLayout
<ConstraintLayout
<Button
<ImageView
<TextView
<TextView
</ConstraintLayout
</RelativeLayout
<RelativeLayout
android:id="#+id/requestButton"
android:layout_width="wrap_content"
android:layout_height="88dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="#drawable/button_background"
android:padding="15dp">
<ImageView
android:id="#+id/iconImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="15dp"
android:elevation="10dp"
android:src="#drawable/cr_icon" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#id/iconImageView">
<TextView
android:id="#+id/requestTextView"
style="#style/request_title_text_view"
android:text="#string/cr_request" />
<TextView
android:id="#+id/bodyTextView"
style="#style/request_body_text_view"
android:layout_below="#+id/requestTextView"
android:text="#string/cr_request_body" />
</RelativeLayout>
</RelativeLayout>

android tab layout text is hiding when text style is set to italic

//Here is the xml code
<com.google.android.material.tabs.TabLayout
android:id="#+id/inforStatusTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#android:color/white"
android:minHeight="?attr/actionBarSize"
app:tabGravity="fill"
android:layout_gravity="center_horizontal"
app:tabIndicator="#drawable/btn_corner3"
app:tabIndicatorColor="#color/materialBlue"
app:tabIndicatorHeight="3dp"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/materialBlue"
app:tabTextAppearance="#style/tab_text"
app:tabTextColor="#android:color/black">
<com.google.android.material.tabs.TabItem
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/information"
/>
<com.google.android.material.tabs.TabItem
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/approval"
/>
</com.google.android.material.tabs.TabLayout>
//Here is the style for tab layout
<style name="tab_text" parent="#android:style/TextAppearance.Widget.TabWidget">
<item name="android:fontFamily">#font/poppins_bold</item>
<item name="android:textSize">#dimen/_10sdp</item>
<item name="android:textStyle">italic</item>
<item name="android:textAllCaps">true</item>
<item name="android:singleLine">true</item>
</style>
I am implementing tab layout in my app the issue is when i try to set italic for tab text the text is not displaying properly.

Android RadioButton shows only text, not the actual button in device emulator

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?

How do we change Rating Bar Star icon with some other icon in Android?

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

Spinner color style in Android

I am making Spinner like below image. It's work fine as I want. But I want to change background color to #FFFFFF and textColor to #000000. But I got revise output...
java code:
Spinner staticSpinner = (Spinner)findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(this,R.array.request_role,android.R.layout.simple_spinner_item);
staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
xml code :
<Spinner
android:id="#+id/static_spinner"
android:layout_width="150dp"
android:layout_height="45dp"
android:layout_marginLeft="30dp"
style="#android:style/Widget.Holo.Light.DropDownItem" ></Spinner>
simple_spinner_dropdown_item.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#000000"
/>
simple_spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textColor="#000000"
android:textAlignment="inherit"/>
styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="colorPrimary">#0072BA</item>
<item name="colorPrimaryDark">#004F80</item>
<item name="colorAccent">#0072BA</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="colorControlHighlight">#FFFFFF</item>
<item name="android:actionBarSize">48dp</item>
<item name="actionBarSize">48dp</item>
<item name="android:windowActionBar">false</item>
</style>
</resources>
By using below code I can solve my problem.
<Spinner
android:id="#+id/static_spinner2"
android:layout_width="fill_parent"
android:layout_height="32dp"
android:layout_marginLeft="30dp"
android:background="#drawable/apptheme_spinner_background_holo_light"
android:popupBackground="#ffffff" />
Spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:background="#FFFFFF"
android:textColor="#000000" />
spinner_dropdown_item.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#000000"/>
java code :
Spinner staticSpinner = (Spinner)findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter =
ArrayAdapter.createFromResource(this,R.array.request_role,R.layout.spinner_item);
staticAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
Use this spinner in your code
<Spinner
style="#style/edit_style"
android:id="#+id/spr_city"
android:background="#drawable/selector_spinner"
android:layout_width="0dp"
android:textColor="#android:color/white"
android:minHeight="#dimen/min_height"
android:textCursorDrawable="#null"
android:textColorHint="#android:color/white"
android:layout_height="fill_parent"
android:layout_weight="9"
/>
and make this style in style .xml
<style name="edit_style" >
<item name="android:textSize">14sp</item>
<item name="android:textColor">#ffffff</item>
</style>
and use this code in activity
ArrayAdapter<String> adp1=new ArrayAdapter<String> (activity,R.layout.spinner_item_selected,city_list);
adp1.setDropDownViewResource(R.layout.spinner_item);
s_city.setAdapter(adp1);
and create xml spinner_item_selected
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/textViewSpinnerItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/common_margin"
style="#style/edit_style"
android:textColor="#android:color/white"
xmlns:android="http://schemas.android.com/apk/res/android" />
and one more xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/textViewSpinnerItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/common_margin"
style="#style/edit_style"
android:textColor="#color/spinner_item_selector"
xmlns:android="http://schemas.android.com/apk/res/android" />
You can set the spinners background color in xml like this:
android:background="YOUR_HEX_COLOR_CODE"
and if you use the drop down menu with you spinner you can set its background color like this:
android:popupBackground="YOUR_HEX_COLOR_CODE"
change android.R to yourprojcet.R
android.R.layout.simple_spinner_dropdown_item
to
letmobility.com.itforte.R.layout.simple_spinner_dropdown_item;
it is taking android layout instead of your app specific layout.
Your call should be like this :
ArrayAdapter<CharSequence> staticAdapter =
ArrayAdapter.createFromResource(this,R.array.request_role,android.R.layout.simple_spinner_item);
staticAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
instead of :
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(this,R.array.request_role,android.R.layout.simple_spinner_item);
staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Categories

Resources