LinearLayout with style and without orientation issue? - android

I have style for LinearLayout:
<style name="clickable_zone">
<item name="android:layout_height">match_parent</item>
<item name="android:layout_width">match_parent</item>
<item name="android:id">#+id/click_zone</item>
<item name="android:background">#drawable/simple_button_holo</item>
<item name="android:orientation">horizontal</item>
</style>
I use this style at different layouts as:
<LinearLayout
style="#style/clickable_zone" />
But in this case I get warning about orientation issue. Of course, I can disable this warning. But, can I resolve my problem, without disabling the warning?
UPD1. Issue message:
Checks that LinearLayouts with multiple children set the orientation
The default orientation of a LinearLayout is horizontal. It's pretty easy to believe that the layout is vertical, add multiple children to it, and wonder why only the first child is visible (when the subsequent children are off screen to the right). This lint rule helps pinpoint this issue by warning whenever a LinearLayout is used with an implicit orientation and multiple children.
It also checks for empty LinearLayouts without an orientation attribute that also defines an id attribute. This catches the scenarios where children will be added to the LinearLayout dynamically.

Related

Constraints are not applied when moving all four of them to themes.XML

I have a ConstraintLayout in which I have placed a ProgressBar right in the center.
<androidx.constraintlayout.widget.ConstraintLayout
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">
<ProgressBar
android:id="#+id/progress_bar"
style="#style/ProgressBarStyle"/>
</androidx.constraintlayout.widget.ConstraintLayout>
All four constraints are located themes.XML file in ProgressBarStyle:
<style name="ProgressBarStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="layout_constraintBottom_toBottomOf">parent</item>
<item name="layout_constraintEnd_toEndOf">parent</item>
<item name="layout_constraintStart_toStartOf">parent</item>
<item name="layout_constraintTop_toTopOf">parent</item>
</style>
Even if the ProgressBar is displayed in the center in Android Studio, see the image below:
I get the following warning:
This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints
So even if the constraints are present in the themes.XML file, I still get this warning. How to solve this?
Because you can not set constraints for view within style.constraints should be under constraint layout.when you write xml code, layout try to find all constraints to place the view in preview window.they won't look at style for constraints.that's why you got warning.to avoid warning move all four constraints from style to layout.
I have come across this in the past - just looks like an case that is not handled when checking the constraints. You are not doing anything wrong.
tools:ignore="MissingConstraints" will get rid of the error. You can apply this to each view that displays the error or in the ConstraintLayout XML so it will apply to the entire layout.

TextInputLayout error message size

Is there a way to force the TextInputLayout error message to take a single line ?
I tried to place app:errorTextAppearance="#style/error_appearance" on the TextInputLayout with the error_appearance style containing :
<item name="android:maxLines">1</item>
<item name="android:ellipsize">end</item>
<item name="android:inputType">text</item>
and it does not work. Changing the color works however. I don't get it, the mErrorView is just a TextView, it should work.
you can fetch error TextView by id
TextView errorView = textInputLayout.findViewById(R.id.textinput_error);
errorView.setMaxLines(1);
errorView.setSingleLine(true);
It's true the the error is a TextView, but a TextAppearance style affects only text, not the View that the text is in. The maxLines and ellipsize attributes belong to TextView, so they will be ignored in a TextAppearance style, which expects attributes like textColor, textSize, typeface, etc.1
Knowing this, we just need to find a way to set those attributes' values on the error TextView. kjurkovic's answer on this very question
shows how to do that in code, for the current versions of TextInputLayout.2 It turns out that there's also a way we can do this in the layout XML, which might be preferable for some.
That error TextView is instantiated with the TextInputLayout's Context, which we can actually modify in the layout XML, to some extent, with the android:theme attribute. Using an overlay with this attribute, we can cause that Context's theme to have an android:textViewStyle – essentially, the basic settings for TextViews – that contains our desired attribute values.
In your theme/style resource XML:
<style name="ThemeOverlay.TextInputLayout.TextView" parent="">
<item name="android:textViewStyle">#style/Widget.TextView.SingleLineEllipsized</item>
</style>
<style name="Widget.TextView.SingleLineEllipsized" parent="">
<item name="android:maxLines">1</item>
<item name="android:ellipsize">end</item>
</style>
On the <TextInputLayout> in the layout XML, simply add the android:theme attribute:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/text_input_layout_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone number"
android:theme="#style/ThemeOverlay.TextInputLayout.TextView">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edit_text_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
Though the current TextInputLayout has different design details than the original, we can see that the maxLines and ellipsize settings have worked:
The main caveat with this approach is that all of TextInputLayout's internal TextViews will have these base attribute values. Currently, aside from the error, those comprise the helper, the counter, the prefix, and the suffix.
The helper occupies the same spot as the error, so if you want single-line-and-ellipsized on one, you very likely want it on the other as well, if you're even using it.
The counter is already being set up as single-line, and the extraneous ellipsize would only have effect if the counter were to grow wider than the EditText.
The prefix and the suffix, by their nature, are meant to be single-line anyway.
If any of that is undesired, then kjurkovic's findViewById() demonstration should work with all of those TextViews,3 though you must ensure to set the corresponding text first, as the TextViews are instantiated on demand.
1 You can find the full list of valid TextAppearance attributes in the platform's res/values/attrs.xml resource file. I don't believe there is any documentation or developer page that covers it; at least, there didn't used to be.
2 The very first versions of TextInputLayout did not assign IDs to the inner TextViews, so getting direct reference to the error TextView required reflection, or awkwardly searching the internal hierarchy. I've not tested the theme overlay solution on those ancient versions, but if for whatever reason you're stuck with one and this doesn't work, previous revisions of this answer have those other options for you.
3 Incidentally, the IDs for all of those are as follows: R.id.textinput_error, R.id.textinput_helper_text, R.id.textinput_counter, R.id.textinput_prefix_text, and R.id.textinput_suffix_text.

