Start activity and change the widget image button on click in android - android-widget

I want to start an activity and change the widget image button on click.
How can i do it? I don't know it properly.After much googling, it still is not solvable.

There are different ways to create a button.
I will use the programmatic way:
Source to create the button in one Activity class:
//making the container for the button
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(tableParams);
//making the button
Button button = new Button(this);
//making the class what will handler the click
button.setOnClickListener(new ClassWithBehavior());
//set the color of button state1
button.getBackground().setColorFilter(Color.parseColor("#ff00ff"), PorterDuff.Mode.MULTIPLY);
//adding the button at the container
tableLayout.addView(button);
source of the ClassWithBehavior:
private class ClassWithBehavior implements View.OnClickListener {
public void onClick(View button) {
//set the color of button state2
this.getBackground().setColorFilter(Color.parseColor("#00ff00"), PorterDuff.Mode.MULTIPLY);
}
}
create a new activity:
//need have first a Intent before change of the activity
Intent i = new Intent(a, Activity2.class);
//share data between old_activity and the new_activity
i.putExtra("id_tag_name_of_the_data_from_activity1_to_activity2",data);
//launch the new activity
this.startActivity(i);
Remember that every activity have its own variables, and views, if you not pass the state of button or the color at the new activity, this wouldn't have these info.

Related

How to access the currently selected item within Wicket Palette

I am trying to override certain features of the Wicket Palette. I have attached a picture of what i am trying to accomplish with Palette. Basically in addition to the select-item-clickbutton-moveToRight functionality of Palette, I also want to know which item has been selected before it is moved. When I select an item in either of the panels and click on a View button, I should be able to display an html page related to the currently selected item from the Palette.
Right now, the button is placed out of the Palette code and as long as I can get the ID of the selected element, I will be able to accomplish my objective.
I am stuck at the point where I need to know which item has been selected within the palette.
Here's what I have tried so far:
1. Adding an onclick listener to the choicesComponent using the AjaxFormComponentUpdatingBehavior
final Palette classFormMapping = new Palette("formsPalette", new ListModel(selectedFormsList),
formsList, new CustomObjectChoiceRenderer(), 8 , false ){
#Override
protected void onBeforeRender() {
super.onBeforeRender();
getChoicesComponent().add(new AjaxFormComponentUpdatingBehavior("onclick"){
#Override
protected void onUpdate(AjaxRequestTarget target) {
System.out.println("REACHED HERE"+ getFormComponent());
/*
* The code reaches here for each click but I am unable to know which item was selected */
}
});
}
};
Adding a Recorder component to the Palette with an "onclick" listener.
This listener does not get called at all.
final Palette classFormMapping = new Palette("formsPalette", new ListModel(selectedFormsList),
formsList, new CustomObjectChoiceRenderer(), 8 , false ){
protected Recorder newRecorderComponent() {
Recorder recorder = super.newRecorderComponent();
recorder.add(new AjaxFormComponentUpdatingBehavior("onclick") {
private static final long serialVersionUID = 1L;
#Override
protected void onUpdate(AjaxRequestTarget target) {
System.out.println("reached record on click ");
}
});
return recorder;
}
};
Trying to create this palette with a custom button
Please help. Thanks in advance.
Palette.java javadoc explains how to "Ajax-ify" it: https://github.com/apache/wicket/blob/529db58c413861677f7ff6736f9363edf42ae85a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/palette/Palette.java#L52-L71
But this won't help you because the selection is done at the client side first and then Wicket is notified:
https://github.com/apache/wicket/blob/529db58c413861677f7ff6736f9363edf42ae85a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/palette/palette.js#L118-L127
You need either to register your own JS event listener for 'change' event before the Wicket one or monkey-patch palette.js to override Wicket.Palette.updateRecorder() function.

How to get the gameobject- which triggered the UI.Button click function

in Unity 5- I am trying to disable a ui.button- once it is clicked. There is a list of buttons- among which- the clicked one will be disabled. But I am not sure, if it is possible to get the event triggering gameobject.
Unity Editor-
Code:
// called from ui.button clicks
public void callThisMethod(string param) {
// how to get the clicked button gameobject here
}
Just Add another event to return the button
public Button ButtonPressed(Button ThisButton)
{
return ThisButton;
}
You can add something like this to your code:
public Button yourButton;
public void yourButtonClicked(){
yourButton.interactable = false;
}
Assign your button to the Button object in inspector. Then where it says "On Click()" in the image you have above, select the script the above code is added to, and select the "yourButtonClicked()" function. That will disable the button once it is clicked.
To get your clicked button game object, you can use: EventSystem.current.currentSelectedGameObject
Pass the gameObject of the button as a parameter while your are adding event listener to the UI button.
Hope that helps:
GameObject myButtonGameObject = myButton.gameObject;
myButton.onClick.AddListener(() => {LogName(myButtonGameObject); });
public void LogName(GameObject buttonGameObject = null){
Debug.Log(myButtonGameObject);
}
Note: The given approach is useful, when you need to dynamically show the clicked buttons name, but if you know the exact button, you can make a public field for the myButton and get the name without passing any parameters.

Actionbar up navigation with fragment and activity

