One attribute for several views in layout XML (Avoiding repetitive code) - android

I have the following layout XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"> // The layout_gravity doesn't seem to affect
// the button Views
// (I guess this is the wrong way to do it)
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Genre1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Genre2"
/>
</LinearLayout>
How do I make a single general attribute configuration e.g. android:layout_gravity="center" for all my Button Views?

You could wrap common attributes for a certain View under a style.
Create a styles.xml file in your values folder, and just declare the styles:
<style name="list_item_title">
<item name="android:textStyle">bold</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:textSize">18sp</item>
<item name="android:padding">0dp</item>
</style>
Then, on your TextView (or Button, or whatever View...):
<TextView
android:id="#+id/tv1"
style="#style/list_item_title" />

Write below line
android:gravity="center"
instead of
android:layout_gravity="center"
it will solve your problem.

have you tried android:gravity="center" instead of android:layout_gravity="center"
or you can try android:layout_gravity="center" in your buttons xml

Related

Theming PreferenceFragmentCompat

I'm having some troubles setting a theme to PreferenceFragmentCompat trying to achieve this:
I have these styles set:
<style name="AppThemeBase" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- 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:windowLightStatusBar">true</item>
<item name="android:statusBarColor">#color/status_bar</item>
<item name="colorControlNormal">#color/colorPrimary</item>
<item name="preferenceTheme">#style/AppThemeBase.RecordingPreferencesTheme</item>
</style>
<style name="AppThemeBase.RecordingPreferencesTheme" parent="#style/PreferenceThemeOverlay">
<item name="android:textColorPrimary">#FD0000</item>
<item name="android:textColorSecondary">#1F6F2B</item>
</style>
With this options I can see what I want to achieve in Android Studio layout preview but when it does not have effect when I execute it
The result when I execute:
The only thing it worked so far is using <item name="android:textColor">#FD0000</item> that will change the tittle colour but I can't find a way to change the summary colour.
Another question I have is if there is a way to set the theme just for that preference fragment instead of using it in all preferences fragments. Using android:theme at the root of the fragment layout isn't working for me :(
Update:
As per #Susan question this is how I try to use android:theme at the root of the fragment layout I've created this theme based in the main one
<style name="RecordingPreferenceTheme" parent="AppThemeBase">
<item name="preferenceTheme">#style/AppThemeBase.RecordingPreferencesTheme</item>
</style>
And this is the preference layout where I have a fragment which will inflate the PreferenceFragmentCompat
<?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"
android:theme="#style/RecordingPreferenceTheme">
<include
android:id="#+id/include"
layout="#layout/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="#+id/fragment_container_settings"
android:name="com.example.app.RecordSettingsFragment$RecordPreferences"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/include" />
</androidx.constraintlayout.widget.ConstraintLayout>
And at the root of the layout I'll set android:theme="#style/RecordingPreferenceTheme" but it doesn't work, just changes in the main theme set in Activity will have some effect. I've already tried customize PreferenceFragmentCompat layout with same results.
Cheers!!
See this sample project here created for your specific case the the only problem for this solution is you have to create your preference from code cant use xml file to inflate and
for those who just want the gist of this solution is that use setLayoutResource() method to set layout and use custom layout i just copied the original layout and hardcoded colors according to #Bhuntupana need
val messagesCategory = PreferenceCategory(context).apply {
layoutResource = R.layout.sample_preference
key = "messages_category"
title = getString(R.string.messages_header)
screen.addPreference(this)
}
Update
use layout tag to specify layout resource to use like this
<EditTextPreference
android:layout="#layout/sample_preference"
app:key="signature"
app:title="#string/signature_title"
app:useSimpleSummaryProvider="true" />
sample_preference.xml just copy of default layout preference_material.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:baselineAligned="false"
android:clipToPadding="false"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingRight="?android:attr/listPreferredItemPaddingRight">
<include layout="#layout/image_frame" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<TextView
android:id="#android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textColor="#FD0000" />
<TextView
android:id="#android:id/summary"
style="#style/PreferenceSummaryTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignStart="#android:id/title"
android:layout_alignLeft="#android:id/title"
android:layout_gravity="start"
android:maxLines="10"
android:textAlignment="viewStart"
android:textColor="#1F6F2B" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout
android:id="#android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="end|center_vertical"
android:orientation="vertical"
android:paddingStart="16dp"
android:paddingLeft="16dp"
android:paddingEnd="0dp"
android:paddingRight="0dp" />
</LinearLayout>

How to remove the space before the first letter in a TextView

