Loading UI elements on user command - java

Right now I have a specific group of UI elements(Spinners, edit-boxes, buttons, etc.) which I want to be in the program's UI multiple times. Specifically one copy when the program first loads up and new copies are 'created' and show up in the UI due to user interaction(such as a button press event). I'm aware of using XML to define a specific UI groups but I'm not sure how to properly set up having those groups appear in the UI at the start of the program and by user command within the Java code.

This should be a good idea implements it group of components as a View subclass, this way you view going to comport as a stand-alone component and ready to re-use.
You can still make the layout.xml to it view, inflate it into the onCreate and assign the behaviour of actions in the components of this group.

Related

ViewChangeEvent not triggered in a view that is contained in a TabSheet

In my Vaadin 8 application, I have a TabSheet, that contains several views. When I override the enter method in the first view of my TabSheet, and then I proceed to enter the view on my application, the method doesn't get called.
Do you have a Navigator, and have you registered the views for it? Navigator's navigateTo is what calls the enter. Registering views happens along these lines:
navigator.addView("", new InitialView());
navigator.addView("second", new SecondView());
Navigation also updates the URI fragment in your browser, and makes it possible to bookmark specific views and enter them via direct URL. See e.g. https://vaadin.com/docs/v8/framework/advanced/advanced-navigator for more information about Navigator.
TabSheet isn't the most trivial thing to use with Navigator and I'm afraid I don't have a ready-made example at hand, but I think it should be doable with a custom ViewDisplay and maybe SelectedTabChangeListener.
If you aren't interested in the Navigator approach, I suppose you could replace the View+enter with something along these lines, although if you need to know the previously selected tab you'll need to keep track of it yourself since this particular event isn't very informative:
tabSheet.addSelectedTabChangeListener(e -> {
((MyClass) tabSheet.getSelectedTab()).myMethod());
});

Android keep button or view in front of all activities within the app without using baseActivity

After a lot of time spent I can't find the solution. I want a button or any clickable view should stay visible for all activities and it should be only for one specific app not like chat heads. I am basically making a library so that's why I can't use base activity.I have attached the image as well for a better explanation. How can I achieve this any suggestions? thankyou...
I
It sounds like you need a ViewOverlay. This is the API documentation.
ViewOverlay is usually tied to a single view, but if you wrap it in a fragment, you should be able to attach it to each view in your application. This should create the effect of an of an application scoped overlay.
There may be a more elegant way of doing this, but I am not aware of it.
EDIT: You can also wrap your layouts inside a frame layout(s) along with a seperate nested view (the view that you want to keep on top of the stack).
Frame layout creates a 'stack' of inner views. If you use this approach, you can programmatically ensure that there are exactly two views present and visible as children of your frame layout at all times. one will be the layout tied to your current activity. The other will be the view that you want to be overlayed.
I know that the term 'programmatically ensure' is vague. This is because there are many ways to make this happen. It is up to you to decide which way best suits your needs.

Can you determine which child view was touched when handling the event in the ViewGroup?

Consider this hypothetical hierarchy...
LinearLayoutA <-- I want to handle the touches here...
|
+-SomeViewX
+-SomeOtherViewY
+-LinearLayoutB
|
+-CustomView1 <-- for these three CustomView objects
+-CustomView2
+-CustomView3
What I would like to do is know which (grand)child view was touched and handle it from within LinearLayoutA. Also, I don't control those views so I can't simply make them handle the touch internally and delegate to their parent(s).
Currently I'm manually adding touch listeners to CustomViews 1-3 but that requires a lot of 'boilerplate' work and also means I won't get notified if someone clicks on SomeViewX or SomeOtherViewY, only the specific ones I've attached the listener to.
Now in other languages such as C# with WPF, if you handle the event at the equivalent of LinearLayoutA, part of the event payload is a source, which is the view that initiated the touch, but I'm not aware of any such thing in Android.
All the examples I've seen require looping through the children and hit-testing them, then disambiguating by z-order if there's an overlap, and when you've identified the one child, then you have to go through its children and repeat.
So, is there a built-in, or 'Androidy' way to know which child in a ViewGroup was touched without manually iterating and hit-testing, or manually attaching listeners to all its children?
The OnTouchListener for ViewGroups passes in the ViewGroup as its View argument and not the child within that View that was actually touched. Since you don't have control over the source of the Views in the layout, you'll have to add the OnTouchListener manually from outside of them.
As mentioned in my comments, you can reuse the same listener for all the Views by attaching it to each of the Views you want to listen on. If you're adding these Views dynamically, it should be trivial to also call setOnTouchListener() on them as you create them.
For more on the subject, check out Android's guide to managing touch events in a ViewGroup which provides a way for the parent to intercept touch events on the child, but not vice versa.

