Is it okay to define same ids in Android? - java

I know that if we define different ids it is easy for us to find it in future. I found many answers but they're not what i'm looking for. I've a java function which is bit lengthy and will be used in many java activities. So it won't be possible for me to change ids for every java and xml activity. So is it okay to have same ids in different XML and JAVA files? Or could they make app crash in any way? IDs example :
XML ACTIVITY ONE :
<TextView android:id="#+id/resultview" />
JAVA ACTIVITY ONE :
printresult = (TextView) findViewById(R.id.resultview);
XML ACTIVITY TWO :
<TextView android:id="#+id/resultview" />
JAVA ACTIVITY TWO :
printresult = (TextView) findViewById(R.id.resultview);
As you can see above there are different XML and JAVA but ids are same.

Yes it is okay to use same id in different in different XML files. The point to be considered here is that the activity inflates the particular layout and the findViewById method finds the view using the id in that particular layout that you inflated. Yes, this doesn't introduce bugs however it is a bad practice.
You can refactor the id's via android studio's refactor function. So if you change your id here android studio will search the usages of that particular id and change them for you.
Right click on the id you want to change.
Click on refactor
Click on rename
You will see the following dialog appear
Then you can change the way of refactor by changing the scope and other things at your comfort.

You will not have problem because the components are in different XMLs. But it's better to put different names for them, you will be more comfortable yourself. For example:
<TextView android:id="#+id/resultview1" />
<TextView android:id="#+id/resultview2" />

there is no problem to give one id to many views but:
the views should be in different layout files
it is not a good practice

There is no problem for duplicated IDs until they used in same file. But it is NOT a good practice.

Related

How to update content of the activity without creating new Activity?

I'm trying to make an app and I have made a blueprint for a specific activity, but I don't know how to implement it. The layout contains few buttons at the top of the activity, and each button features some information, which is displayed inside the view. The view which needs to be updated is present under the buttons. I don't want the activity to be changed, instead it should update the contents of the View, which is different for each category/button.
By doing some research I have realised that the "Tab Layout" can be used to achieve my requirements, but I don't want the tabs and I need some stylish buttons as a replacement.
I know I'm not the best at describing, so I have looked upon Dribble and found one design which is 100% similar to blueprint.
I want to achieve this using XML and Java using Android Studio! Every suggestion will be a great support foy my app.
Thanks a lot.
As far as I know, you could achieve that by using fragments (which is the same concept you would have used on TabLayout). I don't really know how much knowleadge you have on Android, but if you know what a Fragment is, it should be easy for you to recreate the idea.
You have 3 buttons for 3 different fragments, so you must design every fragment by separate and change it depending the button you click.

Can multiple fragments share a single XML layout

I have multiple fragments that share the same layout which is a RecyclerView inside a LinearLayout. In the past, I had files like:
fragment_one.xml
fragment_two.xml
fragment_three.xml
fragment_four.xml
Though, I am currently questioning if it is better, to use a single layout named, for example, fragment_generic. Is this a good or bad idea?
Thanks.
It actually depends on your use case. If you have like 5 fragments which have the same layout i.e a single recyclerview list. It is better to use a single layout for all the purposes. That makes the application really compact.
Later, when some changes come into place and you want to add item , let's suppose a fab icon, then create a new layout and use it for the changed fragment only.
So about your question, yes it would be the best idea to use the same layout for these 4 fragments.

How does LayoutInflater deals with IDs when there are multiple copies of an .xml file?

