Categories
discuss

View on press onpress: Change background color on press? How do I show that the View is being pressed?

I have, for the time being, a custom view with a 9-patch image as a border.

That custom view is placed three times in a LinearLayout, so it looks like this:

+------------------------+
|  CustomView            |
+------------------------+
|  CustomView            |
+------------------------+
|  CustomView            |
+------------------------+

I have attached a click event listener to the View, so it is clickable. But then I click it, I can’t see that I am clicking it – there is no change in color.

So, I’m thought that I’d attach a “onPress” listener, and then change the background of the view but I couldn’t find such a listener.

So, how do I create the behaviour on the View so I can see that it is being pressed? this is normally done in Android with a green background to indicate that it is now being pressed.

Answer

You could set the OnClickListener for the view. That will be called when the view is clicked. But for something as simple as changing the background when a view is clicked you should use a stateful drawable. They work like this, you make 3 9-patch images.

  1. is the normal background like what you have now.
  2. is what the background should look like when the user selects the view with the track-ball / d-pad
  3. is what the view should look like when the user clicks on it

Then you create an new xml file in your drawable folder. It should look like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_focused="true"
    android:state_pressed="true"
    android:drawable="@drawable/background_pressed" />
<item
    android:state_focused="true"
    android:state_pressed="false"
    android:drawable="@drawable/background_focused" />
<item
    android:state_focused="false"
    android:state_pressed="true"
    android:drawable="@drawable/background_pressed" />
<item
    android:drawable="@drawable/background_normal" />

Then, when you set the background of your view set it to the xml file.

Categories
discuss

Implementing the singleton pattern in Java

Can anyone provide an example of a singleton pattern and explain why they are necessary?

Answer

Before going the singleton route, reconsider. Do you really need a singleton? If you’re asking for scenarios when you need to implement singletons, it’s because the need for them didn’t really express. You’re better off not introducing singletons in your code base just because it feels cool to follow design patterns.

Clean Code Talks – Global State and Singletons

Once Is Not Enough

Performant Singletons

However, what’s really worth knowing is Dependency Injection.

Now if you really want to implement singletons in Java, I would recommend Joshua Bloch’s “Effective Java” way of implementing them:

public class Singleton
{
  public static Singleton getInstance() {
    return SingletonHolder.instance;
  }

  private Singleton() {}

  private static final class SingletonHolder {
    static final Singleton instance = new Singleton();    
  }
}

The JLS guarantees the JVM will not initialize instance until someone calls getInstance();

Final note, the Double Checked Locking Pattern is broken in Java up to Java 5. The Java 5 memory model makes the DCL pattern thread safe but it makes it slower than the SingletonHolder class method while the original intent was performance optimization.

EDIT: As @Luno pointed out, since the second edition of the book, the preferred way is:

As of release 1.5, there is a third approach to implementing singletons. Simply make an enum type with one element:

// Enum singleton - the preferred approach
public enum Elvis {
    INSTANCE;

    public void leaveTheBuilding() { ... }
}

This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.

Categories
discuss

How to detect if the user clicked the “back” button

When the user goes history-back-1…how do I detect that? And then, alert “the user clicked back!”

Using binds (and jQuery preferably)

Answer

You generally can’t (browser security restriction). You can tell if the user navigates away from the page (onbeforeunload, onunload fire) but you can’t tell where they went unless you’ve set up your page to allow it.

HTML5 introduces the HTML5 History API; in conforming browsers, the onpopstate event will fire if the user navigates back to an earlier “page” on your site.

Categories
discuss

Best way to use a PostgreSQL database as a simple key value store

I am being required to use a postgreSQL database and it will replace my current use of berkeleyDB. Although; I realize this is not an ideal situation, it is beyond my control.

So the question is… If you were required to make postgreSQL into a key value store how would you go about doing this, while making it as efficient as possible?

My values are byte arrays and my key’s are strings, I could impose some restrictions on the lengths of these strings.

I assume I should use a blob for my value and primary key column holding the key, but as I am just venturing into this journey I am curious if anyone in the stack overflow community has done this, or if there are any specific ‘gotchas’ I should look out for.

Answer

The extension in Postgresql to properly do this is called hstore. It works in a similar fashion as you would expect other key-value store systems. Just load the extension. The syntax is unique but if you have ever used redis or mongo you will get it quickly. Don’t make it harder than it is. I understand, we often don’t get to pick our tools and have to make do.
Here is the document page:

http://www.postgresql.org/docs/9.1/static/hstore.html

Categories
discuss

Is there something like LINQ for Java? [closed]

Started to learn LINQ with C#.
Especially LINQ to Objects and LINQ to XML.
I really enjoy the power of LINQ.

I learned that there is something called JLINQ a JavaScript implementation.
Also (as Catbert posted) Scala will have LINQ

Do you know if LINQ or something similar will be a part of Java 7?

Update: Interesting post from 2008 – LINQ for Java tool

Answer

Look at Scala, which is powerful functional programming language, but is similar to Java and runs on Java platform.

In Scala it is possible to use essentially the same code constructs as in LINQ, albeit without special query comprehensions syntax present in C# or VB.

EDIT :

Here’s an example of Scala’s querying capabilities :

// Get all StackOverflow users with more than 20,000 reputation points.
val topUsers = for{
    u <- users
    if u.reputation > 20000
} yield u;

println ("Users with more than 20,000 reputation:")
for (u <- topUsers) {
    println u.name
}
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..