Changing Views in Java using MVC model

So I have read a lot about MVC online and have learned about it in class, but I am still lost on one aspect - changing and showing Views. I know Views are GUI, they pass user input to the Controller, but I'm having a hard time wrapping my mind around how the following would work:
View A displayed
user clicks button on View A
Controller notified, tells Model
Model tells Controller to display View B
Controller displays View B?!?
The last 2 lines here is what I don't understand how to implement. If the View did not change to another View, I know to use the Observer/Observable interface to update the View. But in my case there is a Home Screen and a Game Screen and when the user clicks Play button on the Home Screen, I want the "view" and the GUI to change to the GameScreen. I want to use 2 distinct Views (I think).
I'm having trouble structuring my code to achieve this, and I don't know where to put the ActionEventListeners
Assuming you're just switching the view, this is the sequence.
View A displayed
User clicks button on View A
Button controller tells view to display View B
View displays View B
The model is not involved at all. Other controllers can change the model.
When coding a Java Swing application, here's what I do.
The view may read values from the model.
The view may not update the model.
The controller(s) will update the model.
The controller(s) may revalidate / repaint the view.
To see an example of the model / view / controller pattern in a realistic Swing application, take a look at my article, Retro Snake Game.

Adding views in layout from the app in Android

I want to develop an Android app, where start page of the app GUI, will contain 4 vertical layouts in the main layout. Now, in each layout, I want to add buttons/slider dynamically from the app (instead of adding buttons/slider dynamically in the source code). That means, initially all these 4 layouts will be blank and when user will select any button or slider in another layout, to add it in any of this 4 layouts, the button or slider will be added in that layout. User will be able to add max 10 views in any vertical layout and the views can be either button, slider or custom view.
My attempt:
First I tried to create 4 vertical layout under the main layout for startup page and I got succeed.
I also find after searching that its possible to add views dynamically in layouts in android.
dynamically adding a view to activity layout
But most examples, add views dynamically in android by running loops, instantiating the desired view class and then add it in the main layout. Although, in this way, views are added dynamically in the layout, it is done by modifying the source code.
Is it possible to write the source code in a way, so that it can be done directly from the app? So that when user will click on Add a slider in "layout 1", a slider will be added in layout 1 and then again, when the user will click on "Add a button" in layout 1, a button will be added at the end of the slider. User will be able to customize button or slider properties. Also, if the user change the value of the slider, the app will remember its value.
Now, next time, when the app will be opened, those views will be there in the layouts, they will not be deleted and the values will remain unchanged (for example, a ticked check box will remain ticked), so I think I also need some kind of storage or properties manager.
My question is, is it possible to do this in android (because I never seen such apps in android) and if possible, any idea, how can I implement it?
I am totally new to android, so my knowledge is limited but I completed the basic tutorials on android app development and I have plugin development experience in eclipse.
Thanks a lot in. I will highly appreciate your help.
Of course it is possible:
Every layout (like LinearLayout, RelativeLayout etc.) extends the ViewGroup-class, which offers the addView-method.
To add a new view (like a Slider) to one of your layouts, just instantiate it programmatically (via new) in your activity and assign the appropriate LayoutParams to it
To store the state of user added content, it is the easiest to use SharedPreferences - a simple key-value-store which holds data over the application's lifecycle
Yes. This is possible. To create the Views dynamically, you simply have to either extend the class of the View or just say new Button(Context, AttributeSet); (Not only for Button's every View has a constructor that takes an attribute set and a context).
Using Layout.addView() you can add any View to the Layout.
Using SharedPreferences you can indicate what View belongs in what Layout.
If you decide to extend the View's class, make sure not to do too much in it. I tried that once and it just gave me an OOM (OutOfMemory Error) because I had a ton a Views trying to do stuff at the same time.

Categories