Open new Fragment screen by clicking the button in Activity android - android-activity

I have created one activity class in android.i need to clicking button show new fragment class screen.

Use the FragmentManager class:
FragmentTransaction ft = getFragmentManager().beginTransaction()
.add(R.id.your_fragment_container, yourFragment)
.addToBackStack();
ft.commit();

Related

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

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.

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.

SmartGwt - Removing one tab clears the canvas

I've built a GUI with a SmartGWT TabSet with Tabs that can be dynamically added and removed.
The Tabs share the same canvas which is moved from Tab to Tab at each tab selection like this:
myTabSet.addTabSelectedHandler(new TabSelectedHandler() {
public void onTabSelected(TabSelectedEvent event) {
[...]
myTabs[myTabSet.getSelectedTabNumber()].setPane(myCanvas);
// Then I fill the contained widgets with the tab-specific data
}
}
This works, but when I try to remove a Tab with
myTabSet.removeTab(iToBeDeletedTab);
The tab is removed but the remaining tabs have a blank pane, I can get the content back only by reloading the page. I found that I have to prevent pane destruction with calls to :
myTabSet.setDestroyPanes(false);
and
myTabSet.updateTab(iToBeDeletedTab, null);
//called right before
myTabSet.removeTab(iToBeDeletedTab);
I understand that the canvas/pane is still destroyed, but I cannot figure out how to prevent this.
Has anyone any hint?
Thank you!
Have you tried to call the redraw() method after removing a tab? This usually helps me when loading/reloading data with smartGWT widgets.
Your calls are correct, but now what you've got is the pane completely unnassociated from the TabSet and not drawn (check the Watch Tab in the Developer Console and you'll see this). Now, call updateTab(someOtherTab, pane) to connect the pane to one of the other tabs where it should be showing.
Ok, I've made some test and got the same as you but had some success with the following code:
1°) in the Javadoc I found:
***public void setPane(Canvas pane)
Specifies the pane associated with this tab. You can change the pane associated with a given tab after the TabSet has been created by calling TabSet.updateTab(int, com.smartgwt.client.widgets.Canvas)***
I tried without setting to null the pane of tab1 , it didn't work.
I think it could be arranged in better way but anyway the point is to use the updatePadmethod
public static void testTabDelete(){
final Canvas theCanvas = new Canvas();
final TabSet theTabs = new TabSet();
theTabs.setWidth("80%");
theTabs.setHeight("80%");
final Tab tab1 = new Tab("Tab1");
final Tab tab2 = new Tab("Tab2");
final Tab tab3 = new Tab("Tab3");
IButton btn1 = new IButton("Btn1");
btn1.setLeft(10);
btn1.setTop(100);
btn1.setWidth(80);
theCanvas.addChild(btn1);
IButton btn2 = new IButton("Delete");
btn2.setLeft(100);
btn2.setTop(100);
btn2.setWidth(80);
btn2.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
#Override
public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
theTabs.updateTab(0, null);
theTabs.updateTab(1, theCanvas);
theTabs.selectTab(tab2);
theTabs.removeTab(tab1);
}
});
theCanvas.addChild(btn2);
theTabs.addTab(tab1);
theTabs.addTab(tab2);
theTabs.addTab(tab3);
tab1.setPane(theCanvas);
RootPanel.get("container").add(theTabs);
}

Titanium - Event listener for annotation

I am a beginner in Titanium Studio. I am opening a new window, when I select the annotation pin on the mapview.
annotation.addEventListener('click', function(e) {
Ti.API.info("Opening detail window");
navGroup.open(detailWindow);
});
But, it doesn't work every time. It works only for the first time, when the annotation is performed.
How could I make it work, whenever I select the annotation pin, I should move to a new window? Thanks in advance.
You should use annotation right button to open a new window.
To catch the annotation right button click event you have to do this:
var mapView.addEventListener('click', function(e){
// if user click on rightButton of annotaion
if ( evt.clicksource == 'rightButton' ) {
Ti.API.info("Opening detail window");
navGroup.open(detailWindow);
}
});

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.