Hi I have a layout which has <include/> in it. I can access this include and change the visibility of some views in it or modify the edittext text.
So what I want is to create a method so that when I press a button the layout inside the <include/> would turn into its original XML state hiding what was hidden in XML and showing what was shown and to return the edittext to their default values from the original XML layout file.
Note that I am not using any fragments or changing the setContentView in my application.
Thank You So Much.
I found the best solution for this problem what I did is to copy the OnCreate code to another method thus it is callable then what I will do is setContentViewto be the layout that I want to turn it back to its default case then calling the method that contains the OnCreate code to return all the ids then using the include method I can show the layout inside another layout.
Related
I was following an android development course and the instructor created an layout file named activity_youtube.xml and also gave the id to the layout as activity_youtube, later in the .java file the code he wrote was
setContentView(R.layout.activity_youtube)
ConstraintLayout constraintLayout = findViewById(R.id.activity_youtube)
I am really confused between this naming convention.
What are we refering to when we call R.layout.activity_youtube and what are we refering to when we call R.id.activity_youtube
When you are calling R.layout.activity_youtube you are referring to the layout xml file, so to everything that's inside.
While when calling R.id.activity_youtube you are referring to a specific component inside that file (a layout, a Button, a textView... ) that has the id property assigned to that name.
I will admit that it might be a little misleading calling the layout. xml file and its layout component the same.
R is a public final class in Android which extends Object class. It is purely there as a mechanism to allow you to easily reference the contents of your res hierarchy from your code. As such, you cannot create sub-hierarchies.
R.id is a nested class file created in android programming its also auto-generated file. It is used to refer to a UI component. Let us suppose you have two buttons in your UI(R.laoyout.your_layout_name) you can distinguish them and decide which action to be performed on the click of each button by using R.id.button1 and R.id.button2. These buttons lie in the layout file which you have used in the activity using R.layout.
setContentView() sets the View to be displayed as the main content view. The method is overloaded, so you can either pass an object of View class (or it's a subclass, such as LinearLayout, TextView, etc.), or you can provide the XML layout resource by using R.layout.your_layoutname.xml.
Thus, R.layout. references any layout resource you have created, usually in /res/layout. So if you created an activity layout called activity_main.xml, you can then use the reference in R.layout.activity_main to access it.
In my application's main layout I have a button of Search task. Now when user clicks that button I want to change the layout xml file to search_layout.xml.
I don't want to create a new activity for Search task and want to use my activity_main.java class for its coding. So how do I inflate search_layout from existing activity_main.xml .
My activity_main layout is Coordinating layout with Collapsing toolbar and Recycler view.
In my search_layout.xml I have a Simple relative layout.
So how to deal with this ? I searched for this but everywhere I get how to inflate view adding to an existing view. But in my case I want to totally change view.
Thanks
You can simply call setContentView(R.layout.your_search_layout) in the click listener of your button.
Example -
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.your_search_layout);
}
});
However, it is always good practice to make your code modular, hence you should consider using Fragments to achieve this.
You can have a look at this question, but, this is not a good practice to follow and you should consider using Fragment or any other mechanism. By doing this you are cluttering your Activity's logic with search logic.
There are two ways to do it: one is make frame layout the parent tag for your layout and then create two layouts: one that you have already created and and one for search layout framed over it and make visibility for search layout GONE and when you click search make its visibility visible.
The second method is to create the new XML layout file for search layout and inflate it like this:
View v = getLayoutInflater().inflate(R.layout.YOUR_LAYOUT_ID, null);
and elements of this layout will go like this:
Button button=(Button)v.findviewbyid(R.id.your_button_id);
Changing visibility of view will help in your case where you totally want to change the views
I think in your case, you should fragments to handle this situation.
Read this for more details on how to replace Fragments using FragmentManager.
Here's a nice example on how to change fragments in android :
I want to dynamically set the contentView in my activity.
Because one time i use a xml as contentView, but at anothertime i use a custom view as contentView.
But how do i change the contentView?
I read about that the ViewFlipper can do this, but a viewFlipper is implemented in a xml file. And within this ViewFlipper you can add your different views.
But i dont know them at the beginning, so i cant write them all in my xml file.
Do you have any idea?
Thank you
You can call setContentView at any time*, not just in onCreate. Just define all the views you want in separate XML files and pass the relevant id when it's time to switch. If you want to define the new layout dynamically in code, then do that and call setContentView and pass the root view of your new layout.
* Technically, you can call setContentView any time you are executing on the event thread. Otherwise you need to use a Handler to call it.
In my custom view I need to use the getParent method and set the visibility on some of it's child views depending on my custom view's state. The problem is that I want to instantiate the child views just once. Where is the best place to do this?
I'm not exactly sure what you want to do. But if all of this is occurring in the same Activity screen why don't you just assign ids (e.g. android:id="#+id/someId" to your elements in the layout.xml file.
This way you can reference any element programatically in your code by calling:
View someView = findViewById(R.id.someId);
I am unclear why you would need to call getParent. If you are trying to manipulate views in a different activity then I think you will need to use a Handler.
I want to create a pocket reference application. So, much of the content would be texts, linkbuttons and images.
I wonder where is a good place to put all of the contents. I could place it hard-coded on the source code, So, when a user click a linkbutton, a new view will be opened and in the source code I specify the TextView and setText, etc. But I think it's not a good idea. Can I put the content in an xml file or in a database? Which one is better for this case?
I see that we are encouraged to put layout in main.xml. But, from what I read, the xml layout is static, what if I want to put some TextView, but I don't know how many TextView would be displayed, because the content would be loaded dynamically/programmatically?
Thank you.
Not sure it this is what you meant:
You can initialize your application ui by an android xml file layout.
to inflate, you use this method.
in your activity's onCreate()-Method or even later, you can then get the TextViews or whatever you want by calling findViewById(R.id.textview). Note that this method will search all over the layout xml file for the specified id and though blocks the ui thread while searching. if your textview is very near at bottom and many other elements come before it, this can take some time.
if you want to build your own layout dynamically, you have to do this programmatically of course.
for general layout declaring, refer this tutorial on android dev guide.
You could write the textView in a xml layout and inflate it dynamically in the activity as many times you want
View view = getLayoutInflater().inflate(R.layout.scroll_project, null);
//then add the view in linear layout as
layout.add(view);