Android GridView Square Layout - java

I have an Android Activity with a GridView as Layout, and I want to let it look like squares (like the first Screenshot of this App: My Study Life). Can I specify this in the Layout File?
This is my Layout:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView1"
android:numColumns="2"
android:gravity="center"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>

The square behavior is handled by the grid items and not the GridView itself.
Google provides an example on how to load bitmaps effectively and shows them in a GridView with squared views.
A good start would be to check out the sample from http://developer.android.com/training/displaying-bitmaps/index.html

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>

How to create fixed layout in Android

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>

Google Play Store Cardview

I have been trying for a long time to make Cards similar to appearance that of Google Play Store, ie cards of Different sizes , with images and all inside the card. Any tips on how to proceed.
Basically you just create a layout (RelativeLayout, LinearLayout, etc) as a child of your cardview element and make it look however you want.
For example...
<android.support.v7.widget.CardView
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardElevation="2dp"
card_view:cardBackgroundColor="#android:color/white"
card_view:cardCornerRadius="2dp">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent">
//Your desired layout here
</RelativeLayout>
</android.support.v7.widget.CardView>
You can take a look at the MaterialList implementations for some good examples.

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.

Categories

Resources