Chronometer font doesn't change in android - android

I want to change the font of my chronometer in android.I have set my default font to be a custom font . The font seems to be applied every where except the chronometer. I have also tried setting android:fontFamily="#font/myfont" but it didn't work. Though the font appears to be applied in the preview window, but it doesn't change when i run my app on my device.
Any idea to get over it ?
Here's my code:
<Chronometer
android:fontFamily="#font/linotte_semibold"
android:id="#+id/audio_player_chronometer"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="#+id/frame_layout"
android:textColor="#color/white"
android:layout_margin="5dp"
/>

You can download Fonts from Google Material
Go to layout editor
Select your view and view All Attributes on your right side and find fontFamily
Now click down arrow, it will open a window for selecting font
Find your desired font, you can also search and click ok
Your Chronometer will look differently
Here is the code
<Chronometer
android:id="#+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:fontFamily="#font/allan_bold"
android:textColor="#color/colorAccent"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
If in case font does not change use this in your Activity.class
Chronometer chronometer = findViewById(R.id.chronometer);
chronometer.setTypeface(ResourcesCompat.getFont(this, R.font.allan_bold));
//replace allan_bold with your desired font
Edit 1
Here is the Output in the device
Hope this will help!

I just follow this to add font to Android
https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html
Copy .ttf / .otf file to "res/font" folder
Generate font resource file with style and weight details.
Set new font as android:fontFamily="#font/sample_font"
Check sample code below, with comparison with and without adding font
<!--With Font-->
<Chronometer
android:id="#+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/framelayout"
android:layout_margin="5dp"
android:fontFamily="#font/sample_font"
android:textColor="#android:color/holo_blue_dark"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/audio_player_chronometer"
app:layout_constraintVertical_bias="0.528" />
<!--Without Font-->
<Chronometer
android:id="#+id/audio_player_chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/framelayout"
android:layout_margin="5dp"
android:textSize="30sp"
android:textColor="#android:color/holo_blue_dark"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/framelayout" />
<!--sample text-->
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:fontFamily="#font/sample_font"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Screenshot

Related

Change the style of a Checkbox to Button style

I would like to change the style of the Checkbox.
Is it possible to let the checkbox look the same like the button?
Thought about adding an image to the checkbox, but don't know how to safe the button image in good quality out of Android Studio. Maybe it's possible without adding an image?
<Button
android:id="#+id/b0"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:insetLeft="3dp"
android:insetRight="3dp"
android:text="#string/_0"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
app:layout_constraintBottom_toTopOf="#+id/b17"
app:layout_constraintEnd_toStartOf="#+id/bUnDo"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/cDouble"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="#+id/cDouble"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="#string/drouble"
app:layout_constraintBottom_toTopOf="#+id/b18"
app:layout_constraintEnd_toStartOf="#+id/b0"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/cTriple"
app:layout_constraintTop_toTopOf="parent" />
Appreciate every hint.
Thanks and regards.

how to align text and icon left on MaterialButton (with icon directly after text)?

I want to create Button with text and Icon, both left-aligned.
I am using app:iconGravity="textEnd" to place the icon directly after the button-text, but the icon stays in old centered position if I change text positioning to left by using android:gravity="left"
my layout-xml:
<com.google.android.material.button.MaterialButton
android:id="#+id/listentry_button"
style="#style/Widget.MaterialComponents.Button.TextButton.Icon"
app:icon="#drawable/ic_arrow_straight_right"
app:iconGravity="textEnd"
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="#string/account_data_personal_data"
android:textColor="#color/primaryTextColor"
app:iconTint="#color/secondaryColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
any idea how to achieve this with material-button or is is it impossible?
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My button"
app:icon="#drawable/ic_arrow"
app:iconGravity="textEnd"
/>
above code can give you the following result
OR
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My button"
android:gravity="left|center_vertical"
app:icon="#drawable/ic_arrow"
app:iconGravity="textStart"
/>
above code can give you the following result
Did you try changing the layout width to wrap content?
android:layout_width="wrap_content"

On a button, text and drawable are too far away when centered

