Responsive layout in Android - java

I want to make a responsive design in Android i.e. I need such a layout in which I will add views programmatically and they will adjust them according to screen size.
For example:
I want to add 10 views in a layout
When I add them if there is place for only three views per line then automatically they get adjust in four rows and three columns i.e
[0,0] [0,1] [0,2]
[1,0] [1,1] [1,2]
[2,0] [2,1] [2,2]
[3,0]
But as in landscape mode, there is more space vertically
[0,0] [0,1] [0,2] [0,3] [0,4]
[1,0] [1,1] [1,2] [1,3] [1,4]

Use flexbox layout
In builde.gradle add
dependencies {
...
compile 'com.google.android:flexbox:0.3.0'
}
and use it in the layout xml. i.e.
<com.google.android.flexbox.FlexboxLayout
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="wrap_content"
app:alignItems="center"
app:alignContent="center"
app:flexDirection="row"
app:flexWrap="wrap">
...
</com.google.android.flexbox.FlexboxLayout>

It seems the Flexbox solution worked for you, however there is another approach that can provide a different avenue for this problem. In case you are not aware, you can actually define different XML layout files with the same name - 1 for portrait mode, and another for landscape mode.
When creating a new layout file, if you right click on the layout directory and select New > Layout resource file you can configure the orientation here. In the Available qualifiers section, scroll down to Orientation and add it to the file. At this point, you will be able to choose the orientation for this layout and, if you name the file the same name as the opposite orientation, your app will automatically inflate the view that is appropriate for the device's current orientation. Android Studio will also understand the different layout orientations you have chosen while in the editor, and the screen you see in the Design tab will be adjusted accordingly i.e. the landscape layout file will have more room vertically to build the UI with.
Note that this adds a level of complexity to the application itself in the sense that the activity lifecycles will need to be accounted for since the views are now being destroyed and re-drawn by the OS in response to when the user switches the orientation of their device.
Hope this helps either in this situation or in some other situation where you want to accomplish different layouts based on device orientation!

Related

Insert multiple items in a layout

Im developing an Android app in Java using Android Studio. I have a layout called activity_way_bill, where it must show a list of trips. Also, I have a layout called item_waybill_trip, where I have labels for displaying the trips details.
I need to insert X item_waybill_trip layouts into the activity_way_bill layout (the X number I will know at runtime). Right now, I just have it included like this, in the XML file:
<include android:id="#+id/trip" layout="#layout/item_waybill_trip" />
But this is a static solution and only allows me to include 1 (or a predefined number) of layouts. I need to include X, and set different texts for each one. How can I do this?
If you want to display the list of element with the same layout but different data than you have to use recycle view.
You can also define the count at runtime and change the count if you needed.
You can visit the site below and check how to use recycle view. :-
https://developer.android.com/guide/topics/ui/layout/recyclerview
As mentioned in the answer by #Mahavir Jain you could use a recyclerview for that but if you want to go the other way, you will have to create the dynamic layouts at runtime and add them to the parent layout using the .addView() method of the parent layout
If your app needs to display a scrolling list of elements based on large data sets (or data that frequently changes), you should use RecyclerView as described on this page.

Android, change text size wrt orientation

Good evening all.
I've created an app using AIDE, which I'll ultimately get set up with google to sell on the play store.
I'm presently working through the differences between portrait and landscape/horizontal.
Presently, my app's main page doesn't allow the whole page to show when I rotate the phone to horizontal. It only shows a single line. And if I try to scroll, it doesn't allow that (not entirely a surprise, because I didn't set it up for scrolling).
My header/intro line is set to sp=30. My subsequent lines are set to sp=20.
I've been looking at how to deal with this issue. I.e., I want the text to resize when the orientation is changed from portrait to landscape. I have already found the androidmanifest screenOrientation="fullUser" code to allow for full rotation. This however does not deal with my real issue--- screen resizing, and text resizing, based on orientation.
I've been reading some older posts on here discussing
get textview()
but my lack of experience in coding is now asking--- where am I suppose to place those code snippets? Would they go on mainActivity.java, and main.xml or new/different pages?
And with newer API's, is there a better/more-efficient way to do this? The articles I'm reading are 3 years old at the newest.
TYIA.
SteveB.
I want the text to resize when the orientation is changed from portrait to landscape
You're looking for how resource quantifiers work.
You can make a res/layout-land or res/values-land folder explicitly defining landscape resources.
Your options include
use number or #dimen or #style in layout xml files for android:textSize
use number or #dimen to define #style in styles.xml over a TextAppearance parent style's textSize
define #dimen value in dimens.xml

A way to adapt the position of items in an android layout

I am currently work on an Android project, I have to show several check-box, now they are in a Relative Layout and all are positioned to their left neighbor.
So in landscape, I have a line of check-box, the problem is if I switch in Portrait the line is cut, because there are too many elements. So is there a simple way too automatically go to the next line when the element will be cut ?
I can change the layout type (relative, linear, ...).
U may check this library flowlayout coz u cant easily achieve this behavior.

Android TextView OverLapping

I am trying to create a top bar for my app that shows the users level, coins. So i have an image view that displays the coin icon and a textview that displays the number of coins, if the number gets high it overlaps the image rather than pushing back the image and keeping everything aligned, is there a way to do that?
Without knowing your layout XML this is pure guesswork, but I'm assuming these two views are in a RelativeLayout. If so, add to one of them an attribute like this:
android:layout_toLeftOf="#id/otherViewId"
Options available are layout_toLeftOf layout_toRightOf layout_above and layout_below.
Post your layout XML and I'll update this with a better answer!
If you are using the relative layout then you can use the z-index attribute of views to manage the ordering.

Linearly align items in a View to go to next line if space is filled

I want to show buttons in an align as shown in the pic.
If i put them in a Linearlayout with orientation Horizontal, its ok.But how can i proceed to next line?
Following image shows some TextViews align Horizontally but i have no idea how to make them go to Next Line.
There are no standard layouts (in the Andorid library) like the Swing's FlowLayout that can solve this problem. There are a couple of open source implementations of layouts that have the functionality you need. Here is an example: https://github.com/ApmeM/android-flowlayout
You could also read the answers to this question (which is essentially the same question): How can I do something like a FlowLayout in Android?

Categories

Resources