I'm going to use home button (Action bar App icon) as back button. I got it to work but not in the way i intended.
My MainActivity is an activity which holds (1) a drawer that shows a list of categories. And a Fragment that displays a list of items in the category chosen in the drawer.
when a item in the list is clicked, a new DetailActivity is started to show the details.
here starts the problem:
From the DetailActivity when i press Back button, it returns to the MainActivity as it was before clicking the item to show details. That is what I expect. However, when use home button as Up navigation, it starts the MainActivity as if I opened the app again. Not showing the list that was previously being shown.
I read in developer documents that for fragments I have to use: .add(detailFragment, "detail") And .addToBackStack() then commit.
But what am I suppose to add in add(---,"---"). And then how should I use it?!
this is my codes:
the method is the MainActivity that shows the content:
public void refreshDisplay(Context context, View view, String category, int i) {
List<Lesson> lessonByCategory = datasource.findByCategory(category, i);
final ListView lv = (ListView) view.findViewById(R.id.listView);
final ArrayAdapter<Lesson> adapter = new LessonListAdapter(context, lessonByCategory);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
Log.i(LOGTAG, "onListItemClick called");
ArrayAdapter<Lesson> m_adapter = adapter;
// get the Lesson object for the clicked row
Lesson lesson = m_adapter.getItem(position);
Intent intent = new Intent(MainActivity.this, LessonDetailActivity.class);
intent.putExtra(".model.Lesson", lesson);
intent.putExtra("isStared", isStared);
startActivityForResult(intent, LESSON_DETAIL_ACTIVITY);
}
});
}
In my LESSON_DETAIL_ACTIVITY that shows the detail content I have this code to enable up navigation for home button:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// I have some other cases here
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
And finally in the Manifest I used the code below to introduce MainActivity as the parrent of LessonDetailActivity:
<activity
android:name=".LessonDetailActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
</activity>
I want the Home button as up navigation to behave like back button so that when its clicked it takes me to the MainActivity as it was before opening the LessonDetailActivity. The code above doesn't do that and every time I press Home in the action bar it starts the MainActivity from scratch.
Could anyone help me with this please?
I also should say that I'm new so I'd appreciate it if the answers were detailed.

Radio button change handler refreshing the page

I'm using UploadItem, RadioGroupItem and some other widgets. RadioButton is having onChangeHandler which will decide what all other components need to be displayed. I've uploaded some file using UploadItem. Then I changed the radio button selection. On changing the radio button, required widgets are getting displayed properly but whatever file I'd selected using UploadItem is going away. Fresh UploadItem widget is getting displayed. In other words page is getting refreshed.
My requirement is whenever I change radio button option, required widget should displayed along with that whatever file I had selected using UploadItem should remain same.
My Code is something like this:
UploadItem upload = new UploadItem();
RadioGroupItem radioGroup = new RadioGroupItem();
HashMap map = new HashMap();
map.put("option1","option1");
map.put("option2","option2");
radioGroup.setValueMap(map);
TextItem textbox = new TextItem();
radioGroup.addChangeHandler(new ChangeHandler(){
public void onChanged(ChangedEvent event) {
String radioValue =((String)event.getValue());
if(radioValue.equalsIgnoreCase("option2")){
textbox.show();
}else{
textbox.hide();
}
}
});
Add all created widgets to DynamicForm object using dynamicForm.setFields(all created widgets)
Changing the radio button should hide and show the textBox. But while doing that page is getting refreshed and whatever file we had selected using UploadItem is lost.
As per the documentation for hide() and show() of FormItem class, invocation of any of these methods, will cause the DynamicForm to be redrawn.
So it may cause the problem you're getting.
To overcome this issue, I would suggest you to put UploadItem in a separate DynamicForm.
fire an event on radio Selection change as
radioButton.addListener(Events.Change, new Listener<BaseEvent>() {
#Override
public void handleEvent(BaseEvent be) {
if(radioButton.getValue()){
//fire an event here for ur widget
}
}
});

Add dropdown menu in a view's ToolBarManager

I want to add several dynamically created actions to a view. This works to add them to the View Menu in the top right corner:
private void fillActionBars() {
IActionBars bars = getViewSite().getActionBars();
IMenuManager manager = bars.getMenuManager();
IMenuManager myMenu = new MenuManager("Menu title", MY_MENU_ID);
// add actions to myMenu
manager.add(myMenu);
bars.updateActionBars();
}
This works fine. However, I want to add the actions to a dropdown menu in the toolbar instead (so the user can see them immediately). If I replace the third line with
IToolbarManager manager = bars.getToolBarManager();
the menu doesn't show up.
You're right, this doesn't work. A workaround that works fine, not using a MenuManager but a drop down action and a menu creator:
IActionBars bars = getViewSite().getActionBars();
IToolbarManager manager = bars.getToolBarManager();
Action act=new Action("Menu title",SWT.DROP_DOWN){};
act.setMenuCreator(new MyMenuCreator());
manager.add(act);
class MyMenuCreator implements IMenuCreator{
public Menu getMenu(Control ctrl){
...
}
}
You need to use IToolbarManager.add(IContributionItem) with a class that implements IContributionItem. See org.eclipse.ui.internal.FastViewBarContextMenuContribution as an example.