I am trying to figure out how to load different groupviews dynamically.
I am creating application that will have menu with 4 categories and each category will have several sub-menu items. I have created four LinearLayouts with sub-menu buttons inside (they are separate xml files).
Now I want to display appropriate sub-menu in my main view every time the category is changed.
So far I have:
HorizontalScrollView sView = (HorizontalScrollView)findViewById(R.id.CustomScrollView);
LinearLayout ll = (LinearLayout)findViewById(R.id.category1_menu_layout);
sView.removeAllViews();
sView.addView(ll);
However this doesn't work because the ll for some reason is null(category1_menu_layout is the id of the LinearLayout inside the category1_menu.xml)
Do you have any ideas why that doesn't work? Maybe there is another ways of doing this?
Thanks
Do you have any ideas why that doesn't work?
Because you do not have a widget whose ID is #+id/category1_menu_layout presently defined in your activity.
Maybe there is another ways of doing this?
Use a ListView. Or use an ExpandableListView. Or inflate the categories using getLayoutInflater(). Or create the categories in Java code.
Related
I was wondering if I want to create a simple list of buttons, a recycler view will be needed or if I can make do with a listview. Thank you
The simplest method would be to create the buttons and add them the view. I strongly recommend you to not do it the following example is for demonstratiom purpose:
onCreate...
LinearLayout root = findViewBy...
for (item: dataList) {
Button btn = newn Button(this);
btn.setText(item.text);
root.addView(btn);
}
In this example Im using a linear with vertical orientation, that should be inside a scrollview.
That is bad because every view is in memory at the same time. If you have just 2 or 3 buttons then there is no problem but if the number raise to hundreds then there will be memory usage problems.
This is why ListView got deprecated, because every row was rendered. Large data set made the UI slow. Instead RecyclerView literally recyle the views as the name implies. In memory there is only the view on the screen and a bit extra, so when a view leaves the window is available to be reused by the incoming row.
By the comments I can see you are also confused with views and viewgroups. A TextView is a View it can not have another View inside. If you only need to have a click, then TextViews can use a setOnClickListener, other is the case if you need the appearance of a button. Anyway, when you create an adapter you can add any layout you want.
A list of buttons can be achieved in both the ways. But ListView is outdated. So better use RecyclerView
I'm making an app where there's one main activity that is a Fragment. I basically have a few tabs and each tab/page layout I want a different layout. For one of them, I want to know if I can dynamically create a table of, say 20-ish rows with X amount of columns. Instead of having to manually create each row inside the layout .axml file.
The reason for wanting to dynamically create it is that I'm obtaining some data from online that I want to automatically put inside a table.
You will only need to create 1 layout for the item and it will be your item 'template' to inflate it into your ListView dynamically. Take a look at this RecyclerView example (which is the improved ListView)
I am developing an activity with a ListView in which I need to change the current row by another layout by clicking on the row, and I'm not finding any way to do as much as I look (I take hours searching for possible solutions and I have not seen any reference to this problem). I do not know if this can be done in Android, but if anyone has an idea of how to do this would be appreciated.
Thanks in advance.
PS: The ListView control is normal and just want to replace a layout with a different layout. I'm using the API 15.
Use a ViewSwitcher
http://developer.android.com/reference/android/widget/ViewSwitcher.html
A ViewSwitcher is -
ViewAnimator that switches between two views, and has a factory from
which these views are created. You can either use the factory to
create the views, or add them yourself. A ViewSwitcher can only have
two child views, of which only one is shown at a time.
I suggest merging the two layouts in a single one and hide the second one. In your adapter data you should have a flag or something to indicate which layout to display. When you click a row, toggle that flag for the selected item and notifyDataSetChanged() on the adapter. This will make sure the changed layout remains even if you scroll up and down and the row goes off screen.
A more optimized solution is to have different item types in the adapter.
I have a major problem with an application im trying to developer atm. I have this view as you can see in the link below. How can I inflate this so I dont have to spam it 10 times in my xml file?
I also want to have separate id's on the items in column area, exercise, set/km, reps/min and the button. There will also be onClickListeners on each of those columns on each item.
And I will save all of these in a database/text file so I can put every back at its place when user opens this view again.
How can I do this in a sample way and keep an overview on each of the items?
And I should mention that all these items is in a tablelayout and on there separate tablerows.
Example Picture
Create your own custom view for a row, in code, which handles the button clicks internally. Use these inside other views (possibly through code only).
hi I want to use a listview (probably) is it possible to fill each entry with say two buttons two text areas, and have them laid out with realtive or linear layouts?
I have used a scroll view with layout inflator to achieve this at the moment, but I'm thinking listview would be better maybe?
yes you can. you need to use a custom list view for that. making an activity by adding a listview in it and then referencing another xml to that listview using an AAdapter so that every element of the listview has the layout of the second xml file. this tutorial should help you understand the idea.
Yes you can place any widgets for the particular List Item for your listview.
For defining that kind of ListView, you have to define a custom adapter, follow the below steps:
Define a one row file for the list item, say for example, RelativeLayout with 2-3 TextViews.
Define a class by extending BaseAdapter.
Inflate the XML and do display operations inside the getView() method of this class.
Set this adapter to the ListView.