Appropriate use of events - android

I've got an Activity which displays a list of custom components (in a LinearLayout). Each component represents a user in the database, and contains a button (among other things). When I click the button in the custom component, I want to pass a message back to the Activity to save the the user. The button knows the id of the user.
I was wondering how best to communicate back to the activity? In the click event of the button, should dispatch a new event (and catch it in the activity)? If so, can I add the user ID into the event? I'm new to events but I think I need to create my own custom event type perhaps?
The other way I thought of was to pass a reference for the activity into the component, so that the button can just call the method on the activity e.g 'component.parentActivity=this' but although I think it would work, I'm not sure if it's the 'proper' way to do it.
Thanks

You can implement the View.OnClickListener interface. The onClick method you'll need to implement will have the View (the thing that was clicked) passed to it. So you should have all the details about what was clicked and thus handle it the way you want from there.
What I'm not clear on in your question is what you mean by 'component'? Is that a custom View? Or a layout of Views? Or other?

Related

how to detect activity button click in fragment?

Only thing that i want is to detect button click in fragments .
There are many ways to interact between fragments and activities, and you can find them by searching here on SO, but the easiest way I've found so far and the one I love using the most is a 3rd party library, called EventBus.
You can simply post an event on button click, custom event which can carry all kinds of data, and then in your fragment, simply write a method that listens for that event and does whatever is necessary.
If your button is interacting with your fragment, it should not be separated.
However, you can do:
getActivity().findViewById(...)
inside your fragment

Whether it is possible to use two activities with same layout

Whether it is possible one layout can be called by two activities.
Say i have a textview and a button.
1) Using one activity i set the textView with a name
2) Using another activity, is it possible to display popup message when i click the button.
first activity should set the textview alone and second activity should be called when i click the button. Because, i will get the text from api webservice call in first activity and will display message in popup window using another activity using some other api webservice call.
yes you can use one layout for two activities,but in that case where you use same controls for both activites:
like activity one:
uses both one textView and Button
like activity two:
uses both one textView and Button
Yes. Two activities don't care if they are using the same layout, and in fact don't really know about it. You can use view.setVisibility to make certain views invisible or visible, and as long as you are using the ids associated with the views can make different calls on them in the activities.
So the answer is yes, you can manipulate that layout however you like in two different activities.
No problem, you can easily use same layout file for more then one activity.
First try to implement this thing in your project, and then see, what happening.
Thanks,
Sumeet.

best practice to register click listeners to widgets

To register a click listener for a button in android you can
1) instantiate the button and create an in-line event listener
2) implement the click listener interface on the activity and provide the method in the activity
3) Define the click method in the xml layout of the activity and define the method in the activity
i was wondering is there a best approach, i like to implement the interface. Is it bad to name the method in the xml as then you would need to also include it in any other version of the layout ie landscape
second approach is rather simple and make the code a lot cleaner but most of the time you may need to have mixed approach of 1st and 2nd.

passing objects to another activity

Situation
In my alarm clock app I start a NewAlarmActivity by clicking a button.
After the user has made all the selections, alarm gets set and `finish() is called.
The matter
I create a new LinearLayout object with images and text within it. That layout must be added to the screen of previous activity (to another LinearLayout placed inside a ScrollView) so the user is able to see the alarm set. Somehow I have to pass that LinearLayout object to the first activity and tell it to receive and add the object to the screen.
How can I do that?
You just need to read some documentation about startActivityForResult().
BTW, IMHO, you should not transport your LinearLayout object between activities, that's ugly.
How to manage `startActivityForResult` on Android?
Starting Activity And Getting Result
This has been asked countless times...
No You don't have to do this. Try the approach of storing your data into a database and then when going into that Activity, build your layout according to the data you have. This is a much better way than passing a layout from one Activity to antoher.

Android: Firing intents from a view

I have an activity with a custom view which extends EditText in it. Usually when someone longclicks an EditView a menu pops up asking the user to choose input method.
I have overridden the onLongClick to make this menu NOT appear, since
I use my own methods of input. However, i want a ListActivity to start when user longclicks the EditText (or rather again my view which extends EditText). But it seems a view cannot fire intents, only activities, am i right?
Fine, so i try to capture the longclick from the activity that spawned my EditText-like view. But inside my EditText-like view i already consume the longclick, either in the overridden onLongClick() by setting it true to avoid the "Please choose input method" menu to appear, or if returning false, then that very menu appears and consumes my longclick. In neither of the cases the intent fires...
In short:
A user longclicks an EditText, and i want a custom ListActivity to start. How do i accomplish this?
EDIT:
So i found startActivity in the Context object. But i will have the user pick an item from a list. That item would then appear in the EditText, so i thought i had to use startActivityForResult, which incidentally does not appear in the Context object :(
EDIT 2:
How do i get a result back from an activity started with startActivity?
You don't need an Activity to start another Activity, but a Context object.
You got two options here. You can either pass an context into you custom view by using a custom constructor or a setter method. Or the OnLongClickListener has to be implemented in the Activity and set to the custom EditText from there.

Categories

Resources