Sophisticated button layout - java

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>

Related

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?

Android check box button style for API < 21

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 ?

How to Create Custom ProgressBar

Edit:
Question 1) is solved. But I still wasn't able to change to color of the ProgressBar. I tried to use my own theme (see code below).
I want my ProgressBar to look like the image below. I have already created a ProgressBar above my ListView.I’m using a ListView , ProgressBar , and two TextViews in a RelativeLayout.
My Question:
1.) How can I align the TextViews above the ProgressBar in the way shown below?
2.) How can I set the Color of the ProgressBar itself and the Background Color of the ProgressBar?
MainActivity.xml
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/progressBarText"
android:progress="0"
android:max="100"
android:paddingTop="0dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:theme="#style/progressBarTheme"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"/>
styles.xml
<resources>
<!-- 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>
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#android:color/holo_green_dark</item>
</style>
<style name="progressBarTheme" parent="#style/Theme.AppCompat">
<item name="colorAccent">#color/myRedColor</item>
</style>
</resources>
1.) How can I align the TextViews above the ProgressBar in the way shown below?
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ProgressBar
android:id="#+id/progressCircle"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<TextView
android:id="#+id/txtvStatusCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/progressCircle"
android:layout_alignParentStart="true"
android:text="Daily Progress"
android:textSize="18sp" />
<TextView
android:id="#+id/txtPercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/progressCircle"
android:layout_alignParentEnd="true"
android:text="0 %"
android:textSize="18sp" />
</RelativeLayout>
2.) How can I set the Color of the ProgressBar itself and the Background Color of the ProgressBar?
In your styles.xml:
<style name="progressBarBlue" parent="#style/Theme.AppCompat">
<item name="colorAccent">#color/myBlueColor</item>
</style>
Then set as the ProgressBar theme in the xml:
<ProgressBar
...
android:theme="#style/progressBarBlue" />
About the ProgressBar itself, what ever you choose for the AccentColor in your styles, will be set for the ProgressBar too. So changing the color of:
<item name="colorAccent">#color/colorAccent/item>
Will do the trick.
Output:
try this:
<android.support.constraint.ConstraintLayout
android:id="#+id/uplaodFile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/upload"
android:layout_marginTop="10dp"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="46dp"
android:layout_height="65dp"
android:src="#drawable/file_image"
app:layout_constraintEnd_toStartOf="#+id/horizontal_progress_bar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/notification" />
<TextView
android:id="#+id/notification"
android:layout_width="282dp"
android:layout_height="22dp"
android:layout_marginBottom="2dp"
android:text="File Detail"
app:layout_constraintBottom_toTopOf="#+id/cancel"
app:layout_constraintStart_toStartOf="#+id/horizontal_progress_bar"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<ProgressBar
android:id="#+id/horizontal_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="201dp"
android:layout_height="26dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="24dp"
android:indeterminate="false"
android:max="100"
android:minHeight="100dp"
android:progress="2"
android:progressBackgroundTint="#aaaaaa"
app:layout_constraintEnd_toStartOf="#+id/cancel"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/process_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="40dp"
android:text="uploading.. 10%"
android:textAlignment="center"
android:textColor="#4B74FF"
android:textSize="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.833"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<ImageView
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="36dp"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_close_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>
this is the java code
ProgressBar progressBar;
process_status = findViewById(R.id.process_state);
progressBar = findViewById(R.id.horizontal_progress_bar);
progressBar.setProgressTintList(ColorStateList.valueOf(Color.parseColor("#4B74FF")));
progressBar.setProgress(currentProgress);
textView.setText("Uploading..."+currentProgress+ "%");
This changed the color of the progressbar.
progressBar.getProgressDrawable().setColorFilter(
Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);

One of the styles I created in styles.xml is not applied to TextView in Java but the other 2 styles I made work fine

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.

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

Categories

Resources