Hi I am trying to set focus on an item in a listview. After a user opens a file the item is added to the listview, but the issue I am having is that the listview is not setting focus on the new item that was added. I have to click the item in the listview to set focus to it. Is there a way to have the listview to highlight the newly added item right away in JavaFX 2.1 .
Assuming that the newly added item has an index of N,
Selecting it:
listView.getSelectionModel().select(N);
Focusing on it:
listView.getFocusModel().focus(N);
Scrolling to it:
listView.scrollTo(N);
You can use combinations of these and preferably in Platform.runLater().
Scroll then select:
Platform.runLater(new Runnable() {
#Override
public void run() {
listView.scrollTo(N);
listView.getSelectionModel().select(N);
}
});
Related
I am trying to create a like and dislike button. I use Checkboxes to do so.
In the XML code I have two checkboxes one called like and the other dislike
I'm trying to toggle between the like and dislike buttons. Such that they both cannot be switched on at the same time.
public void onLike(View view) {
if (dislike.isChecked()) {
dislike.setChecked(false);
}
Toast.makeText(this,"liked",Toast.LENGTH_SHORT).show();
}
The issue that I am having is that set setChecked(true) is not doing anything.
For more context, the XML for the checkbox is defined inside a fragment that has a cardview. Each item in the card view has the checkboxes.
the way I initialized the checkbox in the main activity is as follows: -
View cardViewLayout = getLayoutInflater().inflate(R.layout.text_row_item,null);
like = (CheckBox) cardViewLayout.findViewById(R.id.like);
dislike = (CheckBox) cardViewLayout.findViewById(R.id.dislike);
any ideas what's going on?
ok, I've figured out the solution. Since I am using a recycler view with a custom adapter I need to bind the onClick listener via an interface.
Here is a link to another post that will show the necessary steps to implement click listeners in adapters: https://stackoverflow.com/a/49969478/11379938
I am developing an Android application that has a ListView containing CardViews populated using a custom ArrayAdapter .
The CardViews have an expandable area that becomes visible on click by sliding downward .
However when i view the last CardView that is visible on the screen i can only see the non-expandable part of the CardView i.e. the expandable can only be viewed if i scroll downward myself.
I have seen this type of feature before where the layout automatically adjusts by scrolling downward to include the expandable part .
I have not been able to implement this myself .
How can i achieve this functionality ?
if you are using scrollview as wrapper over your content inside CardView. You can use
mScrollView.post(new Runnable() {
public void run() {
mScrollView.scrollTo(0, mScrollView.getBottom());
}
});
or
mScrollView.post(new Runnable() {
public void run() {
mScrollView.fullScroll(mScrollView.FOCUS_DOWN);
}
});
above two codes are from question Can I scroll a ScrollView programmatically in Android?
also try
scrollView.scrollTo(0, TextView.getBottom());
where textview is at bottom of your cardview
else you can try using
yourview.fullscroll(yourview.FOCUS_DOWN);
if thats even possible
In my JAVAFX application, i am having imageviews in a tilepane , i want to implement the multi select like functionality like in android for images . I tried adding border style to the imageView on click event but that didnt work. Is there any way to achieve this.
You can embed the Image in a JavaFX Button, and set the OnAction methods of the Button:
imageButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
System.out.println("Changing the color of the button's border :");
imageButton.setStyle("-fx-border-color:blue;");
System.out.println("For further reference, you can save the button or the image in a TreeSet:");
treeSet.add(imageButton);
}
});
If a simple click is enough to select the image, you can define the OnAction method of the Button like above. However if you need a long click (push and hold in Android style) to change the selection status of the image, you can find more information on 'push and hold' click here : how to achieve javafx mouse event "push and hold"? .
My application has an intro page made up of some text and image elements in a relative layout. I would like to be able to click any part of the screen and have it go to the next activity. Is it possible to use a entire relative layout as a button? If so how would you do this?
You can add android:clickable="true" to the XML for your RelativeLayout and use a standard OnClickListener as you would for a button.
Depending on what you're trying to do (perhaps touching anywhere to dismiss a screen?), you could also look into extending onTouchEvent(MotionEvent event) in your Activity, which would pick up any touches in the entire activity that were not responded to by views.
You can grab the root view as follows and add a click listener to it:
findViewById(android.R.id.content).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//make your call to startActivity();
}
});
This should require less maintenance than retrieving a specific layout.
Add android:onClick="myFunction" in the RelativeLayout of the XML file and make the following function in the corresponding Activity file:
public void myFunction(View view)
{
...
}
I think you will have to add android:onClick="myFunction" for all the nested XML tags too which are nested inside the main RelativeLayout.
I want to create my own context menu.
When a user clicks the BlackBerry menu button, the menu should open with only my menu items -- and not the 'Hide keyboard' and 'Switch Application' items that are included by default.
protected void makeMenu(Menu menu, int instance) {
menu.deleteAll();
// add your code here
}
Try this out