Inside a tab fragment
public class ClassViewFragment extends Fragment {
Is there a function in which I can catch the event of tab selection?
I know I can catch this inside main activity class in onTabSelected but I want to know if such thing possible inside the fragment class?
Are you using a TabHost? It's tough to say from your snippet, but if you are you can just implement TabHost.OnTabChangeListener and put your code in onTabChanged.
Related
The UI elements in the MainActivity class need to be preserved and the other class file adds a new UI element to the main xml layout that's used by both classes to differing degrees.
It's understood that you can create a base abstract class and two concrete inherited classes however in this case there a third supporting class for the second (other class) and it requires a handler to function.
For perspective, a button (in activity_main) is clicked and it should launch an activity while maintaining the UI elements used MainActivity. Furthermore the button has it's own class file methods and isn't in MainActivity.
What happens now? The button is pressed and nothing happens. Manifest confirmed so its not that. Or I allow the main activity or the other activity and it works, both need to work simultaneously.
Basically MainActivity needs to act as the base abstract activity for the separate class file.
You are messing up activities and views. To reuse the same business logic, you can write common logic in base class for all other activities (i.e. class BaseActivity extends Activity). To reuse different UI parts you should either use fragments or you can use <include>/<merge> tags pair to include a certain layout into another layout.
How would I do this? Or is it even possible get a button within a fragment from the activity?
I've tried many things e.g. trying to get the view or container of the fragment but no luck.
I am doing this as i have 27 fragments in an activity which all have a same button. Instead of doing a button listener on each fragment, i want to reduce my code greatly but just doing a loop in my activity going through each fragment and setting there button to activate the button listener (in the activity) if pressed.
Once i know how to get a button and set it to the button listener, I can easily do the loop bit my self.
Just finishing my first year for computer science and have been coding in java for a couple of years now.
Thanks in advance!
Don't loop or get reference to the button.
Simply make your activity implement OnClickListener and set it as click listeners for the buttons in all your fragments ..
In the fragment
yourBtn.setOnClickListener(((YourActivityType) getActivity()));
another solution without the implements keyword
is to have a public method in your activity, for example, setButtonAction(Button btn)
public void setButtonAction(Button btn) {
btn.setOnClickListener(myClickListener);
}
and declare myClickListener as variable in youractivity
Same idea without having your activity to implement clickLitener
I am trying to put all my ContextMenu logic in a separate class but it seems like I am not able to recognize in this class whether someone selects a item.
I have an application with a main activity. Next to some other things this activity contains a listview. This listview should contain a context menu, so I defined it corresponding to its Clicklistener:
MyListener myListener = new MyListener();
listview.setOnItemClickListener(myListener);
listview.setOnCreateContextMenuListener(myListener);
MyListener implements both OnItemClickListener and OnCreateContextMenuListener. I did this to keep the class readable (like mentioned before there are already some other UI components and some logic). To this point everything works like a charm. Single clicks are recognized and also the ConextMenu is shown.
Now I also want that MyListener also reacts to the item that is selected inside the ContextMenu. Unfortunately only Activities and their corresponding SubClasses seem to provide a method like onContextItemSelected(menuItem item). So I would have to put that logic into my main activity and distribute my ContextMenu logic by doing this (I also tested this, it works, but distributing the logic seems to me like a no-go).
Do I miss here something? Is there a way to define some kind of a ContextMenu ClickListener for my listview in another way than putting it in my main activity? Or am I doing some bad practise without recognizing?
Looking forward to your opinions!
Cheers Eyeless
A quick and easy solution is to forward the clicks to your MyListener class.
Create a new method in your MyListener class. Ideally I would call it just like the original method:
public boolean onContextItemSelected(MenuItem item)
In this method you implement your logic.
Then make your MyListener variable a field of your Activity.
Now, just override onContextItemSelected(MenuItem item) in your Activity and forward the clicks to your listener class:
#Override
public boolean onContextItemSelected( MenuItem item ) {
return myListener.onContextItemSelected( item );
}
What I've been trying to do is create a menu that stays "static" no matter what activity or layout is being showed. Is there any way to do this, or anything similar to this??
Thanks!
Yes,
1- Create a custom Activity (extend it from Activity)
2- Write the code for menu creation in that custom Activty.
3- Now extend all your Activities from this custom Activity.
I am trying to implement an options menu for my app and the same menu is used in different activities. In the Android developers site, it says the following:
Tip: If your application contains multiple activities and some of them
provide the same options menu, consider creating an activity that
implements nothing except the onCreateOptionsMenu() and
onOptionsItemSelected() methods. Then extend this class for each
activity that should share the same options menu. This way, you can
manage one set of code for handling menu actions and each descendant
class inherits the menu behaviors. If you want to add menu items to
one of the descendant activities, override onCreateOptionsMenu() in
that activity. Call super.onCreateOptionsMenu(menu) so the original
menu items are created, then add new menu items with menu.add(). You
can also override the super class's behavior for individual menu
items.
My activities extend from Activity, ListActivity or MapActivity, so what would be the correct way to implement what they are suggesting here? is it possible? Because I cannot extend this new class for all of these, I could only do something like public abstract BaseMenu extends Activity (as explained in this question) but this doesn't work for me. So I am wondering if there is a work around I can implement.
Thanks in advance
For that common Base menu class you need to extend MapActivity class . So you can extend that common base menu class for you all activities.
For that ListActivity you can also implement the list without ListActivity, you can implement it by only Activity or MapActivity.
you have to declare you listview in xml file with the id like below.
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Then you have to declare it in your activity class .
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(your adapter);
Like above you can implement it without extend ListActivity.
you cannot use mapview without extending mapactivity class.... refer this..
MapView without MapActivity ..so you should let your base class extend map activity... and for activity using listview.. put listview in xml and you can use it in your activity..
I would create a static MenuProvider class that implements your onCreateOptionsMenu() and onOptionItemsSelected() methods statically to be called from onCreateOptionsMenu() and onOptionsItemsSelected() of the activities, like so:
public static class MenuProvider {
// You can pass it the activity and other variables used by this method if
// you need to.
// Since the implementation is the same across all activities, they should
// pass the same variables.
public static void onCreateOptionsMenu(MenuItem item, Activity callingActivity, ...)
... // Do stuff on create
}
public static void onOptionItemsSelected(MenuItem item, Activity callingActivity, ...)
... // Do stuff on item select
}
}
And in each of your activities, you would do:
public class MyMapActivity extends MapActivity {
...
public void onCreateOptionsMenu(MenuItem item)
MenuProvider.onCreateOptionsMenu(item, this, ... /*other variables */);
}
public static void onOptionItemsSelected(MenuItem item)
MenuProvider.onOptionItemsSelected(item, this, ... /*other variables */);
}
...
}