Categories
discuss

Android: How to create popup with choices

I want to do something like this: user clicks on a button “Choose color”, and a simple popup with e.g. 5 colors appears. I could do this with PopupWindow and inner ListView, but is there a simpler solution, like a specific widget for this?

How it should look like: alt text

Answer

Use AlertDialog.Builder and supply an Adapter via setAdapter() that generates your rows.

Categories
discuss

How to determine when fancybox is open?

I need to know that fancybox has been open up that to allow or deny another function to start.

Build-in functions Fancybox like ‘onStart’ or ‘onClosed’ not work.

I’m talking about version 1.3.0 RC2

Answer

The version your using apparently doesn’t match the documentation; I looked at the source and saw the names of the options were different the the online docs. I just tested the following with 1.3RC2:

$(document).ready(function() {

    function myStartFunction() { alert('fancy box opened'); }

    $("a#inline").fancybox({
        'onStart': myStartFunction
    });  
});

The option you’re looking for is ‘onStart’ – the alert in myStartFunction goes off every time I open a box. I’m not sure when they changed the option but you can look at the source of any version you use at the bottom, and see what the option should be called.

EDIT

I just double checked on v1.2.5 – the version I use – and the callbacks are indeed named different. callbackOnStart works with 1.25 but, as I said, not 1.3

Categories
discuss

How to diagnose File.delete() returning false / find unclosed streams?

I’m working with a 3rd party JPEG/EXIF manipulation library (Mediautil) that is causing me some headaches. I want to change an image’s EXIF data. To do this, I need to write the updated version to a temporary file, delete the original and then rename the temp file to the original name.

My problem is that the File.delete() call fails and returns false, presumably because the library still has it opened in some way – but I have done everything I can find in the API to make it close all streams. Even worse: the problem seems to be timing-dependant, and the Unit tests where it occurs sometimes fail and sometimes not – but the code is not multithreaded.

Bizarrely, there is one library call that removes the problem – but it also removes the EXIF thumbnail, which I don’t actually want. And looking at the code, I absolutely can’t see where it closes any streams that might otherwise stay open.

Any ideas how to attack this problem?

Edit:
This is on Windows XP, Java 6. And another thing: I have found out that if I call System.gc() before calling File.delete(), it works – presumably because that triggers some finalizer. So it definitely seems to be an unclosed stream.

Answer

I would go for some help with the debugger here. A quick dig through the java.io stuff shows that the only likely finalize() candidate is in FileOutputStream. So slap a breakpoint in there, run your program, and try and get System.gc() to trigger FileOutputStream.finalize() to release your stream. That should give you an answer as to whether or not that’s your problem.

Once you can reproduce that, then you need to start matching instantiation of FileOutputStream instances with their finalization. A good debugger will give you internal JVM object identifiers for each object, so if you can track the OIDs as they get created, and track them as they get finalized, then hopefully you’ll be able to associate the key call to finalize with a specific call new new FileOutputStream.

Might be a long slog, though, depending how complex your application is.

Categories
discuss

platform-independent /dev/null output sink for Java

Other than an anonymous class (new OutputStream() { ... }), can anyone suggest a moral equivalent of new FileOutputStream("/dev/null") that also works on Windows?

In case someone’s wondering ‘what’s this for?’

I have a program that does a consistency analysis on a file. It has a ‘verbose’ option. When the verbose option is on, I want to see a lot of output. The program is not in a hurry, it’s a tool, so instead of writing all those extra if statements to test if I want the output, I just want to write it to the bit-bucket when not desired.

Answer

You can use NullOutputStream from apache commons https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/output/NullOutputStream.html

Or just implement your own

package mypackage;

import java.io.OutputStream;
import java.io.IOException;

public class NullOutputStream extends OutputStream {
    public void write(int i) throws IOException {
        //do nothing
    }
}
Categories
discuss

How do I use obtainStyledAttributes(int []) with internal Themes of Android

So I have looked around and found out that android.R.styleable is no longer part of the SDK even though it is still documented here.

That wouldn’t really be an issue if it was clearly documented what the alternative is. For example the AOSP Calendar App is still using the android.R.styleable

// Get the dim amount from the theme   
TypedArray a = obtainStyledAttributes(com.android.internal.R.styleable.Theme);
lp.dimAmount = a.getFloat(android.R.styleable.Theme_backgroundDimAmount, 0.5f);
a.recycle();

So how would one get the backgroundDimAmount without getting the int[] from android.R.styleable.Theme?

What do I have to stick into obtainStyledAttributes(int []) in order to make it work with the SDK?

Answer

The CustomView API demo shows how to retrieve styled attributes. The code for the view is here:

https://github.com/android/platform_development/blob/master/samples/ApiDemos/src/com/example/android/apis/view/LabelView.java

The styleable array used to retrieve the text, color, and size is defined in the <declare-styleable> section here:

https://github.com/android/platform_development/blob/master/samples/ApiDemos/res/values/attrs.xml#L24

You can use <declare-styleable> to define any list of attributes that you want to retrieve as a group, containing both your own and ones defined by the platform.

As far as these things being in the documentation, there is a lot of java doc around the styleable arrays that makes them useful to have in the documentation, so they have been left there. However as the arrays change, such as new attributes being added, the values of the constants can change, so the platform ones can not be in the SDK (and please do not use any tricks to try to access them). There should be no need to use the platform ones anyway, because they are each there just for the implementation of parts of the framework, and it is trivial to create your own as shown here.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..