Is it right to have same Id for a TextView and a ImageView ? Since they belong to one entity I gave both of them same Id. If yes.. then how can I find these views by id separately ?
-
1if they are on separated activities yes you can – masoud vali Jul 14 '18 at 18:27
-
Look here: https://stackoverflow.com/questions/14759056/two-views-with-same-id – Thommy Jul 14 '18 at 18:28
2 Answers
Is it right to have same Id for a TextView and a ImageView ?
Short Answer: NO
Long Answer: It is not right to use same IDs because by doing it can cause runtime errors. Consider the below example
layout.xml
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="12dp" />
<ImageView
android:id="@+id/location"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginStart="33dp"
android:layout_marginTop="5dp"
app:srcCompat="@drawable/openLoc" />
LocationActivity.java
setContentView(R.layout.activity_profile); //inflated layout
txtLocation = (TextView) findViewById(R.id.location);
Here you will face the problem in Activites because it will be confused that which element should be picked from two.
Btw YES you can use same IDs in the different layouts because it won't make any runtime error as it will search IDs on inflated layout only.
EDIT: You can have same IDs in the same layout. It causes an issue when you call it by findViewById()
and throws similar exception
java.lang.ClassCastException
android.support.v7.widget.AppCompatImageView cannot be cast to android.widget.TextView
Suggestion: I don't know why you want to assign same IDs to two elements but if you want readability then I would suggest you to set ids in a way that elements can be easily identifiable by ID like android:id="@+id/txtLocation"
android:id="@+id/imgLocation"
it makes it easy to identify element type just by reading ID. You can make it even easier by appending layout name in beginning like android:id="@+id/profileTxtLocation"
. Now this will help you while coding as autocomplete feature will assist you. Just type layout name you will get the list of all layout elements, then you will type the kind of element you get the list of all asked elements(es: textViews) in layout.

- 4,175
- 3
- 28
- 35
-
not at all,: in any `ListView` / `RecyclerView` you have items that have children with same ids - so it is a normal situation – pskink Jul 14 '18 at 19:27
-
Its not a good idea to have 2 children of the same layout with the same id. However 2 children of different layouts, or 2 grandchildren with the same id is incredibly normal. That can cause an issue with findViewById on the root, but wouldn't on the intermediate layer. – Gabe Sechan Jul 14 '18 at 19:46
You theoretically can but it's highly advised not to. Duplicate IDs within same layout since it will prevent finding view by ID (You will need to iterate over children or otherwise reference them), and cause collision/outright crash when those views will attempt to store/restore their savedInstanceState
.

- 15,548
- 3
- 36
- 36
-
Important bit buried here -- you should not have same id within same layout. There is nothing wrong with having same id (e.g. for toolbar or something) in different layouts. – Ridcully Jul 14 '18 at 19:39
-
@Ridcully Actually the same layout/different layout makes no difference, once on screen what layout they're in is lost. Being in the same parent view is generally bad. Being grandchildren of the same view generally isn't, and is fairly common with inflated tables or list/recycler views. – Gabe Sechan Jul 14 '18 at 19:48
-
@GabeSechan views generated by list/recyclerview adapters are NOT suspect to `activity`/`fragment` instance saving, so it doesn't matter what IDs they have. However "normal" content views will save their state (for example `EditText` stores current input, `CheckBox` stores checked sate) using their `id` as a key so duplicating it will cause a collision. – Pawel Jul 14 '18 at 20:47
-
@Pawel And how do you think you do a table (not a list) in Android? You inflate the same layout multiple times. Which will duplicate the same ids. Its very common in advanced layouts to use techniques like that, or like include files. Which would cause the same id multiple times. – Gabe Sechan Jul 14 '18 at 21:59
-
@GabeSechan I'm not denying that you should never have duplicates, just pointing out IDs are also used for to store `saveState` so unless parents of those views have save state disabled it can cause problems – Pawel Jul 14 '18 at 23:22