I had this question earlier today, and have been stuck on this for 3 hours!
The answer suggested that it is okay to remove the activity_content as long as I remove the line:
<include layout="#layout/content_menu" />
I did that...And started to edit directly in the layout, after copying the default contents of content, except...the layout isn't even there when editing! So, am I supposed to keep both layouts, and just edit the content one? Why do I even need to do that? Is there a reason for this update in android studio?
I'm just creating my activities like this:
yes you can just leave the activity_yourclassname.xml as it is and make changes to content_yourclassname.xml only for the main content.
and yes this update is made just to make code more cleaner and more reusable. so that you could focus only on the main internal content of the code without getting into complex layouts.
Related
i'm creating an app, and i'm kinda knew in android develpment, i don't have much experience, but i've bee maneging well. My app is almost complete and now i want to improve the looks.
Anyone knows how to put the widgets inside boxes, separated from background ? Like, i'll give you an example:
Anyone knows how to put the widgets inside boxes, separated from
background ?
=> You can use the CardView provided in support library. CardView is nothing but a FrameLayout so you can include whatever views inside it!
For more reference, go through the Creating Lists and Cards.
Use frame layouts for the boxes.
I am wondering is there any reason to do one over the other. I have been adding fragments via xml and using show/hide. I wanted to learn about replace/add/remove so I tried to add them programmatically. This has led to problems fragment remove issue and just seems less straight forward to me.
It also seems easy to place them via xml, whilst I'm still not sure how to programatically (as you don't have a reference to the fragment in the xml of the view that youa re adding it to?). Is there any reason for me to add fragments programmatically?
Fragments added in XML cannot be replaced or removed, other than that major limitation choose whichever method is easiest for you.
I have a short question according to creating GUIs in android. What way is better xml or coding?
I always read that xml is way better but imagine you have a scrollview.
Inside the scrollview is a relativelayout. Inside that there shall be several rows with an ImageView next to a TextView next to a RadioButton. The number of rows can vary.
Is it really better to make lets say 50 Views in the xml or a loop in code where these views are created?
Each has its pros and cons. Just to name a few:
XML
pros -> fast GUI development, keep code clean
cons -> static
Dynamic (code)
pros -> able to react to runtime conditions
cons -> more code, which means poorer maintainability and potentially buggier
If you need to add components dynamically, only way is go with code (or) mixed approach (define layout in XML and add components in code). If your components are static XML may be best.
Dynamic content is, of course, added dynamically. So your example would require some java code. You should only add the dynamic part programmatically though, so you'd still use an xml document for the static parts (it's very unusual for a layout to be completely dynamic).
If you have a fixed number of views then yes, I'd write 50 of them in xml rather than with a loop. I guess you're wondering about code duplication and, as far as I know, you'll get some when using xml.
(One way to minimize code duplication within xmls' is with the usage of styles and themes)
I agree with the above. XML is a better approach to this even when you require dynamic updates you can still use XML bits and pieces to render the content. your code will be based on XML elements but XML files will be independent. hence if you break a funcitonality in the code you know that its your business logic thats broken not the UI part, which will make it easier to develop and find problems easily.
Why you do not use a ListView instead of a ScrollView.
It will be simplier to implement and performances must be better with it.
Create a XML file with a ListView and in your activity implements your own adapter to instanciate the rows.
You can find a lot of tutorials on internet talking about that, I'm sure you will find what you need !
Good luck.
I am new to android programming, and I am using eclipse.
I am using ViewSwitcher, and able to switch back and forth between views.
My question is with eclipse.
In eclipse, how can I edit the different layouts graphically? Only the first layout is visible when I select the res->layout->main.xml.
I know there is a work around (or may be even the right approach); having two layout files, and switching between them. But that is not my intention right now.
Thanks for any suggestions.
Karthik
Its a bit late but I had the same problem and got around it with only xml changes by putting the contents of the 2 layouts in other files and including them in the original file. Not sure if this is proper but it made it easier to test.
<ViewSwitcher
android:id="#+id/detailSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
layout="#layout/layout_file_1" />
<include
layout="#layout/layout_file_2" />
</ViewSwitcher>
I would not create a view graphically. It is really easy to type in the XML and then you know you are making exactly what you want. Use the GUI to check your work. I would certainly build the two layouts in separate files and then paste them into the one with the view switcher when you have it just as you want it.
You just have to change the sequence of the layouts in the view switcher. The first layout in the viewswitcher is visible in eclipse. Edit your first layout which is in the viewswitch and then move the second up to work on it.
Working with Android for the first time, I've blocked out a layout using the relative layout and laid down some buttons and text widgets how I like them. However when I go back to rename the IDs the layout goes all crazy moving elements around and in general destroying the hours of work I spent laying them out.
Does anyone know how I can rename the widgets without Android destroying the positioning for widgets in the Relative Layout? Is this some "feature" of Android? I can't imagine why it would be hard for the UI builder to handle simple renaming of a widget ID without destroying the positioning information.. Do I have to use an external text editor and modify the XML files directly? Ughh I hope not.. I'm using Eclipse IDE.
You can use find and then replace all to change the names every place that they appear. Shouldn't take anywhere near an hour if you're dealing with a small layout.
In general the graphical UI creator that is currently included with the Android SDK is not so great for creating anything but very simple layouts. In my experiences (which were a long time ago, it may have gotten better since) it was terrible with RelativeLayouts.
If you have not modified your xml directly then it is time that you jump in and start learning to do it that way. You'll find that you have a much greater level of control over your layout, and once you get the basics figured out you'll probably be able to create quicker using raw xml then with the graphical tool anyway. I do wish that there were a nice GUI creator for android out there, the best one that I've ever come across is Droid Draw which I found to be better than the one included with the SDK, but still not as good as I was hoping.
To modify the xml directly you don't need any additional text editors, you do it inside eclipse. Open up your layout file and at the bottom click on the tab that says "Source" when you want to switch back to graphical (good to see the changes that you make to the xml graphically) just click back to the tab that says "Design"