This question might be trivial to some of you.
I am facing a dumb issue working with designers and developers.
According to the designers the android button from material design has a particular style, default button style (top button in the image).
Designer defines a fix height and width 50dp for instance. So they expect to have a button of those dimensions.
But developers when using a default android button, the default button contains margins.
So we use a custom drawable background, in this case the button has the expected dimensions (button a the bottom). But the style is not exactly the same (very few differences, but still). We could try to imitate the default style, but I am not sure it is worth.
So how do you do when working with designers that just use a default button from the material android patterns and expect to have the exact size?
You consider size with margins? You create a background style? You try to imitate as much as you can the default button? others?
and this is the simple code used
<Button
android:layout_width="50dp"
android:layout_height="50dp"
/>
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/my_button_shape"/>
tha parameter margin is deferent then hight and width. margin determines the distance from point A to the button.
to et the height and width you can use in your xml file the following code:
example:
<Button
android:id = "#id/myButton"
android:layout_width = "50dp"
android:layout_hight = "50dp"
android:text = "test"
android:layout_marginTop = "100dp"
/>
the margin top 100dp determines that my button is 100dp from the top of my layout but the width and height determines the button size
The problem is 9-patch for the default button already includes a margin, so visually the button looks smaller than expected. Possible solutions include negative margins or duplicating the default 9-patch and removing the margin, but each option has its own drawbacks.
As #egonzal says, it would be interesting to know other developers solutions when facing the problem.
Related
I have just started android.
I made an 8x8 grid for chess game, and made the buttons for it. I am trying to give a background color for my buttons, my buttons have 40dp width and 60dp height. The problem is, when I put a background color, it takes almost 43dp width and 63dp height which creates a problem of overlapping the buttons.
PS: I tried backgroundTint, but it does not show any color on my device as I'm using 4.2.2 jellyBean.
see the image below:
You are using sp instead of dp.
Just try this code.
<Button android:layout_width="40dp"
android:layout_height="60dp"
android:id="#+id/a1"
android:onClick="aOne"
android:background="#000000" />
I wanted to know if it was possible to create a custom TextView that centers text perfectly, no matter what the font is. That's my major issue right now, but I'm also wondering if it's possible to set the specific height using pixels so that the height would also be consistent.
This picture shows how different fonts are sized and centered. The longest black line in the picture is the middle of the white space. Letters in the picture are the same in every way except for the fonts. The text size is the same (text.setTextSize(TypedValue.COMPLEX_UNIT_PX, 450);), and they're all centered. I hope someone knows the answer to these questions! If you need any code from my app, just ask. Thanks!
EDIT: I am aware of android:gravity="veritcal_center", but that only works to an extent. What you see above is that implemented in the textview, but each font has a different center of gravity, so android:gravity="veritcal_center" wouldn't really make all of these center perfectly along the screen. I'm looking for a way to create a custom textView that somehow measures the height of text and centers it with those parameters. A suggestion by #vmironov in the comments works, but only if the textview has one character. I have not been able to mess around with his code, but I will when I get a chance and I'll post here if I find anything. Thanks!
A simple way to achieve what you want is to use following code:
<LinearLayout
android:layout_width="100dp"
android:layout_height="20dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/your_text" />
</LinearLayout>
set the height and width values of LinearLayout fixed if you want to set the text to be alligned in center within constant height and width
Set the gravity of the view to "center_vertical". Something like :
android:gravity="center_vertical"
You should be able to center your text by applying a gravity attribute to the containing TextView.
In XML you would assign an attribute to your TextView, which would look like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/some_text" />
If you do want to specify a particular size for your text, it is strongly suggested you use SP (Scaled Pixels) as your unit of measurement, and not Pixels. You could also set an Appearance Attribute for your TextView to control the size, which is also shown in the code example.
I have an android app with a background image,which I would deploy on emulator and see its behavior.
Initially I was using emulator with skin WVGA800.In the UI all the textView declared in the layout were properly placed.
But when I change the emulator skin to 500x600 then in UI few of the textView are misplaced.
So could anyone please suggest how to handle this.
The good, but difficult thing about Android is that you don't know what size or aspect ratio the device your code runs on will be. If you want full-screen images, you need to be willing to have them scaled (proportionally or disproportionally) or cropped.
If you're trying to position text in a particular place on a background image, you're going to have a bad time. I've tried this before and quickly changed my mind.
Here's my suggestion: Read the screens support docs (as #abhilasha said) and use adaptive UI elements that scale nicely (like 9-patch images, relative layouts, etc).
Then if you want to place text in an image, instead of trying to place a TextView over an ImageView, set the image you want as the background of the TextView. Then you know where the text will be relative to the image.
My options:
1)for simple ui,i think u can use many attars with view,the following;
android:layout_centerHrizontal
android:layout_centerVertical
android:layout_centerInparent
android:layout_alignParentBottom
android:layout_alignParentLeft
android:layout_alignParentRight
android:layout_alignParentTop
android:layout_alignWithParentIfMissing
android:layout_below
android:layout_above
android:layout_toLeftOf
android:layout_toRightOf
android:layout_alignTop
android:layout_alignLeft
android:layout_alignBottom
android:layout_alignRight
android:layout_marginBottom
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop
that is helpful to use layout or view attars to set its position.
2)u can use some layouts xml to fit it.like that
if(screen width < 480)
setcontentview(r.layout.for480lay)
else if(screen width > 480&&screen width <640)
setcontentview(r.layout.for640lay)
..
..
I have a few TextViews in my app where I use a custom typeface. I have given the view centered layout gravity to get the text centered in its parent layout. It looks fine in the previewer with the Sans font, but when I change the typeface, the result looks like the text is shifted down, like this:
Compared to this:
Here is the xml for that element:
<FrameLayout android:id="#+id/Turn_ScoreA_Frame" android:padding="5dp" android:background="#color/teamA_secondary" android:layout_margin="5dp" android:layout_gravity="center" android:layout_height="50dp" android:layout_width="110dp">
<android.view.View android:id="#+id/Turn_ScoreABG" android:background="#color/teamA_primary" android:layout_height="fill_parent" android:layout_width="fill_parent"></android.view.View>
<TextView android:id="#+id/Turn_ScoreA" android:includeFontPadding="false" android:text="5" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="32dp"></TextView>
</FrameLayout>
This also occurs when I use fill_parent for layout_width and height of the text view, with text gravity center. I set the custom Typeface in the OnCreate of the view in which this layout is used, just by using TextView.SetTypeface.
Does anyone know what could be causing this? I'm having trouble tracking anything down on this.
As a note, I've worked around this on several views by setting the margin_bottom to -10dp or so, but I'd like to remove that, and I can't get that hack to work on confined views like this one, anyways.
The font is Anton, by the way.
EDIT: This is definitely the result of the text being too large for its container. The thing is, Sans fits just fine, and the new font would fit but its measured size is too large. What I'm hoping is to find a way to get the text to remain at its current visible size and fit in the center of the container, in such a way that doesn't feel too hacky =)
You need to add:
android:includeFontPadding="false"
You have 3 options
Decrease the text size
Increase the height of FrameLayout
Change the font which can fit correctly
you cannot change the property of the font, it is designed is such a way that it has empty space above it, which doesn't fit in your given android:layout_height="50dp" when android:textSize="32dp"
<View
android:id="#+id/first_sep"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:paddingTop="100dip"
android:layout_below="#id/calculate"
android:background="#FFCCCCCC" />
I have this View in a RelativeLayout and the android:paddingTop property doesn't seem to work. Tried it with various values and still the View lies close to the Button "calculate".
As MGS indicates, you probably want android:layout_marginTop, or perhaps android:layout_height=101dip. I am uncertain if padding has any meaning on a generic View. If it is supported, it certainly will have no difference than simply setting the height to include the padding, since padding is considered part of the background calcluations.
For space between a widget and an adjacent widget, use margins. For space between the edges of a widget and its contents (e.g., between the edge of a Button and its caption), use padding. In the case of a transparent background, these will visually be identical. In the case of a non-transparent background, they will be substantially different visually.