Convert layout to class - android

I currently have a layout that has scrollviewer as main layout, and a relative one (with a lot of views) as my app layout, but now I need to make tabs, the android tutorial instructs to make the actual layout of each tab programatically, how do I convert my current layout (xml) to a class? Is there any guideline to follow? how do I set the relative layout inside the scrollviewer and how do I specify the "android:layout_below="SOMETHING"" and all that properties?

I din't understand your question very well but, when creating the TabLayout programaticaly you define the activities that are used for each of the tabs. Each of them should have setContentView( 'activity layout')

In order to access XML elements in Android you need to do something like the following:
Button submitButton = (Button) myActivity.getViewById(R.id.btnSubmit);
Then you can call things like submitButton.setVisibility(View.HIDDEN) to hide it, or whatever else you need to do with it.
This should work for any layout elements.
Make sure you do this in your onActivityStart method, or else you will throw runtime exceptions.
There isn't any way to automatically "convert" an XML layout file to a class, unless you're using an XML parsing algorithm of some sort, which sounds like way more than you're trying to do.
Hope this helped!

Related

How to add a Layout from the resources to another Layout in Android?

I want to display a LinearLayout from the XML-resources in another pre-existing one so I can easily make copies from the same model.
(without having to declare programmatically all the buttons, textViews...)
I'm looking for a method like the one that works for activities:
setContentView(R.layout.myLayout);
Is it possible? And how?
Thanks a lot.
Create a layout file sublayout.xml and put this file in
YOUR_APP/app/src/main/res/layout
then you can include it by
<include layout="#layout/sublayout"/>
in another layout file.
Anyway, according to my experience, you can not
include sublayout.xml more than once, because
each id, like android:id="#+id/sublayout_id1" can only be used once.

How to set up a layout using code and not XML

I'm learning to develop android apps and in the process I realized that there two ways to get a job done. Using xml or normal code. Suppose I want to change the position of a button, I'll be doing it in xml using align left/align centre etc., This will be done in the XML file. If I want to achieve the same through code, where should I place the code ? Inside which class ?
There are two aspects to your question that I understand.
1. Creating a whole layout file dynamically (without XML).
2. Creating a layout through XML and changing the components positions and properties dynamically through your activity file.
Now, it's upto the developer what he wishes to choose.
To help you further, please view this video link posted by the Android team.
It's all about layouts and includes how to layout apps using Java, not XML. However, you are warned that the android team wants you to use XML.
The code will be placed in the same class as the class where you reference your xml code. Do a read up in your android docs for insight.

Which is faster? Layout from xml or from code [duplicate]

This question already has an answer here:
Should I inflate a layout or programmatically create it?
(1 answer)
Closed 9 years ago.
I've simple dialog with only one ImageView, and im using it frequently in my app, and Im wondering what is faster to load and showup, create layout in XML or make the same thing in code.
EDIT
I ask about theoretical knowledge in this point, and I cant find any useful info about that on Android Dev sites.
Theoretically in my opinion, coding the layout is faster than inflating it from xml, because inflaters need to map the layout by it self, and link id's to each other, but in code you will give the viewgroup (lets say) the reference of the view it self, there is no need to lookup it in the xml.
don't forget the step of converting the xml into java elements (views)
No difference, but if you have only one imageview make this layout in code
For performance, there is no noticeable difference.
But for maintainability you want to separate functionality and layout, hence it is recommended to use the layout XMLs.
No one is necessarily faster.
It will be the easiest to define the layouts in xml though, and the xml option is there as a way not to have to define buttons and ui components in java.
Also, if you define views in java, you will end up adding them to your blank xml layout's viewgroup
( ei. adding your button to the layout)

How to use Android XML Layout?

The purpose of the XML layout is confusing for me, having only worked with UI in Visual Studio with the drag-drop xml layout and C# code-behind. Should I be creating a separate xml layout for every screen (load, main, data) in my application? How do views associate with layouts? How do I reference the xml and use it in my code? I only know of the setContentView(xml layout) call.
For example, for my loading screen I want to display a background image and a loading bar. I'm thinking there's a way for the layout to contain the picture and the widget so the user sees the loading screen immediately, and in the actual code I'm initializing all my necessary variables.
If you are confused on how layout works in Android, better try reading this topic on XML Layouts and also read this tutorial about user interface in Android for a head start.
Here's also a tutorial on how to set background image to progress bar.
You should load the layout resource(main.xml) from your application code, in onCreate method of your Activity.
setContentView(R.layout.main);
Yes, you need an XML file for every page of your application.
Write a XML file for that particular page mentioning the fields required and in your .java file use this command
setContentView(R.layout.main);

Android images and text via Java programming, not xml layout

please is possible to make some activity with images, text etc. via some .java file (Java programming) without xml? - xml use only for strings of text, for multilanguage....
Thanks for help.
Yeah it is possible and we call it Dynamic Layouts. But i think you should stick to XML because it is easier to use as compared to programmed layouts. I know XML layout could be an headache sometime but once you'll be habitual to these XML layouts stuff then you won't find it hard any more. And if there is any other reason behind why you asked this question then yes it is possible as Android SDK provides classes for each and every layout element with the same name as used in XML file.
For ex. you can create a TextView like this in an activity:-
TextView textView = new TextView(this);
now you can call any function on it which would work similar to TextView's properties used in XML file for ex. for android:text property you can use :-
textView.setText("I am a TextView");
And if you want to know more about these Dynamic layouts then you should spend some time on developer.android.com.
I hope my answer will be useful for you.
Anything that can be done using the xml files can also be done using code. You can create your views manually, add TextView and ImageView to a LinearLayout (or another ViewGroup), etc.
The android developer website contains all the documentation to do this.

Categories

Resources