I need to access the view present in a.xml layout included in another b.xml layout.
For example,
This is a.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/xyz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XYZ" />
</RelativeLayout>
And, in b.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include
android:id="#+id/a_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
layout="#layout/a" />
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/xyz"
android:text="Show me below xyz" />
</RelativeLayout>
I have to do it in xml code, because if I do it in Java it must be after setContentView() and then setting LayoutParams of TextView 'label' wont take effect.
I guess, everyone understand what I am trying to ask. Waiting for good replies.
Thanks all.
Image on the right is what I am trying to achieve and left one is what I am getting with the current code.
In b.xml, you should correct:
android:layout_below="#id/xyz"
to
android:layout_below="#id/a_layout"
And then you can use it in your code (Here I place it in onCreate):
setContentView(R.layout.b);
((Button)findViewById(R.id.xyz)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((TextView)findViewById(R.id.label)).setText("clicked on XYZ button");
}
});
The problem is not in accessing the View of included layout, but in the fact that you cannot achieve this layouts "overlapping". Let me explain: if you add some more views below the button in your a.xml and then try to place some view in your b.xml just below the button then it would make the views from b.xml overlap views from a.xml, however this has not been implemented in Android (yet?). So, the only thing you can do is to put android:layout_below="#id/a_layout" as #Hoàng Toản suggested.
P.S. You may observe same behavior with this a + b layouts combination:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/xyz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XYZ" />
</RelativeLayout>
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/xyz"
android:text="Show me below xyz" />
</RelativeLayout>
Related
I want to show a button bar at the end of my activity. I created a layout named: batton_bar_ref.xml:
<?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="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/shape_blue_light_horizental_gradiant_notrounded"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingTop="10dip" >
<Button
android:id="#+id/pay_button"
android:layout_width="100dip"
android:layout_height="50dip"
android:text="#string/pay"
android:textSize="16sp"
android:textStyle="bold" >
</Button>
</LinearLayout>
</RelativeLayout>
And add it with <include layout="#layout/button_bar_ref"/> to another layout.
It is showing a warning message :
This LinearLayout layout or its RelativeLayout parent is useless; transfer the background attribute to the other view.
Could you let me know how can I fix it?
Addenda :
button bar should be at the end of screen.
Note that Linear layout has a background.
It is just suggestion to optimize your layout by removing tags that are not required. In your case since you do not have any children under RelativeLayout other than LinearLayout, this particular layout is not really correctly designed.
First you can ignore this warning, and once you have successfully designed the layout file in which you are planning to include this layout file, you will realize that one of the layouts declared in this file can be eliminated.
Remove the relative layout.
<?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="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/shape_blue_light_horizental_gradiant_notrounded"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingTop="10dip" >
<Button
android:id="#+id/pay_button"
android:layout_width="100dip"
android:layout_height="50dip"
android:text="#string/pay"
android:textSize="16sp"
android:textStyle="bold" >
</Button>
</LinearLayout>
try removing the RelativeLayout tag from the xml file
This warning is triggered, when a Layout has only one child that is also a Layout. In this case one of both can be removed without any problems. It is recommended to remove these redundant layouts as they reduce the overall performance of the UI.
In your case there is no use of RelativeLayout. There will be no change if you remove this. So this warning is showing. But this is just a warning. There is no harm in using this
Please use bellow code fix your issue.
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="#+id/pay_button"
android:layout_width="100dip"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="#string/pay"
android:textSize="16sp"
android:textStyle="bold" >
</Button>
I'm having issues with a ListView using custom rows that are loaded from a database.
If, for the list screen, I place a button above the ListView, no visible rows appear in the listview.
However as soon as I remove the button, everything works fine. I want the button (or any other component) to appear above to make it more user friendly. Attached are the two code samples below.
This is the XML file of the ListView Activity that works:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/real_red_dark">
<LinearLayout
android:id="#+id/llMain"
android:layout_height="fill_parent"
android:background="#drawable/real_background"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:gravity="center">
<ListView
android:id="#+android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/llButton"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/no_sessions"
android:textStyle="bold"
android:textSize="18dp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
However, if I have the Button added above it, it will not show whatsoever:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/real_red_dark">
<LinearLayout
android:id="#+id/llMain"
android:layout_height="fill_parent"
android:background="#drawable/real_background"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:gravity="center">
<Button
android:id="#+id/btnSearch"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="3dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:text="Find Sessions"
android:textStyle="bold" />
<ListView
android:id="#+android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/llButton"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/no_sessions"
android:textStyle="bold"
android:textSize="18dp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
You could try adding the button programmatically as a header view in the listview itself, rather than in the xml layout.
use listView.addHeaderView(View)
http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)
It is better if you use RelativeLayout than LinearLayout,it is also recommended by the android docs.Try to use android:layout_height value as "wrap_content" for ListView and TextView(which you may use to indicate for empty rows),it may help to you.
Something which doesn't look right but I'm assuming it's just a typo in the above...
android:id="#+android:id/list"
...there shouldn't be a + between # and android:. Using #+ is for adding a new resource id of your own, i.e., #+id:. You're also doing the same thing for the TextView...
android:id="#+android:id/empty"
Another thing but not sure it's relevant is you're specifying...
android:layout_below="#+id/llButton"
...I doubt it's the problem as android:layout_below isn't valid for a LinearLayout (it's for RelativeLayout) but there isn't a Button with the id of llButton in your layout. If there was, the + would also be incorrect as you should be specifying an existing id.
Not sure if amending the above would fix things but it could just be that the layout inflation is coming out 'wrong' due to those issues.
I'm coding new XML fils for desinging an android app and I've some problem by using 2 linearLayour into the same xml...
I've "Error in a XML file: aborting build" with the following code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/texte_firsttab"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/accessGraphe"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="test"
android:onClick="selfDestruct" />
<Button android:id="#+id/accessGraphe2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="test2"
android:onClick="selfDestruct" />
</LinearLayout>
Can we put two linear layout in the same xml file?
Yes you can. But not two at top level.
See this example : http://developer.android.com/resources/tutorials/views/hello-linearlayout.html
You cannot have two top-level layouts. How would the system know how to arrange them? You need to enclose them in another layout that defines this.
I assume that the blanks in front of the <?xml ... tag are due to code formatting in your post and are not present in the actual layout file. That would also cause a problem.
No you cannot.... how would you refer to one or the other from the source code ?
If you want to have two linear layouts at the same time (one on top and one at the bottom), then you need to embed those within another layout :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weigth="1">
<TextView android:id="#+id/texte_firsttab"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weigth="1" >
<Button android:id="#+id/accessGraphe"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="test"
android:onClick="selfDestruct" />
<Button android:id="#+id/accessGraphe2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="test2"
android:onClick="selfDestruct" />
</LinearLayout>
</LinearLayout>
You have two root-level items, which shouldn't happen (this is not specific to Android layout files, you can have only one document element in any XML file).
I wouldn't recommend wrapping the to LinearLayouts in another one, that's too complicated; and it's generally a good idea to avoid nesting layouts, see this article about efficient layouts.
For a TextView and two Buttons, a RelativeLayout would be perfect. It's also more flexible than LLs.
Can someone explain to me why the ImageView is not appearing above the LinearLayout?
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/rev_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- some stuff in here -->
</LinearLayout>
<ImageView
android:id="#+id/rev_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_above="#id/rev_main"
/>
</RelativeLayout>
I don't get it.
This happens when you specify alignment relative to another layout. The solution I found was to go the other direction.
Instead of telling the ImageView to be above the LinearLayout, tell the LinearLayout to be below the ImageView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/rev_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rev_arrow">
<!-- some stuff in here -->
</LinearLayout>
<ImageView
android:id="#+id/rev_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
/>
</RelativeLayout>
The following should work for you:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/rev_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_alignParentTop="true"
/>
<LinearLayout
android:id="#+id/rev_main"
android:layout_width="fill_parent"
android:layout_below="#id/rev_arrow"
android:layout_height="wrap_content">
<!-- some stuff in here -->
</LinearLayout>
</RelativeLayout>
I had an similar problem by creating a custom optionsMenu. The simplest way to set z-order of views was to do it programmatically. If you want to switch the order sometimes, your should easily call:
ImageView yourImageView = (ImageView)findViewById(R.id.rev_arrow);
yourImageView.bringToFront();
I don´t know if it is adaptive to your application, but in my case it works perfect. If you need more code, let me know.
If you have such problem in ListView make sure that you use proper inflation method. Parent view group must be specified for correct inflation.
mLayoutInflater.inflate(R.layout.listitem, parent, false);
Don't use
mLayoutInflater.inflate(R.layout.listitem, null);
I have made myself a custom LinearLayout by the name of com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget that hosts a couple of spinner widgets. This custom LinearLayout inflates the following XML layout (You might not need to look at this too carefully but it's here for completeness):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Min value Spinner -->
<Spinner
android:id="#+id/discrete_numerical_range_selector_min_value_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1" />
<TextView
android:id="#+id/to_text"
android:text="to"
android:textSize="14sp"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="0">
</TextView>
<!-- Max value Spinner -->
<Spinner
android:id="#+id/discrete_numerical_range_selector_max_value_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1" />
I have placed one such object in the layout for one of my activities like so:
<?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">
<include layout="#layout/search_form_section_generic_top"/>
<include layout="#layout/search_form_section_car_specific"/>
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget/>
<include layout="#layout/search_form_section_advanced_options" />
</LinearLayout>
The problem is that my app force closes immediately upon startup. I've checked by putting breakpoints in my custom LinearLayout that none of my custom code is even being run yet. Furthermore, if I copy-paste the layout code for my compound widget in place everything works, which indicates to me that I probably haven't left any important XML attributes out. What could be going wrong?
I fixed it by making the LinearLayout XML element in the widget layout into a merge instead, and moved all of the layout parameters out of the widget XML file and into the activity XML itself, thus replacing
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget/>
with
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
If someone could tell me why this worked, it might help me and others doing it again, and you can take credit.
because you must specify the width and height of every view you use in you xml?