Is there any way to avoid creating another activity to show an xml page from another xml by clicking a button.
The same activity must be used instead of creating another one in this case.
You can inflate your XML View to one of your Layout in Activity's xml Layout file.
1. Create Blank RelativeLayout and set visibility invisible.
2. On your action performed set Visibility true and inflate view to Relative layout like below code.
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
Welcome to the Android world , check this out
Fragment
Related
I am new to Android development and have created a new project with the ‘Bottom Tab Navigation’, unlike the blank activity where I get a java class and a layout activity and to put Buttons in the onCreate method the HomeFragment and HomeViewModel doesn’t have any of those fields. I want to add a few buttons And Image Views to them...Cam anyone please help me by telling where to put code like private Button xyz; and then xyz=findViewById()
You can add image and title in bottom navigation bar by going inside this path
res/menu/ bottom_navigation_menu.xml
You don't need to create the object of these buttons and texts. but if you want to access these items then define BottomNavigationView by findViewById() then call below method on this object.
BottomNavigationView.OnNavigationItemSelectedListener()
You need to implement onNavigationItemSelected inside this which will give you the ID's of all bar menus. I hope this will help.
I need to be able to clear the action bar menu and inflate a new one from within a nested fragment when the particular nested fragment becomes visible.
My app consists of a main activity with a ViewPager which uses a FragmentPagerAdapter. This ViewPager has 4 root fragments which are simply blank FrameLayouts (one in each page) that each contain and swap out nested fragments. These nested fragments can be different or the same type (think about how Instagram can show a different profile view in multiple tabs). This means that I can't use the onPrepareOptionsMenu to inflate menus on page swipe, because I won't know which nested fragment is in the root fragment of each page. Additionally, I have set the offScreenPageLimit to 3, so that I can swipe between root fragments without reloading. This means that I can't just inflate the menu from within the nested fragments, because then all four nested nested fragment's menus will appear.
The ideal solution would be able to inflate menus from within the nested fragments through some sort of callback that gets executed when the nested fragment is literally displayed on the screen, e.g. when we swipe to the page containing the root fragment which contains the nested fragment, or we swap the nested fragment for another within the root fragment of a page. Does anyone have a design solution to the problem I'm having? Thanks for any help!
The ideal solution would be able to inflate menus from within the nested fragments through some sort of callback that gets executed when the nested fragment is literally displayed on the screen.
Create a menu for the fragment in the menu directory
Report that this fragment has options menu have a look
Then just inflate it here, and override this method and when the fragment will become visible those menu options from fragment menu will be added to activity menu options.
I have a TabHost with a ListView inside a tab. When the activity starts, I can't scroll the list view until I have clicked off the tab that the activity starts on, and then re-select the tab with the list view inside it.
Note, I can get the list view to focus by calling:
tabHost.getTabContentView().requestFocus(); but unfortunately, I still can't scroll until I have reselected the tab...
Have any remedies?
Thanks!
Fixed - I ditched the tabs in order to simplify the layout.
I have a ListView that I want to dynamically add items to. For the items, I want to use a specific layout that I have defined in XML. How do I go about adding the items to it and have each item use the specific XML layout?
Do I have to create an adapter and jump through all those hoops? There is only a handful of items that I need to display. Each item has a few text views that need to be populated as well as an image that needs to be displayed.
If I go with the Adapter route, I need to basically creating a custom object/class that contains the text for each textview as well as the URL of the image I'm downloading. Seems like way overkill for just displaying a handful of listview items.
Isn't there someway I can just iterate through my items, inflate a view for each and add them to the listview?
you want to use a ScrollView for your purpose. works just like a listview, except the rule is a ScrollView should only have one child layout (that layout would be containing all the items you want to put inside). inflate a layout, then addView(). rinse repeat.
Unfortunately you do have to create an adapter. SimpleAdapter is about as easy as it gets. Here's a nice example that'll have you up and running in a few mins:
http://ykyuen.wordpress.com/2010/01/03/android-simple-listview-using-simpleadapter/
Either you can create an adapter which is really not all that complicated, or you can use a ScrollView with a LinearLayout inside and inflate yourself. Either option is reasonable, depending on your requirements, but inflating yourself and adding the views to the ListView manually isn't one of them. By the description you give, it sounds like you might just want to go the LinearLayout route.
Put a ScrollView with a LinearLayout inside in your main layout XML.
For each child:
Inflate the view for the child item and populate the fields accordingly.
Add the child view to the LinearLayout using addView.
On opening the activity, the user chooses an option from a spinner. I have the spinner working and I can successfully read the onItemSelected listener from the spinner. However, after the user clicks the spinner, I'd like to load a different form layout below the spinner; based on their choice. Can I store the different layouts in the same XML file, and then just call them based on their ID?
You can use different layout on a layout.See how to Inflate Layout.See this link for android layout Inflate.In your main layout create two linearLayout.add your spinner in the first layout.And in the second layout add various layout according to your choice using LayoutInflater