How to I fix the background image to line up? (Blackberry) - java

I used some code that I found from another question. I commented out the setPositionChild() part because I wasn't sure what values to put for positionY and positionX. When I run the app, the background image is on the top and the buttons that I add to the manager later are all pushed together at the bottom of the image.
Background bg = BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("Background.JPG"));
Bitmap bmp = Bitmap.getBitmapResource("Background.JPG");
BitmapField imgField = new BitmapField(bmp);
// Create the field manager
VerticalFieldManager manager = new VerticalFieldManager()
{
// Overide the sublayout of the field manager to set the position of
// the image directly
/* protected void sublayout(int width, int height)
{
setPositionChild(imgField, positionX, positionY)
setExtent(width, height)
}*/
};
// Set the background of the field manager
manager.setBackground(bg);
// add the bitmap field to the field manager
manager.add(imgField);
// add the field manager to the screen
add(manager);

Please mention what you are trying to achieve here. What's your expected behavior? Looking at code, the child fields should be positioned as you have mentioned when you run app. Since you are adding BitmapField (imageField) to manager and then probably adding buttons(not shown in code, assuming you are adding it somewhere else in code) to the manager. So provide more details on what you want to achieve?

Related

How do I change multiple UI items simultaneously from one button's listener (Java)? I need to modify an object in between changing UI

