Frage

I am trying to place a button in the TabWidget, meaning that I want to see all the tabs and the most right one will be a button.
I couldn't do it with the xml (and it will be helpful if someone would tell me how) so I've added the button by code. The problem is that the button is not clicked, I hear the click sound from the device but nothing happens, I don't see that the OnClickListener is called.
Can you please look at the code and tell me what I am doing wrong?
This code is at the onCreate() of my activity.

TabWidget tabWidget = (TabWidget)findViewById(android.R.id.tabs);

Button button = new Button(this);
button.setBackgroundResource(R.drawable.settings);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
button.setClickable(true);
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        LogWrapper.d(TAG, "************************************");
        startActivity(new Intent(MyActivity.this, SettingsActivity.class));
    }
});
tabWidget.addView(button);
War es hilfreich?

Lösung

I have a solution, I don't know if it's the best one, but it's something to offer.

I've built xml file with a single button in it, inflate it and add it to the TabWidget, this seems to work.

settings_button.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/settingsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/settings" >
    </Button>

</LinearLayout>

MyActivity.java

TabWidget tabWidget = (TabWidget)findViewById(android.R.id.tabs);

View layout = LayoutInflater.from(this).inflate(R.layout.settings_button, null);
Button button = (Button)layout.findViewById(R.id.settingsButton);
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        LogWrapper.d(TAG, "*****************************************************");
        startActivity(new Intent(MyActivity.this, SettingsActivity.class));
    }
});
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tabWidget.addView(layout);

Anyway, this is my solution offer. After checking it, it doesn't look good a button in the TabWidget. I'll think what to do with it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top