I have a layout A composed by a set of 2 texts and one image followed by another set of 2 texts and one image (they are separated by a line). I want to inflate this layout (lets say A) and put it inside another one (lets say B). The problem is that the images and the separator are getting in the middle of B, while I wanted them in the middle of A. My code is:
Layout A (to be inflated):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootLayout"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:background="#drawable/bg_numeros"
android:orientation="vertical" >
<ImageView
android:id="#+id/separator"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/divisoria_numeros" />
<ImageView
android:id="#+id/artistsDoll"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/separator"
android:scaleType="fitCenter"
android:src="#drawable/boneco_artistas_numeros" />
<ImageView
android:id="#+id/songsDoll"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:src="#drawable/bonecos_numeros" />
<TextView
android:id="#+id/songsNumbers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/artistsDoll"
android:layout_toLeftOf="#+id/songsDoll"
android:layout_toRightOf="#+id/separator"
android:text="blabla"
android:textColor="#333333"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/songsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/songsNumbers"
android:layout_toLeftOf="#+id/songsDoll"
android:layout_toRightOf="#+id/separator"
android:text="SOME SONGS"
android:textColor="#999999"
android:textSize="11dp" />
<TextView
android:id="#+id/artistsNumbers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/artistsDoll"
android:layout_toLeftOf="#+id/artistsDoll"
android:text="blabla"
android:textColor="#333333"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/artistsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/artistsNumbers"
android:layout_toLeftOf="#+id/artistsDoll"
android:text="SOME ARTISTS"
android:textColor="#999999"
android:textSize="11dp" />
</RelativeLayout>
I am inserting the inflated layout like this:
RelativeLayout A = (RelativeLayout) Patterns.mainContext
.getLayoutInflater()
.inflate(R.layout.A, null);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.BELOW, someTopLayout.getId());
B.addView(A, rlp);
Note that although the attributes android:layout_height="fill_parent" and android:layout_centerHorizontal="true" are set for the images, I expected them to centered in A, not B.
Any help??
The solution found here: How to use layoutinflator to add views at runtime?
Solved my problem.
RelativeLayout A = (RelativeLayout) Patterns.mainContext
.getLayoutInflater()
.inflate(R.layout.A, parentLayout, false);
Related
I'm trying to align 3 buttons in a row in the center of the screen. Each button will have a background color, an image, and some text under the image. I've tried using LinearLayout but I can't put the image over the button. I've tried RelativeLayout, and I managed to get them in a row, but they are not in the center of the screen. Any advices?
<Button
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="85dp"
android:paddingBottom="10dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:background="#drawable/light"
android:text="#string/start"
android:textColor="#color/text"
android:textSize="8pt"
android:textStyle="bold"
android:layout_marginTop="0dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:src="#drawable/hstart"
android:layout_alignLeft="#+id/start"
android:layout_alignStart="#+id/start"
android:layout_alignRight="#+id/start"
android:layout_alignEnd="#+id/start"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="0dp" />
Here is your Button view (your_btn_view.xml in the layout folder):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true"
android:background="#android:drawable/btn_default"
android:gravity="center" >
<ImageView
android:id="#+id/btn_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_add" />
<TextView
android:id="#+id/btn_tv"
android:text="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Here is your layout view containing three buttons:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<include
android:id="#+id/btn1"
layout="#layout/your_btn_view" />
<include
android:id="#+id/btn2"
layout="#layout/your_btn_view" />
<include
android:id="#+id/btn3"
layout="#layout/your_btn_view" />
</LinearLayout>
Then, access and fill in the appropriate TextView and ImageView in your onCreate() like so:
#Override
public void onCreate(Bundle sIS){
super.onCreate(sIS);
setContentView(R.layout.that_layout_above, this, false);
LinearLayout btn1 = (LinearLayout)findViewById(R.id.btn1);
((TextView)btn1.findViewById(R.id.btn_tv)).setText("my text");
...
Use a LinearLayout to hold a RelativeLayout for each button/image combination. "Nested" layouts is pretty common to get this type of visual effect.
I have android layout with three parts (imageview, textview, imageview) and I want the last part move to right side, now its near the second element.
My code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/menu_button_bg"
android:orientation="horizontal"
android:paddingLeft="13dp" >
<ImageView
android:id="#+id/menuItemIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginBottom="13dp"
android:layout_marginRight="13dp"
android:layout_marginTop="13dp"
android:contentDescription="#string/menuItemDesc"
android:gravity="center_vertical" />
<TextView
android:id="#+id/menuItemTitle"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:gravity="center_vertical"
android:textSize="16sp" >
</TextView>
<ImageView
android:id="#+id/menuItemIconRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="13dp"
android:layout_marginRight="13dp"
android:layout_marginTop="13dp"
android:layout_weight="0.1"
android:contentDescription="#string/menuItemDesc"
android:gravity="right" />
</LinearLayout>
You can give the TextView the following attributes:
android:layout_width="0dp"
android:layout_weight="1"
This will make the TextView stretch to use up the space between the two ImageViews. The other alternative is to use a RelativeLayout instead of a LinearLayout, but this should work for your needs.
I have this layout inside of a RelativeLayout:
<LinearLayout
android:id="#+id/llWatchItemCommentButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/myMasterCat_Item"
style="#style/DropShadowEffect"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="99"
android:shadowColor="#ffffff"
android:text="Item"
android:textColor="#515b61"
android:textSize="22sp" />
<ImageView
android:id="#+id/bFollowComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/featured_button_selector"
android:padding="5dp"
android:src="#drawable/comment" />
</LinearLayout>
<TextView
android:id="#+id/myMastCat_Cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/llWatchItemCommentButton"
android:layout_alignLeft="#+id/llWatchItemCommentButton"
android:layout_marginLeft="10dp"
android:text="MasterCat"
android:textColor="#666"
android:textSize="16sp" />
Essentially, if that TextView in the Layout becomes two lines, it will overlap with the TextView positioned below theLinearLayout`.
Is there an attribute perhaps that could correct this? As in, I want the bottom TextView to be below the entire LinearLayout at all times, even if it begins to grow. Right now, the position appears fixed.
Have you tried the following:
<TextView
android:id="#+id/myMastCat_Cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/llWatchItemCommentButton" //add this line in
android:layout_alignBottom="#+id/llWatchItemCommentButton"
android:layout_alignLeft="#+id/llWatchItemCommentButton"
android:layout_marginLeft="10dp"
android:text="MasterCat"
android:textColor="#666"
android:textSize="16sp" />
I'm actually coding an app for Android and I need some help to do something.
I need to do a kind of popup message with this image: http://img716.imageshack.us/img716/9331/postitz.png. This image must have 2 TextView, one of them is a title and the other one is the message.
I have tried some options like Dialog with its own layout, own style, but the image fills all the screen instead of wrap the content of the TextViews. I have also tried to do it with a new Activity and occurs the same, the Image fills all the screen.
My actual layout is this:
<?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:orientation="vertical"
android:gravity="center_vertical"
android:background="#android:color/white">
<LinearLayout
android:id="#+id/img_postit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/postit"
android:orientation="vertical">
<TextView
android:id="#+id/note_titulo"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="30dp"/>
<TextView
android:id="#+id/note_descr"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="30dp"/>
</LinearLayout>
</LinearLayout>
But I have tried with a lot of different combinations, RelativeLayout, FrameLayout, and I cannot find the solution to do this.
Any idea of how to resolve it?.
Thank's for all.
you said you used as a new activity. Did you use android:theme="#android:style/Theme.Dialog in your manifest file?
And be sure that the image is little bit smaller one such that it will be suitable to act as a pop up..
If you used a dialog what exactly is the problem? Didnt you get the dialog as a pop up ?
(i think the image is larger so that it might fill the entire screen)
Create your own class extending dialog.And use layout like following. I use this in one of my apps and the dialog window is wrapped around the buttons :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/ll2"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_below="#+id/ll1"
android:gravity="left"
android:orientation="horizontal"
android:paddingBottom="5dp" >
<RadioButton
android:id="#+id/start"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/stop"
android:background="#drawable/start_on"
android:button="#android:color/transparent"
android:checked="true" />
<RadioButton
android:id="#+id/stop"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="#drawable/stop_off"
android:button="#android:color/transparent" />
<RadioButton
android:id="#+id/restart"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/stop"
android:background="#drawable/restart_on"
android:button="#android:color/transparent" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/lltext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/detail_credit"
android:layout_below="#+id/ll2"
android:layout_marginBottom="15dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/startText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/stopText"
android:text="#string/start_server_icon"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/stopText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:text="#string/stop_server_icon"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/restartText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/stopText"
android:text="#string/restart_server_icon"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="#+id/lltext"
android:layout_centerHorizontal="true"
android:text="OK" />
</RelativeLayout>
final Dialog dialog_my = new Dialog(FIRST.this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog_my.setContentView(R.layout.about_us); //<PUT YOUR LAYOUT ID HERE>
TextView date = (TextView) dialog_my.findViewById(R.id.date);
TextView thought = (TextView) dialog_my.findViewById(R.id.thought);
TextView ok = (TextView) dialog_my.findViewById(R.id.ok);
TextView link = (TextView) dialog_my.findViewById(R.id.link);
TextView contact = (TextView) dialog_my.findViewById(R.id.contact);
WindowManager.LayoutParams lp = dialog_my.getWindow().getAttributes();
lp.dimAmount = 0.0f;
dialog_my.getWindow().setAttributes(lp);
dialog_my.getWindow().addFlags(
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog_my.show();
I have a listview with a custom adapter in which I have 3 textviews, two are next to each other and one is below the others. The problem I am running into though, is when the first textview extends, it pushes the other textview off the screen.
I have not been able to successfully get this to work, here is an example of what's happening:
As you can see when the first textview gets too long, it then pushes the other text off the screen. I would like it so it wraps but the date textview is also showing. It should also be functional in horizontal view.
Here's the listview adapter code:
<RelativeLayout android:id="#+id/vw1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:descendantFocusability="blocksDescendants" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/liName" android:textSize="17sp"
android:textColor="#333" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="left"
android:layout_gravity="left" />
<TextView android:id="#+id/liDate" android:textSize="12sp"
android:textColor="#666" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="right"
android:layout_gravity="right" android:width="60sp" android:layout_toRightOf="#+id/liName" />
<TextView android:id="#+id/liNum" android:textSize="12sp"
android:textColor="#666" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#+id/liName" />
</RelativeLayout>
I am assuming that the text view being pushed is the liDate one.
You need to put this one before the liName in your layout and set the width to "wrap_content" and align it right. Then you lace the liName to the left of it with "fill_parent" width
So it would look like this:
<RelativeLayout android:id="#+id/vw1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:descendantFocusability="blocksDescendants" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/liDate" android:textSize="12sp"
android:textColor="#666" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:width="60sp"
android:layout_alignParentRight="true" />
<TextView android:id="#+id/liName" android:textSize="17sp"
android:textColor="#333" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/liDate" />
<TextView android:id="#+id/liNum" android:textSize="12sp"
android:textColor="#666" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#+id/liName" />
Hope this is what you meant
The key was using "#+id/" for the first textview instead of "#id/"
This worked for me:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/listEventTypeText"
android:layout_toLeftOf="#+id/listEventDateText"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="#+id/listEventDateText"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#id/listEventTypeText"
/>
</RelativeLayout>