How to create custom ProgressBar and use it in Widget? - android-widget

I would like use my own extended ProgressBar class in Widget. I has created really simple MyProgressBar and if I place this into standart activity layout, it works. Of course I set also Style attribiute to "?android:attr/progressBarStyleHorizontal" in MyProgressBar property in designer.
But if I do same and place it to Widget layout, its visible good in layout designer in eclipse, however, widget does not work in traget. There is only "Problem loaing widget" message showed instead of widget layout .... on target device....
Source code for MyProgressBar is here :
public class MyProgressBar extends ProgressBar {
public MyProgressBar(Context context) {
super(context);
}
public MyProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected synchronized void onDraw(Canvas canvas) {
// First draw the regular progress bar, then custom draw our text
super.onDraw(canvas);
}
}

Widget don't support custom view.
If you just change the progressbar's background,you can use the system's progressbar,and change the drawable in the layout xml.

App Widget can support the following layout classes: FrameLayout, LinearLayout, RelativeLayout, GridLayout.
And the following widget classes: AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, TextView, ViewFlipper, ListView, GridView, StackView, AdapterViewFlipper.
Descendants of these classes are not supported.
Copied from: http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout

Related

Adding button to SimplePanel results in error

I am performing the following action in GWT
public class FooPanel extends SimplePanel {
private String url;
public FooPanel () {
super(DOM.createAnchor());
Button button = new Button();
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
foo();
}
});
add(button);
}
}
however when I run the code I get the following error
SimplePanel can only contain one child widget
However Button is a single widget so I am not sure what the problem is? The problem doesn't occur if i don't add the button
Remove this line:
super(DOM.createAnchor());
You don't need it.
You can simply use your Button in your code, or extend a Button widget. Adding a Button to a SimplePanel does not offer any benefits.
Have a look at source code of SimplePanel#add() to analyze this error.
#Override
public void add(Widget w) {
// Can't add() more than one widget to a SimplePanel.
if (getWidget() != null) {
throw new IllegalStateException("SimplePanel can only contain one child widget");
}
setWidget(w);
}
Now its clear from the source code that you have already added a widget in SimplePanel.
Call SimplePanel#getWidget() to get the already added widget.
Look at the source code of default constructor if SimplePanel class. It might help you to understand that how SimplePanel enclose the widget inside it.
/**
* Creates an empty panel that uses a DIV for its contents.
*/
public SimplePanel() {
this(DOM.createDiv());
}
Try with setWidget(button); instead of add(button);

Android spinner adapter setDropDownViewResource custom layout with radiobutton

I use Spinner in Dialog Mode.
I set SimpleCursorAdapter for the Spinner with setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
That works fine.
Now instead of simple_spinner_dropdown_item I'm trying to pass my custom layout it does work well too.
But there is a but... it does not have radio button that original simple_spinner_dropdown_item does.
Is it possible to add radio button inside of my custom spinner_dropdown_item that would be selected when spinner dialog is shown?
yes its possible but you have to define a another class for spinner.Just look at this
you have one more option to get your requirement. that is Alert dialog
just check out this Alert Dialog Window with radio buttons in Android and How to create custom and drop down type dialog and Dialog in android
Well I have found solution. ListView (what is inside of the spinners dialog) will check if your View is Checkable and call setChecked. Since android.R.layout.simple_spinner_dropdown_item is checkable it works.
So for my custom List item i have created LinearLayout that implements Checkable
public class CheckableLinearLayout extends LinearLayout implements Checkable
{
private boolean _isChecked = false;
public CheckableLinearLayout(Context context)
{
super(context);
}
public CheckableLinearLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
#Override
public void setChecked(boolean checked)
{
_isChecked = checked;
for (int i = 0; i < getChildCount(); i++)
{
View child = getChildAt(i);
if (child instanceof Checkable)
{
((Checkable) child).setChecked(_isChecked);
}
}
}
#Override
public boolean isChecked()
{
return _isChecked;
}
#Override
public void toggle()
{
_isChecked = !_isChecked;
}
}
So ListView calls setChecked and I propagate that down to children views and my CheckBox / RadioButton will get checked / unchecked correctly.

