How to create fixed layout in Android - java

My activity has spinners, which supposedly should be in the centre. Relative to the spinners positions, buttons and labels are placed.
Another activity has 8 buttons in which certain values will be displayed. I have used a RelativeLayout in all activities. My app works well on my Moto G, but on bigger devices, the alignment of all the buttons and spinners changes.
I want to fix that issue.

Use this in the case of RelativeLayout
android:layout_centerInParent="true"
This will make the element always stick to the center of the device irrespective of the size of the device. Try it..
eg:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton android:id="#+id/btnSample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"></ImageButton>
</RelativeLayout>

Related

Element of listview out of bounds(visually) because of round edges

I am making an android app using Java in android studio. I have defined drawable background for listview with round edges. But last shown row is visually out of listview container. It is not just the line separating it from next record but also "-" symbol can be halfway out of the container. The app looks like this
is there a way to keep content inside the bounds?
It's happening bcz your background is different to listview and list view is always a square layout.
So basically, use cardview and set app:cardCornerRadius="" to cardview and list view set inside of cardview.
like this.
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="YOUR_BACKGROUND"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
OR
The other option is to use a round corner layout.
Git link
and set list view inside of any RoundCornerLayout.

Android randomly positioned Views in Layout

On Android I need to populate a fragment dynamically (programmatically) with a layout view item. Those items need to appear randomly positioned in the fragment screen, so I guess I need to set their params of position with a random function.
However, AbsoluteLayout is now deprecated and I don't see how can I consistently populate the fragment by setting a random position to each of those views. With RelativeLayout they're all relative to each other, with LinearLayout they all are ordered, with constraints layout would be chaotic to do this.
What would be the best way to populate a layout with randomly positioned views dynamically?
My background Layout is just a Constraint Layout with an image at the center. I need the other images to appear randomly in the screen.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/center"
android:src="#mipmap/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Android TabWidget White Space Issue

I am working on the TabWidget and i am getting a white space between the TabActivity and the Tabs.
I don't know how to remove this white space please can any one help me out how should i remove it, my tab_main.xml is as follows :
<?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" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
I tried to put background color of TabWidget to android:color:transparent but still same white space comes between the Activity and the Tab.
I do not want any separation space between my Activity and the Tabs (Is it possible to do it?)
Please can any one suggest me how to get rid of this white space.
The output you've shown most likely suggests a Margin/Padding error, rather than a layout error. Either your TabWidget or your FrameLayout could have a margin getting inherited by the overall theme that you're using.
Most likely the following will fix the problem (note the extra margin/padding styles):
<FrameLayout
android:layout_marginBottom="0dip"
android:layout_paddingBottom="0dip"
android:background="#android:color/holo_green_light"
android:id="#android:id/tabcontent"
....
.... />
<TabWidget
android:layout_marginTop="0dip"
android:layout_paddingTop="0dip"
android:background="#android:color/holo_blue_light"
android:id="#android:id/tabs"
....
.... />
Your FrameLayout could also have any nested components which have a margin/padding, and which in turn may cause the white space. I've added the android:background to both the FrameLayout and the TabWidget, just to help troubleshoot if there is any white space in between.
You said that you've tried putting background color of the TabWidget to transparent - but it won't help, because if there is a margin - then the underlying activity's background color shows up as white, below the transparent area. If you set the overall RelativeLayout's background color to something like blue - then you will see the blue coming through. So fix the margin/padding rather than trying to hide it.
It Look's Like you are having similar issue as i was having in my project, check out this project and try to implement the drawable xml files and provide your TabWidget with the specific custom dimensions as given in this project. It will surely work for you.
From your screen shot, it looks like you are using a WebView. Webview comes with a default margin and padding > 0. You can set the padding and margin to zero by overriding the WebView's body tag, or using css.
For further details, please see the answers to the following SO question:
Remove unwanted white space in webview

Is it possible to include a MapView as an element of another activity?

Just as the title says...I want to include a MapView inside of another activity. For instance, I want the MapView to take up half of the screen, and below that, include some other widgets - text fields, buttons, whatever. I have been tinkering with it, but so far I have been unsuccessful - the map does take up only half the screen, but no widgets show up.
Any clues? Is this possible, or is the Google Map integration strictly using the map as a full screen?
Please post your xml layout!
You are just doing it wrong. :-D
A mapview will work like any other widget. I strongly believe there is a mistake in your layout.
This one will work for instance!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/h1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox
android:text="Satellite"
android:id="#+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
<ImageView
android:id="#+id/image"
android:layout_width="75dip"
android:layout_height="75dip"
android:layout_marginRight="6dip"
android:src="#drawable/uoicon" />
</LinearLayout>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0zDdYFYf6Ir2W-NuiHPLAoFjsq0nmqRhPfzjY3A"/>
</LinearLayout>
Just a guess but you're using a LinearLayout to contain the MapView and other widgets but you forgot to set the orientation to vertical? The default for LinearLayout is horizontal.
It came to mind because it's something I've done 2 or 3 times myself. Totally confusing as effectively the other widgets 'disappear' off the right-hand side of the screen. It eventually got engrained in my mind and I always make sure to explicitly specify orientation now when I use LinearLayout.

Android FrameLayout can't set a View visible when it's initially set to invisible

I have met a very strange issue in android FrameLayout.
I am trying a very simple game which has a SurfaceView, and when the game ends, I want to popup another view on top probably displaying some text. This is very similar to android sample lunarlander code. However I can't get it work. Here is the strange issue:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/game_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.tmh.FunMatch.MainGamePanel
android:id="#+id/game_panel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.tmh.FunMatch.MyText
android:id="#+id/text"
android:text="test"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#88ffffff"
android:textSize="24sp"/>
</RelativeLayout>
For above FrameLayout, I want to show the second TextView when game finished. However it won't show after I call TextView.setVisibility(VISIBLE).
I did a lot experiment, if I initially set the TextView as VISIBLE, then at some point set it to INVISIBLE in the code. (My understanding is the TextView must be initially showed on the screen once then I turned it into INVISIBLE). If I did so, when the game finished, the TextView will be correctly shown.
I highly doubt it's an android bug. I changed android LunarLander code, I don't know why LunarLander did work and show the Text on the screen.
If anyone had run into the same issue, please tell me what's wrong with my code or is there a hidden bug?
Thanks

Categories

Resources