Accessing layout from inside Vaadin 23 view - java

I'm using Vaadin 23.
I defined a "MainLayout" class to carry the drawer among other things.
In my view declaration, i have the annotation :
#Route(value = "", layout = MainLayout.class)
In the view, if I try
this.getParent();
the returned optional is empty.
So, how can I call a method in my MainLayout class from my view instance ?

UI event bus
Instead of creating strong coupling between your views and main layout, you can use the Eventbus with custom events thrown by your view and your layout listening on them.
You can find a official example in the cookbook of Vaadin, Communicate between components attached to one UI.

I'd try with:
UI.getCurrent().getChildren()

Related

Java - Android - view.getContext() meaning

Hello i have just started learning android application development and i am watching a lot of tutorials but none of them really describe step by step so my question is :
i have created a simple app which contains on TextView one EditText and one Button
i have added android:onClick="onButtonClick" to my Button so it will trigger the onButtonClick method , now , i would like it to print out the userinput from EditText so what i did is :
public void onButtonClick(View v){
Toast.makeText(v.getContext(), email.getText().toString(), Toast.LENGTH_SHORT).show();
}
but why the method has to contain the View v ? where is it passed from ? and what does it contain ? it contains the button which i clicked ? and what does the v.getContext() do? why my app does the same when replacing the v.getContext() with this ?
That are many questions at once, but I try to answer them one by one.
but why the method has to contain the View v ? where is it passed from ? and what does it contain ?
Consider the documentation of View.OnClickListener:
View: The view that was clicked.
So you are correct in your assumption that it is the View that has been clicked.
and what does the v.getContext() do?
The first parameter of the Toast#makeText method is a Context. Basically the Context is a container of global information in an Android application. The Toast needs it to retrieve information to show itself.
why my app does the same when replacing the v.getContext() with this ?
I assume your method resides in an Activity. An Activity is a subclass of Context and can be used as a parameter.
If you click a button then View is passed. ViewGroup is a group of View example LinearLayout, Relative Layout, FrameLayout,etc. View is a part of ViewGroup. According to Official Documentation, A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). The ViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.
I hope you understand well about what is View and ViewGroup!!

Create a composite view in Android

I'd like to create a custom view (I'll call it MyComplexView), for example a RelativeLayout with an Imageview, a TextView, and a Button.
I'd like to declare an xml with the layout and then create the class:
MyComplexView extends RelativeLayout{...}
But I don't know what I should override to indicate which layout should be inflated.
How can I do this? Thanks
Something like this:
add the constructors from the super class. (the one with just context is for creating the views programaticaly, the others are for when you add the view in XML.
create a method called init() for example and call it from each constructor.
inside the init method do:
LayoutInflater.from(context).inflate(R.layout.my_view_layout, this, true);
now in inflate the additional params actually mean:
true -> attach the layout to the root in your case relative layout (pro-tip: so inside the xml you can have just merge tags if your layout root is also relative layout and align them in code so the hierarchy is simpler) or any layout you like.
this -> the layout to attach the inflated view to in your case the relative layout you are extending.
it will automatically be attached to the root -> extends RelativeLayout.
then you can use findViewById like:
this.findViewById(R.id.myView);
I'm not 100% sure what your main goal is, so I try to be thorough:
If you want to include a complex layout in other layouts, then you can simply define my_complext_layout.xml, and in your other layouts put:
<include layout="#layout/my_complext_layout" />
If you need to run your own code, then you could simply make the root of this layout to be MyComplexView, and you can run code when the view is created.
If you have intended to let your code operate on the layout, then simply implement an OnGlobalLayoutListener and add it to your layout in your views constructor.
Implement a Constructor for the MyComplexView:
public MyComplexView(Context context, AttributeSet attrs){
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.header_view, this, true);
mHeaderView = (TextView)findViewById(R.id.header);
if(mHeaderView != null)
mHeaderView.setText("Test");
}
See Custom Components in the developer docs. In particular the Compound Controls section.
Once you've made your java file, in order to refer to it in xml you'll have to use a fully qualified packagename i.e:
<com.yourpackage.YourCustomView
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
Creating a custom view usually is aimed to create a widget which doesn't exist yet. What you're trying to do is to have the same layout repeated at multiple places.
You have severall options to do that according to your context.
If the layout is to be placed in a lest, just create your layout in a separate file, and use it in a ListAdapter. Take a look at the ListView Tutorial for this.
If this layout is a generic layout to be embedded in multiple activities, try using a Fragment instead. Fragments are subparts of an activity, with their own views. Alternatively, you can just embed the layout in severall xml using the tag.
If really you want a custom class and single widget, then you need to extend the View class. Extending a layout means you wan't to organize child widgets differently (for example, organize them in circle). Extending a View, you can have exactly what you want (button, image, text) organized always in the same way. But I won't lie to you, this will mean lot of work.

android ViewFlipper - slideshow of images with async image loading

