Change View height on state selected - android

I'm new to Android so this question might be dumb but, is it possible to change layout_height on a View through selector? I'm trying to make a custom TabHost/TabWidget where I have to change some views depending on their select state. This is what I'm trying to achieve:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true" android:height="10dp" />
<item android:height="5dp" />
</selector>
And then I'm trying to append this selector to View like this:
<View android:layout_height="#drawable/tab_lineheight_selector" ... />
I'm obviously doing something wrong cuz my app keeps crashing. Is it even possible?

As per developer docs:
android:layout_height
Specifies the basic height of the view. This is a required attribute
for any view inside of a containing layout manager. Its value may be a
dimension (such as "12dip") for a constant height or one of the
special constants. May be a dimension value, which is a floating point
number appended with a unit such as "14.5sp". Available units are: px
(pixels), dp (density-independent pixels), sp (scaled pixels based on
preferred font size), in (inches), mm (millimeters). This may also be
a reference to a resource (in the form "#[package:]type:name") or
theme attribute (in the form "?[package:][type:]name") containing a
value of this type.
So, technically its NOT POSSIBLE what you are trying to do.

Related

How do items of a LayerList drawable scale to fit the container View?

From this developer guide about Layer-list drawables,
All drawable items are scaled to fit the size of the containing View,
by default. Thus, placing your images in a layer list at different
positions might increase the size of the View and some images scale as
appropriate.
In the first sentence, they say that the items are scaled to fit the
container view (and Not that the view is scaled according to the
size of the items contained in it). Then they say that the size of
the container view might increase (which means that the View is
being scaled, right?). So doesn't the second sentence contradict
the first one? Can somebody explain what is meant there?
android:drawable
Drawable resource. Required. Reference to a drawable resource.
...
To avoid scaling items in the list, use a
element inside the element to specify the drawable...
...
For example, the following defines an item that scales to
fit its container View:
<item android:drawable="#drawable/image" />
To avoid scaling, the following example uses a element with centered gravity:
<item>
<bitmap android:src="#drawable/image"
android:gravity="center" />
</item>
Again, they say that android:drawable is a required attribute, and then they give an example which does not use this attribute. What is correct?
To avoid scaling items in the list, use a <bitmap> element inside the
<item> element to specify the drawable and define the gravity to
something that does not scale, such as "center"
How is gravity scalable and how is center as its value make it unscalable?
It seems that layer-list items are effectively stretched in both X & Y dimensions to fit the container.
You need a drawable for each item. The bitmap child element effectively becomes the drawable for that item.
If you have an empty item (with no drawable resource), it will cause an error when you try to load the layer-list.
Gravity has several options (like FILL, which is probably the item's default gravity, or FILL_HORIZONTAL that stretch the item to fill its container).
Additionally, you can set android:gravity="center" on an item tag itself (without a <bitmap> child) and it seems to have the same effect (in API 23, at least).
In addition to what #Cristopher_Boyd said. The screen density is also important, so different bitmaps should be created for different screen densities. If there is a single image inside the drawable folder directly, it may not scale properly. Hence, generate different images, or place a single image in drawables-xxhdpi folder, for example (but not on drawables). See this answer.
Hope this helps someone,
Xavi

"setElevation" vs "setTranslationZ" vs "setZ"

Lollipop has all those new functions for the "View" class. What exactly are the differences between them, and why do we need so many functions for them?
I've read the docs, and I want to understand (or better understand) the difference and the need for each of them, so that I would know when to use each of them :
elevation
base z depth of the view
Must be a dimension value, which is a floating point number appended
with a unit such as "14.5sp". Available units are: px (pixels), dp
(density-independent pixels), sp (scaled pixels based on preferred
font size), in (inches), mm (millimeters).
This may also be a reference to a resource (in the form
"#[package:]type:name") or theme attribute (in the form
"?[package:][type:]name") containing a value of this type.
This corresponds to the global attribute resource symbol elevation.
So this is what it starts from?
translationZ
translation in z of the view. This value is added to its elevation.
Must be a dimension value, which is a floating point number appended
with a unit such as "14.5sp". Available units are: px (pixels), dp
(density-independent pixels), sp (scaled pixels based on preferred
font size), in (inches), mm (millimeters).
This may also be a reference to a resource (in the form
"#[package:]type:name") or theme attribute (in the form
"?[package:][type:]name") containing a value of this type.
This corresponds to the global attribute resource symbol translationZ.
So this is added in addition to the "elevation" ? Why is it needed?
And, the most confusing : setZ
Sets the visual z position of this view, in pixels. This is equivalent
to setting the translationZ property to be the difference between the
x value passed in and the current elevation property.
I don't understand the explanation of this at all. Does this affect the shadow? What is it used for? To handle the case that 2 views have the same total elavation value ? Would Animating this value change the way a view is shown?

Set text size in .xml or programmatically

I have variable at dimens.xml
<resources>
<dimen name="btn_text_size">12sp</dimen>
</resources>
And i can use it in layout file:
<TextView
android:textSize="#dimen/btn_text_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dialog_tags_complete"
/>
or programmatically
tagButton.setTextSize(c.getResources().getDimension(R.dimen.tag_text_size));
But this 2 methods give different results. I know that getDimension are based on the current DisplayMetrics associated with the resources.
But what should i do to make this 2 ways looks the same?
setTextSize( float ) expects a scaled pixel value. So, setTextSize( 12 ) would give you the desired result. However, getDimension() and getDimensionPixelSize() return the size in units of pixels, so you need to use the unit-typed variant of setTextSize() as follows:
setTextSize( TypedValue.COMPLEX_UNIT_PX, getDimensionPixelSize( R.dimen.tag_text_size ) );
tagButton.setTextSize(c.getResources().getDimensionPixelSize(R.dimen.tag_text_size));
this will work just fine :)
You should also remember that textView has a setTextSize(int unit,float size), which should be used while setting size from code but not from xml dimen.
I have currently the same thing. Did set a dimension in dimens.xml and applied it programmatically, which is 3 times that big, than when settings via xml.
I checked also:
TextView.getTextSize() = 92f
getResources().getDimension(R.dimen ...) = 92f
TextView.setTextSize(92) != TextView with size from XML, other flags like TypedValue.COMPLEX_UNIT_PX make it even bigger.
The default setTextSize does apply COMPLEX_UNIT_SP by default btw. So once again, the Android API is inconsistent, so setting programmatically only and adapt sizes, so they fit, will be my solution.
Edit: Setting text size programmatically under Galaxy Note 2 (4.4.2) vs Note 4 (5.0.1) leads to a totally different result -.-

