i want to get views from layout defined in my layout folder but i get null pointer exception, i know that before i use findViewById i have to have the layout somewhere used in any activity so it gets initialized, is there anyway to get views from layout that has not been used in any activity cause i got one activity that uses layout which i dynamically change and in that layout i remove all of it's child views and apply one of the three layouts that i have aside as potential layouts to fill in the default activity's layout..
I need this to make dialogs cause i have one dialog that has one layout which is transparent and has attached onTouchListener and when i click the layout the dialog dismisses, now my idea is to get other layouts which have size defined and then fill in that layout in the default transparent layout of my custom dialog, and do this with any layout i could image so i can make dialogs from all the layouts i want..
Thanks, if you need anymore info about my idea please write me this is important to me!
You can use getResources() from the context to retrieve resources individually:
getResources().getLayout(id);
More info here: Accessing Resources
Related
I would work with a piece of layout that isn't shown, the user must scroll to see what I have did. How can I do it? Can I join 2 different layout in one? I prefer to collocate item as I do with default layout. If it can help, I use Android studio. I prefer use elements without code, adding them from palette. An example :
You can use two different layout in main relative layout & whenever u want child layout just set visibility.
You will want to use a ScrollView for this. From the documentation:
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display.
I am developing application in android.What I want is ,my activity should represent two xml layouts files.concept is like,
->when the activity is started it should show one layout(screen)
->when I click on the button exist on the first layout, it should show 2nd layout in the bottom of the screen,keeping first layout visible.
Have both the layout in a single XML. Keep the visibility of the the second layout to secondLayoutObject.setVisibility(View.GONE) initially and then on the click of the button change its visibility to secondLayoutObject.setVisibility(View.Visible).
On method to call two xml files in on activity is by using layoutmanager and assign the screen ratio for both xml files. Use relative layout in both xml. Small code snippet is
RelativeLayout layleft = (RelativeLayout)inf.inflate(R.layout.firstxml,null);
RelativeLayout layright = (RelativeLayout)inf.inflate(R.layout.secondxml,null);
for detail info Layout Reuse help
For this you have to use the concept of visibility. Initially set visibility of second layout as GONE and when you press button set Its visibility True.
you can try use the bellow example:
https://github.com/AdilSoomro/Iphone-Tab-in-Android
this source code to change layout like button click to load another layout!
I want to add the Another Layout in to the current Layout. . .
I have use the TabView in My Application and now i want to change the Layout while the specific tab is clicked. and the Main TabView remain in Same Place. .
How it is possible ???
Help me Please..
Thanks.
use 2 layouts and set the visibility to layout according to situation
You can inflate your views on the fly from layout xmls, and them to the current layout / view.
This thread about dynamic number of gui elements in Android shows you how to achieve it.
i have to create a layouts dynamically depending on the some action for example depending on action i have created 5 layouts which is horizontal scroll and i want the control of each layout how to go about it please give me some suggestion
Your requirement is not entirely clear. If you want to load layouts from a xml resource fiel you can use LayoutInflater. This will also return a handle to the loaded layout that you can use for further manipulation.
You should use the layoutInflater to load the layout.xml files, later in List add the handlers in for loop.
unless you set the inflated layouts to some Activity to display it has no effect. Hence consider ViewStub and inflate the things inside the ViewStub.
If you are inflating and adding it as a part of some component ( like listView items), then you can get the references using a global ArrayList, to store all those handles.
I am unable to understand your question fully, can you please elaborate your problem?
I'm just writing my first android app and in one layout xml file I have four buttons that will display a different layout when pressed. Instead of creating four seperate screens, I've just put the four layouts in the same layout as the buttons with android:visibility="GONE", and then when a button is pressed, I set it's corresponding layout to being visible.
My question is, is there a best practice or suggested method for keeping track of the active layout so that when a button is pressed you can set the active layout back to visibility="GONE" before making the new one visible. I thought I could just set a string value with the ID of the active layout, but then findViewById wont take the string to get a hold of the layout. Any help or suggestions would be great. Thanks!
Why do you use a string to save the ID of the layout? Can you not just use an int since the ID's are such? Or set the visibility of the layouts from the onClickListener you attached to each button?