Layout as a subset of another layout in android - android

I built a layout like this in XML say "block.xml"
Now, i want to create an XML like this
so that i can access each block as an array. Can somebody tell me how can i use block.xml as a template to generate my new xml file to be put as a UI part in android. Thank You
Just to be sure , i want to use table layout i dont know how to procees.

In your layout xml you may use the include tag and re-use your block.xml
as:
<include
android:id="#+id/block1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/block" />
And in your java code you can access views from block.xml as
View firstBlock = findViewById( R.id.block1 );
View blockButton = firstBlock.findViewById( R.id.someButtonId );

You should inflate and add the sub layouts programmatically.
This link has example code:
Android using layouts as a template for creating multiple layout instances

Related

How can you use tools in android layout to replace a merge item for designer

My project contains a number of custom layouts that (mostly) extend ConstraintLayout
I use xmls like this:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
......
to create the views and then I inflate them in my code
The problem is that I can't really see the changes I make to the view unless I change the merge tag with ConstraintLayout
Is there a tools parameter that could help me in this, so that they would at least show correctly in the designer?
Adding tools:parentTag attribute to your merge tag with appropriate value should allow you to see the layout in the Preview window. Depending on which library you use it should be either
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout"
or
tools:parentTag="android.support.constraint.ConstraintLayout"
To be able to use the tools namespace you also need to add
xmlns:tools="http://schemas.android.com/tools"
to your merge tag.

Creating id for Android Views

This question is quite basic. Let me go with an example.
I have two activities
activity_a.xml
ActivityA.java
activity_b.xml
ActivityB.java
Both the XML files contain only a TextView to display a simple text. As usual, the TextViews are going to be referenced in the corresponding .java files using their View id
My question is, if it is right to reference the TextView in both the XML files with same id? (like using the below code with exactly same id for activity_a.xml and activity_b.xml)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I was practising this procedure without any problems. When trying to reach the corresponding xml code for the TextView using Ctrl + Click (on Windows), I am provided with two options (to display the TextView's xml code from activity_a.xml or from activity_b.xml).
Also, what is the recommended way to name a View in Android? This will be helpful, when your Android project contains multiple layout files.
Yes , if you have same name of view or view group in different layout then it tells from which layout file it belongs to, ask for select required layout file.
so for that you have to follow proper naming conventions to avoid this type of confusion
https://github.com/ribot/android-guidelines/blob/master/project_and_code_guidelines.md
or you can give name like
activity_home_tvUserName if username textview from home activity
and
activity_profile_tvUserName if username textview from profile activity.
if it is right to reference the TextView in both the XML files with same id?
It is totally fine, the compiler will only look at the ID under a single view hierarchy.
e.g.: findViewById(R.id.textview) inside ActivityA.java will only search for textview ID inside activity_a.xml (assuming you have setContentView(R.layout.activity_a); beforehand.
what is the recommended way to name a View in Android?
In my opinion, you just need to be consistent in naming your view throughout the app. The main goal is to avoid misinterpretation and confusion.
Hope it helps!
They are in different activities, so you should have no problem using the same id. you could event declare them both as #+id/textview. However, why not just use the same XML file for both activities? No reason you can't.
You can also create an ids.xml file under the values folder and declare all your ids under it, so you don't have to declare them in your layouts, but this is not a very common approach.
if it is right to reference the TextView in both the XML files with same id?
My answer is Yes, It is right.
Whenever we set the setContentView(R.layout.activity_a), then it'll search for the given id within the above activity. Local attribute having the same id will take the more preference over the other attributes with the same id.
But having unique id's is Best practice.

Is it possible to convert java views to xml layout?

I a created a linearlyaout and added views to it using java code. is it possible to convert this layout to xml layout and save it to the storage ?
There is nothing stopping you from parsing all those Views attributes and then building a xml layout file. However you will not be able to use that constructed xml layout(like setContentView(R.layout.built_layout)) like other layouts from the res/layout folder.

Create custom widgets for android

I'm trying to create a rotating imageView. Looking at this (http://stackoverflow.com/questions/1930963/rotating-a-view-in-android) I know I'll have to override onDraw.
But when I create my own derrived TextView class, how do I use it in my xml layouts?
To use it in your xml layout add the following:
<view class="yourpackage.yourclass.yourview" ...additional parameters... />
note the lowercase "view" or by directly specifying its name
<yourpackage.yourclass.yourview ...additional parameters... />
How to create and access custom components is discussed here
http://developer.android.com/guide/topics/ui/custom-components.html

How do I used values declared in XML on my custom layout?

Im creating a custom layout and I want to use the text declared in the layout.xml file in my layout.
Like when I create a TextView in XML and set android:text=#string/text1 when I run the app text view automatically loads the text from android:text. So how can I access attributes declared in the layout XML file in my custom component.
I have learn how to do it from this:
WebImageView: Buy one, get two!
In simple steps:
Define the string of your XMLS.
Get the attributes in the code.
Did you complete the Android Developer Tutorials? The Hello, Views | Relative Layout tutorial will walk you through how to do this.

Categories

Resources