Intercepting clicks from subview in Android - java

My application has a custom view which contains other custom views of a different type. The subviews have their own click listeners (which I can't change, as these are in 3rd party libraries). How can I intercept a user's click at the level of my view to do some processing, and then pass the click on to the proper subview?

Justin, you can play with dispatchTouchEvent() or onInterceptTouchEvent().

I'm not entirely sure about this, but in Java what I would do is define various subview objects in my main view and simply send those to the draw/paint function. That way I'd only have ONE click listener which would be in the view anyways.
Can you do something like that in Android?

Related

Is there a way to disable clicks on Views behind the ShowcaseView (inside the TargetView)?

Is there a way to disable clicks on views behind the ShowcaseView (inside the TargetView)? I have a very short tutorial and don't want users to be able to click through to elements behind the ShowcaseView because this interrupts its flow. Is there any way to disable clicking inside the TargetViews supplied to the ShowcaseView?
Create a view temporarily under the ShowcaseView but above anything you don't want clicked. Then call view.setClickable(false) this should prevent clicks from affecting that view or anything under it.
When you are done your showcase, just delete the extra view from the hierarchy.
https://developer.android.com/reference/android/view/View.html#setClickable(boolean)

Android - Mutiple views or run-time fragment replacement?

I would like to create an user interface that always displays a menu panel on the left side, and a content block which changes depending on which menu entry is selected, i.e something similar to many websites.
I am new to creating Android user interfaces so I am wondering how to properly and dynamically change the content.
Is it better to create two different views, each of them including the same panel fragment (meaning 2 instances ?) and a different content fragment, and change that view using Activity#setContentView(), or should I create a single view and replace the fragment of the content dynamically ?
You should definitely use the Fragment approach.
Use a FragmentActivity and use the FragmentManager (getFragmentManager in the Activity) to create a transition and replace the Fragment depending on what item the user clicked.
See: http://developer.android.com/training/basics/fragments/fragment-ui.html
Use this below code with library from github it will answar you perfectly
https://github.com/neokree/MaterialNavigationDrawer

Why do i have to cast a Button?

This must be a really dumb question because I cant find an answer online.... I know that casting is changing one datatype to another. How is this button ever changing it's data dype? Button button = (Button)findViewById(R.Bla.Bla) Why cant we just write Button button = New Button() And then assign the xml to it another way? Please explain, I'm lost.
You can set a Button to a new button.
But findViewById returns a view. If you want to access any of its Buttonosity, you must cast, otherwise the reference isn't a button. There are times that may be okay, of course.
See In Android You can create the UI Elements in two ways:
1. create UI elements through layouts (.xml) files.
And to use them in java class map them to their corresponding class.
And to do so we have to call method findViewById(int id); which returns the view of that perticuler element with given id.and thus we have to type cast it to respective component.
And thus if you have created a element already in xml why will you create a different object again at java end. so just map the element created with xml file.
2. crate UI elements through java end.
To use this feature use have to create the elements in java with new keywords ex. Button button = new Button(); and then set the all properties on that object.
But But But,
According to android philosophy you should create UI in xml, and write your core business logic in java end. And with this concept you can write neet and clean application code.
But it is only recommended not compulsory at all. now its up to you....
and i think at starting you feel it different but after some time you will start loving it...
Thats the beauty of android.
Thanks. i hope, i answered your question throughly.
Also, remember that Button is a subclass of View. The findViewById() method returns a generic View (any View or subclass of View that you put in a layout file). The cast to Button is saying "It's okay - I know this is a Button, not just a regular View," which allows you to access properties and methods of the Button that aren't available in the View superclass.
final Button callButton = (Button) findViewById(R.id.callButton);
I believe that when finding an XML view using findViewbyId(), it returns the view in the UI, but the returned view must be cast in order to be used as a button within the Java code, and have access to the button methods.
There are ways to create a button in the Java code without specifying it in the XML, but this practice differentiates the UI from the logic.
Plus, declaring UI elements in the XML is better because it is makes the process changing entire layouts easy through usage of of setContentView().
You have two options to create View component in android including Button
1- Define it in a layout XML file and access it using (Button) findViewById(R.id.button)
2- Create it dynamically in the code e.g. Button button = new Button();
both has their own advantages and disadvantages, for example, defining the UI in layout xml makes your Activity concise and and give you more flexibility by separating the UI from the actual code
Dynamic UI creation is useful in many applications that needs to create Views on-the-fly

How to move between two layouts in java

I am currently developing a java application
since Im a noob
I dont have any idea how to move between two initcomponent view
for example when the user click search than the whole view change to a new view with different classes
than there are some cases that the user press the back button then go to the previous view
and how to pass the parameters between the two view
thx I'll appreciate your reply
probably you have look at CardLayout, then there no needed create a new Window or another separated View

How to implement a Compound View/Presenter for a GWT DecoratedTabPanel?

I'm new to GWT, and I've been reviewing the MVP implementation which uses the rpcService and the eventBus. I was wondering how a tab panel can be implemented such that each tab has its own sub-view. I have been waffling between making a custom widget that derives from a panel, or to figuring out how to make a presenter use another presenter, or to make a compound presenter class which handles it all for me.
Does anyone have advice on how to separate the functionality for each tab as opposed to keeping the implementation within one view/presenter pair?
I was in the same situation, but decided to change my implementation to simulate a TabPanel. If your views and presenters don't need to interact with each other (e.g. dragging something from one tab to another) then I think it'll be easier to separate functionality by loading your View into a shared SimplePanel. You can simulate the tabbed portion of the TabPanel with a widget that listens for PlaceChangeEvents (to change the highlighted tab) and sends goTo commands to the PlaceController your app is using (to handle clicks on the different headers).
It took a couple of hours to implement this, and the resulting code is much cleaner. My initial attempt involved listening for PlaceChangeEvents and then calling the appropriate tabPanel.selectTab() function, but trying to figure out how to start and stop the presenters for the different tabs was too jumbled up - like you suggest, you'd have to implement your own compound view model.
I solved this without faking a main tab, but using the one provided with GWT's basic SDK. I did this by:
Add an ArrayList of Presenters to the MainTabPresenter
Constructed each tab's present plus view within the 'go' method of the MainTabPresenter
Called 'go(null)' on each of the tab presenters.
Handled the null situation on each.
Implemented a method in the MainTabView to add the tabs to the DecoratedTabPanel
It all works like a charm. The MainTabPresenter so very thin, and allows for complete implementations of View/Presenters to be written into their own files.

Categories

Resources