How to draw on ActionBar tab when using ActionBarSherlock in Android? - android-actionbar

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...

Related

How to trigger red squigglies with UltraSpellChecker without user intervention?

The Infragistics docs say to set Mode to AsYouType and when the user types a space the spell checker will create the squigglies.
I also see there is an AsYouTypeManager class but don't know where or how to use it either. The documentation online is not that great.
When a box is loaded with the data and it has errors, I would like to have the red squigglies appear without any user interaction. How can I do this?
The following example shows how the UltraSpellChecker must be initialized to have the red squiggles appear without any user interaction.
Form1.cs
private void Form1_Load(object sender, EventArgs e)
{
// The extender property must be set for the rich text box so that it may be spell checked.
// This can also be done through the property grid in the forms designer
this.ultraSpellChecker1.SetSpellCheckerSettings(this.rtbSpellChecked, new SpellCheckerSettings(true));
}
Form1.Designer.cs
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.rtbSpellChecked = new System.Windows.Forms.RichTextBox();
this.ultraSpellChecker1 = new Infragistics.Win.UltraWinSpellChecker.UltraSpellChecker(this.components);
((System.ComponentModel.ISupportInitialize)(this.ultraSpellChecker1)).BeginInit();
this.SuspendLayout();
//
// rtbSpellChecked
//
this.rtbSpellChecked.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtbSpellChecked.Location = new System.Drawing.Point(0, 0);
this.rtbSpellChecked.Name = "rtbSpellChecked";
this.rtbSpellChecked.Size = new System.Drawing.Size(411, 266);
this.rtbSpellChecked.TabIndex = 5;
this.rtbSpellChecked.Text = "UltraSpellChecker Class\nPerforms spel cheking on one or more conrols.";
//
// ultraSpellChecker1
//
this.ultraSpellChecker1.ContainingControl = this;
this.ultraSpellChecker1.Mode = Infragistics.Win.UltraWinSpellChecker.SpellCheckingMode.AsYouType;
this.ultraSpellChecker1.ShowDialogsModal = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(411, 266);
this.Controls.Add(this.rtbSpellChecked);
this.Name = "Form1";
this.Text = "UltraSpellChecker";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.ultraSpellChecker1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private Infragistics.Win.UltraWinSpellChecker.UltraSpellChecker ultraSpellChecker1;
private System.Windows.Forms.RichTextBox rtbSpellChecked;
The this.ultraSpellChecker1.Mode is set to SpellCheckingMode.AsYouType as described in the Infragistics documentation and the code above shows the following form:

JavaFX - using toggle isSelected() in conditional statements

I'm building a simple sketch program using JavaFX. I want the user to be able to switch between drawing a rectangle, circle or line and I've put toggle radio buttons in the menu for these options.
Is it possible to write an if/else statement so I can write code for three different functions depending on which is selected? At the moment it will only draw lines. This is part of my code so far (sorry it's messy):
package Sketchbook;
public class Sketchbook extends Application {
final static int CANVAS_WIDTH = 800;
final static int CANVAS_HEIGHT = 600;
ColorPicker colorPicker1;
ColorPicker colorPicker2;
#Override
public void start(final Stage primaryStage) {
final Canvas canvas = new Canvas(CANVAS_WIDTH, CANVAS_HEIGHT);
final GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
initDraw(graphicsContext);
canvas.addEventHandler(MouseEvent.MOUSE_PRESSED,
new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent event) {
graphicsContext.beginPath();
graphicsContext.moveTo(event.getX(), event.getY());
graphicsContext.setStroke(colorPicker1.getValue());
graphicsContext.stroke();
}
});
canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED,
new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent event) {
graphicsContext.lineTo(event.getX(), event.getY());
graphicsContext.setStroke(colorPicker1.getValue());
graphicsContext.stroke();
}
});
canvas.addEventHandler(MouseEvent.MOUSE_RELEASED,
new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent event) {
}
});
Group root = new Group();
ToggleGroup toggleGroup = new ToggleGroup();
RadioButton rectangle = new RadioButton("Rectangle");
RadioButton circle = new RadioButton("Circle");
RadioButton line = new RadioButton("Line");
rectangle.setSelected(true);
rectangle.setToggleGroup(toggleGroup);
circle.setToggleGroup(toggleGroup);
line.setToggleGroup(toggleGroup);
You've pretty much already described what you need to do:
canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED,
new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent event) {
if (toggleGroup.getSelectedToggle() == line) {
graphicsContext.lineTo(event.getX(), event.getY());
graphicsContext.setStroke(colorPicker1.getValue());
graphicsContext.stroke();
} else if (toggleGroup.getSelectedToggle() == rectangle) {
// etc...
} // etc...
}
}
});
Obviously you may need to reorder the code a little to make sure variables are declared and initialized before you use them.

Hiding / showing markers: OSMDroid / OpenMaps

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)

Built-in ScrollView that scrolls with head motion

