In Android I'm trying to get a layout looking like this image using xml.
What I've been trying to do is to separate the view vertical into two parts. One for the blue part and one for the white. For the blue part I use a shape as background to get the gradient and for the white part just a white background. It works good for the "Title" and "Some text" part. But as the image view should overlay both layouts it isn't working.
I can't figure out how to setup the xml layout to get it working, any ideas?
I would do it like this:
<RelativeLayout>
<TextView
android:id="#+id/title"
android:layout_alignParentTop="true"
android:text="title"
/>
<TextView
android:layout_below="#id/title"
android:layout_alignParentBottom="true"
android:text="some text here"
/>
<ImageView
android:layout_alignParentLeft="true"
android:text="title"
/>
</RelativeLayout>
Of course, you'll need to set the layout width, height, ... on those elements.
I think that you'll find the answer here: set the absolute position of a view in Android
Related
I want to create a label like this:
Requirements are:
The rectangle should grow with the text
the length of the red line is dependent of a property
What I tried was to create a 9ng image for the background of the text and use the padding property for the length of the red line. The red line is drawn by overriding the onDraw method.
The result is that the red line is correct but the background is expanded including the padding like this:
What's the best way to do this? Building a custom view with a label included? a custom view drawn 100% programmatically (in this case, what's the method to resize a 9ng image?)?
Thanks
Julien
try this one
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The text"
android:textColor="#000000"
android:background="#drawable/<your_drawable>"
android:textSize="16sp" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#f00"
android:layout_weight="1" />
</LinearLayout>
Hope it will help you
Best way to do this is probably using two different controllers.
You set a simple textView with your rectangle as background and with the width set to wrap-content.
After this you create the line positioning her where you want (to the right of the controller or to the end of the page or where you please) and you resize it as you want.
For any doubt I'm here
I have the following code snippet:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/collected_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/card_text_color"
android:textSize="#dimen/card_headline_textsize"
android:layout_marginBottom="#dimen/card_headline_marginBottom"
android:text="Foo" />
<ImageView
android:id="#+id/collected_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="140dp"
android:background="#android:color/holo_red_dark"
android:adjustViewBounds="true"
android:layout_below="#id/collected_item_title"
android:layout_marginEnd="8dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/collected_item_image"
android:layout_below="#id/collected_item_title"
android:text="Foo"
android:textColor="#color/card_text_color" />
</RelativeLayout>
I like to have the ImageView on the left and the second TextView either on the right of this image or unter the image, dependent on the size of the image.
The image should grow, until it reaches a maximum height of 140dp.
How can I automatically wrap the text if the image get's too width?
Unfortunately I have no idea at the moment how to do it.
Yes I can do it programmatically, but I like to reach the goal only with xml.
The wrapping RelativeLayout doesn't have to be a RelativeLayout, if this behaviour is possible with an other layout.
It sounds like you need a FlowLayout. Android does not provide one out-of-the-box, but check out this answer. How can I do something like a FlowLayout in Android?
Thanks to the answer of chessdork, I found the flexbox layout of google.
It gets provided by the following git repository.
https://github.com/google/flexbox-layout
I want to make a layout as given in the image below, but I am unable to make same as it is showing in the image. I have to align the buttons exactly same as that they are given in the image attached. Here is my code how I am making it and I am not able to get the alignment exact as displayed.
I have added my resources also (buttons and background)
<Button
android:id="#+id/button_home_driver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="60dp"
android:layout_marginLeft="38dp"
android:background="#drawable/driverbtn" />
<Button
android:id="#+id/buttonPassengerHomeScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button_home_driver"
android:layout_marginBottom="10dp"
android:layout_marginLeft="70dp"
android:background="#drawable/passengerbtn" />
</RelativeLayout>
I am not able to align the buttons along the Tilted Black Divider Screen.
Anyone please help me in the same. (Both the buttons should start from the Black Boundary as shown in the image)
Below is how I have designed my xml. Now what I am trying to fit a textview inside the white box shown below. But am being restricted by FrameLayout (at least I think so) that I need to hard code values to make the text view fit in the middle or some where inside the white box. I cannot use Relative or other layouts for this purpose as I have understood by my trials as this whole is a single image.
Here is my layout,
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:visibility="visible" android:layout_marginTop="60dip"
android:layout_gravity="center" android:id="#+id/xxx">
<ImageView android:id="#+id/calloutquizImage"
android:background="#drawable/callout" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:scaleType="fitCenter" />
<ImageView android:id="#+id/triviaImage"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignTop="#+id/calloutquizImage" android:layout_gravity="left"
android:src="#drawable/trivia" android:background="#drawable/trivia"
android:layout_marginTop="50dip" android:layout_marginLeft="85dip"></ImageView>
<TextView android:text="TextView" android:id="#+id/triviAnswerText"
android:layout_marginTop="125dip" android:layout_marginLeft="85dip"
android:layout_gravity="left" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#000000"
android:typeface="sans"></TextView>
<ImageButton android:id="#+id/triviaanswercloseButton"
android:src="#drawable/closebtn" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/closebtn"
android:layout_marginRight="8dip" android:layout_marginTop="43dip"
android:layout_gravity="right" android:onClick="triviaanswerClose"></ImageButton>
<ImageView android:id="#+id/buttontoclose"
android:layout_gravity="left"
android:visibility="visible" android:onClick="triviaanswerClose"
android:layout_marginTop="50dip" android:layout_marginLeft="75dip"
android:layout_width="230dip" android:layout_height="170dip"></ImageView>
</FrameLayout>
Because of this the text view looks in different positions in various handsets.
Any guesses what can be done for this instead?
Below is my image :
I think you are not doing the right thing. If you want a text to appear inside a white box (or even resize it, if there is to many text to fit to it) - you can still avoid any layouts ad do it with only one TextView.
Please have a look what is NinePatch image in Android:
http://developer.android.com/reference/android/graphics/NinePatch.html
http://developer.android.com/tools/help/draw9patch.html - drawing tools
So basically you will need only 1 textView and your image, properly converted to 9-patch with 2nd link. (Basically - just add a few black pixels on image border).
No just set this 9-patch as a background of textView. It will place text right where you need, and will shrink white box if you'll define so in 9-patch.
UPD:
Please find the resulting screenshot:
As you can see, textView not handles
WhiteBox" itself, filling it with text and resizing the box if necessary.
Here is how to make it work:
<RelativeLayout 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">
<TextView
android:id="#+id/first"
android:background="#drawable/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manymanymany text
Manymanymany text
Manymanymany text
Manymanymany text
Manymanymany text
Manymanymany text" />
<TextView
android:layout_below="#+id/first"
android:background="#drawable/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not so many text" />
</RelativeLayout>
And here is your image, converted to 9patch. Just place it to "drawable/" folder. Note: it MUST have "back.9.png" name.
For details of how 9patch works you can check links above. The main idea: by making black dots on left and top border - you specify which part of the image will be stretched when image must be upscaled. By making dots on right/bottom side you tell the View where to place the content. In our case content is a text of the TextView.
Hope it helps, good luck
I think you can use a RelativeLayout within the FrameLayout for the ImageView and the TextView, and by using the parameters, you can navigate the TextView to the white box. Refer to the LayoutParams documentation for details.
for eg. you can add the ImageView block first and then the TextView, so that the TextView will lay over the ImageView, and by using align bottom, and specifying top margin with a negative value, you can make the TextView go over the image. Or rather, if you are using eclipse, you can directly move the text view in the graphic layout.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:id="#+id/xxx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="0dp"
android:visibility="visible" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margintop="0dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/user2" />
<TextView
android:id="#+id/Textviewtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:layout_below="#+id/imageView1"
android:layout_marginTop="-10dp"
app:context=".TestActivity" />
</RelativeLayout>
</FrameLayout>
Similar to above, you can specify margin left and right to properly position your TextView as you want. Check with graphic layout for feedback to know the correct position.
Please reply if this helped.
Use your images and values for the height and width. I just tried for testing.
I have a TextView with a background that declared as:
<TextView
android:id="#+id/questionText"
android:layout_width="250dp"
android:layout_height="100dp"
android:gravity="center"
android:textSize="26px"
android:textColor="#FFFFFF"
android:background="#drawable/blue"
/>
the problem is that the background image is like a frame, so i want the
text inside to have a margin from the background image dimensions.
Is this possible?
10X alot,
have a gr8 weekend :)
What you want sounds like padding.
<TextView
android:id="#+id/questionText"
android:layout_width="250dp"
android:layout_height="100dp"
android:gravity="center"
android:textSize="26px"
android:textColor="#FFFFFF"
android:background="#drawable/blue"
android:padding="5dp"
/>
This would put a 5 pixel border around the actual text showing the background through.
Incidentally, layout_margin is also a parameter and is used to put space between the entire view, background included, and other views.
I'd recommend to make 9-patch drawable. It defines where the text can be without specifying padding in the control.