Categories
discuss

Android tool to generate selector xml for buttons

I was wondering if anyone knows of a tool to generate xml selector files for your custom buttons. I’m getting a bit tired creating buttons, convert them to .9.png files and then copying and pasting a custom selector in xml…

I am aware of these tools:

http://www.androidicongenerator.net/

http://android-ui-utils.googlecode.com/hg/asset-studio/dist/index.html

As handy as they are in generating assets/resources, I miss the option to select 4 images and generate a ready-to-use selector xml, which outputs something like:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- pressed -->
    <item android:state_pressed="true" android:background="@drawable/img_pressed" />
    <!-- focused -->
    <item android:state_focused="true" android:background="@drawable/img_focussed" />
    <!-- default -->
    <item android:background="@drawable/img_default" />
</selector>

I am considering in creating one myself, but can’t believe no-one already did it 🙂

Thanks

Answer

inspired from this post I made a XML StateList generator, you can find it here, I would love to see if somebody have better generator.

Have a great day!

Made an update to my Android State list generator, now you can make use of all the supported states.

Categories
discuss

Java: How to check if a Field is of type java.util.Collection

I have a utility method that goes through various classes and recursively retrieves the fields. I want to check if that field is a Collection. Here is some sample code:

void myMethod(Class<?> classToCheck)

Field[] fields = classToCheck.getDeclaredFields();

for(Field field:fields)
{
   // check if field if a Collection<?>

}

Thanks in advance for the help.

Answer

if (Collection.class.isAssignableFrom(field.getType())) {

}
Categories
discuss

Fragment doesn’t call onSaveInstanceState when swiping ViewPager?

I’m having an issue with the ViewPager where my ListView is loosing it’s scroll position.

The state of the ListView can easily be stored and restored using:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{
    View v = inflater.inflate(R.layout.frag_cool_things, container, false);

    AdvListView listView = (AdvListView) v.findViewById(R.id.lv0);
    listView.setOnItemClickListener( mOnListItemClicked );

    if (null != savedInstanceState)
    {
        listView.onRestoreListViewInstanceState(savedInstanceState.getParcelable("list_state"));
    }

    mListView = listView;

    return v;
}

@Override
public void onSaveInstanceState (Bundle outState) 
{
    super.onSaveInstanceState(outState);

    outState.putParcelable("list_state", mListView.onSaveListViewInstanceState());
}

However the problem is that when fragments are being swiped onDestroyView() gets called but never calls onSaveInstanceState (Bundle outState).

Rotating the screen and such restores the ListView state just fine but swiping I can’t figure out how to restore the list properly.

Answer

Update 12/17/11:
I actually found the correct way to save the content of the Fragments. You must use FragmentStatePagerAdapter. This adapter properly saves the state of the fragment! 🙂

OLD:
Well I found a way to do this.. Please share your input if you believe this is a huge no no! 😛

Here is my FragmentBase class that fixed this issue:

public abstract class FragmentBase extends Fragment
{
    private boolean mInstanceAlreadySaved;
    private Bundle mSavedOutState;

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
    {
        if (null == savedInstanceState && null != mSavedOutState) {
            savedInstanceState = mSavedOutState;
        }

        mInstanceAlreadySaved = false;
        return onCreateViewSafe(inflater, container, savedInstanceState);
    }

    @Override
    public void onSaveInstanceState (Bundle outState) 
    {
        super.onSaveInstanceState(outState);
        mInstanceAlreadySaved = true;
    }

    @Override
    public void onStop() 
    {
        if (!mInstanceAlreadySaved)
        {
            mSavedOutState = new Bundle();
            onSaveInstanceState( mSavedOutState );
        }

        super.onStop();
    }

    public abstract View onCreateViewSafe (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
}
Categories
discuss

Turn Monitor on from Google TV App

Is there any way to turn a TV on or off [technically putting it to sleep] using a Google TV Android app using the standard Android APIs?

I understand that I could hack something together with IR or otherwise, but I’d really just like my app to perform the same function as pushing the TV Power button on my GTV remote.

Answer

Programatically toggling power for TV is not supported at this time. Please file a feature request here:
http://code.google.com/p/googletv-issues/

Categories
discuss

Java Swing JLabel, HTML and custom fonts

In our Java Swing application, we’re loading a custom font and adding it to a JLabel:

try {
  this.font = Font.createFont(Font.TRUETYPE_FONT, new File("resources/fonts/ourcoolfont.ttf")).deriveFont(16f);
} catch (Exception e) {
  this.font = new Font("Arial", Font.PLAIN, 16);
}
this.label.setFont(this.font);

Easy and worked fine on 3 different systems. Until someone else tried to run it. The font was loaded (as we’re also using on some other Swing elements), but not used in the JLabel.

After some searching, I’ve found out you can’t use both HTML and a loaded font. For some reasons it works on my system (I assume it has something to do with the Java version), but not on some others. As we would like the project to work in outdated Java versions, just asking to update isn’t an option.

One option is to install the font on the computer, something we don’t like to do. The best solution I’ve found is this one: How can I create a Java/Swing text component that is both styled and has a custom font?

However, that question is about a JTextPane. A JLabel doesn’t seem to have a getStyledDocument() method I can use for that.

Is there any way to let our font work with the JLabel?

Answer

To use some font:

<html><head><style type="text/css">
body { font-family: Cool; } </style></head><body>...

The Font you created has to be registered first in the singleton GraphicsEnvironment to be accessible to all:

GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
genv.registerFont(font);
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..