I have seen that there some short key for generate new string, dimens value etc from Layout XML file...But I don't remember it.
Example I have input new dimes in Layout XML called time_padding. its showing red error that there no any value called time_padding in dimens file....Now I want generate its from xml so it can be handy for fast work. Let me know if someone know it.
Thanks
You can use Alt + Enter then "extract string" to create new string under resources.string.
You can do the same for extracting dimesions as well.
In android, the layout parameters depends on the type of layout.
For example:
Suppose you are using Relative layout then you can use
android:layout_alignParentBottom
android:layout_alignParentEnd
android:layout_alignParentLeft
android:layout_alignParentRight etc
But these parameters are not available for the Linear layout.
So, please mention which layout you are using.
Related
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 am using the drag and drop feature to add a Linear layout in my activity.
when i check the activity_main.xml there is the component created but no "android:id"
I cannot manually add the id , because there is no id for this layout in R.java . how to solve it?
If you add android:id="#+id/YOUR_ID" to your XML, your IDE (I'm assuming eclipse) will recompile R.java, and you should be able to use R.id.YOUR_ID in your activity.
You needn't use R.id for setting id programmatically. You can use any positive number for id:
myNewLayout.setId(newLayoutIndex);
According to View documentation
The identifier does not have to be unique in this view's hierarchy.
The identifier should be a positive number.
I don't want to specify hard coded test size values in my layout.xml, hence i am using the following specification :
android:layout_height="#integer/intervalViewHt"
and
#integer/intervalViewHt value is as follows:
<integer name="medium">15</integer>
Now, the while inflating android is creating a problem saying that it cannot inflate the view. I want to actually specify value in dp so the actual value should be like
android:layout_height="15dp"
Can anyone help me here ?
What you're looking for are "dimensions" rather than plain integers.
Declaration:
<dimen name="intervalViewHt">15dp</dimen>
Usage:
android:layout_height="#dimen/intervalViewHt"
Have a look at the given link for more examples in the Android docs.
I use something like this for text size across different devices. It's java based, not xml.
tvOutput.setTextSize(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));
I haven't tested the code so you might have to play with it. I'm sure you could do something like.
button1.setHeight(TypedValue.COMPLEX_UNIT_DP, getResource().getInteger(R.integer.medium));
Though again I haven't tried it and you may need something different then TypedValue
What if you give it a String rather than an int. i.e in strings resource you have a string "15dp" that you reference
I have been working in android for past few months. The problem for me is now related to Z index value. I am having a layout with a textview,edittext and imageview.
Suppose i have a xml file something like this..
<Layout1>
<edittext><zindex>3</zindex></edittext>
<textview><zindex>2</zindex></textview>
<imageview><zindex>1</zindex></imageview>
</Layout1>
So my question is that am reading this xml file by DOM parser and i want to set the z index value for all these by the values defined in the xml. Now is there any function or property that i can use to do it.
I have learnt about coding it with xml, but that will make it hardcoded. I want a dynamic display so how do i adjust layout with the zindex values.... HELP PLZ
there is no Z-index in android layouts. You'll need to use FrameLayout or RelativeLayout if you need to place elements on top of each other in reverse order.
see Placing/Overlapping(z-index) a view above another view in android
You can use view.setZ(float) starting from API level 21. Here you can find more info.
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.