I am implementing an app where user can search some songs in a separate tab.The search results for a song are info like lyrics, meaning etc. From the results, user can mark certain songs as favorite which are in another tab. When I click on the favorite songs tab, it should switch to the first tab with fragment for the search results. Here are more specific details:
Main Activity spawns action bar which creates 2 tabs.
Search Tab - Separate Fragment, Results- separate fragment
Favorites Tab - Separate Fragment
public class TabsListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/** Constructor used each time a new tab is created.
* #param activity The host Activity, used to instantiate the fragment
* #param tag The identifier tag for the fragment
* #param clz The fragment's Class, used to instantiate the fragment
*/
public TabsListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
/* The following are each of the ActionBar.TabListener callbacks */
#SuppressLint("NewApi")
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
#SuppressLint("NewApi")
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
//ft.remove(mFragment);
}
}
#SuppressLint("NewApi")
public void onTabReselected(Tab tab, FragmentTransaction ft) {
ft.attach(mFragment);
// User selected the already selected tab. Usually do nothing.
}
}
How can I implement this switching from one tab to another programatically for Action Bar?
Related
I have a Modal class, when this modal is opened, it shows a panel asking the user if the user wants to proceed with the operation. If the user selects Yes the request is sent to the DB, which takes some time, during this time the first panel should be replaced by the second (which displays a spinner). This indeed happens if we do not use the third panel. Although I want to replace the second panel by the third panel in order to inform the user if the operation was successful or not (which depends of the message object,if it is null or have an error message).
So when I use addNewPanel(panel3, target) I never see panel2. I put a thread.sleep(5000) instruction after addNewPanel(panel2, target) and even so, this panel didn't appeared, I only get the initial and panel3 in the end.
If I do not use panel3 I see panel2.
Does anyone have an idea why is this happening?
Below I have the code of the Modal class
public class DetailsModal2 extends Modal<IModel<UserDomain>>{
#SpringBean
private IService service;
private BootstrapAjaxLink<String> noButton;
private ResponseMessage message;
private ProcessingPanel panel2;
private AlertPanel panel3;
private Panel replacedPanel;
public DetailsModal2(String id, IModel<UserDomain> model){
super(id);
replacedPanel = new AreYouSure("replacedPanel");
replacedPanel.setOutputMarkupId(true);
add(replacedPanel);
panel2 = new ProcessingPanel("replacedPanel");
panel3 = new AlertPanel("replacedPanel");
addButton(new BootstrapAjaxLink<String>("button", null, Buttons.Type.Warning, new ResourceModel("details")){
private static final long serialVersionUID = 1L;
#Override
public void onClick(AjaxRequestTarget target) {
// TODO Auto-generated method stub
//I was expecting to see this panel
addNewPanel(panel2,target);
// this puts this button invisible
this.setVisible(false);
target.add(this);
//this changes the label of the No button to Close
noButton.setLabel(Model.of("Close"));
target.add(noButton);
if(!service.retrieveData())
{
message = service.addUser("X");
if(message == null){
panel3.updateClassAndText(true);
addNewPanel(panel3,target);
}
else {
panel3.updateClassAndText(false);
addNewPanel(panel3,target);
System.out.println(""+ message.getError());
}
}//close if
else if(service.retrieveData())
{
message = service.removeUser("X");
if(message == null){
panel3.updateClassAndText(true);
addNewPanel(panel3,target);
}
else{
panel3.updateClassAndText(false);
addNewPanel(panel3,target);
System.out.println(""+ message.getError());
}
}
else{
System.out.println("It was not possible to access the db");
}
}
}
});
noButton = new BootstrapAjaxLink<String>("button", null, Buttons.Type.Primary){
private static final long serialVersionUID = 1L;
#Override
public void onClick(AjaxRequestTarget target) {
close(target);
}
}.setLabel(Model.of("No"));
addButton(noButton);
}
public void addNewPanel(Panel addpanel, AjaxRequestTarget target ){
Panel newPanel = null;
newPanel = addpanel;
newPanel.setOutputMarkupId(true);
replacedPanel.replaceWith(newPanel);
target.add(newPanel);
}
}//close class
HTML
<wicket:extend>
<div><span wicket:id="replacedPanel"> </span></div>
</wicket:extend>
Wicket atmosphere is deprecated from wicket 8 and will not be supported anymore, so do not use it ..
I have fragment activity from which I call say Fragment A then Fragment B from Fragment A and so on. I used backstack properties. So when I click back button from fragment B I goes to fragment A. But from fragment A it does not going to fragment Activity instead it exit from the activity and going to main activity.
Any help
My Fragment Activity
public class ShowAllAccounts extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_all_accounts);
......
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
TableViewFragment mTableViewFragment = new TableViewFragment();
fragmentTransaction.replace(R.id.table_fragment_container, mTableViewFragment,"tabFrag");
fragmentTransaction.commit();
}
}
In my Table view fragment
ViewAccountDetailsFragment AccountViewFragment = new ViewAccountDetailsFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.table_fragment_container, AccountViewFragment, "viewAcctFrag");
fragmentTransaction.addToBackStack(null);
AccountViewFragment.setArguments(dataBundle);
fragmentTransaction.commit();
from here i am calling account view fragment
i am having one more doubt here. From second fragment(only view) i am calling another activity(for edit) and if i want to cancel edit of this activity can i able to go to previous fragment ie. second fragment which was meant to view details.????
please help i am new in android
It looks like your stack is empty do you use Intent Flags?
Try a look here http://developer.android.com/guide/components/tasks-and-back-stack.html
if ya have just one Activity
override the OnBackPress event and do your thing just return or something.
#Override
public void onBackPressed() {
return;
}
Xamarin People:
public override void OnBackPressed()
{
base.OnBackPressed();
return;
}
I have an app which uses or googlemaps or openMaps (offline) depending of connection state.
In each case there are markers, for places or point of interest or… I want that the user can display or hide some category of markers.
When using google maps I have a menu and in the action bar when some item is selected it toggles between showing or hiding the markers from the correpondent category; As for google maps that works easily & perfectly using isVisible();
As for osmdroid i have not found in the doc any equivalent to isVisible(), neither any show() or hide() method. So I have tried to use as a workaround somemarkers.getAlpha() & somemarkers.setAlpha(), toggling between 0 & 1 alpha values.
No error occurs but the visibility of markers remains the same, not toggling, or only randomly when i tap 10 or 20 times on the action icon.
In the log i get "InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed" which seems to me to be the cause.
But what to do to avoid this?
KitKat, SonyXperia Z
In osmdroid, the method to hide/show overlays (markers) is:
Overlay.setEnabled(boolean enabled)
I have done this bit differently.
Extend ItemizedIconOverlay
Add as an overlay to mapView
Hide markers by using removeAllItems or removeItem
Show marker by adding it to the itemized overlay list
Create a new Overlay class by extending ItemizedIconOverlay.
Note: WaypointOverlayItem extends OverlayItem. {It's your custom overlay model class}
public class NavigatorItemizedOverlay extends ItemizedIconOverlay<WaypointOverlayItem> {
private Context mContext;
public NavigatorItemizedOverlay(final Context context, final List<WaypointOverlayItem> aList) {
super(context, aList, new OnItemGestureListener<WaypointOverlayItem>() {
#Override
public boolean onItemSingleTapUp(int index, WaypointOverlayItem item) {
return false;
}
#Override
public boolean onItemLongPress(int index, WaypointOverlayItem item) {
return false;
}
});
// TODO Auto-generated constructor stub
mContext = context;
}
}
Add this Overlay to your map
//Add Itemized overlay
navigatorItemizedOverlay = new NavigatorItemizedOverlay(getActivity(), waypointOverlayItemList);
mapView.getOverlays().add(navigatorItemizedOverlay);
To Add marker:
navigatorItemizedOverlay.addItem(waypointOverlayItem);
To hide all markers:
navigatorItemizedOverlay.removeAllItems();
There are other methods:
removeItem(position) and removeItem(waypointOverlayItem)
I've faced with the well-known problem in LWUIT. My list component with the checkbox renderer scrolls very slow. If to test my application on emulator it runs quite smoothly (nevertheless I see CPU utilization splashes up to 60% during scroll action), but if to run it on mobile phone it takes a couple of seconds between focus movements.
There's a code of renderer:
public class CheckBoxMultiselectRenderer extends CheckBox implements ListCellRenderer {
public CheckBoxMultiselectRenderer() {
super("");
}
//override
public void repaint() {
}
public Component getListCellRendererComponent(List list, Object value,
int index,boolean isSelected) {
Location loc = (Location)value;
setText(loc.getLocationName());
setFocus(isSelected);
setSelected(loc.isSelected());
return this;
}
public Component getListFocusComponent(List list) {
setText("");
setFocus(true);
getStyle().setBgTransparency(Consts.BG_TRANSPARENCY);
return this;
}
}
that's the code of my form containing the list:
protected void createMarkup() {
Form form = getForm();
form.setLayout(new BorderLayout());
form.setScrollable(false);
Label title = new Label("Choose location zone:");
title.getStyle().setMargin(5, 5, 0, 0);
title.getStyle().setBgTransparency(Consts.BG_TRANSPARENCY);
title.setAlignment(Component.CENTER);
form.addComponent(BorderLayout.NORTH, title);
list = new List(StateKeeper.getLocationsAsList());
list.setFixedSelection(List.FIXED_NONE_CYCLIC);
// list.setSmoothScrolling(true);
list.getStyle().setBgTransparency(0);
list.setListCellRenderer(new CheckBoxMultiselectRenderer());
list.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
// List l = (List)ae.getSource();
// l.requestFocus();
// l.setHandlesInput(true);
Location selItem = (Location)list.getSelectedItem();
selItem.setSelected(!selItem.isSelected());
}
});
form.addComponent(BorderLayout.CENTER, list);
}
I would be very thankful for any help!
We must be so carefull building lwuit List. If we have made something wrong they can work worse than expected. I recommend you to take a look on this
LWUIT Blog ListRender
You can also rewrite your paint method. You list's speed will be increased.
I am using ActionBarSherlock to provide ActionBars for pre HoneyComb devices.
My Activity has four fragments namely 1. User 2. Chat 3. Video 4. Extra, see image
I have created actionBar using following code:-
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setTitle("Meeting");
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
/* Set Custom view */
ActionBar.Tab tab = actionBar.newTab();
// tab.setText("Meeting Users");
tab.setIcon(R.drawable.users);
tab.setTabListener(this);
actionBar.addTab(tab);
tab = actionBar.newTab();
// tab.setText("Chat");
tab.setIcon(R.drawable.chat);
tab.setTabListener(this);
actionBar.addTab(tab);
tab = actionBar.newTab();
// tab.setText("Video");
tab.setIcon(R.drawable.video_call);
tab.setTabListener(this);
tab.select();
actionBar.addTab(tab);
tab = actionBar.newTab();
// tab.setText("Extra");
tab.setIcon(R.drawable.extra);
tab.setTabListener(this);
actionBar.addTab(tab);
I want to draw something on those tabs, for example draw and/OR blink on chat tab, whenever chat messages arrives and user is on some other tab.
How can I do this ? please help.
Use custom view for your tabs
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setCustomView(R.layout.custom_tab_view);
Then you can get views on your custom layout and make blinking
This is How I solved my problem, Hope it can be useful for someone else too....
First I created a CutomImageView by extending ImageView
package com.myexample.newsessionwindowsrbrdemo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.renderscript.Font.Style;
import android.widget.ImageView;
public class CustomImageView extends ImageView {
private int notificationCount;
/**
* #param context
*/
public CustomImageView(Context context) {
super(context);
notificationCount = 0;
}
public synchronized void incrementNotification() {
notificationCount--;
this.invalidate();
}
public synchronized void decrementNotification() {
notificationCount++;
this.invalidate();
}
/**
* #return the notificationCount
*/
public synchronized int getNotificationCount() {
return notificationCount;
}
/**
* #param notificationCount
* the notificationCount to set
*/
public synchronized void setNotificationCount(int notificationCount) {
this.notificationCount = notificationCount;
this.invalidate();
}
/*
* (non-Javadoc)
*
* #see android.widget.ImageView#onDraw(android.graphics.Canvas)
*/
#Override
protected void onDraw(Canvas canvas) {
System.out.println("OnDraw is called");
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
paint.setFakeBoldText(true);
paint.setTextSize(15);
canvas.drawText(String.valueOf(notificationCount), 15, 20, paint);
}
}
Then While creating tabs, I used this image as
/* Set Custom view */
mView = new CustomImageView(this);
mView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
mView.setBackgroundResource(R.drawable.users);
ActionBar.Tab tab = actionBar.newTab();
tab.setCustomView(mView);
tab.setTabListener(this);
actionBar.addTab(tab);
Now whenever the notification changes I call increment/decrement or setter method of CustomImageView and new Notifications are displayed on the image..
Suggestions to improve this solution are really welcome...