It comes to Log1 then crashes.Whats wrong? - imageswitcher

"is" is the ID of Imageswitcher
s is images number in array
The Array contains 5 images:play stop next previous pause
public class MainActivity extends AppCompatActivity {
Button btnnext,btnprev;
ImageSwitcher is;
int s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Integer[] img=new Integer[]{R.drawable.next,R.drawable.play,R.drawable.previous,R.drawable.stop,R.drawable.pause,};
btnnext=(Button)findViewById(R.id.btnnext);
btnprev=(Button)findViewById(R.id.btnprev);
is =(ImageSwitcher) findViewById(R.id.is);
Log.i("log1","1");
is.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
ImageView imageView=new ImageView(getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
Whats wrong with the line below?
imageView.setLayoutParams(new ImageSwitcher.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
is.setImageResource(img[s]);
Log.i("log","4");
return imageView;
}
});
Log.i("log2", "2");
Button Next codes:
btnnext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation right= AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_out_right);
Animation left= AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_in_left);
is.setAnimation(left);
is.setAnimation(right);
is.setImageResource(img[(s+1)]);
}
});
Log.i("log3", "3");
Button Previous codes:
btnprev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation right= AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_out_right);
Animation left= AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_in_left);
is.setAnimation(left);
is.setAnimation(right);
is.setImageResource(img[(s-1)]);
}
});
}
}

this code should be removed:
is.setImageResource(img[s]);
instead of:
is.setImageResource(img[(s+1)]);
I shoul use:
currentImage++;
currentImage = currentImage % img.length;
is.setImageResource(img[currentImage]);
and instead of:
is.setImageResource(img[(s-1)]);
I should use:
currentImage--;
currentImage = (currentImage + img.length) % img.length;
is.setImageResource(img[currentImage]);

Related

Marshmallow does not show my custom analog clock widget

