Linear and Relative Layout in Android - android

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>

Related

Android: ScrollView blocking buttons from RelativeLayout

My app currently looks like this:
When I click on a button, the border color changes to green as shown above. I added a scrollview for my list of words as shown on the right side of the image. The problem is that this scrollview covers nearly the entire width of the screen and I am unable to click on the the first three rows of buttons.
A brief description of the layout:
I created a relative layout. The 25 buttons are in this relative layout. Then also within the relative layout I created a scrollable linear layout containing 20 textviews
Can someone please help me fix this layout? I want to be able to click on all the buttons and have the scrollview not block the buttons.
And I thought it might be a good idea to put the 25 buttons in its own relative layout within the main relative layout. Does anyone know how I can do that without redoing the entire layout?
Also, if you guys have any advice on the correct way to code the layout please let me know. I am fairly knew to android programming and don't know if I should use relative layout vs linear layout vs frame layout or match_parent vs fill_parent vs wrap_content. Please let me know if I am misusing any of them.
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".Game" >
<Button
android:id="#+id/button1"
android:layout_width="#dimen/size"
android:layout_height="#dimen/size"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:background="#drawable/button"
android:onClick="ButtonOnClick" />
.
. (Buttons 2 - 24)
.
<Button
android:id="#+id/button25"
android:layout_width="#dimen/size"
android:layout_height="#dimen/size"
android:layout_alignBaseline="#+id/button24"
android:layout_alignBottom="#+id/button24"
android:layout_alignLeft="#+id/button20"
android:background="#drawable/button"
android:onClick="ButtonOnClick" />
<ScrollView
android:id="#+id/scrollable"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scrollbars="vertical" >
<LinearLayout
android:id="#+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp"
android:layout_marginTop="20dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/word1" />
.
. (TextViews 2 - 19)
.
<TextView
android:id="#+id/textView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/word20" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Use a LinearLayout instead of RelativeLayout as your top layout and make it android:orientation="horizontal".
Then put all of your buttons in a RelativeLayout with an android:layout_weight="1" and android:layout_width="0dp".
Also put the ScrollView in a second RelativeLayout with a width of "wrap_content".
Here is the structure:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_widtht="0dp"
android:layout_heigh="fill_parent"
android:layout_weight="1">
All buttons go here...
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent">
Scroll view goes here...
</RelativeLayout>
</LinearLayout>

Android Layout: cannot see the children over the parent

I have the Layouts as you can see in the picture (sorry I cannot post them normally yet):
https://dl.dropboxusercontent.com/u/28640502/Unbenannt.bmp
However, when I execute the app I can only see the camera preview and not the text nor the SeekBar. I know they work, because when I reduce the size of the camera I can see them and interact, but if I want the camera to be like background and then the children over it, it doesn't work.
I have been checking a lot of threads with similar problems but I don't find the solution:
1, 2, 3... Any idea? Thanks a lot!
Here is my xml code just in case:
<?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_weight="0.55"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/show_height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:layout_marginRight="10sp"
android:layout_weight="0.12"
android:text="H" />
<SeekBar
android:id="#+id/select_height"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="5sp"
android:layout_weight="0.91" />
<TextView
android:id="#+id/show_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.06"
android:text="Dist" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
try to input both of your layouts in relative layout for example
<LL>
<RL>
<FL>
</FL>
<LL>
</LL>
</RL>
</LL>
this is hapenning because of overlaiyng of LL by FL
You could use the bringToFront() method of the View class.
SeekBar seeker = (SeekBar)findViewById(R.id.select_height);
seeker.bringToFront();
Alernatively you could use the sendToBack() function to put a view behind other views.
Also remember that a view's z-index is determined by the order in which the view is declared in the xml layout file.
I'd also suggest changing the camera preview layout and it's first child layout to be siblings. The one added last in the xml file will be on top.

Have ways for remain previous layout in all pages?

I want to create an application that have 5 buttons in bottom of page, when user clicks
on the any button, the page will changes but 5buttons remain in bottom of page. now i create
this program but when i call setContentView() in other class becuase i call other layout,
buttons removed. have ways for remain 5buttons? or i should create 5buttons again in 5layout?
Thanks
Cheers
Easiest and Simple way is to make a layout having 5 buttons and it in all your layout files
Example
<include layout="#layout/titlebar"/>
Or another way is to use fragments so tht only fragments with change keeping other of your layout stuff same
Edit
OR Use the <merge> Tag
The <merge /> tag helps eliminate redundant view groups in your view hierarchy when including one layout within another. For example, if your main layout is a vertical LinearLayout in which two consecutive views can be re-used in multiple layouts, then the re-usable layout in which you place the two views requires its own root view. However, using another LinearLayout as the root for the re-usable layout would result in a vertical LinearLayout inside a vertical LinearLayout. The nested LinearLayout serves no real purpose other than to slow down your UI performance.
To avoid including such a redundant view group, you can instead use the element as the root view for the re-usable layout. For example:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/delete"/>
</merge>
Now, when you include this layout in another layout (using the tag), the system ignores the element and places the two buttons directly in the layout, in place of the tag.
Please go through this link to gain more information about Reusing layouts
From what I can gather, I think you are looking for tabs instead of Buttons. Take a look at this.
Thanks for answer. I read developer link and i add merge and include tag to xml file but the program has errors. Why?
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<include layout="#layout/location"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/button_home"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/home_icon"
android:text="#string/button_home"
android:textColor="#color/text_home" />
<Button
android:id="#+id/button_product"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/product_icon"
android:onClick="Product"
android:text="#string/button_product"
android:textColor="#color/text_product" />
<Button
android:id="#+id/button_places"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/places_icon"
android:onClick="Places"
android:text="#string/button_places"
android:textColor="#color/text_places" />
<Button
android:id="#+id/button_rewards"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/rewards_icon"
android:onClick="Rewards"
android:text="#string/button_rewards"
android:textColor="#color/text_rewards" />
<Button
android:id="#+id/button_more"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/more_icon"
android:onClick="More"
android:text="#string/button_more"
android:textColor="#color/text_more" />
</LinearLayout>
</LinearLayout>
location.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView>
<com.google.android.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:apiKey="0cPRv243zM1_S3ydsNg8MJP9_6BfCp642jOhPvQ"/>
<LinearLayout
android:id="#+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</merge>
what is incorrect?
Thank you

LinearLayout Problem (2 LLayout in the same XML)

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.

Crashes whenever I try to use a custom view object in XML layout

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?

Categories

Resources