I'm implementing a custom view where I use ViewFlipper which shows my custom View that consists of RelativeLayout parent and two children - ImageView and ProgressBar.
Initially I display thumbnail images in the ViewFlipper and when the particular child View is shown, I want to inintiate the full size image download for display. The problem is that in ViewFlipper I need to add all child View at the beginning (in onCreate of my Acticity). There are situations where my gallery consists of > 200 images and I do not want to initiate download of all the fullsize images because user may not navigate to all pages of the ViewFlipper at all. Is there a way to be notified in ViewFlipper that a particular view gets activated? I can hardly see it in this class.
Regards
It seems that there is no callback to inform you that a view gets activated; you need to implement this yourself. It's not hard: you need to subclass ViewFlipper and create a custom listener ViewFlipperListener with a method public void onViewFlip(ViewFlipper view, View newView).
Then override the ShowNext() and ShowPrevious() methods inherited from ViewAnimatior. In these methods, call their super counterparts and then call the listener's onViewFlip callback passing in the new view (which you can get with getCurrentView()).
Hope this helps.

Best practise for displaying multiple controls which require an extended class?

If I want to display a MapView inside my Activity I would have to extend my Class from MapActivity. If I want to display a Tabbed interface I would have to extend my Class from TabActivity. Similar is the case with some other controls which require user class to extend from a specific class.
Let's say inside my Activity I want to display both a MapView for displaying Google Map and a TabView to display some other data. I can't do it directly because Java doesn't support multiple inheritance. My question is how can I achieve this scenario? How can I display multiple controls inside my activity where each require your class to extend from a specific class. Is it possible first of all? If yes, what are the best practises to achieve this scenario?
Update I want to achieve this
I am using Map and Tab for sake of an example. I would like to know how you can tackle this scenario in general?
In this case it's simple: You can use the MapActivity within the TabActivity (it's designed to manage activities as tabs).
As general approach I always prefer to use views and nest them in an activity. I never use such things like ListActivity. They should make things easier but often look like a bad design decision to me. I never faced the fact that I had to combine two activities (expect TabActivity).
You can take a look at this question. It seems that activities never meant to be used that way. I think the situation which you describe is the reason why fragments where introduced.
You could build it via object composition. Initially I am not sure how to get the Activity started and add it to the layout, but then I found out about LocalActivityManager which allow you to embed other Activity as your view. Note that this class is deprecated since API Level 11. In any case here are the steps to embed other Activity that require extension as a View:
Create a LocalActivityManager to enable creation of Activity within Activity
Start the activity that you want to embed and get the View via getDecorView()
Add the View in your layout
The following is my test code that I tried within my Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create local activity manager so that I could start my activity
LocalActivityManager localActivityManager = new LocalActivityManager(this, true);
// dispatch the onCreate from this manager
localActivityManager.dispatchCreate(savedInstanceState);
// layout to hold the activity, optionally this could be set through XML file
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
this.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
// start the activity which is in this example is an extension of a TabActivity
Intent tabIntent = new Intent(this, DummyTabActivity.class);
Window tabWindow = localActivityManager.startActivity("tabView", tabIntent);
View tabView = tabWindow.getDecorView();
// start the activity that extends MapActivity
Intent mapIntent = new Intent(this, DummyMapView.class);
Window mapViewWindow = localActivityManager.startActivity("mapView", mapIntent);
View mapView = mapViewWindow.getDecorView();
// dispatch resume to the Activities
localActivityManager.dispatchResume();
// add to the tabView, optionally you could use other layout as well
layout.addView(tabView);
// add to the mapView, optionally you could use other layout as well
layout.addView(mapView);
}
My limited experiments show that object composition via the above method will achieve what you are trying to do. Having said that, I am not sure how common this approach is. Without your question, I wouldn't probably look for the above method, but your use case is interesting and might be applicable for future use. I will look into Fragment and see if I could do the same with it and update my answer if it is applicable.

Creating xml template for feed/list items in Android?

I'm helping a friend create an android app that will have screens with lists of info similar to a feed. I've been learning xml layout in Android and have some of the basics down, but don't have a lot of familiarity with doing the java stuff. I've successfully created includes to seperate layout files for compontents within a screen, but what I'm wondering is if such a component can be used as a kind of template for feed/list items that get inserted programmatically on the back end. IE, is there a way to have Android create a list and for each list item it uses the external xml as a template? Sorry if this is somewhat vague, I'm new to this and trying to understand what our options are. TIA!
Yes, every list item can be a custom layout. In fact you always have to define a layout for the list entries. You can either choose a prebuilt one from android.R.layout or you can use your own from R.layout. You can specify it when you create the list adapter in code.
Have a look at one of the ArrayAdapter constructors for example:
public ArrayAdapter (Context context, int resource, int
textViewResourceId)
Since: API Level 1 Constructor Parameters
context - the current context.
resource - The resource ID for a layout file containing a
layout to use when instantiating views.
textViewResourceId - The id of the TextView within the layout resource to be populated
The constructor takes a layout that will be used for the ListView childs. Works similar with other adapters.
What you usually do is inflating the layout inside getView() of the adapter though. When you did that, fill all the data you need into the views of the layout, and return the view.
Note that you get an argument called convertView. This is one of the older layouts you already inflated before. In most cases the user just scrolled down and that entry is not visible anymore. If this convertView is not null, you can fill your data in there instead of inflating the whole layout again (thats expensive).
You can find a working example inside the
ANDROID_SDK\samples\android-10\ApiDemos\src\com\example\android\apis\view\List5.java file. Also take a look at the other list examples in that folder.

Categories