How to reach two layouts in one java class? - java

I want to include two layouts in one java class. How can I make it? I am using android studio.
I want to use both layouts. And I want to use two buttons in different layouts from one java file. I am using setContentView but I am using a button from different activity too. So when I try my app, it crashes. I am using findviewbyid from different layout and app crashes. How can I block it?

If you have two separate layout files, you can use include to add those the layout you are inflating in that activity.
<include layout="#layout/layout1"/>
<include layout="#layout/layout2"/>
Add these inside the layout of that activity and you can then use the different layout and the buttons in them from the same activity.
Although without much clarity on your specific use case, the help we can give is limited.

Related

Android: What is the best way to avoid code duplication when several screens of the app have similar layout?

My application has several screens, and each of them has almost the same layout.
I know I can use the "include" tag in xml to avoid rewriting the same layout again, but I also do not want to duplicate the java code of setting the behavior/properties of views and layouts.
What is the best practice to be more organized in this case?
Edit: For example in several screens, the first half of the layout is a gallery that scrolls horizontally with text below it. This is the same throughout my app
If the screens are the same for example. Activity/Fragment
You can create a "generic" parent that handles the logic and use include for the layout.
Or
Create a custom view that uses a layout as content and you can write your logic there.
I create a library that uses this principle.
here is a tutorial on how to create a view with a layout file

XML layout display Order

I have more than one XML-layout on my app and i am looking for a way to change the order in which my XML-layout displays when running my app
like which one of the layouts is displayed first ?
I think I didn't understand your question exactly but you can have as many as layouts you want. your activity shows the layout inside oncreate with setContentView(R.layout.yourLayout). If you want some particular parts in your layout, you can use fragments.

What is the difference between layout and activity?

I have some problems with the layout and activity and I don't know are they different,are they related?
I think the layout is a place we can add or remove our views and activity is just a place that shows any thing in our layout, is this true?
An activity:
is an instance of Activity, a class in the Android SDK. An activity is responsible for
managing user interaction with a screen of information.
You write subclasses of Activity to implement the functionality that your app requires. A simple application may need only one subclass; a complex application can have many.
A layout:
defines a set of user interface objects and their position on the screen. A layout is made up of definitions written in XML. Each definition is used to create an object that appears on screen, like a button or some text.
A layout deals with the user interface. Its where you set all your views that will be visible on the user interface.
The code behind (.java) sets the layout you created as the content view and manipulates the behavior of the views you have set. For example, sets the text for a text view.
The activity then is the whole thing, the layout and the code behind.
An activity is the java code which attaches actions and puts content to/in a layout. For this the Activity loads the layout.
Briefly,
Activity is the java part of your projects. The program and any kind of algorithms are implemented here. Also layout views come to life in an activity.
Layout is where you organize the views in your page. But without activity, they have no meaning. Because in activity, you have to get these views and use them programmaticaly.
All together, you load views from layout to activity and in activies you implement your whole program.
A layout defines all the appearance of an app and this is of no use without a java program which helps in real functioning of that visual display.
Thus we define what an app does by writing its java code and a special java class called activity decides which layout to use at a particular instant and tells the app how to respond to the user.

How to Determine XML layout for Fragments in Tabs?

Okay, So I just started Android development (I am average at VB.Net, so I understand basic code even if its not in VB). I've made a couple play around apps which used Text-To-Speech, Async Tasks and Reading/Writing files. However, I now wish to make a tabbed Android app.
I started the project in Eclipse Juno and filled in all the details. I then selected (For navigation) the Tabs/Swipe layout. I now have the default code for that layout type (Link to Tabs on developer.android.com - http://developer.android.com/design/building-blocks/tabs.html).
I understand mostly what the default code is doing. The only problem I am having, is determining the individual layout of my Tabs. I wish to have 2 Tabs, 1 in which the user selects an option, and the other, in which an image is shown depending on the selection in Tab 1.
So the question is: How do I create a .xml file in Layout to determine what is shown on the Fragment?
If you want to do this in XML the answer is simple, it can't be done just with XML, you must create a class that's implementing a ActionBar.TabListener.
Than you can override the onTabSelected method in which you can exchange the content.
A proper solution would be:
Use a LinearLayout as root container, and implement two Fragments for each of your Tabs (there you can design an individual XML-layout). Now you can add one fragment initially to the root-container and implement the exchange of the layouts inside the onTabSelected method and you are done.

Two Java classes in the same view

I am new to Android and Java Programming and I want to ask if its possible one r.layout View to "hold" two or more different classes. I'v tried to put the same r.layout.id in both constructors but it seems that the second class call creates a second instance of the View.
Thank you in advance for your help
Activities uses layouts. Layout themselves have no idea about which activity is using them. So yes you can have one main.xml layout which can be used by two different activities' setContentView. But both Activities will have separate instance of layouts in-memory.

Categories