Can I assign the same id name to two different views which are in two different xml files?
It won't let me change it using "Edit ID.." button, but when I change it manually, it doesn't say anything and works fine. But might it become a problem and why?
If possible, please add a link or an general explanation on android visual tree build, I would really like to learn on the subject.
Thanks.
It's fine to have the same ID in multiple XML files. The only time this might be a problem is if you had a nested layout containing an ID which is also present in the parent.
Having the same ID can actually be useful in some cases- for example if you load a different XML file in different circumstances (such as portrait and landscape) you can give views the same ID, so in code you just call findViewById once and it will work.
You can't use the same ids in same layout .. but if their behavior is different then it is possible.
public class MainActivity extends Activity {
Button btn;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btn1);
tv=(TextView)findViewById(R.id.btn1);
}
You can use the same ids in different layouts but it is recommended that you use different ids for different layouts because when you will deal with lots of layouts then it will create a problem to recognize which id is for which layout ...
Related
I want to display a LinearLayout from the XML-resources in another pre-existing one so I can easily make copies from the same model.
(without having to declare programmatically all the buttons, textViews...)
I'm looking for a method like the one that works for activities:
setContentView(R.layout.myLayout);
Is it possible? And how?
Thanks a lot.
Create a layout file sublayout.xml and put this file in
YOUR_APP/app/src/main/res/layout
then you can include it by
<include layout="#layout/sublayout"/>
in another layout file.
Anyway, according to my experience, you can not
include sublayout.xml more than once, because
each id, like android:id="#+id/sublayout_id1" can only be used once.
My Android app has two layouts, one for <= normal, and one for >= large. I can re-use my corresponding activity 99.9% in both cases, but I need it to know different bits of information for the two layouts. I could of course write a giant if expression, but I'd rather let Android work its magic, since it's already doing it by loading the two different layouts.
So, can I embed pieces of information in the two XML files and retrieve them in my activity class? Or am I completely off the map and the right approach is completely different?
Sure you can, just in the values directory define values for each size and retrieve them dynamically in your program.
/res/values-xxx
-> a.xml
/res/values-yyy
-> a.xml
...
here is an example:
<resources>
<integer name="maximum">100</integer>
...
</resources>
in your program just put:
int max = getContext().getResources().getInteger(R.integer.maximum);
for each size android will magically do the job and give you the correct value!
If you're willing to go the custom View route, then yes, you can. What you have to do is create custom attributes and apply them to your custom views which are parsed when they are created. Here is a thread that goes in to a great bit of detail about it.
The Views themselves don't have to be special. You can say, have a view called a PropertyView view which extends FrameLayout and has a method called getProperty(). You then put the property in the XML like so:
<com.example.ProperyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:property="My Custom Property"/>
Then you would parse it using the methods described in that link.
EDIT:
Alternatively, there are other elements in the XML landscape that can be put in to buckets similar to how Layouts are. Any folder in the /res folder can have the same buckets that the Layouts can. That includes anything in the values, drawables, raw, or xml folders. You can reference these in your Layouts and the Android system will pick which ones you want. More example for how dimens work.
If you are using values to differentiate between the two layouts, then you can have different values that overload depending on screen size. See this answer.
You could do the same sort of thing with the layouts directory to create different layouts, but then use common subsections using the < include > tag to make the different views based on common sections.
Or a combination of the two. If you the want to change the behaivoir of the Activity/Fragment, you could key that on the presence of missing or present screen widgets.
I have two activities that have the same code. The main difference is that they have different content views (and therefore a few different elements).
What is recommended? Should I leave it like it is in two activities or should I make it one activity and and solve it using if else queries each time when there is something about the view (e.g. setcontentview and a few (5) other view related code blocks)?
For example
if (isLandscape) { //landscape looks different than portrait
setContentView(R.layout.activity_landscape);
} else {
setContentView(R.layout.activity_normal);
}
Is this recommended / good coding style?
If you are setting different views for portrait and landscape this can be done through the resource files using a layout-land directory and adding the new activity layout with the same name in the landscape directory.
So you would have a activity.xml file in both the layout and layout-land folder.
To answer your question in general though, it works well to use a member variable in your activity to distinguish between your two cases. Then when you need to do something based on the state of this variable using the if/else logic.
For example, using a separate layout with a unique view container on tablets. You could check if(findViewById(R.id.tablet_container)) != null) then you would set your isTabletView member variable to true or false based on this. Any time you needed to do something based on if the app is being run on tablet or phone you can reference this variable.
For ex. I have 2 Activities.
In first: setContentView(R.layout.activity_first);
In second setContentView(R.layout.activity_second);
activity_first.xml contain View with id android:id="#+id/my_view", and activity_second.xml contain another View. To that View I can set the same id (android:id="#+id/my_view") and all works great.
But the way to set equal id's in different xml files correct? May be I miss some google post about that situation?
There is no harm in setting the same ids to different views as long as they are not in the same view. From the developers site,
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).
It is recommended that you use different ids for different layouts. On the long run, when you will have a lot of layouts and a lot of ids it will get very complicated to differentiate them.
There is no problem with same id.It will work properly. But for a good programming this habit is bad.
I am beginner in android development and my problem is that ı want to add some buttons in runtime. I mean, number of button will be change according to flow of program so i need to create different number of buttons at different situations. In code section i can handle it by using array but what about layout file? how can ı set the layout file according to flow of program. I hope ı could explain my problem. Thank you very much.
The xml files in res/layout are static descriptions of layouts. You can create different ones to be used in different contexts (different activities, dialogs, etc). You can actually even replace one layout with another one in the same activity. What you cannot do is to modify the xml files during runtime.
If your UI depends on runtime variables, then you will have to act accordingly. If it's just the number of buttons that will change, you can either
Add new buttons using addView(button);
Add a ListView to your xml file and use an ArrayList and an ArrayAdapter to determine how many buttons you will need.