Can a drawable shape have it's size set to fill_parent?

Is it valid for a drawable shape in Android to use fill_parent for it's size?
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="fill_parent"
android:height="fill_parent"/>
</shape>
EDIT
This for the background of ImageButton views. I want the icon for the button to have a circle behind it, but I don't always know what the size of the button will be (different sizes per layout).
Not really. Not using a ShapeDrawable alone. If you go through the ShapeDrawable document, you will see (you are already using them in the tag) that the only valid attributes there are px, dp, sp, in and mm
A quote from the doc: android:width="...."
Available units are: px (pixels), dp (density-independent pixels), sp
(scaled pixels based on preferred font size), in (inches), mm
(millimeters)
This is true for the attribute: android:height
This is speculation on my part, but I suspect why the fill_parent attribute value will not work is because a ShapeDrawble, unlike an XML Layout file will not have a parent container.
Leaving out the <size.... /> attribute entirely and setting the layout_width and layout_height on a Widget that will reference the said ShapeDrawable is the only option I suspect (if the fill_parent is to be honored).
I don't know which API level this took effect, but per the Shape drawable documentation, it will be scaled proportionately to fit the view. So you can, for example, put width=1dp and height=1dp. See https://stuff.mit.edu/afs/sipb/project/android/docs/guide/topics/resources/drawable-resource.html#Shape
How do you plan to use this shape? You can set it as src or background for any view and set view height in "fill_parent" - it's more usual way.
You can't do it from the xml file, but you can do it from the java file. Just use
public final void resize (float width, float height)

Android layout width?

When making the android application layouts we have to define the layout width , what is the meaning of the android:layout_width="wrap_content" ?
Either attribute can be applied to View's (visual control) horizontal or vertical size. It's used to set a View or Layouts size based on either it's contents or the size of it's parent layout rather than explicitly specifying a dimension.
fill_parent
Setting the layout of a widget to fill_parent will force it to expand to take up as much space as is available within the layout element it's been placed in.
Setting a top level layout or control to fill_parent will force it to take up the whole screen.
wrap_content
Setting a View's size to wrap_content will force it to expand only far enough to contain the values (or child controls) it contains. For controls -- like text boxes (TextView) or images (ImageView) -- this will wrap the text or image being shown. For layout elements it will resize the layout to fit the controls / layouts added as its children.
see the official docs for more details!
android:layout_width="wrap_content" means that the layout is fixed it expand up to exactly as your content size .If your content is more it expand up to that limit and same as in short content.
Here is an example..
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btnButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button ABC"/>
</RelativeLayout>
wrap_content Image:
wrap_content means that width is determined by the width of the widgets, respectively not the whole display' width but only how widgets need.
Referring to the official Android documentation:
android:layout_width
Specifies the basic width of the view. This is a required attribute
for any view inside of a containing layout manager. Its value may be a
dimension (such as "12dip") for a constant width or one of the special
constants. May be a dimension value, which is a floating point number
appended with a unit such as "14.5sp". Available units are: px
(pixels), dp (density-independent pixels), sp (scaled pixels based on
preferred font size), in (inches), mm (millimeters). This may also be
a reference to a resource (in the form "#[package:]type:name") or
theme attribute (in the form "?[package:][type:]name") containing a
value of this type. May be one of the following constant values.
fill_parent -1 The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent.
match_parent -1 The view should be as big as its parent (minus padding). Introduced in API Level 8.
wrap_content -2 The view should be only big enough to enclose its content (plus padding).
android:layout_width="wrap_content"
means that layout width is not determined or fixed, it will take space according to its components.
Suppose in a textField u have only one word then your textField will take space for a word only and then it will take space for two word if you have two word in your textField when you will declare your width as wrap_content.
Same thing is applicable for layout_height also.

Categories

Resources