Android Expandable ListView Icon (Group Indicator) - java

I have been able to successfully swap in my own image for the expandable listview arrows. I have two issues at the moment.
One is that the icons i substitue in with the code below are each strechted to the height of the row and too wide as well. The second is, that this code is only changing the initial state of the Group Indicator. How do I change it from one image to another when a particular row is open?
Drawable plus = (Drawable) getResources().getDrawable(R.drawable.plus);
getExpandableListView().setGroupIndicator(plus);

Android's default behavior is to stretch the groupIndicator icon. It's a ninepatch, so if you don't want it to stretch the image, you have to make the stretchable area of your ninepatch the transparent regions on both sides of the icon.
To make it change appropriately when the ExpandableListView opens, you have to use a StateListDrawable. Add the appropriate Drawables for the following states:
int[][] states = new int[][] {
new int[] { android.R.attr.state_expanded },
new int[] { },
};
You do that like so:
StateListDrawable sld = new StateListDrawable();
sld.addState(states[0], YOUR_EXPANDED_DRAWABLE);
sld.addState(states[1], YOUR_UNEXPANDED_DRAWABLE);
And set that to the groupIndicator.
Hope that helps.

What you could do, is onCollapse and onExpand change the group indicator drawable. Source

Related

How to define a clickable area in an image used in ImageView

Is there a way to define a clickable area in an image? Something equivalent or similar to map-area tags in HTML? Or is there a standard way of doing this sort of thing?
A possible method
1) get the width, and height of the image view using (in pixels)
2) get the coordinates of the image view (which is the top left corner of the image view)
3) create a touch listener, and record the coordinates of the users touch
4) if the user's touch coordinates fall on the image do whatever you want to
If you only want to do certain things based on where the user touches you will have to calculate the coordinates of those regions using the coordinates of the image view, and the image views width and height.
I think there might be no standard way for this, but you can achieve it by placing a transparent VIEW over your imageview. Set click listener to that view and make imageview clickable false.
When i have to do something similar, i use an include view.
As for the layout the include view will be accessing, i would use FrameLayout as root.
Its children would be the ImageView that i want to depict, along with a LinearLayout infront of it containing Buttons at the positions i want the user to choose from.
Then in code, i set listeners to these buttons.
Lastly, i would set the buttons so that there background is transparent and text is not available.
For example in a project i am working on, i have an ImageView with an Image of Ocean as background.
In front of the view i have a LinearLayout with smaller ImageViews with images of islands.
I place listener on the islands to see when they are clicked to open a new activity.

Display the modified image of an ImageView object

I've been using an ImageView object to try and modify an Image and then draw it. However I could only make the two following outcomes happen:
1) After ImageView has been modified, I tried to grab the image out of it for display with getImage() but that only displayed the original Image I had in the first place.
example code:
sheet = new Image("file:sprite_sheet.png");
ImageView iv = new ImageView();
iv.setImage(sheet);
iv.setViewport(new Rectangle2D(0, 0, 32, 32));
g.drawImage(iv.getImage(), 100, 100);//'g' is GraphicsContext2D
2) I managed to get the modified image to show by adding it to the Group obj like so:
root.getChildren().add(iv);
But then it only displayed the image in the top left corner of the screen and I couldn't move it or do anything with it.
I prefer (if possible) to approach this issue with a method more like the first outcome has presented. And just saying this in advance, I'm aware of that that there's another post similar to this atm, but I couldn't get anything useful out of it.
ImageView, as the name implies, is just a view of the data (image in this case). All modifications to the view only affect the view and not the data. There are two general methods for what you want to achieve.
Use scene graph and ImageView object. Perform all visual operations on the view. This is the preferred way as the data stays the same, meaning you can easily create a new ImageView with the original image and modify it in a different way should you wish to. If the parent is Pane, then you can easily move the object by calling setTranslateX/Y.
If you absolutely need to use Canvas, then you will need to modify the data itself or create a copy of the data and modify it instead. You can do this by using a combination of the following: PixelWriter and PixelReader
Example for 1.:
ImageView view = new ImageView(image);
// create root with dimension 800x600
Pane root = new Pane();
root.setPrefSize(800, 600);
// attach view to root so it can be displayed
root.getChildren().add(view);
// translate view to 100, 100
// on the screen this means the image will move 100 in X and 100 in Y
view.setTranslateX(100);
view.setTranslateY(100);

