Tarnak
Tarnak

Reputation: 11

About XML layout & custom android class

I'm using Eclipse IDE. How I should define class, initialized (for example) in activity_main.xml ? Lets presume, in activity_main.xml I have definition of ListView object like:

 <ListView
    android:id="@+id/ListViewAction"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
</ListView>

Normally I could handle this in file MainActivity.java and it's fine.

But I want to split code to different .java file and create class extending ListView. So, I create new java class file and what I should put in that class to overload ListView ? If I place standard class definition in that new java file, it doesn't work:

public class ListViewAction extends ListView {

    public ListViewAction(Context context) {
        super(context);
        System.out.println("ListViewAction()");         
    }       
}

How I should correctly define class in new java file and XML?

Upvotes: 1

Views: 2959

Answers (2)

Blo
Blo

Reputation: 11978

If you want to customize your own ListView, you must to do these things:

1 . Extends the new class with the widget and attach the context:

public class myOwnListView extends ListView { 
    final Context context;

    public myOwnListView(Context context) {
        super(context);
        this.context = context;
    }

    public myOwnListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public myOwnListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }
}  

2 . Change your layout's ListView:

// instead of
<ListView
    android:id="@+id/ListViewAction"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

// you need to do as below  
<com.my.package.name.myOwnListView
    android:id="@+id/ListViewAction"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />  

3 . Use it in your onCreate method:

public class MainActivity extends Activity {

    // initialize your ListView:
    myOwnListView myListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // find your view in your layout:
        listview = (myOwnListView) findViewById(R.id.ListViewAction); 

        // set to an adapter...
        listview.setAdapter( [...] );

        // more stuff
    }

}

4 . Avoid some bugs:

In your layout, try to always set the height of a widget as GridView or ListView to the height of the parent layout with match_parent / fill_parent.


With this project HeaderListView by tokou or this one HFGridView by Sergey Burish, you can see how does it works and you will have a best approach to do what you want.
Hope this will be helpful.

Upvotes: 7

user2046264
user2046264

Reputation:

If you added MyListView:

package my.app;

public class MyListView extends ListView {
  // ...
}

You can reference it in the XML using the full class name:

<my.app.MyListView
   ...
/>

Upvotes: 1

Related Questions