Is there a way to wrap text in an EditText?

I am setting the texts inside of EditTexts in Java. Right now, what happens is, when the text is long, it just gets cut off.
In the XML, the height of the EditText is set to wrap_content. I want the height to change depending on the length of the String I will place in it so that all the text can be seen. Any solution for this? Thanks in advance!
Edit: My EditText's setup
<item name="android:layout_width">310dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:padding">15dp</item>
<item name="android:gravity">center</item>
<item name="android:editable">false</item>
<item name="android:textColor">#color/color_darktext</item>
<item name="android:textColorHint">#color/color_hint</item>
<item name="android:textAllCaps">true</item>
<item name="android:textSize">18sp</item>
<item name="android:layout_marginBottom">8dp</item>
<item name="android:background">#drawable/button50radius</item>
<item name="android:stateListAnimator">#null</item>
<item name="android:inputType">none</item>
The reason could be your padding and restricting the width of the EditText.Please do change those values and you can notice the difference.Try changing the width to wrap_content and add the attribute maxWidth="310dp" to restrict your width.
The height of the EditText is set to wrap_content
That means it will be wrapped properly how ever long your text is, It doesn't matter.
Any solution for this ?
Yes there are some possibilities, You have not posted your XML so that it would be hard to recognize the mistake. But one of the possibility is
Your parent layout which is containing the TextView may be having the paddingBottom attribute so that your TextView won't be able to show the full text inside.
If the above one is not what you did in your case, Then you can addandroid:layout_gravity attribute,
It is Standard gravity constant that a child supplies to its parent. Defines how the child view should be positioned, on both the X and Y axes, within its enclosing layout.
You can set it to android:layout_gravity="fill" which will grow the horizontal and vertical size of the object if needed so it completely fills its container.
also set the parent layout, Layout_width property .
For example
layout_width=250sp
otherwise , EditText or TextView dont wrap to next line till it reach the end of mobile border. So define the Layout_width

FrameLayout not use the styles correctly

I am very happy with this website. I'm learning a lot.
Today I doubt has arisen. And I want to put a style to a FrameLayout. and do not use.
The style is as follows:
<style name="textAsk">
<item name="android:textColor">#000000</item>
<item name="android:padding">2dp</item>
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">36dp</item>
<item name="android:textSize" >18dp</item>
<item name="android:layout_marginRight">12dp</item>
<item name="android:layout_marginLeft">12dp</item>
<item name="android:layout_marginTop">5dp</item>
</style>
I show fragments that are changing in a FrameLayout. Each Fragment contains a TextView with a text in it.
There are a lot of fragments, and I would like to set a style, a common one, for all the TextViews, in order to save time and not setup the style in each TextView.
I had tried this code:
<FrameLayout
android:id="#+id/fragmentaskGRP1"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
style="#style/textAsk"/>
But the only thing that works for me is:
<item name="android:layout_marginRight">12dp</item>
<item name="android:layout_marginLeft">12dp</item>
<item name="android:layout_marginTop">10dp</item>
Thank you very much for everything
The FrameLayout doesn't support the textColor or textSize attribute (API). So it's never set for the FrameLayout and ignored. See the style properties section from the guide Styles and Themes for more information.
Quote from the Guide:
However, if you apply a style to a View that does not support all of the style properties, the View will apply only those properties that are supported and simply ignore the others.
Define the text-related styles in a separate style definition and use it for this one for the matching views like TextView
The thing is that child views don't inherit styles from their enclosing ViewGroup. Styles can have parents, but in your case TextViews are not going to get these attributes from the FrameLayout.
The other styles will not be applied to fragments. You have to create another logic to apply styles to all fragments (Most probably you will have to apply styles individually to each fragment)
FrameLayout have nothing to do with text so textColor, textSize will have no effect.
Where as minWidth, minHeight are properties of View it think they should work.

How can I set a max width on a LinearLayout so that it doesn't stretch in Landscape mode?

My app looks great in portrait mode, but it stretches out in landscape and looks bad. I know I could make a separate layout for landscape, but I'd rather just set my root element's maximum width so that landscape mode just results in some white space on the sides. Is there a way to do this?
Keeping different layout files is a correct solution but it adds complexity to your project (you'd have to maintain multiple files in case the designer redesigns the view layout).
If all you need is to alter the width of a LinearLayout, you can do the following. Keep one xml file in the layout folder and give it a value of
<LinearLayout
style="#style/width_match_parent_max_200"
android:layout_height="wrap_content">
then, in your values-600dp/styles.xml write:
<resources>
<style name="width_match_parent_max_200">
<item name="android:layout_width">200dp</item>
</style>
</resources>
and in your values/styles.xml:
<resources>
<style name="width_match_parent_max_200">
<item name="android:layout_width">match_parent</item>
</style>
The BoundedViews library allows to set boundedWidth/boundedHeight on views even layout_width="match_parent"
The quickest solution I can think of is to set right and left margins, saved in separate dimen.xml files for portrait and landscape.

Categories

Resources