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.
Related
The old Android Studio used to create the layout files using RelativeLayout, but the new one use ConstraintLayout, and this let me very pissed off >:[ , cause it have to import and sync... I really do not like.
I do not used to use both (RelativeLayout or ConstraintLayout), I prefer LinearLayout vertical, but I did not find how I can change the layout template in order to Android Studio create a layout using LinearLayout vertical, Anyone knows?
I looked for in Preferences -> File and Code Templates however I didn't find where I can change the xml layout template...
Thx
I'm not sure if you can do that, so just replace relative with linear in the xml & don't forget to add to add the orientation
Just delete the content in the layout file, and use the linear layout in that file
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.
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
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.
How to edit xml files within layout directory in android. And how can I use controls (defined in xml file) in my java code.
If you are using Eclipse, double click on the XML file, it will be opened in the editing pan at the right side. You can edit it in either Graphical Layout or XML by clicking on the tabs below.
Update: You can use the controls (or widgets) defined in XML file in Java code in the following way. Say, you've defined an EditText element with android:id="#+id/textFirstName", you can access this in code like this,
EditText textFirstName = (EditText) findViewById(R.id.textFirstName);
textFirstName.setText("Bugatti");