Android ImageButton vs ImageView - onclick - imageview

At first ... I had a Imageview in my XML file layout but then
I changed my opinion and delete the Imageview and put an ImageButton in it.
So far so good
The Problem occurs that the onclick method of the ImageView doesn't work
with the ImageButton :(
But I don't know why
Here ist my XML Piece of the ImageButton
<
ImageButton
android:id="#+id/LPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="#drawable/t189"
android:onClick="onButtonClick" ***
I marked the line with three stars - if I delete the line -
the app worked - but no click Action anyway
If I put it back - eclipse says nullpointerexeption
couldn't not execute method of the activity - onclick
My onbuttonclick method stays the same as with the Imageview
public void onButtonClick (View view){
bla bla bla ..}
Inside I changed the pic of the ImageButton with
ImageButton LPic = (ImageButton)findViewById(R.id.LPic);
String Path = SavePath.toString()+"/"+PicFiles[ZL];
Drawable d = Drawable.createFromPath(Path);
LPic.setImageDrawable(d);

Related

JavaFX Adding Dynamic Tabs Permanently

Is there a way where I could add a permanent tab in javafx dynamically? I mean, I have already created a code which can add a tab dynamically on a button click, but when I close the window and open it again, the tab I just added is gone.
Here's my code snippet..
//on button click
#FXML
private void addNewTab(ActionEvent event) {
add_tab_button.setOnAction(e -> {
main_tab.getTabs().add(createTab()); //main_tab is the TabPane
main_tab.getSelectionModel().selectLast();
});
}
//the new tab to be created
private Tab createTab() {
tabIndex++;
Tab newTab = new Tab ("Tab " + tabIndex);
//content of tab goes here..
return newTab;
}
Everything goes well, except that, again, when I close the window and open it again, the newly created tab gets removed.. Is there a way to add it permanently? As if I'm overriding the fxml file of the view?

get wrong position in recyclerview which in fragment with viewpager

I have a viewpager with 4 Fragment, there are a RecyclerView and SwipeRefreshLayout in each Fragment. xml code like this:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_refresh_widget"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/orderList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"/>
</android.support.v4.widget.SwipeRefreshLayout>
When I switch ViewPager from left to right, I get the RecyclerView in Fragment like
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//set swipeRefreshWidget can not refresh when recyclerView is not in the top
mSwipeRefreshWidget.setEnabled(orderLayoutManager.findFirstCompletelyVisibleItemPosition() == 0);
}
}
});
But when I switch viewpager from page A to page B and to page C ,when in the page C, the recyclerView is not in the top(findFirstCompletelyVisibleItemPosition is not 0),but at that time ,I can not get the findFirstCompletelyVisibleItemPosition when switch to page C ,so the swipeRefreshWidget can refresh , but I want it can not refresh.
like this.
PS.the recyclerView in page B is in the top,but in page A and page C,recyclerView is in the middle.
I have encountered similar problems. first page display the second pages' content.
Because i set:
static string week;
and week belongs to class. when i return new Fragment()
twice. the first fragment.week had changed. the result is first.week == second.week.
also because FragmentStatePagerAdapter will Execute getitem() twice. so the content of first pager is wrong. he will display second fragment's content in first page.

How to creat a button programmatically with JavaFX?

I want to make a button on an AnchorPane without drag it from the library in the FXML file I want to do it progammatically: if the search button clicked, should show a new button not existed in the AnchorPane before I did this code but I don't know what is wrong with it:
private void searchButton(ActionEvent evt) {
Button tab = new Button();
tab.setLayoutX(147);
tab.setLayoutY(102);
tab.setText("Tab1");
tab.setPrefHeight(27);
tab.setPrefWidth(69);
WebView wb = new WebView();
wb.setLayoutX(-1);
wb.setLayoutY(128);
wb.setPrefWidth(1604);
wb.setPrefWidth(700);
}
I am assuming that you searchButton method is in controller attached to some FXML. Then all you need to do is this:
yourAnchorPane.getChildren().add(tab);
If you don't have already published reference to anchorPane in your controller, then add this into your controller
#FXML
AnchorPane yourAnchorPane;
And in SceneBuilder select your anchorPane, go to code tab and enter "yourAnchorPane" as fx:id.
Further info on working with anchorpane is javadoc.
You probably also want to set some constraints on the tab to locate it at a position within the AnchorPane. For instance, the following code will locate your button tab relative to the top left corner of the AnchorPane: Ten pixels down and fifteen pixels to the right.
AnchorPane.setTopAnchor(tab, 10.0);
AnchorPane.setLeftAnchor(tab, 15.0);

Where should I put my code in ActionBar.TabListener

I have just started with an ActionBar.TabListener with 3 tabs.
I selected new "Tabbed activity" in Android Studio.
My activity is called test...not the best name but I'm just trying to learn:)
I have a listView in fragment_test.xml that I want to fill with data after a raw sql search.
If I put this code in onCreateView then everytime I click on a tab it will re-write that tab with the same info in the listView.
What I want is to have diffrent information in those tabs.... then I need to know which tab that is clicked. That I did with mViewPager.getCurrentItem() ....Is this right? How can i get the name of the tab instead?
I have also found onTabSelected...should I put my code here? In this case I think I know which tab that is selected but is it really wise to put the code here?
If I do this then listView1 becomes null...why?:
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
listView1 = (ListView)findViewById(R.id.listView2);
listAdapter2 = new ArrayAdapter<String>(context, R.layout.simplerow, testArray);
listAdapter2.notifyDataSetChanged();
listView1.setAdapter(listAdapter2);
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView2"
android:layout_centerHorizontal="true" />
If I understand it correctly, you want to load Fragment's ListView according to Tab's selected position.
If that's the case, you should not modify Fragment's ListView in Activity. Instead, pass Tab's selected position to Fragment as an argument from Activity and load ListView in Fragment according to that position.

How to save the changes permanently in fxml file while adding menuItems through code

The following piece of code works good as long as the window is open. Menuitem added through code is visible in dropdown as expected
#FXML
public static MenuButton youradd;
#FXML
public void static addingMenuItem()
{
yourAdd.getItems().addAll(new MenuItem(newName));
}
But the issue is , when the particular stage or window is closed, the added menuitem disappers in the dropdown. I want the menuitems added to be stored permanently in the fxml code so that the menuitems are visible everytime i run the stage
NOTE: MenuButton has been created through scenebuilder. but adding MenuItems through code

Resources