Use relative layout in java code - java

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.

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.

Change include tag layout programmatically

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.

Moving one element without moving others [Android ADT]

I'm currently developing an android app and I'm just wondering if there is a way to move the elements around on the graphical view without moving other elements around.
It's really a pain to get everything lined up the way I want it when moving one edittext moves 3 others too.
Thanks all!
This will depend on what your layout is so the simple answer is no. With certain layouts and attributes it might work but everything kind of depends on everything else.
If you're using a RelativeLayout then a View will be positioned relative to certain other views so this will cause problems.
If it's a LinearLayout then moving one to the top is going to move other things.
Solution
Do yourself a favor, learn to design them in the xml and don't use the graphical editor to place the Views/ViewGroups. I use the graphical tab only for checking how it looks. Using xml is easier and more flexible, IMHO

Android 4 settings style

I really like the androi 4 settings page and im trying to style my app the same way.
Here is a screenshot: http://cdn.androidtapp.com/wp-content/uploads/2011/10/Android-4.0-Settings-Screen.jpg.
Ive just gotten into java and android 3 or 4 days ago so style i havent really focused on style and dont know anything about it so i have a couple of questions:
1) How to set similar caption to list (Wiress and network on the picture for example)
2) How to set the same colors to my app? Is this somekind of a default style?
3) Is padding used on the picture to make list distance from borders? Can padding be applied thought whole application or does it have to be defined in every xml?
Thank you!
Yes you can apply a universal style to your app (this includes padding), it's quite straightforward
look here for a starting point http://developer.android.com/guide/topics/ui/themes.html
If you want to style your app the same, you should follow these guidelines about standard metrics and grids:
http://developer.android.com/design/style/metrics-grids.html
1) To set a similar caption you have to implement your own customized view and add it to your list.
2) You can set a theme you need in AndroidManifest.xml or you can use standard items. To find them have a look at android.jar/res.layout folder in your Android library in your project. There are all the standard layout files for Views.
3) What you need is to set margins in your layout-file.
You have to make a layout file and specify all the View-elements and margins in it.
However, if you want to make preferences there are ready classes available for that (PreferenceActivity or PreferenceFragment).

Is there a way to layout Swing components like divs in HTML?

Let's imagine situation where you're making the layout for a webpage. In HTML you can put the stuff in divs and use CSS to set positioning, e.g. size, positioning, etc.
Let's have another situatinon, but same requirements. You're programing in Java Swing, and you also want to make layout with similar requirements, e.g. size, positioning, etc.
I haven't found layout managers useful in this situation, because they make it one way, but never as you want it, e.g. one part will be next to another part, and it will have some size.
In short: I want the layout to handle positioning and setting sizes of GUI parts as happens with divs in HTML & CSS. I'm programing it in Java Swing.
I've tried swinghtmltemplate, but know about xito try to look through these projects, maybe you will find good solution for you. And yes, there is MiG layout

Categories

Resources