Is there any way to avoid hundreds of "android:" statements in an Android XML layout file? I am new to Android and I find the "android:" statements make layouts very hard to read. Plus they are a pain to constantly type.
For example, instead of this:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
I would like to just see this:
layout_width="wrap_content"
layout_height="wrap_content"
layout_column="2"
If you're using Eclipse (and presumably Android Studio has similar functionality, but I've not used it) you can auto complete layout properties, e.g. if you type w then ctrl + space it will auto complete this to android:layout_width in most instances. You never really need to type the android: part.
I'm not aware of a way to remove the namespaces.
No. That is needed to know it is part of the Android framework. Instead of say a custom style or custom View. I've done a little bit of Android coding and it really isn't a big deal.
For example, there is a copy/paste feature which can make it go faster. Also, most IDEs have autocomplete which is very handy.
Related
So i'm updating my apps to fully support Monet and Material You guidelines, and the official site mentions a new design for the switches. I used it, and that's the result:
I have a preference screen using Androidx preferences library, latest version available at the time of writing, and the only way i found to theme the switches (except the manual theming, which makes no sense) is to use this line in the app's theme:
<item name="switchStyle">#style/Widget.Material3.CompoundButton.MaterialSwitch</item>
And using SwitchPreferenceCompat (it doesn't work in the regular SwitchPreference) this is what i get:
Regardless of the width (which is different, but can be changed) the disabled state is completely different and doesn't match the rest of the app. Why? and most importantly, why do they suggest to use a library which:
Doesn't support Material You out of the box
Doesn't support any new Material3 component
It's hard to properly customize in general
?
I don't want to be too critical, but this is out of my understanding.
EDIT: at the moment, i'm using switchCompat everywhere, to make the app uniform. Looking at the system apps, i can find 4 different type of switches: a custom switch similar to the second screenshot, the old one and the two types in this question. That's hella confusing.
I understand Google stance on this, they don't want to make androidx.* packages dependent to Material library itself, maybe they should provide a separate preference package but this time with fully Material widgets.
In order to have the brand new MaterialSwitch of Material 1.7.0 with preference, I've overridden its widgetLayout with a custom layout by android:widgetLayout="#layout/preference_material_switch" (in fact I applied that programmatically like .widgetLayoutResource = R.layout.preference_material_switch) and put the following on preference_material_switch.xml layout file,
<?xml version="1.0" encoding="utf-8"?>
<!-- Derived from https://github.com/androidx/androidx/blob/8cb282cc/preference/preference/res/layout/preference_widget_switch_compat.xml -->
<com.google.android.material.materialswitch.MaterialSwitch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:clickable="false"
android:focusable="false" />
And here is the result,
The same problem, After seeing these replies, I'm thinking to build the settings fragment without androidx.preference.
MKevin3 said:
I hate the provided Android preferences setup and look. So many times they changed the rules and broke what I had.
Not that this helps you probably but I just did my own to avoid all the headaches and I am in full control of the look. If push comes to hove you might consider doing this as well instead of fighting the "Android Way".
sc00ty said:
I gave up trying to use their widgets and fragments. It was so much less of a headache to spend a little time making my own compound widgets for each setting type.
My question is about the ConstraintLayout because since Android Studio 2.3, when I want to create a new Layout, the default Layout in my xml file is the ConstraintLayout.
Is the ConstraintLayout the new standard ?
What about the others layouts (LinearLayout, GridLayout, RelativeLayout,...) ? Will they be depreciated ? Do I have to Convert all my layout using the converting tool ?
What is the best practice? Use as much as possible the ConstraintLayout ?
This is a bit confusing because I don't like to use the visual editor and it's a quite difficult to do ConstraintLayout using the code editor.
when I want to create a new Layout, the default Layout in my xml file is the ConstraintLayout
Whether or not a ConstraintLayout is created depends entirely on what templates you are using.
Is the ConstraintLayout the new standard ?
It is what many of the Android Studio 2.3 templates use. Those templates are merely examples used a starting point by some developers. Those templates differ on older versions of Android Studio and most likely will change again in the future.
What about the others layouts (LinearLayout, GridLayout, RelativeLayout,...) ? Will they be depreciated ?
That is very unlikely.
Do I have to Convert all my layout using the converting tool ?
No.
What is the best practice? Use as much as possible the ConstraintLayout ?
Use what works for you. Consider ConstraintLayout as a candidate, but do not feel obligated to use it. In particular places where you determine that layout performance is critical (e.g., scrolling lists), if you determine that your existing approach is not performing well, consider testing ConstraintLayout to see if it helps (it may or it may not). Also note that ConstraintLayout itself does not handle all scenarios — for example, I have yet to see a working means of implementing a TableLayout structure (with automatic column sizing) using a ConstraintLayout.
Zen Coding/Emmet seems to be perfect to build those tedious Android XML layouts. Is it available for Android in any way or on any text editors? If not, would it be possible to start coding it for one?
Emmet is not tied to HTML and CSS. For XML syntax, it takes any expression and converts it to XML code so basically it supports Android XML layouts out-of-box. You can also add your own element aliases to increase productivity: http://docs.emmet.io/customization/snippets/
I am very new to Android development and am having some trouble. I am creating an XML file using Eclipse, both the Graphical Layout feature is what I'm having trouble with.
Also, I am working in Android 2.3 for compatibility reasons.
I am wondering if there is a layout which enables me to place buttons or text fields or any attribute where I want to put them. This may sound stupid, but it seems that every layout has some sort of order in which it lets you add attributes, and whenever I try to drag them elsewhere on the layout things just get very very messy.
If what you want is an AbsoluteLayout, this has been deprecated since Android 2 (IIRC). You can try using the RelativeLayout, which let you position freely your widgets. Else, if you only use LinearLayout, then yes the widget will be positioned in a strict way.
use Relative Layout. I recommend that you read up on Android layouts so you understand why, what and hows of Android way. There are some good tutorials that I found helpful. TutsPlus: Android Layouts
There is also a very good video from Marakana.
Marakana: Android Bootcamp
If you are just starting out with Android check other tutorials/videos on Marakana. They are a very good resource for beginners.
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