Great Issue with setting a button in landscape mode - android

I have an activity which is set up in the landscape mode and must remain this way(!So as an answer to my question don't tell me to set my activity in the PORTRAIT mode cause this is not a solution).
And I wanna put a button at the bottom of the screen like this:
http://i52.tinypic.com/30n9o5t.png
but I just can do it.All my attempts got me only this:
http://i53.tinypic.com/54acjs.png
Here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/surface_camera"
/>
<Button android:id="#+id/btnPhoto"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Photo !!"></Button>
</FrameLayout>
Don't forget my activity is in LANDSCAPE mode.
Thank you!

Using Image button instead.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/surface_camera"
/>
<ImageButton android:id="#+id/btnPhoto"
android:layout_gravity="right"
android:layout_width="your_button_width"
android:layout_height="your_button_height"
android:background="#drawable/your_button_here">
</ImageButton>
</FrameLayout>

The trick is done using layout_weight which is telling the layout to use all free space available. So you are creating two layouts the second with fixed height and the tell the first to use all the free space. This solution will work in both cases Landscape and Portrait mode.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:layout_weight="1.0">
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="40dp">
<!-- place your buttons in this layout -->
</LinearLayout>
</LinearLayout>
UPDATE
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:layout_weight="1.0">
</LinearLayout>
<LinearLayout android:layout_width="40dp"
android:layout_height="fill_parent"></LinearLayout>
</LinearLayout>

Related

Android LinearLayout with Button and FrameLayout using the Maximum Height

I am trying to make a layout where there are a LinearLayout and inside it I want a FrameLayout and a button.
I want the button to use the bottom of the linearlayout and to use the maximum width and the enough height for the button, using wrap content.
The other component, the FrameLayout I want to use it the remaining part of the screen.
How can I do it?
Here it the layout so far..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/button_capture"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="snapIt"
android:text="#string/Capture" />
</LinearLayout>
Is not showing the button :s
Thanks alot in advance ;)
Better to work out with relative layout with button aligned to parent bottom and your frame layout above it occupying the rest space of the screen.Like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/button_capture"/>
<Button
android:id="#+id/button_capture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="snapIt"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
use the following xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button_capture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="snapIt"
android:layout_alignParentBottom="true"
android:text="capture" />
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button_capture"
/>
</RelativeLayout>
i am just trying to make these layout, after compare my layout code and your layout code, i find a different code, there is in my code. Did you missing it or just move it away cause it useless from the layout code ?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<FrameLayout
android:id="#id/flayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world">
</FrameLayout>
</LinearLayout>

Two fragments in one layout but only one is shown

I am working on an android project and I am trying to make use of fragments but I am having an issue.
In my fragment activity I have a layout which contains two fragments. For some reason when I load the activity, I am only seeing the first fragment which is the query editor.
Below is the layout for the fragment activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment android:name="com.BoardiesITSolutions.MysqlManager.QueryEditor"
android:id="#+id/fragment_queryEditor"
android:layout_width="fill_parent"
android:layout_height="100dp"/>
<fragment android:name="com.BoardiesITSolutions.MysqlManager.ResultViewer"
android:id="#+id/fragment_resultViewer"
android:layout_width="fill_parent"
android:layout_height="300dp" />
</LinearLayout>
</RelativeLayout>
Below is the layout for the query editor fragment
<?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:background="#c1c1c1c1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="QUERY EDITOR"/>
</LinearLayout>
Below is the layout for the result view fragment
<?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:background="#000000">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RESULT SET VIEW"/>
</LinearLayout>
For some reason only the query editor is being shown.
Although at the moment the query editor is set to 100dp and the result view is 300 dp I am only doing this as a test.
What I am actually wanting to do is to the query editor set to 100dp and the result view take up the rest of the space.
Thanks for any help you can provide
Change
<fragment android:name="com.BoardiesITSolutions.MysqlManager.QueryEditor"
android:id="#+id/fragment_queryEditor"
android:layout_width="fill_parent"
android:layout_height="100dp"/>
<fragment android:name="com.BoardiesITSolutions.MysqlManager.ResultViewer"
android:id="#+id/fragment_resultViewer"
android:layout_width="fill_parent"
android:layout_height="300dp" />
with
<fragment android:name="com.BoardiesITSolutions.MysqlManager.QueryEditor"
android:id="#+id/fragment_queryEditor"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="100dp"/>
<fragment android:name="com.BoardiesITSolutions.MysqlManager.ResultViewer"
android:id="#+id/fragment_resultViewer"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="300dp" />
this way your fragments will occupy the same space in the LinearLayout
Why do you have a LinearLayout inside a RelativeLayout?
This should do the thing:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.BoardiesITSolutions.MysqlManager.QueryEditor"
android:id="#+id/fragment_queryEditor"
android:layout_width="fill_parent"
android:layout_height="100dp"/>
<fragment android:name="com.BoardiesITSolutions.MysqlManager.ResultViewer"
android:id="#+id/fragment_resultViewer"
android:layout_below="#+id/fragment_queryEditor"
android:layout_width="fill_parent"
android:layout_height="300dp" />
</RelativeLayout>

How to show other views after list view height set to fill_parent

