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!!
Related
I have 2 buttons
I want only one of them to be selected at most at a time.
how can i find a view's sibling while in onClick ?
I have tried:
siblingView = v.getParent().findViewById(R.id.rightBtn);
but parentView has no findViewById method.
is it possible to get an ancestor view (even not direct) ? which is view for sure?
getParentView return type is an interface ViewParent
you can test if it is actually a View (or do not test, if you are sure), and downcast it to View.
If I understand this correctly, you need some kind of tabs, not two buttons.
Check this out for more details about how to implement that kind of behavior, using tabs not buttons.
I'm writing a glass app.
In one activity I want to scroll between few cards (which were popups in my android app).
1) I thought to use cardsScrollView.
problem: Is it possible to set customView to a card object?
2) I thought to use LiveCard
problems:
Is it possible to publish them inside my app and not in the timeline?
Is there an equivalent LiveCardsScrollView?
Any other idea how to implement this?
From Google's sample code at https://developers.google.com/glass/develop/gdk/ui/theme-widgets and API documentation at https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/widget/CardScrollView and https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/widget/CardScrollAdapter, it seems your 1) is possible, because:
1) The CardScrollAdapter's method public View getView(int position, View convertView, ViewGroup parent) returns a View (not a Card);
2) CardScrollView's get methods also return a View or Object, not Card specifically;
3) You can replace private List<Card> mCards; in the sample code (link #1 above) with private List<MyView> mViews;
But the documentation at those links also use Card as example, and the word cards seem to refer to static cards. So will have to test to find out for sure. However, there's a statement in link #1 that says "You can build a standard view hierarchy yourself or use the Card class.", suggesting it's possible to use a custom view.
I'll get back with you within 12 hours after I test with my Glass tonight.
As for your question 2, the answer is yes - you publish the scrollable content inside your app and not in the timeline. You can launch the activity (as in the sample code in Google's link #1) from a menu item selection, and the menu is attached to your livecard. Then inside that scrolling view, you can only swipe left and right to see other cards (or maybe custom views) in the scrolling view, but not the timeline. You have to swipe down to exit the activity (immersion) to go back to livecard, then you can swipe left and right and see the timeline. Note the scrolling view is not like static cards and will never show in the timeline. Also note that inside the scrolling view, you may use GestureDetector to capture other gestures (besides swipe left and right and down).
Just confirmed: custom views can be added to CardScrollView! I used a view that extends FrameLayout and inflates a layout xml file, and added three such views to CardScrollView. It works nicely!
Also tried to add a custom view that does the Canvas drawing, but haven't been able to see it shown in the scrolling view. Will try more later.
Just tested and found you can add any views to the CardScrollView - I'm able to add 4 custom views to a scrollview: one static Card, one view with Canvas drawing, one with OpenGL ES 1.0 drawing, and the final one with OpenGL ES 2.0 drawing. This is good to know to me! Thanks for your question.
I have an android activity with a webView.
How can I hide/close this webView?
is this the proper way?
webView.setVisibility(0);
You need to use the setVisibility(int visibility) method that all the various UI components inherit from the View class.
The documentation says that it can take either of the three values from View class:
1. VISIBLE: This view is visible
2. INVISIBLE: This view is invisible, but it still takes up space for layout purposes.
3. GONE: This view is invisible, and it doesn't take any space for layout purposes.
Now, use the method as and when needed with the appropriate constants from the View class.
Since you want to hide, I believe you will need to use webView.setVisibility(View.GONE)
you can hide any View by calling yourView.setVisibility(View.INVISIBLE) or yourView.setVisibility(View.GONE);
Difference: INVISIBLE means your View in not visible anymore, but it still takes its screen space. GONE means the View is completely hidden and doesn't take any space
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.
I have a GridView in a layout. It is populated with Foo views by the activity using a extended BaseAdapter.
When I select an item in this grid it gets orange tinted (thus selected). That's nice. But I want to access this selection from outside the GridView and it's parent activity: from within another View higher in the layout hierarchy. I therefor call upon gridView.getSelectedItem(). However it always returns null.
How could I get this to work?
"Selection" doesn't mean the same thing in AndroidOS as it does in other UIs. In particular, there isn't any "selected item" when you're in touch mode. You probably need to use a click listener instead of relying on there being a "selected item". See this article for details.
You can use the following to get the view:
View childView = gridView.getChildAt(position - gridView.getFirstVisiblePosition());