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.
Related
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.
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.
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.
After a lot of time spent I can't find the solution. I want a button or any clickable view should stay visible for all activities and it should be only for one specific app not like chat heads. I am basically making a library so that's why I can't use base activity.I have attached the image as well for a better explanation. How can I achieve this any suggestions? thankyou...
I
It sounds like you need a ViewOverlay. This is the API documentation.
ViewOverlay is usually tied to a single view, but if you wrap it in a fragment, you should be able to attach it to each view in your application. This should create the effect of an of an application scoped overlay.
There may be a more elegant way of doing this, but I am not aware of it.
EDIT: You can also wrap your layouts inside a frame layout(s) along with a seperate nested view (the view that you want to keep on top of the stack).
Frame layout creates a 'stack' of inner views. If you use this approach, you can programmatically ensure that there are exactly two views present and visible as children of your frame layout at all times. one will be the layout tied to your current activity. The other will be the view that you want to be overlayed.
I know that the term 'programmatically ensure' is vague. This is because there are many ways to make this happen. It is up to you to decide which way best suits your needs.
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.