Trying to write a game such that most of the screen gets filled with my GameView (custom view, derived from View)
I then want to have an area at the bottom of the screen for messages etc. In the following I'm just trying to put a button there to illustrate the issue.
<?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">
<my.GameView
android:id="#+id/gameview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Action1" />
</LinearLayout>
When this runs the button never shows. If I move the button before the GameView then it works with the button at the top of the screen. As it is above GameView is somehow grabbing all the screen. Tried also giving GameView a layout_weight of 1 with button having 0 (and vice-versa)
Do I have to implement the onMeasure stuff in GameView (which I couldn't quite get my head round yet) or am I missing something?
If the size of your GameView is as big as the screen, your Button won't show, because with "wrap_content" you give your GameView the permission to take as much of the LinearLayout as it needs.
Try it something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="+#id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<my.GameView
android:id="#+id/gameview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/myButton"/>
</RelativeLayout>
Hope this helps
With the LinearLayout, just give a layout_weight of 1 to the GameView, don't add any weight to the button. That should work.
If it still doesn't work, then also use 0px for the GameView layout_height, eg:
<my.GameView
android:id="#+id/gameview"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"/>
Related
My Android app's main activity looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:orientation="vertical"
android:paddingLeft="60dp"
android:paddingTop="53dp" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/billAmountText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/billAmount_string"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/billAmount"
android:layout_width="173dp"
android:layout_height="wrap_content"
android:layout_below="#id/billAmountText"
android:layout_marginTop="3dp"
android:inputType="numberDecimal" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
In order to make sure the background image (defined in the first RelativeLayout) does not get squeezed when the keyboard pops up, I have set android:windowSoftInputMode="stateVisible|adjustPan in the manifest file for my activity. All good with the background and layout now, nothing gets resized.
But the problem is that my ScrollView doesn't work now because I suppose the system thinks the window doesn't need resizing due to my above modification to the manifest - so the ScrollView is not activated. The opposite happens when I take android:windowSoftInputMode="stateVisible|adjustPan out, the scrolling works, but the background gets squeezed when the keyboard pops out.
Is there any way to code it so that my background doesn't get squeezed, and my ScrollView still works? Thanks!
Try to use in your activity the property windowSoftInputMode with the following values:
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"
I don't know if it's necessary but you can try to add to your layout's ScrollView tag the fillViewPort to true.
So it should look like something I show you below:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<!-- Other Views -->
</ScrollView>
Let me know about your progress.
In your scrollview put a relative layout, child 0 is an imageview, and child 1 is your content. Use the imageview for your background image (src) rather than the background of the scrollview. The imageview will respect scaleTypes, whereas the background of a layout will not.
when I turn the phone and the screen is rotating I cannot see the whole screen anymore. This is ok but I cannot scroll! Do I have to set some flag or property for scrolling? This is part of my xml...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*">
...
</TableLayout>
<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
...
</TableLayout>
</LinearLayout>
Thanks!
You have to put your complete layout inside a ScrollView in order for it to scroll.
It scrolls if your layout height is more than the screen height, which happens generally in landscape mode.
In your case put the LinearLayout inside a ScrollView, since ScrollView can only have 1 child.
You must know that in order to use an scrollView , you must put only one component inside (maybe a linearLayout) .Maybe that's your problem if you are trying to put in more components at that level.
I have a problem with my ScrollView. It has an ImageView inside it. The imageView holds an image which is 480 * 3000 px. So It needs to be in a ScrollView so the user will have the ability to scroll down.
The problem is: when I test the application, the ScrollView does not wrap to the image hight. There is a black space under the image which is strange for me.
This is my XML code. I look forward for your opinion
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:src="#drawable/image01"
android:scaleType="fitStart" />
</ScrollView>
</RelativeLayout>
Update:
This is how my image looks like with #ruhalde code. I can't take a screenshoot since it is big and you'll not see the whole picture so I draw it.
Rewrite your layout. Make the ScrollView the outer most element. Take a look at this good tutorial for more information: http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts
Use a LinearLayout instead of RelativeLayout which is deprecated by now. Set fillViewPort=true in your ScrollView XML or programatically if you want with setFillViewPort(true).
Put imageview inside a LinearLayout and that inside ScrollView, your XML should look like this:
LinearLayout
---ScrollView
----LinearLayout
------ImageView
Check out my XML here working good with a 800 x 4000 pixels image, perfectly scrolling vertical:
<?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">
<ScrollView android:id="#+id/scrollView1"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:id="#+id/linearLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="#+id/mapa" android:layout_height="wrap_content"
android:layout_width="wrap_content"></ImageView>
</LinearLayout>
</ScrollView>
</LinearLayout>
To those who might be interested:
My problem was with the image I'm using, not how I organize my layouts. Thanks.
I believe you should use scrollView tag before your layout tag
Probably my question might be silly. I posted to know whether there is a possibilities to add two surfaceview object under a Relativelayout.
I need One surface view to stream Video behind and another view do sprite animations.
I tried putting them under a relative layout but the surfaceview i put first on layout takes preference and second view disappears.
Kindly suggest me a solution.
EDIT :
<RelativeLayout android:id="#+id/widget31"
android:orientation="vertical"
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent">
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="#+id/widget30" android:orientation="vertical">
</LinearLayout>
<com.mycalss.CameraView
android:id="#+id/camerapreview" android:clickable="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:focusable="true"/>
</RelativeLayout>
Now i add my graphic's surfaceview inside linearlayout-widget30 in code
You don't appear to be properly sizing your Views: try setting the layout_height of both to 0dp and then setting the layout_weight of both to 1 and see if they both show up.
I am trying to put seekbar below Webview that shows Map.
However when i place the seekbar below WebView in main .xml, it doesn't come up. Can someone tell me what's wrong?
<?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"
>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webkit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<SeekBar
android:id="#+id/frequency_slider"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="1"
android:progress="0"
android:progressDrawable="#drawable/seekbar_progress"
android:paddingLeft="4dip"
android:paddingRight="4dip"
/>
</LinearLayout>
Your WebView has height set to "fill_parent" and thus it will take up the whole screen. So your seek bar is getting pushed off the bottom. You have a few options: set the height for your webview to something smaller than "fill_parent" (some static value in dip units, or wrap_content). Or use a RelativeLayout instead of linear and make use of the layout_alignParentBottom="true" attribute for the seekbar. And layout_above="#+id/frequency_slider" for the WebView. There are other ways you could solve the problem, but those are the easiest two that come to mind for me.