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>
Related
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
I am setting style,color and size to textview , that is working
this is my code `
<android.support.v7.widget.AppCompatTextView
android:id="#+id/txt_agent_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center"
android:textColor="#000"
android:textSize="14sp"
android:textStyle="italic"/>
Please check your parent layout is correct some times transparent layer or such things create visualise problem with colour
<android.support.v7.widget.AppCompatTextView
android:id="#+id/txt_agent_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="AppCompatTextView"
android:textColor="#FFFFFF"
android:textSize="114dp"
android:textStyle="italic" />
Use Dimension for setting up a text size ,always avoid hard coding
If you want to set same style for text-color,text-size and text-style than you can create something like
<style name="MyTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">?android:textColorPrimary</item>
<item name="android:textSize">#dimen/abc_text_size_body_1_material</item>
<item name="textColor">?textColorPrimary</item>
<item name="textStyle">italic</item>
</style>
I have the following setup:
<android.support.design.widget.NavigationView
style="#style/NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextAppearance="#style/DrawerTextAppearance"
app:menu="#menu/drawer"/>
menu/drawer.xml
<menu>
<item
android:id="#+id/messages_item"
android:icon="#drawable/ic_notifications_neg"
app:actionLayout="#layout/counter"
android:title="#string/message_center"/>
<item
android:id="#+id/search_item"
android:icon="#drawable/ic_search_neg"
android:title="#string/search"/>
</menu>
layout/counter.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="26dp"
android:layout_height="26dp"
android:text="55"
style="#style/Bubble"/>
style:
<style name="Bubble" parent="android:Widget.TextView">
<item name="android:gravity">center</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:background">#drawable/bubble</item>
</style>
this produces the following result:
the bubble is shown at the top despite style's gravity setting.
How can I position the actionLayout in the middle of a menu item?
I managed to solve the problem by accident.
I had to put the TextView inside a container:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/counterView"
style="#style/Bubble"
android:layout_width="26dp"
android:layout_height="26dp"
android:text="55"/>
</LinearLayout>
Now the counter is shown right in the middle of the menu item!
UPDATE
Counter changing code:
TextView view = (TextView) drawer.getMenu().findItem(R.id.counter_item).getActionView().findViewById(R.id.counterView);
view.setText("" + count);
Try below:
In layout/counter.xml use
android:gravity="right|center_vertical"
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="26dp"
android:layout_height="26dp"
android:text="55"
android:gravity="right|center_vertical"
style="#style/Bubble"/>
I'm trying to create menu layout where I can place buttons in two colums with same width.
I don't want to stretch buttons to fill all screen (unless there are long lables).
I want all buttons to have same width - the width of the button with the longest label.
I'm trying two vertical linear layouts inside one horizontal linear layout.
All solutions that I found suggest to use same layout_weight for vertical linear layouts and layout_width=fill_parent for horizontal layout which is forcing buttons to occupy entire screen width.
Is there any common solution for my problem?
This is what I expect - https://dl.dropbox.com/u/2751770/android-layout1.PNG
This is how common solution looks like - https://dl.dropbox.com/u/2751770/android-layout2.PNG
And here is code which I'm using as a common solution:
<LinearLayout style="#style/menuLayout"
android:layout_width="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
<Button style="#style/menuButton"
android:text="Title 1"/>
<Button style="#style/menuButton"
android:text="Title 2"/>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
<Button style="#style/menuButton"
android:text="Title 3"/>
<Button style="#style/menuButton"
android:text="Long title 4"/>
</LinearLayout>
</LinearLayout>
Styles:
<style name="menuLayout">
<item name="android:layout_gravity">center</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
</style>
<style name="menuButton">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:paddingLeft">30dp</item>
<item name="android:paddingRight">30dp</item>
<item name="android:layout_margin">5dp</item>
</style>
You are setting the width in the style "menu_layout" and also again in the second line of your code.Remove one of them. And try using android:weightSum="1" in the parent LinearLayout and then android:layout_weight="0.5" in each LinearLayout.
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