views:

190

answers:

1

i would like to create a custom button like this:

http://i77.photobucket.com/albums/j74/bertyhell/button.png?t=1279626910

but that doesn't work i need to show the text on the buttons with absolute positioning relative to the buttons origin all strings need to be changeable at runtime

but the button below can be a regular button

how can i do this?

if i create a button by inheriting from android.widget.button i have to draw the whole button myself?

is there an easier way?

EDIT

ok i tried the inherit from relative layout

like this:

    package be.smarttelecom.datacheck.views;

    import android.content.Context;
    import android.util.Log;
    import android.widget.RelativeLayout;
    import android.widget.TextView;

    public class AlertButton extends RelativeLayout {

        private TextView roomNumber;

        public AlertButton(Context context, String roomNumber) {
            super(context);
            try{
            this.alertId = alertId;
            // TODO Auto-generated constructor stub
            this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            TextView room = new TextView(context);
            room.setFocusable(false);
            room.setText(R.string.room);
            room.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));

            this.roomNumber = new TextView(context);
            this.roomNumber.setFocusable(false);
            this.roomNumber.setId(R.id.roomNumber);
            this.roomNumber.setTextSize(26);
            this.roomNumber.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            this.roomNumber.setText(roomNumber);

            }catch(Exception ex){
                Log.i("test", "problem during construction of alert button: " + ex.getMessage());
            }
        }

        public void setRoomNumber(String number){
            roomNumber.setText(number);
        }

        public void setDescription(String description){
            this.description.setText(description);
        }
    }

when i add it like this:

        TableRow row2 = new TableRow(this);
        AlertButton alert = new AlertButton(this, "1","14","wc","krist");
        row2.addView(alert);
        container.addView(row2, 0);

i get an empty page and no errors at all

any idea what i did wrong?

how do i put the textfields on certain positions? put them inside an absolute layout and add x an y coordinates? (absolute layout is depricated, alternative?)

A: 

I think the easiest way is to create a custom view that extends RelativeLayout. You can then add 4 TextViews in whatever arrangement you wish with whatever customization you need. To make it look and act like a button, you can add a button to your layout with the width and height set to fill_parent. Make sure that your TextViews are not focusable and that the button is sent to the back of the layout.

Alternatively you can set the RelativeLayout's properties in your custom view to be clickable=true and focusable=true. Then add a drawable as your view background that looks like a button. Use a selector to handle all the focused/pressed states like a real button does. Details on how to do that can be found here: http://stackoverflow.com/questions/1521640/standard-android-button-with-a-different-color

CodeFusionMobile
i tried methode 1i edited the first post with new problemsit doesn't display the custom controllerthx for the help so far :)
Berty
@Berty Did you remember to setContentView(container) in your activity's onCreate?
CodeFusionMobile
yes i added iti also tried to add a textview before and after the custom buttoni get something like thishttp://i77.photobucket.com/albums/j74/bertyhell/errors/custom_button.pngso the button isn't displayed i did set the setContentViewsetContentView(R.layout.alerts);
Berty
@Berty Try setting an absolute height and width for the custom button. It might just be the way you're defining layout.
CodeFusionMobile