To prevent multiple GetView calling I have set the height of ListView to fill_parent.
How to show other views than the ListView itself?
The following layout is inflated in a ViewPager:
<?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:padding="15dp" >
<ListView android:id="#+id/lv_viol_infraction"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<include layout="#layout/activity_prontuario_home" />
</LinearLayout>
where the included layout is:
<?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:padding="20dp" >
<ScrollView
android:id="#+id/prontuariohome_scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
.
.
.
.
</ScrollView>
</LinearLayout>
EDIT: This solution won't work for view higher than a button
I don't understand what you want to achieve, but if you NEED to have the height as fill_parent for some reason and can't be any other value, you can put the ListView with inside a LinearLayout with the height="0dp" and weight="1". So would be something like 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:padding="15dp" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<ListView android:id="#+id/lv_viol_infraction"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<include layout="#layout/activity_prontuario_home" />
</LinearLayout>
Try to use following code
android:layout_height="0dp"
android:layout_weight="1"
in ListView tag
With android:layout_height="0dip" the view will take the rest of the free place
You said this solution is wont work:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" android:layout_above="#+id/bt"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bt" android:layout_alignParentBottom="true"/>
just add it a android:layout_above="#+id/bt" to listview.
I'm sorry for being not clear.
Here is the link of the (probably) main thread asked here in Sof about the issue I was talking about.
After some tries I'm happy to give you the solution of my problem.
Giving another layout above the ListView lets the LV cuts the space not filled by the ListView itself
<?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:padding="15dp" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView android:id="#+id/lv_viol_infraction"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<include layout="#layout/activity_prontuario_home" />
</LinearLayout>

Android layout_gravity

I can't understand why the following code places my TextView in the top left corner of my screen. Based on my understanding of layout_gravity, it seems that it should place my TextView at the bottom of my screen instead.
Can anyone please shed some light on this for me? This Linear Layout, despite seeming very simplistic, has caused me numerous headaches. How can something that seems so simple, be such a royal pain to grasp?
<?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:background="#FF0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:layout_gravity="bottom"
android:background="#FFF"
/>
</LinearLayout>
A LinearLayout set to vertical orientation will ignore all layout_gravity parameters that refers to vertical alignment. So for example "bottom" wont work but "center-horizontal" will work. (The vertical alignment is always done so that each view in the layout i placed under the other views in the layout.)
A relative layout might be the way to go or maybe you can use this code that probably is pretty close to what you want.
<?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:background="#FF0000"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:gravity="bottom"
android:layout_weight="1.0"
android:background="#FFF" />
</LinearLayout>
Edit This blog explains things further.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:layout_alignParentBottom="true"
android:background="#FFF"
/>
</RelativeLayout>
you can use relative layout were you can align bottom easily
If that is the simplicity of your layout, you shouldn't wrap it in a LinearLayout at all.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#FFF"
android:text="HELLO HELLO" />
If you need to wrap it though, remove the vertical orientation:
<?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:background="#FF0000" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#FFF"
android:text="HELLO HELLO" />
</LinearLayout>
I think this code do what you want:
<?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:gravity="bottom"
android:background="#FF0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:background="#FFF"
/>
</LinearLayout>
And if you want the textview on center, you can use:
<?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:gravity="bottom"
android:background="#FF0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:layout_gravity="center"
android:background="#FFF"
/>
</LinearLayout>
Hope this help you.

Bottom bar layout in android

This is my android xml source code for a map - bottom bar layout. I am displaying a huge map in the screen and i need a 30dp tall layout in the bottom of the screen to display a few tab like images. How do i push the horizontal linear layout to the bottom of the page ?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height=" what here ? "
android:layout_width="fill_parent"
android:orientation="vertical">
<com.google.android.maps.MapView
android:id="#+id/map_view"
Map goes here. Almost full screen. Exactly full - 30dp;
/>
<LinearLayout>
I want 3 images here. Bottom of the screen.
</LinearLayout>
</RelativeLayout>
Use android:layout_alignParentBottom="true" to set the linearlayout at the bottom of the parent for linearlayout and android:layout_above for mapview to set the mapview above the linearlayout.
Your code will be:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout android:layout_height="30dip"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:id="#+id/linearLayout1"
>
<!-- ... -->
</LinearLayout>
<com.google.android.maps.MapView
android:id="#+id/map_view"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_above="#+id/linearLayout1"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="#+id/map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/rel_bottom" />
<RelativeLayout
android:id="#+id/rel_bottom"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_height="30dp" >
<ImageView
android:id="#+id/img1"
android:layout_width="40dp"
android:layout_height="fill_parent" />
<ImageView
android:id="#+id/img2"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_toRightOf="#+id/img1" />
<ImageView
android:id="#+id/img3"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_toRightOf="#+id/img2" />
</RelativeLayout>
</RelativeLayout>
use the properties of the RelativeLayout, like android:layout_alignParentBottom and android:layout_above.
I would do something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout
android:id="#+id/footer"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:width="fill_parent"
android:height="30dp">
add your images here
</LinearLayout>
<com.google.android.maps.MapView
android:id="#+id/map_view"
android:height="fill_parent"
android:width="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="#id/footer"/>
</RelativeLayout>
Try the following layout. Using android:layout_alignParentBottom="true" won't be enough as the MapView with android:layout_height="fill_parent" will fill the whole screen and hide the LinearLayout. You must also add android:layout_above="#id/menu" so it won't hide it.
<?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" >
<com.google.android.maps.MapView
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/menu"
android:apiKey="#string/google_maps_api_key"
android:clickable="true" />
<LinearLayout
android:id="#+id/menu"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true" >
</LinearLayout>
</RelativeLayout>

Categories

Resources