popuppanel show up beneath the widget

I am new to GWT and the web stuff.
I am working out my own project based on
http://code.google.com/p/cloud-tasks-io/source/browse/#svn%2Ftrunk%2FCloudTasks-AppEngine%2Fsrc%2Fcom%2Fcloudtasks%2Fclient
I am trying to use popup/dialog. The popup and dialog always show behind the widget. I keep googling around and the most relevant I found is this
http://groups.google.com/group/gwt-google-apis/browse_thread/thread/40db4fcbe10d2060 which does not provide any answer. Anyway, I have 3rd party library, bst-player 1.3, which uses flash. So I disabled it(later remove it too), the popup just won't come to the top! It is still hiding behind the widget.
I have learned that popuppanel/dialogpanel alikes do not need to get added to another widget. A different way of saying is that it is not a normal widget in a sense that it cannot attach to a parent but it attaches itself to the dom to guarantee being on top (from GWT composite widget )
I am at my wit end and I am here at SO ...
UPDATE
Here is my Popup class
public class PopUp {
static PopupPanel simplePopup;
public static void init() {
simplePopup = new PopupPanel(true);
simplePopup.hide();
simplePopup.setVisible(false);
// DOM.setIntStyleAttribute(simplePopup.getElement(), "zIndex", 3);
}
public static void showpopupmsg(String msg, int left, int top) {
if (simplePopup == null) {
init();
}
if (msg != null && !msg.equalsIgnoreCase("")) {
simplePopup.ensureDebugId("cwBasicPopup-simplePopup");
simplePopup.setWidget(new HTML(msg));
simplePopup.setVisible(true);
simplePopup.setPopupPosition(left, top);
simplePopup.setWidth("475px"); //575
simplePopup.setGlassEnabled(true);
simplePopup.show();
}
}
public static void show(String message){
if (simplePopup == null) {
init();
}
simplePopup.setGlassEnabled(true);
simplePopup.setTitle(message);
simplePopup.center();
}
}
Here is how I am calling
tasksTable.doneColumn.setFieldUpdater(new FieldUpdater<TaskProxy, Boolean>() {
public void update(int index, TaskProxy task, Boolean value) {
String msg = "Here is the popup. All the way underneath";
Widget source = tasksTable;
int left = source.getAbsoluteLeft() - 50;
// source.getAbsoluteLeft() + 25;
int top = source.getAbsoluteTop() - 25;
PopUp.showpopupmsg(msg, left, top); //Here is the calling method
TaskRequest request = requestFactory.taskRequest();
TaskProxy updatedTask = request.edit(task);
updatedTask.setDone(value);
request.updateTask(updatedTask).fire();
}
});
Here is how the Popup is beneath the widget.
The source of the problem has been quite elusive since I am still new to the webapp, yet, I finally solve it myself. The culprit is the CSS. It is defining the z-index for the whole thing to quite high as seen in the following code line 1333.
http://code.google.com/p/cloud-tasks-io/source/browse/trunk/CloudTasks-AppEngine/war/CloudTasks.css#1333
I have doubted about the z-index before and try it out with a paltry value 3 as seen in the commented out code segment of Popup class in question. I have to uncomment it and set it to 101.
DOM.setIntStyleAttribute(simplePopup.getElement(), "zIndex", 101);
I was , you know, #$%###$*.
z-index is only decides which widget should show on top..
the widget popup is under benath might be having z-index value high.
set the z-index for popup thru css (recomended) or DOM will work for you
According to my feeling, using static methods of your "PopUp" object is a bit strange...
In that way, I think things a relative to the top rather than caller object.
Maybe you could consider make your class 'Popup' extending 'popupanel'
and in your calling code, just make
new PopUp(msg,left,top).show() ;
I recently wrote my own solution for a popup panel that needs to be aligned with its caller. The solution consists out of an PopupPanel extension and a Button extension.
The button extension has an instance of the panel extension, and the moment it is clicked it gives its coordinates and width and height to its panel.
#Override
public void onClick(ClickEvent event) {
if (optionsPanel.isShowing()) {
optionsPanel.hide();
} else {
optionsPanel.setButtonBounds(new Bbox(
getAbsoluteLeft(), getAbsoluteTop(), getOffsetWidth(), getOffsetHeight()));
optionsPanel.show();
}
}
(The Bbox class is just a convenience class I could use for wrapping coordinates; write your own class or 4 methods for that matter)
The main work is then done in the onLoad() override of the PopupPanel, in which the coordinates of the button are used to position the panel;
#Override
protected void onLoad() {
super.onLoad();
if (null == bounds) {
super.hide();
} else {
left = (int) bounds.getX();
top = (int) bounds.getMaxY();
setPopupPosition(left, top);
}
}
(bounds are the coordinates of the button; getMaxY() == bottom coordinate of button)