To make my point easier to explain, I will use an example:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thisIdIsVeryCool"/>
this is the .xml that I want to inflate and I will call it sublayout.xml
Now imagine that I inflate sublayout.xml in the main_layout.xml three times.
It does not give any error so I run the app on my device.
Now, right in front of my eyes, there are 3 EditText, all of wich with thisIdIsVeryCool as id.
this makes no sense to me...
What if i want to check if thisIdIsVeryCool is empty or not, and 2 of these EditText are indeed empty, but one of them is not?
Ok, it seems like i haven't ever tried to actually write a code like that and i'm lazy, but trust me, i've tried.
Actually, i've tryed a slightly different thing, and it seems that, when in the .java I refer to thisIdIsVeryCool, it affects only the inflated sublayout.xml that was created first.
so... how are called the others EditText?
And in general:
How does LayoutInflater deals with IDs?
Actually, i've tryed a slightly different thing, and it seems that, when in the .java I refer to thisIdIsVeryCool, it affects only the inflated sublayout.xml that was created first.
I am not sure that the described behavior is reliable. I would not make any assumptions about which widget you get for that ID, if there are multiple possibilities, given where and how you call findViewById().
how are called the others EditText?
Sometimes, you hold onto them when you inflate them.
Alternatively, depending on what you inflate them into, you might be able to distinguish between them by their parent.
When you call findViewById() to retrieve a widget, it is your job to ensure that you will get a unique result, by a combination of what View you call findViewById() on and what ID you supply as a parameter.
How does LayoutInflater deals with IDs?
It assigns the android:id values to the widgets it creates, as you have them defined in the layout resources. Otherwise, it ignores them.
this makes no sense to me...
Widget IDs are not guaranteed to be unique. In fact, frequently they are not unique, such as with rows in a ListView or RecyclerView.
This is not significantly different than how the id attribute works in HTML. It's not like an HTML page is somehow invalid, and browsers will refuse to render it, just because you have two tags with the same id. Now, if because you have multiple tags with the same id, you encounter problems when trying to manipulate the DOM in JavaScript, that's your fault, not the browser's, not Sir Tim Berners-Lee's, or anyone else's. The same thing holds in Android: android:id attributes are there as a means of helping you find widgets. It is up to you to use layout resources, widget IDs, and findViewById() using patterns that work.

Designing two column Android application - switching views?

I am building a two column application for Android and I'm wondering how to do the navigation. The left column is the navigation bar. The right one is the content view. Both of them are LinearLayouts.
I have a different activity for all the options. How do I implement my menu into these? It is the same for all the activities (except the current one is highlighted), so copying the same code multiple times seems waste and makes it harder to change it later because I would have to change all the files.
I also have to change the title for every activity.
The typical answer would be Fragments. Here's a great tutorial on that topic.
However, depending on the triviality of your requirements, you could also consider using a horizontal LinearLayout containing your two original LinearLayouts.
in my opinion you should use fragments for your columns.
http://developer.android.com/guide/components/fragments.html
you Should use Fragment control for this. you can call content Fragments on right side (Content View) area with the click on leftSide(Content item/Index) .
I feel you should follow this link.
http://developer.android.com/training/multiscreen/screensizes.html
Am not sure but hopes u asked for the same.
Thanks..

Can multiple android views be stored in the same .xml file without belonging to the same parent?

I have an android project that has several small views which I need to instantiate at runtime. I haven't been able to figure out a way to store all of these related views in a single xml file and I now there are going to be many of these xml files. I was just wondering if there is any way to have them all in a single file, but not belonging to some parent ViewGroup.
The layout folder in android kinda sucks since there's no way to make subfolders, everything is just piled into the same place, ugh.
I hope someone can tell me of a better way of organizing these things.
If I understand you correctly you want several views meged onto one screen or merged into one xml file. You can include other xml's into one.
The articles showed you how to use the tag in XML layouts,
to reuse and share your layout code. This article explains the tag and how it complements the tag.
http://developer.android.com/resources/articles/layout-tricks-merge.html
Also, this video might help (about 19 minutes in). Shows you how to extract a current layout and be able to include it in others.
a couple things:
Yes, the layout folder is a pain. I use strict naming conventions to make it bearable, and in eclipse use the shortcut ctrl + shift + r to quickly find the layout I am looking for. Try naming your layouts after your activity: activity1_menu_overlay and activity1_main. With the above shortcut, just type out Activity1 and it will only show you the relevant layouts.
And if that doesn't work, you can try wrapping all your views in LinearLayouts and using view.setVisibility(View.Gone); or view.setVisibility(View.Visible); to show/hide the appropriate views.
Here is an example of that second one, because it's tough to explain.
one XML file:
<LinearLayout>
<LinearLayout ... android:visibility="visible">
<copy/paste of view 1>
</Linearlayout>
<Linearlayout ... android:visibility="gone">
<copy/paste of view 2>
</Linearlayout>
<Linearlayout ... android:visibility="gone">
<copy/paste of view 3>
</Linearlayout>
<Linearlayout ... android:visibility="gone">
<copy/paste of view etc.>
</Linearlayout>
</Linearlayout>
keep in mind this approach will require you to define a reference to each "child" LinearLayout view in your activity, so you can call setVisiblity appropriately.
This approach works well for animations, and I would only use it for 2 or 3 possible views in one xml file.

Categories