"Header" Views and buttons: how do I attach listeners to Buttons in a "header" that does not have its own Activity?
I have touched on this question here, where Christopher gave an answer to this, but I don't really understand it, so I thought it's time to make it a real question, not just a "follow up" =)
As it stands, the application I'm writing has 4 different screens:
- Screen 1 - list of nodes (main screen)
- Screen 2 - options menu, tableLayout with buttons
- Screen 3 - navigation
- Screen 4 - text details on version etc
These screens can be navigated to/from using a "header" View that is placed on top. The header then has 4 different buttons:
+--------------------+
| menu with buttons |
+--------------------+
| |
| |
| |
| C O N T E N T |
| |
| |
| |
+--------------------+
The header is just an XML-file (header.xml) with a few buttons. That header.xml is the included in the Layouts using the include-markup. For example, the main.xml has the line:
<include layout="@layout/header"></include>
The header show up alright, but the question is - what is the correct approach to attach OnClickListeners for the buttons in the header?
Christopher pointed out that you could create an Activity class and do the hooks there, like this:
public class BaseActivity extends Activity {
protected View.OnClickListener mButtonListener;
protected void setupHeaderButtons() {
findViewById(R.id.header_btn_1).setOnClickListener(mButtonListener);
// ...
findViewById(R.id.header_btn_n).setOnClickListener(mButtonListener);
}
}
public class FirstActivity extends BaseActivity {
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.first_activity);
// This needs to be done *after* the View has been inflated
setupHeaderButtons();
}
}
First, I can't make it work since the method setupHeaderButtons
isn't accessible from FirstActivity. Secondly, is this the right way to go at it?
Asked by: Sam420 | Posted: 25-01-2022
Answer 1
The setupHeaderButtons()
method is protected
so it can only be accessed by classes that extend
that base class, which is BaseActivity
in this case.
Are you sure that your FirstActivity
is extending BaseActivity
?
Answer 2
I would prefer this so you don't have to remember (and probably forget) to invoke setupHeaderButtons for every derived Activity. BTW, setUpHeaderButtons it's a better name.
public class BaseActivity extends Activity {
protected View.OnClickListener mButtonListener;
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
setupHeaderButtons();
}
protected void setupHeaderButtons() {
findViewById(R.id.header_btn_1).setOnClickListener(mButtonListener);
// ...
findViewById(R.id.header_btn_n).setOnClickListener(mButtonListener);
}
}
public class FirstActivity extends BaseActivity {
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.first_activity);
}
}
Answered by: Sam443 | Posted: 26-02-2022
Answer 3
I personally don't think you should overcomplicate things. Having to call setupHeaderButtons should be fine, especially if you only have a handful of activities.
If you're using standard launch modes, the activity will be relaunched. Check out Application Fundamentals if you're interested in learning about launch modes.
Answered by: Wilson830 | Posted: 26-02-2022Similar questions
android - Dialog Activity - Adding button click listeners
I am trying to create an Activity which can be used like a dialog.
My Activity has a dialog theme, so it has the dialog feel.
In my layout I have added a TextView, to display a message, and three buttons (Positive, Negative, Neutral). The text message which is to be displayed is passed as an extra in the Intent. So are the Button names. I am hiding the button for which an empty string has been passed in the intent-extras. ...
android - Remove all listeners for Activity before changing to another Activity
I use a lot of animation in a current Android app - I hook AnimationListener to them and thus control the flow. I also use some listeners for sounds and so on also for flow control.
When changing to another activity halfways through some of these animations/sounds, some of these listeners cause crashes because the listeners call code after the activity itself has ended/been deallocated. Is there anyway to force all animati...
where is the correct place to put onclick listeners in android Activity?
android - Super Activity class dialog box listeners handling in sub class activity
Hi I have created generic Activity class which have a dialog box(with DatePicker) in it. Extending it allover the application. I would like to handle the listeners of the date selector in sub class activity.
I tried with implementing a interface in the super class and override the same in sub class. and i was struck at overriding the same in sub class.
Now my question is.. .Is this really a bad idea...? Any alterna...
Android: same activity, different onClick listeners
at the moment i have 2 activities which have exactly the same view. However, when you click on an item, the actions performed are different. This is redundant and I would like to use only 1 activity with 2 different onClick behaviours.
Both activities can be reached by clicking on a button in the main menu. How do i distinguish which button was clicked and customise the onClick actions?
A.java:
Android: Add event listeners to every item in a ListView
I have an Android app with a ListView, and each row in the list has a TextView and a Button. What I want to do is add an OnClickListener to each Button in the ListView, but I can't figure out how to get some sort of reference to every Button... Can anyone please give me a hint?
Here's my XML that's bound to the ListAdapter:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android...
Service with listeners in Android
I want to implement an Android Service that periodically downloads data and notifies various listener activities that the data has been updated.
There seems to be various way of communicating with a Service. What's the simplest and best way to do the above? A code example or a tutorial will be greatly appreciated.
How to use listeners for changing data when I click Next Button In Android?
I am writing a questionairre app in android.
Question is populated from database
to textview01 while answers are in
the form of buttons.
Buttons are created dynamically for
each answer based on number of
answers in the database.Suppose there
are 4 answers.4 buttons are created.
I am able to do this successfully,there is an image button as well for next.When I press next ...
android - java events and listeners, multiple events in one class
I am new to android/java and currently am trying to learn custom events and listeners. I currently have this code implemented below that is working successfully. I now would like to add another event to DataRobot. The event will trigger when a certain piece of data is analyzed with the analyzeData function. I was wondering if someone could show me how to implement two events in that one class? I am using this event to trig...
Android location aware app - Set multiple points of interest without using multiple listeners
I'm making an app which should show alerts whenever the user/device gets close to one of the predefined points of interest.
The only way I found for doing that (in fact i got it working like that) is to create a pending intent (with a unique name) for every single point of interest. But I think thats not the best way to do it in terms of resources..
Is it possible to achieve such functionality using just on...
android sim card listeners
HI all
i want to implement a getSimstate() listener in my service class. I m not able to do that.
I know it can be done with the help of sim listeners in Telephony class. but i didnt get any code to implement it.
If anybody is having code to listen sim wither is ready or not...pls provide me...
onclick - Android touch listeners?
I need to nest s few touch listeners. For example I have a ViewGroup that will have the following listeners: onItemClick, onLongItemClick and onTouch.EV == move.
The items inside the view group will have an onClick as well.
In my tests both sets of listeners work independently, but not interdependently. Is there any way I can interlink the listener groups?
Thanks,
~Aedon
java - Do listeners create memory leaks if not removed from a destroyed activity?
If you add a listener to a control/view and do not remove it, will that create a memory leak? For example, onCreate adds a listener to an EditText that listens for modifications. Do you need to remove this listener in the onDestroy?
I imagine that if you use an anonymous listener or a local variable that implements the listener, the memory would be free'd when the Activity is ...
android - how can I set up multiple listeners for one event?
I want to set up multiple listeners for one event, and have found that using composite listener is the key.
Could anyone give me an example?
Is it possible to chain event listeners in Android?
I have an activity which contains QuickContactBadges. I'm looking for a way to either chain event listeners on the QuickContactBadge, or to call the default listener from within an override.
Specifically, what I am looking to do is have the QuickContactBadge, when clicked to show the QuickContact card, and then to setResult and finish, to close my activity.
So either I want to ad...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android