tags:

views:

34

answers:

1

Hello In my application i am displaying rows dynamically. I am using the below code so as to focus the clicked row even if the user press back key. But the issue is like if the row is not focussed we need two clicka to navigate.First to make it focussable and then next click to navigate.I need all at once any suggestions.

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    for(int a=0;a>16;a++)
    {
        tr[a].setBackgroundColor(Color.BLACK);

    }

    flag=v.getId();
    if(v.getId()==1)
    {   

        Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
        startActivity(i);

    }
    if(v.getId()==3)
    {
        Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
        startActivity(i);

    }
    if(v.getId()==5)
    {
        Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
        startActivity(i);

    }

    if(v.getId()==7)
    {

        Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
        startActivity(i);

    }

    if(v.getId()==100)
    {
        Intent i = new Intent(TableImageLayout.this, TableImageLayout3.class);
        startActivity(i);

    }   


}

@Override
public void onFocusChange(View v, boolean hasFocus) {
    // TODO Auto-generated method stub
    for(int a=0;a>16;a++)
    {
        tr[a].setBackgroundColor(Color.BLACK);

    }
        if(hasFocus)
    {
        ((TableRow)v).setBackgroundColor(Color.rgb(255, 180, 40));
    }
    else
        {((TableRow)v).setBackgroundColor(Color.BLACK);}

}

    protected void onResume() { 
        super.onResume();
        for(int a=0;a>16;a++)
        {
            tr[a].setBackgroundColor(Color.BLACK);

        }

        tr[flag].requestFocus();
        tr[flag].setFocusableInTouchMode(true);
        tr[flag].setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub

                if(hasFocus)
                {
                    ((TableRow)v).setBackgroundColor(Color.rgb(255, 180, 40));
                }
                else
                    {((TableRow)v).setBackgroundColor(Color.BLACK);}
            }
        });


    }




     @Override   
     public void onPause() {      
    super.onPause(); 

     }   

Please let me know your valuable suggestions. Thanks in advance.

A: 

If your only need for focus listener is to change background colors, you would do better setting their background to a drawable that has different backgrounds for focused and normal states, then you don't need to control it in code at all. As for requiring two clicks to navigate, I don't see anything in your code about it. It seems to start an activity as soon as a view is clicked once.

totramon
could you please share some sample code with me as how i can create drwable with different backgrounds for focused and normal states.But when i try to run it for first 2 or 3 clicks it works fine but later it behaves like on first click focussed and second navigate
Remmyabhavan
Actually i am drawing rows dynamically.Can i change the background of thos using drawable.If can what shall i place inside the xml which we provide as drawable. For example <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/red_button_pressed" /> <item android:state_focused="true" android:drawable="@drawable/focuss" /> <item android:drawable="@drawable/red_button_rest" /> </selector> Then what shall i place inside focuss.xml
Remmyabhavan
Of course you can change dynamically created view's drawables, too. You can set their background from a resource with setBackgroundResource() by giving it the resource id (e.g. R.drawable.my_drawable). focuss can be any drawable itself. Drawable resources are documented with examples here: http://developer.android.com/guide/topics/resources/drawable-resource.html
totramon
Thanks totramon.Yes i got drawables working.Could you please let me know if there is any way in android to make the highlighted row focussed.I am setting as below but doesn't work.tr[flag].requestFocus(); tr[flag].setFocusableInTouchMode(true);
Remmyabhavan
My issue is like if i set tr[flag].requestFocus(); tr[flag].setFocusableInTouchMode(true) in onPause() or onResume() it requires focus to navigate,Please help me with any suggestiona.
Remmyabhavan