ObjectAnimator Choppy Scrolling

I have an activity that contains a HorizontalScrollView. The HorizontalScrollView has three fragments in it. On a button click the scrollview will scroll from left to right or vice versa. I am accomplishing this like so...
private void scrollTo(int x) {
ObjectAnimator animator = ObjectAnimator.ofInt(mScroller, "scrollX", x);
animator.setDuration(800);
animator.start();
}
On my Nexus 7 (1st Gen) it scrolls smoothly, but on my Moto G phone, it is super choppy. Any ideas how I could correct this?
Update
I figured out that if I changed the background drawable on one of the fragments the problem will go away. The image I was using is very plain, so I never thought that would be the problem. The image I replaced it with is much larger in size, but is just a repeated pattern.
I set the background with a .png like this
android:background="#drawable/my_background"
Old Background (non working)
New Background (works)
It ended up being my background image. I used a 9 patch tool located here to generate different size images and all is well now.
http://android-ui-utils.googlecode.com/hg/asset-studio/dist/nine-patches.html

Drag-Drop Android Launcher / GridView Issue - Android / Java

I have a gridView I've created from an online example/tutorial and I'm trying to figure out how to change the behavior when a new item is dragged over a previously existing item in the GridView. Currently when a new item is dragged over the gridView it removes the current icon in place (leaving it with the red empty square [icon.png is a grid of empty squares the code uses as a background image])
I've poured through the example's source quite a few times and I can't figure out how to change the behavior of what happens when one item is dragged over the other
Screenshot:
Source code:
https://drive.google.com/file/d/0B6jCh_IJdtoFYWFJMlk5MHhlX3c/edit?usp=sharing
P.S.
I believe the issue may be in either the DragView or DropTarget class. (feel free to download the entire project - I've made the entire thing available for an easy download)
Look at methods onDragEnter and onDragExit in ImageCell. Those two methods are the ones called as your finger passes over a cell on the grid. They set the background of the image view.
onDragEnter:
int bg = mEmpty ? R.color.cell_empty_hover : R.color.cell_filled_hover;
setBackgroundResource (bg);
onDragExit:
int bg = mEmpty ? R.color.cell_empty : R.color.cell_filled;
setBackgroundResource (bg);
The color definitions are in mycolor.xml.
Reference: http://blahti.wordpress.com/2012/03/03/improved-drag-drop-for-gridview/

Libgdx ImageButton padding?

so I have this problem with ImageButton class of libgdx, the problem is that in android the image doesnt expand to the full size of the button, and in desktop it does or at least it does more, so I have to ask if theres a way to force the image to get the size of the background(button)? so i can try to make an equal visualitation on both plataforms.
heres a screen shot, the back space button is the ImageButton...
edit: heres the code....
private void createButtons() {
ImageButtonStyle ibs = new ImageButtonStyle(buttonD,buttonD_p,buttonD,bsIcon,bsIcon,bsIcon);
buttonBS = new ImageButton(ibs); // This is the backspace button
....
}
private void addButtonsToTable() {
float pad = 1;
float BUTTON_SIZE = this.BUTTON_SIZE - pad *3;
table.top();
table.center();
table.add(buttonBS).width(BUTTON_SIZE).height(BUTTON_SIZE).pad(pad);
table.row();
...
}
If you want the image to be the background instead of just an icon, you should consider using plain Button or TextButton instead of ImageButton. ImageButton should be used only for buttons that draw an icon additionally to its background. An example of ImageButton usage could be the window closing button with the "X" (cross) image, or music toggle button with a loudspeaker icon.
When you need the image to fill the whole button area, set it as ButtonStyle#up - it will become button's background. ImageButton#imageUp is just an icon that will not be scaled in any way (by default), so that might be the reason why your application behaves differently on each platform.
(Although it still shouldn't, unless you use different assets.)
If you need an icon and still want to use ImageButton, consider that internally it is just a Button with an Image instance added to one of its cells (Button is a Table). You can use ImageButton#getImageCell() to access Cell in which the Image is stored and modify it - for example, force a specific width and height. You might also want to use ImageButton#getImage() to change scaling with Image#setScaling(Scaling).
Anyway, creating styles at runtime can be error-prone - the style constructor is huge and I'm honestly unable to guess which drawable draws what without checking out image button style sources. You should consider using Skin and define your styles with JSON files (you can find some free themes here).

Categories

Resources