when I make a button width match parent, the drawable start and text are too far away, and they don't seem to respect android:drawablePadding setting. I tried as well with android:gravity="center" and android:textAlignment="center" to no avail. Setting some paddingStart/End affect indirectly how close they are to each other.
See the result below:
The code for button:
<Button
style="#style/ActionButton.Primary.Light.Large"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:drawableLeft="#drawable/ic_filter_and_sort_white"
android:drawablePadding="0dp"
android:textAlignment="center"
android:text="Apply filters"
app:layout_constraintBottom_toTopOf="#id/someViewBelow"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/someViewAbove" />
the style defines background, textColor, margins, minDimensions, paddings, radiuses, textSize, textAllCaps, radiuses, stateListAnimator, fontPath - so nothing that should affect what I'm looking for.
Use the MaterialButton with app:iconGravity="start" and defining the padding between the icon and the text with the app:iconPadding attribute.
Something like:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/...."
app:iconGravity="start"
app:iconPadding="..."
This value can be negative.
Otherwise you can use app:iconGravity="textStart".
Here the difference of using start and textStart as iconGravity.
You can use the MaterialButton now which lets setting the icon gravity ex:app:iconGravity="textStart".
<com.google.android.material.button.MaterialButton
android:layout_width="0dp"
android:layout_height="56dp"
android:layout_margin="16dp"
android:gravity="center"
android:textAllCaps="true"
app:backgroundTint="#fc0"
app:icon="#drawable/ic_filter_and_sort_white"
app:iconGravity="textStart"
app:iconPadding="10dp"
app:iconTint="#f00"
app:layout_constraintBottom_toTopOf="#id/someViewBelow"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/someViewAbove"
tools:text="Download Pdf" />
Add dependency implementation 'com.google.android.material:material:1.1.0-alpha10'
Reference
Did you try to use android:textAlignment="textStart"?
Try with this, it should work.
<Button
style="#style/ActionButton.Primary.Light.Large"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:drawableLeft="#drawable/ic_filter_and_sort_white"
android:drawablePadding="5dp"
android:textAlignment="textStart"
android:layout_gravity="start"
android:text="Apply filters"
app:layout_constraintBottom_toTopOf="#id/someViewBelow"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/someViewAbove" />

Android bypassing theme setting

I have a Layout with a TextView in an application - not written by me. I want to set it to bold or change the font family in the xml. In the application written by me it works correctly. But in this one, the change doesn't show. I suppose that's because a theme was applied. If I change the setting programmatically it works and if I set it to bold programmatically it works.
myTextView.Typeface(Typeface.DEFAULT_BOLD).
How can I overwrite the theme setting?
UPDATE:
The Layout that i have is this:
<LinearLayout
android:id="#+id/lFiltro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:layout_marginEnd="10sp"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:id="#+id/tUno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="#font/archivo_black"
android:shadowColor="#33b5e5"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="5"
android:text="This is my text"
android:textAlignment="center"
android:textColor="#0000ff"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="visible" />
</LinearLayout>
Try to add
android:textAppearance="#style/TextAppearance.AppCompat.Subhead"
to your TextView xml and change what is after TextAppearance. according to the suggestions you will get.

Android: layout size issue when changing device settings

I am currently facing a trouble regarding size change.
I have coded my xml files using dp's, but when I change the device screen size, the layout would change.
For example, when I go into Settings - Display - Screen zoom and font and set screen zoom from medium to large, some of the elements in my layout such as imageview or button would grow and go beyond the screen.
Since I'm using a complex layout containing LinearLayouts and FrameLayouts, I am not clear about how I should make this layout look alright in any other devices or screen sizes. It seems like when I change the screen zoom setting, the value of dp changes.
Is there a way to avoid this problem, or maybe to programmatically set screen zoom setting to medium?
Here is my code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginTop="65dp">
<android.support.constraint.ConstraintLayout
android:id="#+id/frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="95dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/home_map" />
<Button
android:id="#+id/pick1"
android:layout_width="34dp"
android:layout_height="56dp"
android:background="#drawable/top_deselected"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="#+id/imageView"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toStartOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.18" />
<Button
android:id="#+id/pick2"
android:layout_width="34dp"
android:layout_height="56dp"
android:background="#drawable/jungle_deselected"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="#+id/imageView"
app:layout_constraintHorizontal_bias="0.28"
app:layout_constraintStart_toStartOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.367" />
<Button
android:id="#+id/pick3"
android:layout_width="34dp"
android:layout_height="56dp"
android:background="#drawable/mid_deselected"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="#+id/imageView"
app:layout_constraintHorizontal_bias="0.424"
app:layout_constraintStart_toStartOf="#+id/imageView"
app:layout_constraintTop_toTopOf="#+id/imageView"
app:layout_constraintVertical_bias="0.43" />
<Button
android:id="#+id/pick4"
android:layout_width="34dp"
android:layout_height="56dp"
android:background="#drawable/support_deselected"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.615"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/imageView"
app:layout_constraintVertical_bias="0.71" />
<Button
android:id="#+id/pick5"
android:layout_width="34dp"
android:layout_height="56dp"
android:background="#drawable/adc_deselected"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.733"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/imageView"
app:layout_constraintVertical_bias="0.71" />
</android.support.constraint.ConstraintLayout>
<Button
android:layout_width="335dp"
android:layout_height="42.5dp"
android:layout_marginBottom="5.5dp"
android:background="#drawable/home_mode"
android:id="#+id/home_mode"/>
<Button
android:layout_width="335dp"
android:layout_height="42.5dp"
android:background="#drawable/home_match"
android:id="#+id/home_match"/>
</LinearLayout>
The constraint layout used to be a frame layout, but I was just now trying out the constraint layout.
You can also use this library, it is working on all devices, no need to make any dimen file and all. but use this only for the size of widgets, for text size use "sp" only.
https://github.com/intuit/sdp

Categories

Resources