Getting a ListView to go back to highlighting a previously selected item

Summary of the problem:

On the Google TV platform you can’t touch the screen.  This means you mainly depend on the up,down,left,right nav.  What posses a navigation issue is when you have have a list view on the left hand side with a grid view on the right.

The idea is when the a listview item is clicked then you populate a different set of gridview items.  When you select the right nav and then move around and then go back to the list view, it will go to the closest item but not the last one that was selected.

What you would think the answer is:

this.getListView().setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
{
mListView.setSelectionFromTop(mCurCheckPosition, itemPosition); //posibly use the setSelection instead but still the same idea

}

}
});

However this doesn’t work.  I imagine it’s something with the order of items getting focus and what not but still no worky.

Actual Answer/Hack

this.getListView().setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
{
mHandler.postAtFrontOfQueue(new Runnable() {
public void run() {
if (!firstTry)
{
mListView.setSelectionFromTop(mCurCheckPosition, itemPosition);
}
else
{
firstTry=false;
}
}
});

}

}
});

You also need a firstTry = false as a private;

mCurCheckPosition is most listly going to be set on the onItemSelected or onListItemClick depending on what behavior you want, but you should get the idea.  This works and honestly I’m not really sure why and if someone knows of a better way to get this behavior please please comment on it and I’ll update the post!

Moving an EC2 instance from one account to another.

So after being on a project where needed to offload the servers we were paying for to a different decision I came into the interesting scenario of moving the ec2 instance to another account.  I couldn’t find any simple solutions on google so here goes my two cents on doing it the easy way.

 

1. select your instance and creates an EBS ami. (note this will take down your instance so make sure to do it during a maintenance period)

2. go to your your AMI list.  Select the newly created AMI and click on permission.  Now add the account you’re transferring to.

3. In the new account select the private AMI from the list and choose to launch a new instance.

At this point you should be able to kill the old instance in the old account and you’re newly create instance in the new account should be good to go :)

 

Fragments with tabhosts controling fragments

So on a past project I used a tabhost in android to control fragments.  After seeing a post for someone needed help we exchange a few email I thought I would post Here :)   here are the basics.

—I am trying to get fragments working within a TabHost which is within
a
fragment handled by the ActionBar.  The top layer fragments work ok,
however I have not been able to get the lower level fragments to
display.
Could you please be more specific on how you accomplished this, or
maybe, send me some examples.

 

So I haven’t done a fragment inside a tabhost inside a fragment,
although I don’t think that have the parent fragment controlled by the
ActionBar would make any differences although the lifecycles could get
pretty complex in terms of popping the fragments in an out.  Anyways,
here are a couple snipets for the tabhost and the fragments being
changed in and out.  This is from a project that I was working on a
while back so I’m a little cobwebish about it.  You may want to try
getting the tabhost and fragments to work on their own and then bring
it into the parent fragment so that you can narrow down what’s causing
the probs. Hopefully this will help a bit.

FragmentManager fm = this.

getSupportFragmentManager();
fm.addOnBackStackChangedListener(this);
setContentView(R.layout.main);

mTabHost = (TabHost) findViewById(R.id.tabhost);
mTabHost.setup();

mTabHost.addTab(mTabHost.newTabSpec(“tab_seasonal”).setIndicator(“”,
getResources().getDrawable(R.drawable.nav_inseason)).setContent(R.id.content_fragment));
mTabHost.getTabWidget().getChildAt(0).setTag(“tab_seasonal”);

mTabHost.addTab(mTabHost.newTabSpec(“tab_collections”).setIndicator(“”,
getResources().getDrawable(R.drawable.nav_collections)).setContent(R.id.content_fragment));
mTabHost.getTabWidget().getChildAt(1).setTag(“tab_collections”);
mTabHost.addTab(mTabHost.newTabSpec(“tab_search”).setIndicator(“”,
getResources().getDrawable(R.drawable.nav_search)).setContent(R.id.content_fragment));
mTabHost.getTabWidget().getChildAt(2).setTag(“tab_search”);
mTabHost.addTab(mTabHost.newTabSpec(“tab_favorites”).setIndicator(“”,
getResources().getDrawable(R.drawable.nav_favorites)).setContent(R.id.content_fragment));
mTabHost.getTabWidget().getChildAt(3).setTag(“tab_favorites”);
mTabHost.addTab(mTabHost.newTabSpec(“tab_facebook”).setIndicator(“”,
getResources().getDrawable(R.drawable.nav_facebook)).setContent(R.id.content_fragment));
mTabHost.getTabWidget().getChildAt(4).setTag(“tab_facebook”);

mTabHost.setOnTabChangedListener(new OnTabChangeListener()
{
@Override
public void onTabChanged(String tabId)
{

changeTab(tabId);

}
});

mTabHost.setCurrentTab(1);
mTabHost.setCurrentTab(0);

—- here’s the sample changing the fragment out.

private void changeTab(String tabId)
{

Fragment df = null;

df = SeasonalFrag.newInstance(1);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_fragment, df);

ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
getSupportFragmentManager().popBackStack(null,
FragmentManager.POP_BACK_STACK_INCLUSIVE);
ft.commit();

——-

 

In looking at your sample, I realized you must have extended FragmentActivity from the Compatability Package.  I cannot do that since I am in a Fragment which already extends Fragment.  I am taking your advice and am going to try getting it working in another test project.

——

 

No worries :)

