How can we set id as a number for UI Elements? - android

I am quite interested and confused both. Sometimes ago I was working on a project who has already implemented by some one. And after worked on that i found that he was using a unique number for find ID for its UI elements. Just like if we need to find id for TextView then we write,
TextView textview = (TextView)findViewById(R.id.text);
but in that code it was like,
TextView textview = (TextView)findViewById(012345678);
something like this.I have searched a lot in project also tried out to find that number but i couldn't. In R.java file i have also checked but there was also not. Its unique id its generated automatically as usual but i couldn't find a number in whole project.
So my question how can we use like this way? Is this possible??
TextView textview = (TextView)findViewById(012345678);
If this is possible then where we can define that id number and use it for UI elements.
Help will be appreciated!!

from folder gen you can find R.java there is a class named id like in image.
for example
public static final int action_bar=0x7f06001c;
so instead of R.id.action_bar i can use 0x7f06001c.

That is not directly possible as you might think. You can define your own custom ids which are not used in any xml files. I think that is what you want. To do this create a new xml files in res/values/ids.xml with this content:
<resources>
<item name="your_custom_id" type="id"/>
</resources>
Than you can use it as R.id.your_custom_id I think that is what you want.

Related

Use Strings with placeholder in XML layout android

I have a string with placeholder e.g
<string name="str_1">Hello %s</string>
I want to use this in xml layout as android:text="#string/str_1". Is there any way to use this in xml layout to fill the placeholder?
Thanks in advance. I already know String.format(str,str...) in java/kotlin but i want to use this in xml layout without data binding.
You can use something like this.
Write your string in your (strings.xml) with declaring a string variable (%1$s) inside it. For decimal, we use (%2$d).
<string name="my_string">My string name is %1$s</string>
And inside the android code (yourFile.java), use this string where you want it.
String.format(getResources().getString(R.string.my_string), stringName);
This is not a good answer but it may help you get some idea to get going.
Thanks.
It's not possible to format your strings directly in your XML. Inside your code you can use both String.format and context.getString() to get and format your string.
If you want to show something just in the XML and not have it in the final build (only have it in compile time), you can also use tools: namespace like following:
<TextView
...
tools:text="Hello, this is a test"
/>
This will only appear in the layout editor and will not have any effect in the APK. It can also be used for other fields too, like tools:visiblity.

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.

Text in android app (beginner...)

My problem is very simple: I've started an app for playing Darts. The app will have several activities ('pages').
One page will be about the rules of the game. I'll be using a scroll layout because it's quite some text. But how to get the text there?!
I assume working with strings is not the best way? Do I use the XML file to get the text on screen then or does it work via Java (Assetmanager)?
Maybe there are sample apps in which large chunks of text are used?
I know this really might seem like a trivial question but I haven't a clue where to begin.
Thanks in advance!
You should put your string in your strings.xml in your res\values folder.
You can define strings by ID which allows easier internationalization (i18n), so that you can easily adjust the strings used in your app to locale (which is done automatically using resource identifiers, and it falls back to strings.xml if it can't find a strings-hu.xml in case you have Hungarian locale set as system language).
You can also define string-array and the like in XMLs. Then all you need is create a layout XML with a ScrollView in it that has a TextView in it and then you set android:text="#string/rules" for that TextView and you're done.
It is so simple my friend.
You can simply use TextView and in "android:text" you refer to the string that you delared in strings.xml file (by its name)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="#string/text_name"
/>
If your text is dynamic, you can modify it in Java code!
Make a String Resource like this.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1"> your text </string>
<string name="string2"> your text </string>
</resources>
and access like this if you are in Activity.
getResources().getString(R.string.string1);
If you are a begginer you should read some tutorials after post a question...
I give you a three nice tutorials below :
Want to Learn How to Program for Android? Start Here
Android Programming Tutorial
Android Development with Android Studio or Eclipse ADT
About your question, if you don't know how to use the string.xml resource just read the string-resource guide
Hope it helps.

android help activity page

I want to build a help page for my app so obviously it is gona be texty. Other than putting those texts inside my string.xml or in my layout xml, is there any other ways for me to do it? how do I include paragraphs in them? any help would be very much appreciated!
You should put your help text into strings.xml file and use \n\n for line break as #alex suggested because in future if you want help text in different language then you can easily do this by adding different strings.xml file for different localization
But if you still do not want to put help text into strings.xml then you can use
static final String helpText = "help text";
and set help text into textView.
Put your text strings in strings.xml.
Reference them in your layout's XML.
Use \n\n to mark the beginning of a new paragraph.

When should you use `#+id` instead of `#id`?

I have a bunch of Views in a <merge>, and I included that <merge> into a RelativeLayout. I try to refer to the IDs of those included Views to act as anchors for my other Views, but Eclipse complains that the IDs are not resolving. I found a workaround by using #+id rather than #id when I first refer to them rather than when I actually define the objects they refer to. I've already defined the two IDs in a Style and in the included <merge> where they are declared, so it feels a bit inefficient if I keep repeating the definition of the ID.
Is this the correct way of doing it? I'm assuming it's bad cause the '+' is another initialization. My current hypothesis is that you should use #+id when you first use the ID rather than when you initialize the object that the ID is going to represent, a bit like C/C++ and how they require at least a function prototype in the lines prior to the actual code that uses the function.
Another question I have is when you use the GUI-based UI builder of Eclipse, I noticed that they always use #+id rather than #id. Is this acceptable, cause it seems inefficient to me; it's as if the application will be spending more time determining whether or not the ID has been declared in R.id.
Using #+id format tells the Android asset compiler to assign an ID to your element, it isn't actually an id itself. So if I use #+id/myNewId the asset compiler will create a new id named myNewId and provide a number for it. The actual number can be accessed from your code as R.id.myNewId.
If you use an #id, the compiler will look for R.id.id. You can define your own id's in XML files, as explained here: http://developer.android.com/guide/topics/resources/more-resources.html#Id. You could create your own file in res/values/[your_filename].xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="id_name" />
</resources>
and then refer to #id_name, for e.g.
You can also use the Id's defined in the Android namespace: #android:id/empty
This is well explained in the Android documentation: http://developer.android.com/guide/topics/ui/declaring-layout.html#id
There's also some further discussion here: android:id what is the plus sign for

Categories

Resources