So I have 3 vertical aligned TextViews. The second one has a larger font size.
Now, if the second TextView holds a string starting with e.g. a 'B' it looks like the View is indented:
Here's my XML layout:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView style="#style/TextAppearance.LabelSmall"
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1" />
<TextView style="#style/TextAppearance.LabelBig"
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B TextView2" />
<TextView style="#style/TextAppearance.LabelSmall"
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView3" />
</LinearLayout>
And here's the corresponding styles.xml file:
<resources>
<style name="TextAppearance.LabelBig" parent="TextAppearance.MaterialComponents.Headline3">
<item name="fontFamily">#font/opensans_regular</item>
<item name="android:fontFamily">#font/opensans_regular</item>
<item name="android:textSize">48sp</item>
</style>
<style name="TextAppearance.LabelSmall" parent="TextAppearance.MaterialComponents.Body1">
<item name="fontFamily">#font/opensans_regular</item>
<item name="android:fontFamily">#font/opensans_regular</item>
<item name="android:textSize">16sp</item>
</style>
</ressources>
Is there any way to make these TextViews look equally indented?
Thanks for your help.
You don't need to use three textviews for single line text, just go for the spannable.
Find some info here: https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568

How to set transparent background during translate animation android?

I've the following problem. I've a TableLayout and two TableRows. Both TableRows contain two ImageButton. When a button is clicked I want to fade out other three buttons and translate the clicked button at the center of the screen. When it is translated the top or bottom half disappeared like it's hidden by the other table row. I've tried to set transparent background of table rows, to set alpha=0, to change layout and also I've tried to use ScaleAnimation, but the problem doesn't disappear. How can i show the disappeared half? I post also the layout code and a link to the screen with the problem.
http://it.tinypic.com/r/1zejhir/5
`
<RelativeLayout
android:id="#+id/l34"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_alignParentTop="true">
<ImageButton
android:id="#+id/four"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top|right"
android:src="#drawable/unor"
android:alpha="0"
android:onClick="translate"
android:layout_alignParentLeft="true" />
<ImageButton
android:id="#+id/three"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top|left"
android:src="#drawable/unor"
android:alpha="0"
android:onClick="translate"
android:layout_alignParentRight="true" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/l12"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true">
<ImageButton
android:id="#+id/one"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top|right"
android:src="#drawable/unor"
android:alpha="0"
android:onClick="translate"
android:layout_alignParentLeft="true" />
<ImageButton
android:id="#+id/two"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top|left"
android:src="#drawable/unor"
android:alpha="0"
android:onClick="translate"
android:layout_alignParentRight="true" />
</RelativeLayout>
</RelativeLayout>
`
The problem is that you want to use "space" from other viewgroups, your view is bound to the hosting viewgroup (eg tablerow). It doesn't have anything to do with the transparent background. The solution would be to have one viewgroup holding all your icons. For example use a RelativeLayout.
make transparent
in java code-> imagebtn.setBackgroundColor(Color.TRANSPARENT);
xml-> android:background="#android:color/transparent"
create modify the styles.xml file resource in values folder, with following code
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
in manifest.xml file add the below step to which activity make you as transparent.
android:theme="#style/Theme.Transparent"

Android: how to make two TextViews placed in different lines if there is not enough space in one line

I'm trying to organize two TextViews to behave like that:
So, if there is enough space for both TextViews in line, android should place them in line.
If there is not enough space, the second TextView must be placed on the next line with right alignment.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/takeoffCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/flightItem" />
<TextView
android:id="#+id/landingCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/flightItem" />
</LinearLayout>
<style name="flightItem" parent="#android:style/TextAppearance">
<item name="android:textSize">14dip</item>
<item name="android:textColor">#color/flightItemFont</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textStyle">bold</item>
</style>
To accomplish what you are looking for you could change you're layout to a RelativeLayout and add android:layout_alignParentLeft="true" to your TextViews, however, this doesn't set these TextViews to act dynamically. Meaning if they can fit side-by-side then they should be in-line but the bottom TextView will still align to the right.
I don't think what you are looking for is technically possible. However this may be a good alternative:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/takeoffCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
style="#style/flightItem" />
<TextView
android:id="#+id/landingCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
style="#style/flightItem" />
</RelativeLayout>

Android Wrap TextView to width of ImageButton

I have a LinearLayout which contains an ImageButton, and underneath that, a TextView as a label. I would like to have the TextView to be centered, and to have it wrapped to the width of the ImageButton it is underneath.
EDIT: I should probably put the code I'm currently using. Here's the LinearLayout with Button and TextView
<LinearLayout
android:id="#+id/contestLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageButton
android:id="#+id/contestButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/contestsbtnimg"
android:layout_weight="1">
</ImageButton>
<TextView
android:id="#+id/contestLabel"
style="#style/MainMenuStyle"
android:text="#string/contests">
</TextView>
</LinearLayout>
And here's the relevant style declaration for the TextView:
<style name="MainMenuStyle" parent="android:style/TextAppearance">
<item name="android:textColor">#000000</item>
<item name="android:layout_centerHorizontal">true</item>
<item name="android:textSize">12sp</item>
<item name="android:singleLine">false</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
What I want is for the ImageButton to be on top of the LinearLayout, with the TextView acting as a label underneath, and wrapping its text to a second line if it would be wider than the button.
You should at least try to find a solution yourself.
Or give some explanation why (and what) your own findings were.
Something like this should do the trick:
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="..."/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="...."/>
</LinearLayout>

Categories

Resources