Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Layout - when to use app: vs android:?

I've been writing some Android apps but I don't really understand when to use app: and when to use android:. When styles are not being applied the way they're supposed to, I use trial and error and sometimes find that using app: instead of android: solves the issue but I don't understand why. It'd be great if someone could point me in the right direction. Thanks!

like image 366
Nyein Chan Wynn Avatar asked Jan 20 '15 12:01

Nyein Chan Wynn


People also ask

What layout should I use Android?

Takeaways. LinearLayout is perfect for displaying views in a single row or column. You can add layout_weights to the child views if you need to specify the space distribution. Use a RelativeLayout, or even better a ConstraintLayout, if you need to position views in relation to siblings views or parent views.

What is difference between app and Android?

android is usually used for attribute coming from Android SDK itself. app is often used if you are using the support library. You may also see other namespaces if you are using custom views (of your own or form a library).

What is the difference between different layouts in Android?

Android Layout TypesTableLayout is a view that groups views into rows and columns. AbsoluteLayout enables you to specify the exact location of its children. The FrameLayout is a placeholder on screen that you can use to display a single view. ListView is a view group that displays a list of scrollable items.

What layout should I use Android Studio?

Use FrameLayout, RelativeLayout or a custom layout instead. Those layouts will adapt to different screen sizes, whereas AbsoluteLayout will not. Definitely right. I recommend RelativeLayout since it keeps the view hierachy flat.


2 Answers

You can use the app namespace to have app compatibility with older API versions.

For example app:srcCompat="@drawable/customborder" has the same effects with android:background="@drawable/customborder" The difference is that the first will work correctly with older API's and the second will not display what you would like.

like image 135
alkas Avatar answered Oct 20 '22 09:10

alkas


You are talking about custom namespace.In android we can create custom views in additional to already available views. As per in Google developer docs.. To add a built-in View to your user interface, you specify it in an XML element and control its appearance and behavior with element attributes. Well-written custom views can also be added and styled via XML. To enable this behavior in your custom view, you must:

Define custom attributes for your view in a resource element Specify values for the attributes in your XML layout Retrieve attribute values at runtime Apply the retrieved attribute values to your view

Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to the http://schemas.android.com/apk/res/android namespace, they belong to http://schemas.android.com/apk/res/[your package name]

So for if you use default views you can use android namespace and if you want to set and use attributes for custom view you can define your own name.

Refer this

like image 34
karthikeyan Avatar answered Oct 20 '22 10:10

karthikeyan