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.
Related
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.
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)
Just start developing with android and think instead of reading a book a webinar could be better because a webinar could also teach me short ways and how an android developer thinks when writing the code but now got a problem
<CheckedTextView
android:id="#android:id/text1"
android:checkMark="?android:attr/listChoiseIndicatorMultiple"
</>
I dont understand the above code up to now see lots of different id definitions some of them was for resources and start with #resource/name, and some of those id definitions was like #+id/name just for creating a new id for the component but this time it is using android:id/text1 and I dont understand why it is using it in that manner
Besides, the checkMark thing make me confuse more what are all those ?android:attr/listChoiseIndicatorMultiple means?
Could you please explain me and show me some resource where can I find all those magic attributes so I can cope next time by myself and hope someday can answer other newbie questions
Thanks a lot in advance, and all comment will be appreciated.
Well, reading the docs has always been helpful to me:
Android Developer Site
XML Layout specific docs
#android:id/text1 is just a format used when the id has been previously defined. When you put a + in there that means the framework should create the resource id if it doesn't already exist.
It's normal to use #+id/thisid when defining a new view in a layout, and then use #id/thisid to reference the aforementioned view from another part of the layout (say, in a RelativeLayout where you need to tell one widget to be below another).
A question mark before the ID indicates that you want to access a style attribute that's defined in a style theme, rather than hard-coding the attribute.
#android:id/text1 basically this is used when you create any android component like button, layout, textviews etc.
but when you need any external component which is general for different platform like any color, image etc then you can declare it as #resource/name.
actually there is nothing different just keep one thing in mind that in #android:id/text1, id will simply work as an class name will contains other objects like textview, imageview or any other.
now if you declare #resource/name then in that also instead of id class name will be resource. actually when you will use it in java then these(#android:id/text1) will be converted into object hierarchy.
I want to write an app where (at least for now) the content is always the same but the layout is loaded dynamically at run time based on a user preference. Essentially I want the app to apply a "skin" which may look completely different to other skins.
I found some tutorials using SAXparser:
http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/
http://twigstechtips.blogspot.com/2010/12/android-how-to-parse-xml-string.html
and can imagine writing something from scratch that recognizes all the standard xml layout tags and then dynamically loads each part of the layout. But that's a lot of work to do from scratch! Surely this functionality is available in android, or surely someone has written some open source code which can be run at the start of your activity's onCreate method, which takes in an xml file and sets your layout?
I found a similar but unsatisfactorily answered question here:
How to create a layout file programmatically
which makes me think that since setContentView must take an integer resourceID as its argument, the fact that these are pre-baked at compile time might be a problem. (setContentView may also take a View object as its argument, but I don't want a ton of if statements and to pass it each View object one by one, I want some code that inputs an xml file or xml string and sets the content view.)
Maybe I'm way off track. Is there another way to do this? I would think that the ability to have an app with dynamically loaded skins is important.
Thanks!
I had similar requirements and tried the same approach - it does not work.
Documentation clearly states this: http://developer.android.com/reference/android/view/LayoutInflater.html
Update:
Since OP needs to load XML layouts created at runtime:
Possibly this could be done, by creating XML layout files, copying them to dummy project, create .apk and then load apk on to device.
DexClassLoader can be then used to load classes inside apk.
well, android makes the hard work for you, but no all the the work....
first that all you have to forget about parsing xml layouts... instead you can make skeletons layout, that manages his inner childs position, size, etc... and later inflate that 'skeleton' xml with LayoutInflater and obtain a View instance...
When you have that View instance then you can do what you want with it, applying the users preferences like backgrouds, foregrounds colors, position, sizes, etc...
maybe i dont understand your question but you can get any view inflated from a xml resource at compile-time and later apply other style or set another propertys
It seems it is impossible to load the layout & change the skin dynamically according to the doc :
Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)
http://developer.android.com/reference/android/view/LayoutInflater.html
I have know core java, I want to develop android apps, in few sites I saw apps in XML in few Java.Which is best and easy to use Java or XML?
I'm assuming you are talking about the UI, not the complete app:
For everything statically I use XML, because it is easy to find in the structure of your project.
Some parts you want to create dynamically and you have no other choice then to use Code. Be smart, in this, so if you have to add several Views that look the same do this
Make an XML with your views
In the loop where you are adding the several Views, inflate this xml, set your id's etc, and add them
You can have all the basics, styles etc in your XML, and still add stuff dynamically.
Maybe you want to check out some of the hello world code?
You can't build apps in XML. You use XML to define the UI and a few other things, but the logic of the app itself has to be written in Java.
Android development is in Java, but when you declare the layouts (where the buttons and so are going to be on the screen) you do that in XML.
So you have to use BOTH anyways.
XML for layout
JAVA for programming your app