Android randomly positioned Views in Layout - java

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>

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.

How to make a layout in android that sticks to the bottom of the screen until the scrollview is scrolled to a certain position it gets scroll too?

I want to make a android activity screen where a layout is sticked to bottom until scrollview get scroll to a certain position. like in this video samples links:
https://drive.google.com/open?id=0B14wNBitoI33LWtUNThqcXRIUWc
https://drive.google.com/open?id=0B14wNBitoI33QW1BOGR1di1TcGc
amarjain07's StickyScrollView maybe a solution you would want to look into. His methodology of assigning the "sticky" views as identified attributes in the layout xml is quite a nifty feature IMHO.
Use a RelativeLayout and put the Button-Container inside it.
Pass the container in your_scrollview_activity.xml the following attribute
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1 (reft)" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2 (right)"/>
</LinearLayout>
</RelativeLayout>
NOTE: You need to set a listener in your activity to get the ScrollViews current position / current height. With this current height you can calculate the "left space" to the bottom / end of the ScrollView. If your RelativeLayout reaches the bottom or if it's close to the bottom AND within the visible area, then call your animation.

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>

Android GridView Square Layout

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

How to set a fixed size for ListView that is laid inside a Dialog and elements are added dynamically

I am trying to include a ListView within an Activity that is running inside a Dialog. The dialog is basically used to search for bluetooth devices, so I need two Button's, a TextView and a ListView. Here is my XML:
<?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"
android:padding="20dp">
<TextView
android:text="Bluetooth List"
android:paddingTop="15dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</TextView>
<ListView android:id="#+id/bluetoothPanel_lvBluetooth"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_margin="10dip"
android:drawSelectorOnTop="false">
</ListView>
<LinearLayout
android:orientation="horizontal"
android:paddingLeft="4.0dip"
android:paddingTop="5.0dip"
android:paddingRight="4.0dip"
android:paddingBottom="1.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/bluetoothPanel_btnSearch"
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:text="Search"
android:layout_weight="1.0" />
<Button
android:id="#+id/bluetoothPanel_btnCancel"
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:text="Cancel"
android:layout_weight="1.0" />
</LinearLayout>
</LinearLayout>
At the beginning, the ArrayAdapter that is within the ListView is empty, and I am dynamically add elements to it.
The problem is, I want to set a fixed size for the ListView so that it won't change its size upon adding elements dynamically (List of bluetooth devices are shown). Could you please help me to fix my XML?
EDIT:
I am not talking about array size. I am talking about the listview itself (the view), the height of the listview.
Set a custom ListAdapter for the list and override the getCount() method so it returns always your fixed size.
Example: http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx
I think you are asking how to reserve a fixed-size space for a dynamically changing ListView. In a similar situation, I wrapped the ListView in a LinearLayout, as shown below. Note that this is the xml for the ListView itself, not for the cell contents. The fixed height in this example is 100dip x 200dip.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/background_image"
android:layout_width="100dip"
android:layout_height="200dip">
<ListView android:id="#+id/list_view"
android:layout_width="100dip"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
/>
</LinearLayout>
Notice that the layout_width of the ListView also is set to the desired fixed width. I'm not sure why, but this seems to be necessary.
Also notice that the background image or color is set on the LinearLayout, not on the ListView. Setting the ListView's cacheColorHint to transparent makes the ListView play nicely.
If you were asking something else (perhaps, for example, fixing the scrollable height regardless of elements, which seems to be answered by #Stephen), you may want to clarify the question.

Categories

Resources