Speaking "ok glass" brings up a command list that automatically scrolls based on the user's head motion.
Is there a built-in UI element in the GDK that implements this? Or will I have to write my own code that uses sensors?
I tried reimplementing parts of this. It's not as shiny as the google one, but those could serve as a starting point:
https://github.com/pscholl/glass_snippets/blob/master/lib/src/main/java/de/tud/ess/HeadListView.java
https://github.com/pscholl/glass_snippets/blob/master/lib/src/main/java/de/tud/ess/HeadScrollView.java
I went through the GDK's Developer Guides at https://developers.google.com/glass/develop/gdk/dev-guides and Reference at https://developers.google.com/glass/develop/gdk/reference/index and there's definitely no such built-in UI elements in GDK, as of XE 12 released in December 2013.
So the answer for now is yes you have to use sensors to implement that.
There is currently no native GDK UI element for scrolling a list using sensors (in fact, according to this issue, use of ListView at all appears to be discouraged).
However, I was able to get the following to work reasonably well in my app. My list is fixed at 4 elements (which helps determine how much scrolling happens), so you can tweak this accordingly (see comments).
import com.google.android.glass.media.Sounds;
import com.google.android.glass.touchpad.Gesture;
import com.google.android.glass.touchpad.GestureDetector;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.view.MotionEvent;
import android.widget.ListView;
/**
* Implements sensor-based scrolling of a ListView
*/
public class SensorListController implements SensorEventListener, GestureDetector.BaseListener {
static final String TAG = "SensorListController";
Context mContext;
ListView mList;
SensorManager mSensorManager;
private float[] mRotationMatrix = new float[16];
private float[] mOrientation = new float[9];
private float[] history = new float[2];
private float mHeading;
private float mPitch;
boolean mActive = true;
GestureDetector mGestureDetector;
public SensorListController(Context context, ListView list) {
this.mContext = context;
this.mList = list;
history[0] = 10;
history[1] = 10;
mGestureDetector = new GestureDetector(mContext);
mGestureDetector.setBaseListener(this);
}
/**
* Receive pass-through of event from View
*/
public boolean onMotionEvent(MotionEvent event) {
return mGestureDetector.onMotionEvent(event);
}
#Override
public boolean onGesture(Gesture gesture) {
switch (gesture) {
case TWO_LONG_PRESS:
// Toggle on and off accelerometer control of the list by long press
playSuccessSound();
toggleActive();
return true;
case TWO_TAP:
// Go to top of the list
playSuccessSound();
scrollToTop();
return true;
}
return false;
}
/**
* Should be called from the onResume() of Activity
*/
public void onResume() {
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),
SensorManager.SENSOR_DELAY_UI);
}
/**
* Should be called from the onPause() of Activity
*/
public void onPause() {
mSensorManager.unregisterListener(this);
}
/**
* Toggles whether the controller modifies the view
*/
public void toggleActive() {
mActive = !mActive;
}
#Override
public void onSensorChanged(SensorEvent event) {
if (mList == null || !mActive) {
return;
}
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X,
SensorManager.AXIS_Z, mRotationMatrix);
SensorManager.getOrientation(mRotationMatrix, mOrientation);
mHeading = (float) Math.toDegrees(mOrientation[0]);
mPitch = (float) Math.toDegrees(mOrientation[1]);
float xDelta = history[0] - mHeading; // Currently unused
float yDelta = history[1] - mPitch;
history[0] = mHeading;
history[1] = mPitch;
float Y_DELTA_THRESHOLD = 0.13f;
// Log.d(TAG, "Y Delta = " + yDelta);
int scrollHeight = mList.getHeight()
/ 19; // 4 items per page, scroll almost 1/5 an item
// Log.d(TAG, "ScrollHeight = " + scrollHeight);
if (yDelta > Y_DELTA_THRESHOLD) {
// Log.d(TAG, "Detected change in pitch up...");
mList.smoothScrollBy(-scrollHeight, 0);
} else if (yDelta < -Y_DELTA_THRESHOLD) {
// Log.d(TAG, "Detected change in pitch down...");
mList.smoothScrollBy(scrollHeight, 0);
}
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private void scrollToTop() {
mList.smoothScrollToPosition(0);
}
private void playSuccessSound() {
// Play sound to acknowledge action
AudioManager audio = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
audio.playSoundEffect(Sounds.SUCCESS);
}
}
I used the above in a ListActivity. I initialize it in onCreate(), and here is the method that initializes it:
private void initListController() {
mListView = getListView();
mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
mListView.setSelector(android.R.color.transparent);
mListView.setClickable(true);
mListController = new SensorListController(this, mListView);
}
This also removes the selection indicator from view by making it transparent.
The above controller also uses two finger press to pause/resume scrolling, and a two finger tap to scroll to the top of the list (and acknowledges both these actions with a sound). Note that for these gestures to work, you will need to override onGenericMotionEvent() in your Activity and pass through the event, like:
#Override
public boolean onGenericMotionEvent(MotionEvent event) {
// We need to pass events through to the list controller
if (mListController != null) {
return mListController.onMotionEvent(event);
}
return false;
}
Full source code for this solution can be seen on Github, and the APK can be downloaded here.

Action Bar switching tabs automatically:

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?