I have created custom analog clock which I want to display in the widget. This analog clock is visible in Api 16+ but not visible in Marshmallow (Api 23). Marshmallow allowing to create the widget but does not display my analog clock on the home screen.
Is there any permission in marshmallow to show my custom analog widget??
This is my Widget Class
'public class Widget extends AppWidgetProvider {
private Intent mUpdateService;
#Override
public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_DISABLED)) {
mUpdateService = new Intent();}else{
if (mUpdateService == null)
mUpdateService = new Intent(context, UpdateService.class);
context.startService(mUpdateService);
}
}
public static class UpdateService extends Service {
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
private boolean isScreenOn = true;
#Override
public void onReceive(Context context, Intent intent) {
String key = intent.getAction();
if (key.equals(Intent.ACTION_SCREEN_ON)) {
isScreenOn = true;
updateWidget();
} else if (key.equals(Intent.ACTION_SCREEN_OFF)) {
isScreenOn = false;
} else if (isScreenOn) {
updateWidget();
}
}
};
public RemoteViews mRemoteViews;
public WidgetAnalogClock widgetAnalogClock;
#Override
public void onCreate() {
mRemoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.widget_layout);
widgetAnalogClock = new WidgetAnalogClock(getApplicationContext());
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
registerReceiver(mReceiver, filter);
}
#Override
public void onDestroy() {
unregisterReceiver(mReceiver);}
#Override
public void onStart(Intent intent, int startId) {
updateWidget();}
private void updateWidget(){
WidgetAnalogClock myView=new WidgetAnalogClock(getApplicationContext());
mRemoteViews=new RemoteViews(getApplicationContext().getPackageName(),R.layout.widget_layout);
myView.measure(150, 150);
myView.layout(0, 0, 750, 750);
myView.setDrawingCacheEnabled(true);
Bitmap bitmap = myView.getDrawingCache();
mRemoteViews.setImageViewBitmap(R.id.customClock, bitmap);
AppWidgetManager.getInstName(this, Widget.class), mRemoteViews);
bitmap = myView.getDrawingCache();
mRemoteViews.setImageViewBitmap(R.id.customClock, bitmap);}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
}`

Clear SuggestBox on blur in GWT

I have a SuggestionBox in GWT. Is there a way to clear it when it blurs (unless the user made a selection, in which case an action should happen)?
Add a BlurHandler:
suggestionBox.getValueBox().addBlurHandler(new BlurHandler() {
#Override
public void onBlur(BlurEvent event) {
// your code goes here
}
});
Try this one using ValueChangeHandler:
Note: ValueChange event has same behavior as Blue event but it is fired only if value is changed in SuggestBox.
class MyMultiWordSuggestOracle extends MultiWordSuggestOracle {
private Set<String> values = new HashSet<String>();
#Override
public void add(String value) {
super.add(value);
values.add(value);
}
#Override
public void clear(){
super.clear();
values.clear();
}
public boolean contains(String value) {
return values.contains(value);
}
}
You code:
final MyMultiWordSuggestOracle oracle = new MyMultiWordSuggestOracle();
oracle.add("A");
oracle.add("AB");
oracle.add("BCD");
oracle.add("BCDE");
final SuggestBox suggestionBox = new SuggestBox(oracle);
suggestionBox.addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
if (!oracle.contains(event.getValue())) {
suggestionBox.setValue("");
}
}
});

When is Widget OnUnload() is called?

Can you please tell me when a widget's OnUnload() is called?
I tried to override it however, it is never been accessed. Also what is the best way to unload a composite, I am using RootPanel.get("Dev1").clear();
If you want to stop the timer when you leave the page then use CloseHandler.
This handler is called while page closing and refreshing.
Window.addCloseHandler(new CloseHandler<Window>() {
#Override
public void onClose(CloseEvent<Window> event) {
timer.cancel();
}
});
If you want to stop the timer when clearing a vertical panel then use removeFromParent.
This overridden method is to be called when any widget is removed form its parent.
private Timer timer = null;
/*
* This is the entry point method.
*/
public void onModuleLoad() {
final Label label = new Label("Hello ") {
#Override
public void removeFromParent() {
if (timer != null && this.isAttached()) {
timer.cancel();
System.out.println("timer is stopped");
}
super.removeFromParent();
}
};
timer = new Timer() {
#Override
public void run() {
label.setText("Hello " + (int) (Math.random() * 100));
}
};
timer.scheduleRepeating(500);
final VerticalPanel verticalPanel = new VerticalPanel();
verticalPanel.add(label);
Button button = new Button("Remove Label");
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
label.removeFromParent();
}
});
verticalPanel.add(button);
RootPanel.get().add(verticalPanel);
}
As per you comments try below code where clear method is overridden for VerticalPanel.
final VerticalPanel verticalPanel = new VerticalPanel(){
#Override
public void clear(){
if(this.isAttached()){
timer.cancel();
}
super.clear();
}
};
verticalPanel.getElement().setId("Div1");

how to delete item from listview and refresh that

I need some help in my homework here...
I want to do a listview with itens from editText.. but I dont know how to delete and edit the item, I tryed to use ContextMenu. I will post my code, and if you think that i have to change anything, tell me.. I'm begginer.
private Button bt;
private ListView lv;
private EditText et;
private ArrayList<String> Arraylt = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l);
bt = (Button)findViewById(R.id.bt);
lv = (ListView)findViewById(R.id.lv);
et = (EditText)findViewById(R.id.et);
lv.setOnCreateContextMenuListener(this);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view,
int posicao, long id) {
Object o = lv.getItemAtPosition(posicao);
et.setText(String.valueOf(o));
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addadicionar();
}
});
}
public void addadicionar() {
Arraylt.add(et.getText().toString());
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Arraylt));
et.setText("");
}
hey this is my note pad code it will help you .. have a look
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.listView1)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.edit:
// edit stuff here
return true;
case R.id.delete:
ArrayAdapter <String> adapter=new ArrayAdapter<String>(menu.this,android.R.layout.simple_list_item_1,data);
Cursor cursor2=mydb1.rawQuery("SELECT * FROM notes;", null);
data.remove(info);
lv.setAdapter(adapter);
cursor2.moveToPosition(info.position);
int id= cursor2.getInt(cursor2.getColumnIndex("id"));
mydb1.delete("notes", "id=?", new String[] {Integer.toString(id)});
list(view); // this will refresh my list by calling my display list method here
return true;
default:
return super.onContextItemSelected(item);
}
try to remove the item from listItems(List), then call adapter.notifyDataSetChanged();
try this code :
// note : you need to create xml file in res/menu folder which conatain deleter and editer item
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.listView1)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.Deletar:
// edit stuff here
return true;
case R.id.editar:
ArrayAdapter <String> adapter=new ArrayAdapter<String>(menu.this,android.R.layout.simple_list_item_1,data);
data.remove(info);
lv.setAdapter(adapter);
list(view); // this will refresh my list by calling my display //list method here
return true;
default:
return super.onContextItemSelected(item);
}
Yes, I win!.. now, I can edit and delete the item from listview...
private Button bt;
private ListView lv;
private EditText et;
private ArrayList<String> Arraylt = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l);
bt = (Button)findViewById(R.id.bt);
lv = (ListView)findViewById(R.id.lv);
et = (EditText)findViewById(R.id.et);
lv.setOnCreateContextMenuListener(this);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int posicao, long id) {
Object o = lv.getItemAtPosition(posicao);
et.setText(String.valueOf(o));
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addadicionar();
}
});
}
public void addadicionar() {
Arraylt.add(et.getText().toString());
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Arraylt));
et.setText("");
}
sorry for the bad indent..

drop widget beyond the AbsolutePanel gwt dnd

I'm trying to drop my draggable widgets out of the boundary panel (AbsolutePanel). In my case draggable widgets is an image. And I want to drop it, so that there will be visible only a part of the image, but when I drop it, and some parts of image beyond absolute panel, it drop automatically within absolute panel.
I tried :
dragController.setBehaviorConstrainedToBoundaryPanel(false);
and thought it means that I can drop it where ever I want, but it doesn't work.
And the working solution :)
Here is my code:
public class myEntripointClass implement EntryPOint{
AbsolutePanel droper;
public void onModuleLoad() {
Panel main = new AbsolutePanel();
droper = new AbsolutePanel();
droper.setHeight("300px");
droper.setWidth("500px");
main.add(droper);
content=new AbsolutePanel();
bt = new Button("Drag and drop it");
content.add(bt);
lb = new Label("Label drag and drop");
content.add(lb);
main.add(content);
manageDnD();
RootPanel.get().add(main);
}
private void manageDnD() {
PickupDragController dragController = new PickupDragController(
(AbsolutePanel) content, true);
dragController.makeDraggable(bt);
dragController.makeDraggable(lb);
dragController.addDragHandler(new DragHandler() {
#Override
public void onPreviewDragStart(DragStartEvent event)
throws VetoDragException {}
#Override
public void onPreviewDragEnd(DragEndEvent event) throws VetoDragException {}
#Override
public void onDragStart(DragStartEvent event) {
}
#Override
public void onDragEnd(DragEndEvent event) {
// TODO Auto-generated method stub
DragContext context = event.getContext();
int x=context.mouseX;
int y= context.mouseY;
droper.add(context.selectedWidgets.get(0),x,y);
}
});
NameDropController dropController = new NameDropController(droper);
dragController.registerDropController(dropController);
dragController.setBehaviorDragProxy(true);
}
and my DropController class is:
public class NameDropController extends AbstractDropController{
public NameDropController(Widget dropTarget) {
super(dropTarget);
}
#Override
public void onDrop(DragContext context) {
int x=getDiff(getDropTarget().getAbsoluteLeft(), context.mouseX);
int y=getDiff(getDropTarget().getAbsoluteTop(), context.mouseY);
((AbsolutePanel)getDropTarget()).add(context.selectedWidgets.get(0),x,y);
System.out.print("("+context.mouseX+"::,::"+context.mouseY+")");
}
#Override
public void onMove(DragContext context){
}
private int getDiff(int val1,int val2){
return Math.abs(val1-val2);
}
}