There really isn’t a problem even if you’ve already extended as
fragment instead of fragmentactivity from the Comptability pack.   Depending on the complexity it may be better in the changeTab
method to do a callback to the activity and have it control the flow.
you can do something like the following in the fragment

OnFragmentTabListener onFragmentTabListener; // make sure to create
the OnFragmentTabListener interface with a changeTab Method or what
not

@Override
public void onAttach(Activity activity){
super.onAttach(activity);
try {
onFragmentTabListener = (OnFragmentTabListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.

toString() + ” must implement
OnFragmentTabListner”);
}
}

then in the tabhost listener in the fragment just call the
onFragmentTabListener.changeTab (or whatever you call the method).

Implement that in the FragmentActivity and now all the control logic
is back to the activity instead of the fragment :)  make sense?  That
should allow whichout a problem the use for a tabhost controlling
fragments in a parent fragment and keep all the control in the
activity.

I’ll clean this up later but it should give somewhat of an idea on how to do this. :)

Automator and applescript for grabbing a file on a smb share and getting windows friends link to send via email

So, you’ve kicked yourself the 20th time because you sent a link to someone via email, IM, smoke signals, etc that looked like smb://blab/sharedfolder/file.xls where they run windows and well it should look more like file:\\\\blab\\sharedfolder\\file.xls.

There are a few ways around this and if you connect to the same server all the time here is your answer (if you connect to multiple servers all the time then use this as a start and figure out the rest and post it please!!!)

First lets start with automator.  create one for a service and make sure that the service receives selected files or folders in finder.  Second add a task that runs an applescript.  it’ll look something like this
———————————–
on run {input, parameters}
    tell application “Finder”
        set fileURL to URL of file input
        set newfileURL to my replaceText(“file://localhost/Volumes”, “file:\\\\yourservername”, fileURL)
        return my replaceText(“/”, “\\”, newfileURL)
    end tell
end run

on replaceText(find, replace, subject)
    set text item delimiters of AppleScript to find
    set subject to text items of subject
   
    set text item delimiters of AppleScript to replace
    set subject to “” & subject
   
   
    return subject
end replaceText
——————————————————–

Then add a copy to clickboard task.

now save it as a service and you should be good to see a context menu when you right click.  This will paste the content to the clipboard so you can now paste the newly formatted string in your mail client or whatev… :)

hijacking touch events from Android for clickable items in a Gallery view

So to begin with if you know of a better way of doing this…. PLEASE TELL ME… I spent a day or so on this trying to find more elegant solutions and in the end this seems like it will have to do.

First the problem.  Android doesn’t have by default the swipie Scroll Content controller that iOS has examples of (I should clarify that iOS doesn’t have this by default but at least has examples.)  So essentially I wanted something that would act like a gallery view (although just swipe one item onto the screen at a time) and then have views with buttons and what not that would be clickable.  The problem being that if you have a clickable buttom and you start the swipe by touching it, it doesn’t swype… and if you set the ontouchlistener to the button to a custom one well say goodbye to the onclick…  exmple–
————————  g is a galleryview btw
        OnTouchListener mOnTouch = new View.OnTouchListener() { 
            @Override
            public boolean onTouch(View v, MotionEvent event) {
             if (g.onTouchEvent(event)) {
                 Log.e(“onTouch”, “captured by gallery”);
                
                            return false;
                        }
       
             Log.e(“onTouch”, “this has been touched!”);
            return false;
           
            }

        };

button.setOnTouchListener(mTouch);
—————————

In this case for some really weird reason checking the  if (g.onTouchEvent(event)) actually eats the event and even return false afterwards much to my dismay doesn’t even pass the event on!!!!  this is what really kills me and if someone knows why… please oh please tell me.

Solutions->  Create a custom Gallery  sample bellow…  which overides the onInterceptTouchEvent with a custom Gesturedetector in an inner class that then calls the Galleries OnFling if there is a fling. The nice added part to this is that now you don’t need to set an ontouch listerner for each clickable item in the gallery.

couple other notes -> invalie return for position is so that it doesn’t try to click the item no matter where you are…  It may not be needed anymore but I’m tired of messing with it for the time being… I’ll update if it can go by the way side.   The velocityx / 4 is so that only one item gets flung :) and the * 3 later on I have no idea why it’s needed :(   seems like the galleries ontouchevent does some black magic before sending the values to the onfling.  3 seems to be the majic number to make it work like the /4 :)

public class MyGallery extends Gallery {
    GestureDetector gestureDetector = new GestureDetector(new innerGestureDetector());

    public MyGallery(Context context,AttributeSet attrSet) {
    super(context, attrSet);
    // TODO Auto-generated constructor stub
    }
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
    Log.e(“FlingGallery”, “Galery was flinged”);
        super.onFling(e1, e2, velocityX / 4, velocityY); 
        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

    if (gestureDetector.onTouchEvent(ev))
    {
        Log.e(“FlingGallery”, “WT1″);
        super.onTouchEvent(ev);
        return true;
    }
    Log.e(“hijack”, “intercepted”);
   
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public int pointToPosition(int x, int y)
    {
    return INVALID_POSITION;
    }
        class innerGestureDetector extends SimpleOnGestureListener {

   
    @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  
        Log.e(“fling”, “innerfling”);
        return MyGallery.this.onFling(e1,e2,velocityX * 2,velocityY);

        }

    }

}

Hopefully someone will find this of use or at least it will help me with my memory… This was enough to make me hate how events are passed around (perhaps just because I don’t fully understand them)

Cheers,