Categories
discuss

getLayoutParams returning null?

I’ve created a class that extends a View:

public class BoardView extends View  {

and I’ve specified the width and height of the BoardView in the application’s main.xml file:

  <mypackage.BoardView
        android:id="@+id/board"         
        android:layout_width="270px" 
        android:layout_height="270px" /> 

I’m trying to get the width and height from a function that is called from the BoardView’s constructor. Here’s what I’m doing:

ViewGroup.LayoutParams p = this.getLayoutParams();
int h = p.height;

but getLayoutParams is always returning null. Any idea why this isn’t working?

Answer

I am not sure that layout parameters(i.e. instance of LayoutParams) will be available inside constructor of the View. I think, it will only be available after “layout” passes have been made. Read about How Android draws views here. Also, this thread tries to pin point when exactly should you expect to get Measured dimensions of a View.

Note that if you are interested in just getting the attribute values passed via layout XML, you can use AttributeSet instance passed as an argument to your constructor.

public MyView(Context context, AttributeSet attrs){
// attrs.getAttributeCount();
// attrs.getAttributeXXXvalue();
}
Categories
discuss

Is Java’s RNG (using seeds) platform-independent?

Apologies in advance for asking a (seemingly obvious) question. I haven’t found an answer online, so I figured I’d ask:

Is Java’s Util.Random platform-independent?

For Example, is new Random(50) going to produce the exact same sequence of random numbers in both *nix and Windows systems?

Answer

From the docs:

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code.

Categories
discuss

Reseting the form when usering the jquery validations plugin

I have a simple form like below that I have added the jQuery validations plugin onto (http://docs.jquery.com/Plugins/Validation). I have this form in a modal popup window so if there are errors and the user closes the window when they open it again the form still has the errors. In my popup close callback I tried calling resetForm() but it says the method doesn’t exist.

Form HTML:

 <form class="validations" id="commentForm" method="get" action="">
   <p>
     <label for="name">Name</label>
     <em>*</em><input id="name" name="name" size="25" class="required" minlength="2" />
   </p>
   <p>
     <label for="email">E-Mail</label>
     <em>*</em><input id="email" name="email" size="25"  class="required email" />
   </p>
 </form>

Popup Close Callback:

function(){
  $(this).find('form.validations').resetForm();
}

Thanks in advance for the help.

Answer

resetForm is part of the object returned by the validate method, not the form. Example:

var validate = $('#commentForm').validate({ ... });
// Later...
validate.resetForm();
// Or if variable scope is in the way...
$('#commentForm').data('validator').resetForm();

The validation plugin stores a reference to the validation object in the form’s data store.

Categories
discuss

Want to improve math skills for programming [closed]

I have just started programming with Java and noticed I might need to improve my math skills if i’m to do anything worthwhile with it. I just noticed that some of the exercises should be easy for someone with a solid understanding in math as there seems to be some math formulae running the show. I finished high school two years ago and did not get any opportunity to do math at the highest level there. I’m deeply worried by this and so i’m looking for pointers from more experienced and knowledgeable people on what I should study to get a basic grasp of mathematics before indulging in deep waters.

I was generally a sharp student in math and I have got a real quick mind. My goal is to become a certified and well-experienced Java programmer by the end of the year. I know people will say experience takes time and effort but i’m willing to sacrifice whatever that I have towards fulfilment of this goal. I have programmed with PHP and MySQL a bit before and got a general understanding but Java gave me a real kick when I read about it. If you know any other stuff that may help me in this endeavour, please let me know.

Thanks.

Answer

I strongly encourage you to read Steve Yegge’s Math for Programmers.

If you’re looking for some books, I recommend Concrete Mathematics and The Art of Computer Programming.

If you’re looking for some problems to practice with, check out Project Euler.

Do not be discouraged by challenging problems. It’s is only through overcoming challenges that you will grow. The community here is quite willing to give you assistance and nudges when you reach a roadblock.

Let’s also not forget the famous quote:

Whatever your difficulties in mathematics, I can assure you mine are far greater.—Albert Einstein

My goal is to become a certified and well-experienced Java programmer by the end of the year.

Finally, the only way to become well-experienced is to write code, and lots of it.

Categories
discuss

How to use Android’s CacheManager?

I’m currently developing an Android application that fetches images using http requests. It would be quite swell if I could cache those images in order to improve to performance and bandwidth use.

I came across the CacheManager class in the Android reference, but I don’t really know how to use it, or what it really does.

I already scoped through this example, but I need some help understanding it:

/core/java/android/webkit/gears/ApacheHttpRequestAndroid.java

Also, the reference states:

“Network requests are provided to this component and if they can not be resolved by the cache, the HTTP headers are attached, as appropriate, to the request for revalidation of content.”

I’m not sure what this means or how it would work for me, since CacheManager’s getCacheFile accepts only a String URL and a Map containing the headers. Not sure what the attachment mentioned means.

An explanation or a simple code example would really do my day. Thanks!

Update

Here’s what I have right now. I am clearly doing it wrong, just don’t know where.

public static Bitmap getRemoteImage(String imageUrl) {
        URL aURL = null;
        URLConnection conn = null;
        Bitmap bmp = null;

        CacheResult cache_result = CacheManager.getCacheFile(imageUrl, new HashMap());

        if (cache_result == null) {
            try {
                aURL = new URL(imageUrl);
                conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();

                cache_result = new CacheManager.CacheResult();
                copyStream(is, cache_result.getOutputStream());
                CacheManager.saveCacheFile(imageUrl, cache_result);
            } catch (Exception e) {
                return null;
            }
        }

        bmp = BitmapFactory.decodeStream(cache_result.getInputStream());
        return bmp;
    }

Answer

I don’t think the CacheManger can be used outside of a WebView as noted in this bug report
http://code.google.com/p/android/issues/detail?id=7222

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..