Cell colors in a GWT CellTable

I'm using a CellTable and would like to programatically change the background color of certain cells in some situations. I tried it with an Custom Cell as described in the documentation and changed the background color with
sb.appendHtmlConstant ("<div style=\"background-color:blue;\">");
sb.append (safeValue);
sb.appendHtmlConstant ("</div>");
This basically works, but seems to be quite slow. Is there a better way to do this?
Actually you can Override getCellStyleNames() and return the wanted style for the cell
TextColumn<Composant> nameColumn= new TextColumn<Composant>() {
#Override
public String getCellStyleNames(Context context, Composant object) {
return "styleName";
}
#Override
public String getValue(Composant object) {
return object.getName();
}
};

Replace GWT DockLayoutPanel Contents

I'm trying to replace the contents of the CENTER portion of a DockLayoutPanel from a MenuBar. The MenuBar will sit at the North, but "content" (forms, reports, etc) will reside in Center.
I thought I could do it by grabbing the entire DockLayoutPanel from the RootPanel (index 0, since that's the only widget directly attached to it), then somehow remove the current contents of center and insert the new. Unfortunately, getWidget(int index) is non-static, so it's not working.
Can anyone how me with the right way to do this? Non-working code is below:
// snipped package and import details
public class menubar extends Composite {
private static menubarUiBinder uiBinder = GWT.create(menubarUiBinder.class);
interface menubarUiBinder extends UiBinder<Widget, menubar> {
}
#UiField MenuBar applicationMenuBar;
#UiField MenuBar processMenuBar;
#UiField MenuBar reportsMenuBar;
#UiField MenuItem addPowerRequirementCmd;
#UiField MenuItem powerUsageReportCmd;
public menubar() {
initWidget(uiBinder.createAndBindUi(this));
// command to replace whatever is in DockLayoutPanel.CENTER w/ AddPowerRequirement
// FormPanel
addPowerRequirementCmd.setCommand(new Command() {
#Override
public void execute() {
// get DockLayoutPanel from the RootPanel (it's the only widget, should be
// index 0
DockLayoutPanel dlp = (DockLayoutPanel) RootPanel.getWidget(0);
// clear out DockLayoutPanel.CENTER
// insert the FormLayoutPanel into DockLayoutPanel.CENTER
dlp.add(new PowerRequirementForm());
}
});
// command to replace whatever is in DockLayoutPanel.CENTER w/ power usage report
powerUsageReportCmd.setCommand(new Command() {
#Override
public void execute() {
}
});
}
}
Thanks!
Use a ui:field attribute, like you do for the MenuBar, to get a reference to the DockLayoutPanel from UiBinder:
<g:DockLayoutPanel ui:field="dockPanel"/>
and in the class:
#UiField DockLayoutPanel dockPanel;
However, it sounds like you want to have a set of widgets that are shown in the center of the panel depending on application state. A DeckPanel is a better solution for this:
<g:DockLayoutPanel>
<g:center>
<g:DeckPanel ui:field="deck">
<g:FlowPanel>Panel #0</g:FlowPanel>
<g:FlowPanel>Panel #1</g:FlowPanel>
</g:DeckPanel>
</g:center>
</g:DockLayoutPanel>
And then to switch the displayed child of the DeckPanel:
deck.showWidget(1); // Show Panel #1
deck.showWidget(0); // Show panel #0