Change include tag layout programmatically - java

I'm doing an Android project and I'm trying to figure out how to be dynamic in layouts.
So I have layout files complex_card.xml and simple_card.xml, each containing a ConstraintLayout tag with some other layout elements inside. complex_card takes more space than simple_card so I will use complex_card whenever I have a lot of space and can switch back to using simple_card when I don't have space.
In activity_main.xml, I've use both of the layouts via <include layout="R.layout.complex_card"/> tags. However, in real time, I want to be able to change that to <include layout="R.layout.simple_card"/>. How can I do that?

I've found a work around to this. simple_card is a light weight version of complex_card which means I can use view.visibility = View.GONE and view.visibility = View.VISIBLE to hide certain layouts in complex_card which looks exactly like simple_card.
However, the original problem isn't solved if there are 2 completely different layout files.

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

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.

Use relative layout in java code

I m making and app which allows the user to change the interface by moving items like buttons, erasing them and adding new. So i need to know how i can change the layout properties (like toRightOf) of buttons in a relative layout through the java code, so that i can dynamically relocate them.
I have tried using layoutparams but i havent manage to find a solution. I have read other post that suggest using an absolute layout and changing the properties x and y. Is this the only way or can i use the relative layout ?
Please help.
You will need to use combinations of addRule/getRule from RelativeLayout.LayoutParams class.
Here's a link to some example.

Replicate a view inside a xml

Is possible to generate/inflate a view declared within a layout?
For example: I have a complex layout with a button defined inside and I want to duplicate that button and insert it in a GroupView. Is there a better way of doing that rather than inflate the whole xml layout for only picking one of the elements? Thanks
No, it's not possible but you can extract that view or part of the layout which you think you are gonna re-use many times and including it where your need through the tag:
<include android:id="#+id/id_layout" layout="#layout/layout_xml" />
Take a look at the documentation.

Categories

Resources