I'm working in Android Studio using Java. I'm using madrapps/pikolo (github) and fornewid/neumorphism (github). I think this question can be answered without knowledge of these 2 dependencies since I've had the same problem in Java with button listeners.
Background info:
Here's a picture of the UI element I'm trying to manipulate (called btnNeuTest):
Neumorphic button... basically a button with a shadow color and highlight color.
setShadowColorLight changes the white highlight (top left), and setShadowColorDark changes the grey shadow (bottom right)
madrapps/Pikolo is a color picker UI element. I have a ColorSelectionListener that executes lines of code just like a button's onClick listener (below). I need the code within onColorSelected() to occur simultaneously whenever onColorSelected() is called:
colorPicker.setColorSelectionListener(new SimpleColorSelectionListener() {
#Override
public void onColorSelected(int color) {
// works like onClick()
// 'color' is the currently selected color from the color picker
// sets the fill color of an ImageView (a circle)
// this line works
currentColor.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
// sets background color of the button
// this line also works
btnNeuTest.setBackgroundColor(color);
// sets the shadow and highlight colors of the button
// the 6 lines below don't seem to work
float[] hsl0 = new float[3];
ColorUtils.colorToHSL(color, hsl0);
float[] hsl1 = new float[]{hsl0[0], hsl0[1], hsl0[2]-15f}; // makes 'color' darker
btnNeuTest.setShadowColorDark(HSLToColor(hsl1)); // sets shadow color of button
float[] hsl2 = new float[]{hsl0[0], hsl0[1], hsl0[2]+15f}; // makes 'color' lighter
btnNeuTest.setShadowColorLight(HSLToColor(hsl2)); // sets highlight color of button
}
});
I have tried the following and it worked as intended (all colors were set simultaneously), although it's not exactly what I'm trying to do:
colorPicker.setColorSelectionListener(new SimpleColorSelectionListener() {
#Override
public void onColorSelected(int color) {
currentColor.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
btnNeuTest.setShadowColorDark(color);
btnNeuTest.setBackgroundColor(color);
btnNeuTest.setShadowColorLight(color);
The issue is, I need to modify 'color' before executing setShadowColorDark() or setShadowColorLight(), and it needs to be modified each time that onColorSelected() is called.
I also tried executing most of the changes within a separate method, and it did the same thing as the first block of code in this question:
colorPicker.setColorSelectionListener(new SimpleColorSelectionListener() {
#Override
public void onColorSelected(int color) {
currentColor.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
setBtnNeuColors(color);
}
});
// new method created in the same class as the listener
public void setBtnNeuColors(int color) {
btnNeuTest.setBackgroundColor(color);
float[] hsl0 = new float[3];
ColorUtils.colorToHSL(color, hsl0);
float[] hsl1 = new float[]{hsl0[0], hsl0[1], hsl0[2]-15f}; //darkens color
btnNeuTest.setShadowColorDark(HSLToColor(hsl1));
float[] hsl2 = new float[]{hsl0[0], hsl0[1], hsl0[2]+15f}; //lightens color
btnNeuTest.setShadowColorLight(HSLToColor(hsl2));
}
I also tried moving 'float[] hsl0 = new float[3];' to the top of the class so it's only done once, but it didn't solve the issue.
So, I have only been able to change all of the colors I needed to change when I don't modify them first (the lines with the float[]). How can I execute all of the lines in the original code block each time onColorSelected() is called?

Android: make dynamically added imageviews full screen on click

This is an often asked question but I just can't find the right solution for my case. I'm working on a news app and I want to fullscreen images in the news content when the user clicks the image, and another click will return to the news page. In my code, I have an ArrayList of data with the type of image or text, this method will go through the ArrayList and dynamically add an image view or text view according to the type. The code is like
public void setText(ArrayList<HashMap<String, String>> datas) {
for (HashMap<String, String> hashMap : datas) {
String type = hashMap.get("type");
if (type.equals("image")) {
int imagewidth = mTypedArray.getDimensionPixelOffset(R.styleable.customTextView_image_width, 100);
int imageheight = mTypedArray.getDimensionPixelOffset(R.styleable.customTextView_image_height, 100);
imageView = new ImageView(mContext);
params = new LayoutParams(imagewidth, imageheight);
params.gravity = Gravity.CENTER_HORIZONTAL;
imageView.setLayoutParams(params);
addView(imageView);
DownloadPicTask task = new DownloadPicTask(imageView, handler);
task.execute(hashMap.get("value"));
} else {
//Add some text view
}
I have tried simply changing the image to FIT_XY on click, but only the last spawned image enlarged, and I don't want other content to still remain around the image.
Perhaps I should set an onclick listener to each image, then create a new fullscreen activity? But I'm new to android and I don't know how to pass the image data correctly to fullscreen activity, and how to make another click back to the original content.

GWT get old and new window size

I want old and new height, width of window on window re-size event.
How I can do that, I am just getting new height and width on window re-size.
Thanks in advance
There is no way to get old dimensions in the ResizeEvent received when the window size changes. But you can save old values attributes inside your ResizeHandler.
Window.addResizeHandler(new ResizeHandler() {
// Save old dimensions
int oldW = Window.getClientWidth(), oldH = Window.getClientHeight();
public void onResize(ResizeEvent ev) {
// Get new dimensions
int newW = ev.getWidth(), newH = ev.getHeight();
// Do something with old and new dimensions
myResizeMethod(oldW, newW, oldH, newH);
// Update old dimensions
oldW = newW; oldH = newH;
}
});
Additional info: Window class tracks old sizes in order to fire the resize event only in case new values are different to old ones, so it will be ease to add those values to the ResizeEvent object. You can either request or send a patch to GWT with this feature.

Setting height, and width of a relative layout in android

Here is my scenario.
I have an image which is made as per resolution 320*480 (assuming i am using mdpi mobile). I am adding this image as a background to a relative layout.
Now my screen has three main things.
1- Title bar (The standard title bar of android)
2- My relative layout and its sub view image relative layout which is matched parent.
3- My menu bar at the bottom.
Now since my image is getting stretched in between menu bar and title bar. I want to make it fit the screen. Below is the code I am using.
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout()
{
if (notCreated == true)
{
notCreated = false;
}
else
{
mnHeight = mainLayout.getHeight();
Rect rect = getRawCoordinatesRect(mainLayout);
h = getWindowManager().getDefaultDisplay().getHeight() -
- rect.top - mainLayout.getHeight();
// rect.top to remove android standard title bar
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(image.getWidth(),LayoutParams.MATCH_PARENT);
params.topMargin = -1 * rect.top;
params.bottomMargin = -1 * h;
image.setLayoutParams(params);
}
}
});
Now this is not working. A little help would be appreciated.
Note: Actually I just want to stretch my image relative layout to 320*480. Right now it is sandwitched between title bar and my menu bar. E.g right now its dimensions are 320*400
Not sure if it will help, but try to play with setScaleType() trying different options.
image.setScaleType(ScaleType.FIT_CENTER);
Here are listed all the constans the ScaleType class has.
I must be honest and say i'm not completely sure what you want to do here. But i'm going to assume that you want to make the image function as a background..
If that's what you want, you can do this simply by setting the android:background="#drawable/your_image" attribute on the main layout in your layout file.
If what you want is remove the titlebar, this is done pretty simple by adding this line to your onCreate method in your activity class:
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
If what you want to do is remove the notificationbar, this is also done in the onCreate method in your activity class:
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
I hope this can help you :-) If it's not what you're looking for, then dont hesitate from trying to reformulate what you want.

Make JCheckbox bigger..?

i want to make my JCheckboxes in a JTable bigger (for Touchscreen), but it doesn't change the size.
I tried it with
setPrefferedSize
setSize
What should I do?..
I assume you mean you want a bigger check box. If so then you need to create images to represent the unselected and selected icons of the check box. Then you can create a renderer and editor using these icons. Finally you would need to increase the height of each row in the table. The code might look something like:
Icon normal = new ImageIcon(...);
Icon selected = new ImageIcon(...);
JTable table = new JTable(...);
table.setRowHeight(...);
TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class);
JCheckBox checkBoxRenderer = (JCheckBox)renderer;
checkBoxRenderer.setIcon( normal );
checkBoxRenderer.setSelectedIcon( selected );
DefaultCellEditor editor = (DefaultCellEditor)table.getDefaultEditor(Boolean.class);
JCheckBox checkBoxEditor = (JCheckBox)editor.getComponent();
checkBoxEditor.setIcon( normal );
checkBoxEditor.setSelectedIcon( selected );
IMPORTANT NOTE: This was only tested with the default 'Metal' look and feel. I do not guarantee that this will work for any other look and feel. Also I am not entirely sure how it works because it is admittedly a bit of a hack.
I was able to solve this in a slightly different way.
I wanted to use the existing images and just apply a scale to it. I am already scaling the font of my application using the UI defaults and so I have a rather large font. I wondered if I could leverage that and scale the check boxes accordingly.
After scouring the internet and trying a bunch of things I came up with this method:
public static void scaleCheckBoxIcon(JCheckBox checkbox){
boolean previousState = checkbox.isSelected();
checkbox.setSelected(false);
FontMetrics boxFontMetrics = checkbox.getFontMetrics(checkbox.getFont());
Icon boxIcon = UIManager.getIcon("CheckBox.icon");
BufferedImage boxImage = new BufferedImage(
boxIcon.getIconWidth(), boxIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB
);
Graphics graphics = boxImage.createGraphics();
try{
boxIcon.paintIcon(checkbox, graphics, 0, 0);
}finally{
graphics.dispose();
}
ImageIcon newBoxImage = new ImageIcon(boxImage);
Image finalBoxImage = newBoxImage.getImage().getScaledInstance(
boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
);
checkbox.setIcon(new ImageIcon(finalBoxImage));
checkbox.setSelected(true);
Icon checkedBoxIcon = UIManager.getIcon("CheckBox.icon");
BufferedImage checkedBoxImage = new BufferedImage(
checkedBoxIcon.getIconWidth(), checkedBoxIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB
);
Graphics checkedGraphics = checkedBoxImage.createGraphics();
try{
checkedBoxIcon.paintIcon(checkbox, checkedGraphics, 0, 0);
}finally{
checkedGraphics.dispose();
}
ImageIcon newCheckedBoxImage = new ImageIcon(checkedBoxImage);
Image finalCheckedBoxImage = newCheckedBoxImage.getImage().getScaledInstance(
boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
);
checkbox.setSelectedIcon(new ImageIcon(finalCheckedBoxImage));
checkbox.setSelected(false);
checkbox.setSelected(previousState);
}
What it does is get the size of the font from the checkbox's font metrics. Then using that it derives a new icon based on the icon found in the 'Look and Feel'.
One odd thing that I am not able to explain is how the icon for the checkbox in its 'un-selected' or default state, changes to the 'selected' icon, when I am accessing the same property to get each one.
I start by saving the state of the control so I can restore it at the end. This is done because in order for the icons to be set properly, the state needs to be unchecked when you first request the icon from the UIManager and then it will need to be checked when you request the icon the second time to get the 'selected' icon.
I am not entirely sure how the UIManager works or why the checkbox icon changes when we call the same property just by setting the 'selected' value of a single checkbox, but that is what is required in order to get both the necessary icons.
If you did not want to base the size on the font you could easily just pass in the height and width as parameters and use them instead of the font's height when setting the buffered image size.
I might mention that this same methodology works with radiobuttons

Categories

Resources