I experienced some awkward behavior. The LinearLayout below is assigned the correct background, but all corner radiis are simply dismissed. The question is why, and how do I solve this? If I set the android:background on the TextView it works fine.
Why do I wrap the TextView in a LinearLayout at all? I want to animate the TextView's text. Only the text, not the background, so I wrapped it into a LinearLayout.
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/summary_title_horizontal_last" >
<TextView
android:id="#+id/evaluation_highscore_title"
style="#style/Summary.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/evaluation_highscore"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
#drawable/summary_title_horizontal_last:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#color/summary_title_start"
android:endColor="#color/summary_title_end"
android:angle="270" />
<corners android:bottomLeftRadius="#dimen/summary_box_radius" />
</shape>
Are you sure your TextView is not drawing the background in a solid corner? You don't seem to have padding in your LinearLayout to keep the children away from the corners, so that they don't redraw the background (though I don't know what's in your Summary.Title style - you could be using margins there).
Hierarchy Viewer is invaluable in cases like this, since you can see what each view is drawing, against different backgrounds.
Related
There are buttons that use the custom style. The distance from the text of the button to its bottom edge differs depending on the length of the text - in case the text is transferred to the second and more lines, the distance to the edge is significantly reduced. Is there a way to somehow make these distances the same? Both buttons are in the LinearLayout. Also i use Calligraphy lib to set custom fonts.
This picture describes the problem.
Buttons in Layout:
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_in_text"
android:textAllCaps="false"
android:text="short text"
android:gravity="left|center_vertical"
style="?android:attr/borderlessButtonStyle"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"
android:background="#drawable/btn_in_text"
android:textAllCaps="false"
android:text="Very long text. The longes text that you saw before"
android:gravity="left|center_vertical"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
btn_in_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Tried to use
android:includeFontPadding="false"
android:paddingBottom="2dp"
and
android:lineSpacingMultiplier="1.2"
but it did not help.
The very short answer: add this attribute to both of your <Button> tags:
android:minHeight="0dp"
Because you're using <Buttton> instead of <TextView>, you are getting all of the default styles applied to Buttons by the android framework. One of these is a minimum height. That minimum height, combined with your gravity attribute including center_vertical, means that your text will float in the middle of the button until your text is long enough that it wraps enough times to exceed the button's default height.
Another possible solution is to use <TextView> tags instead of Buttons, and remove the style="?android:attr/borderlessButtonStyle" attribute. This will also let you remove android:textAllCaps="false".
Iam not sure about the specifics of your problem but I had a similar problem where my textsize was changing the button's size as well and fixed it using
android:baselineAligned="false"
In the layout which carries the Buttons since by default the bottom edge of all the text in a row is at the same height.
Here is the link of my solved problem which includes a picture similar to yours :-
In XML Button size changes with its Font size
If that doesn't work try making a fixed button height instead of wrap_content.
I am using a LinearLayout as view group which holds two children (TextView and EditText). My XML code looks as follows:
<?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="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View"
android:textSize="20sp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Edit Text"
android:textSize="20sp" />
</LinearLayout>
which will produce the following design:
As depicted in the zoomed-in view on the left, TextViewand EditText are not equally aligned vertically (EditText is indented a bit more as shown by the small red arrows).
It seems there is a bit of a padding (a few dp) around the hint and the line underneath which prevents them from "touching" the left edge of their view field. Is there any way to force the hint within EditText to squeeze to the left of its view field?
How can I get rid of this indentation, other than by adding paddings and margins?
Thanks for any ideas and advice!
Without add any paddings and margins you can use a custom drawable for the background of your Edittext to remove the default padding
in drawable/edittext_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent" />
</shape>
</item>
<item
android:top="-2dp"
android:right="-2dp"
android:left="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#000000" />
</shape>
</item>
</layer-list>
Now use it in your editext:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/edittext_bg"
android:hint="Edit Text"
android:textSize="20sp" />
Output:
It should not be in this way... You should check your style.xml, maybe you set additional margins there.
Also, try to specify layout_gravity:"left" attribute.
try typing letters on Edit Text then you can see both textview and edit text was aligned equally
Did you try Constraint layout? It's removing the need to have so much hierarchy in xml layouts and is easy to use.
this preview, run the simulator for correct preview.
But, for best result use https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html
I'm trying to draw a box around two TextView objects in Android. Prior to this I had the two TextViews in a LinearLayout, but I'm working on flattening my view hierarchy to improve performance. When they were in the LinearLayout, the background showed up as desired, but when I create an empty layout with the same dimensions, I don't see the background show up at all.
My guess is this is because there isn't anything in the FrameLayout (I also tried an empty LinearLayout). I also tried setting this value at run-time, but same result.
This works fine in the layout editor in Eclipse, but when running on my tablet, the background just isn't there.
Any suggestions to accomplish the same thing or any suggestions for what I'm missing would help. Here's the shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#508E8F8E" />
<stroke
android:width="1dip"
android:color="#8E8F8E" />
<corners android:radius="4dp" />
</shape>
And here's the layout:
<FrameLayout
android:id="#+id/box_around_text"
android:layout_width="35dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#drawable/background_gray"/>
<TextView
android:id="#+id/first_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="first_text" />
<TextView
android:id="#+id/second_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/first_text"
android:text="second_text" />
The issue was that the shape must have real dimensions defined somewhere. I can make it show up if I define a size in the shape itself or if I use numbered dimensions in the layout. It would never show up if I used the 'wrap_content' or 'match_parent' options.
I have a ListView. In each row I have a LinearLayout with items inside. The LinearLayout has a layout_margin of 10dp. I have placed it a selector background when pressed. But the problem is that at the border of the margin of 10dp an orange background color appears, while inside the LinearLayout black background appears correctly.
How I can remove that orange background in margin from appearing?
selector
<item android:state_pressed="true">
<shape>
<gradient android:angle="90" android:startColor="#color/negro" android:endColor="#color/negro" />
</shape>
</item>
<item android:state_enabled="true">
<shape>
<gradient android:angle="90" android:startColor="#color/grisOscuro" android:endColor="#color/grisOscuro" />
</shape>
</item>
xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#color/black"
android:orientation="horizontal">
<LinearLayout
android:baselineAligned="false"
android:id="#+id/fondoListviewRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/grey"
android:orientation="horizontal">
...
</LinearLayout>
code
holderName.fondo.setBackgroundResource(R.drawable.listview_negro);
I suppse there is a orange background behind your view.
Padding
is the space inside the border, between the border and the actual view's content. Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent).
Margins
are the spaces outside the border, between the border and the other elements next to this view. Note that, like the padding, the margin goes completely around the content: there are margins on the top, bottom, right, and left sides.
Try using android:padding="10dp" insted. it should solve you problem.
EDIT:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FF0000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="#00FF00" >
</LinearLayout>
</LinearLayout>
the above code gives me
Based on your description of the problem, I believe you should switch or alter the theme of your app, which is chosen in your app's manifest, under application » android:theme. Styles can be found under res » values. Or you should redefine styles related to elements of interest.
Before digging deep into this, switch theme to:
android:theme="#android:style/Theme.Black"
or
android:theme="#android:style/Theme.Light"
or
android:theme="#android:style/Theme.NoTitleBar"
... and see what happens. The latter shows no action bar.
Is it possible to specify border in Android button in the main.xml?
[p.s.
without the 'separate xml file containing stroke tag' but in the original file where I define the button and also
without the 'dynamically by programming' solution
and 'images' solution]
<Button
android:background="#FFFFFF"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="Player 3"
android:layout_x="0px"
android:layout_y="0px"
android:id="#+id/p3"
android:layout_weight="1"
/>
here I am changing the background dynamically but the problem is, for 2 buttons there is no border.
Try to use shape
my_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:radius="0.1dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#E8E6E7" />
</shape>
And Button
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/my_shape"
android:text="Button" />
Screenshot
I hope this will help you.
This is not the recommended way to do it because it causes overdraw and adds unnecessary views, Gunaseelan has the proper method.
There's no concept of borders as an attribute. The accepted way is to use a separate drawable as the background to the View (using stroke as you've mentioned and as #gunaseelan writes).
The other (not recommended) way is to enclose your Button in another View like a LinearLayout, set the background color to the desired border colour on the encapsulating View and padding on the outer View too.
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#342334"
android:padding="5dp"
>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="whatwhaaat"
/>
</LinearLayout>
The value of the padding will indicate the thickness of the border. This method is not recommended as you end up with an extra View in your layout, and another layer of overdraw (it draws the LinearLayout then the Button on top).