I have a dynamic UI that I need to generate and I would like to know what the best approach would be to do this and why? The application that I need to make will either way only have a single activity in which to display various different views which are generated by code, so not in an XML file.
So basically I want to draw one set of views and have a user interact with them (textview, button, radio button, edittexts etc). Then save the data he generated, clear the canvas or screen and within the same activity generate the next set of views for the user to interact with.
I have done extensive research and I know that I can use either a Fragment or an Activity with a LinearLayout to achieve this, but I am not sure which would be better and why?
Thanks,
Wihan
Related
I have looked everywhere but don't seem to be able to find what I am looking for. I have an activity with multiple buttons, each button opens a new activity with an identical recyclerview layout, but different data. I am wondering if it is possible to use one activity and layout instead of multiple? this way instead of having 10+ activities (one for each button) I only have to manage one when a button is clicked and simply pass the necessary list data to it.
I believe you could set the intents for each button to call the same activity but with an integer such as 1-10, in the activity it takes the value and decides which list should be presented? If anyone thinks of how this could be done or a simpler way I would greatly appreciate it!
Yes, there are various ways to do that.
You could use multiple fragments on the same activity. Then add/remove fragments on each button click.
You can have multiple layouts within your activity. Say you have two buttons, and you have three layouts layout1, layout2, layout3, sequentially one after the another. So if initially, layout1 is visible and the rest are gone using layout.setVisiblity(View.GONE), if you click button1, ypu can do layout1.setVisiblity(View.GONE); layout3.setVisiblity(View.GONE); layout2.setVisiblity(View.VISIBLE) and vice-versa for pressing button2.
Are all the activities opened by the buttons similar? If so, you could only take care of the changes in the elements of the layout & specify conditions.
For instance, if you click a button, instead of changing the whole thing, you only go into the buttons & change their texts with btn.setText("..."). You could define different conditional statements inside the onClickListener of that button.
It could be something like:
if(btn.getText().equals("a certain text that you set to the button")){
doSomething();
else if(btn.getText().equals("another option")){
doSomethingElse();
Following this logic, you could continually update the elements in your layout & your code will decide what to do depending on what's stored in these elements.
The second option that comes to my mind would be creating different xml layout files & simply changing the layout of your MainActivity to the appropriate one depending on what stage of your process you are at.
I hope this helps,
I have one of those screens where you fill user info, some other EditTexts etc. and send it to server. But there is section something like add note (just example). User can add as many notes as he want. At the beginning there is just button add note. When user press it then new TextView spawns above and he can fill it, or remove it. Then he can touch add note again and whole process repeats. You sure know what I mean. At the end I take all filed notes and send it to server.
I know that the way to do it is - on btn press inflate my TextView + delete btn layout and add it to linear layout. On btnPress then get its parent or whatever and remove it from layout.
However this is pretty annoying thing to do as it requires inflating, children views managing and all the other stuff. What I am asking is - is there some other way to do that? Maybe some library, or possibility to set adapter to linear layout etc.
Thanks :)
Let's say i have the Android XML file home_page.xml.
on this home_page.xml i have some variations that i want to show at different activities, and i'd like to reuse the same main layout home_page.xml.
For example, imagine variations on the page such as:
there's 2 more buttons if the user is in state A
there's 1 more editText field if the user is in state B (same activity as state A)
there's a different arrangement of layout on the Z-axis in a frame layout if the user is in state C (same activity as state A)
i know it's possible to programmatically say hide views and set views as visible. but is there a better way to do this via xml or something?
Android recommends using 2 Tags for re-using the layouts across different screens.
Include
When to Use ?
If you already know the layout that you want to re-use, create a new XML file and define the layout. Use tag to re-use it.
Merge
When to Use ?
To eliminate redundant view groups in your view hierarchy when including one layout within another, we can use tag.
Refer to this link - http://developer.android.com/training/improving-layouts/reusing-layouts.html for code sample and more details.
You can hide views but using the Visibility flag.
View v = findViewById(R.Id.my_view);
v.setVisiblity(View.GONE); //etc.
I've tried stuff like this before. I had mixed results. This is fine if you are doing things, like asking the user for a name, then showing an address input or something. But if you find yourself with like 3 or 4 conditions for one editText and then different ones for a button in the same class you might want to just use different layouts. A lot easier to manage.
I would like to create a game with Marmalade, but I don't know how to make "levels" in it. I mean levels, as on Android the screens, or the activitys.
For example I don't know how to make a menu, then click on a button (I can make the button, control the touch...) and create a fully new screen for a game level.
On Android there are Activitys for this reason, but how can I do it in Marmalade?
You can use marmalade's IwUI api for this. It is used for creating the UI of the game. You can either use .ui files (similar to xml files in android/xib in iOS) or can do it programmatically too.
What you need to do is, create separate CIwUIElement* objects which can represent complete views in your game. These can be called as you main page elements or super parent elements. You can assign childs to those super parents, such as buttons, images etc.
According to button clicks you can add or remove these super parents to the IwUIController and change the views according to that. You can add the elements without removing previous ones, which will result in an overlay.
You might want to create separate class for separate Super Parent element. I've created a base class for UI and subclasses to hold super parent elements. These subclasses works as an activity for me and instead of calling intent, I do add or remove them from IwUIViewController
In my android project I've two layouts in that first layout having one button if i click that button I need to display the second layout in the same layout for this I created a view by using LayoutInflater and attached it to "Table Layout" which is present in first layout.
Everything should be fine but the corresponding class file for the second layout is not loading. Without loading I'm not able to call events like click and some other loader events so any one help me how can I load the corresponding class file when i click button in first layout?
It's difficult to understand what you mean by class not loading.
If you want some layout objects to be hidden at a particular time, look into the visibility parameter in xml or setVisibility(true/false) in code.
If you want to display a whole different screen, create a second activity and call it:
Intent i = new Intent(CallingActivity.this, ActivityToStart.class);
startActivity(i);
Your question is difficult to understand, but you appear to be trying to achieve an effect like embedding one activity inside another. You may want to look at the android developer docs about the "fragments" API, which is the currently-recommended way of achieving this kind of effect.