alezvic
alezvic

Reputation: 384

Different widgets (views) for different screen orientations?

I'm quite new developing for Android so I have a very basic question.

Basically, I have two screen layouts, one for portrait and another for landscape orientation. I use the folders res/layout and res/layout-land. Both orientations are drawn fine, but I have different widgets (buttons) for each orientation, btnPortrait and btnLandscape.

The problem arises when I try to call onSetClickListener(). Because when the device is oriented in portrait the framework can't locate the btnLandscape and viceversa, I handle the orientation changes manually using onConfigurationChange(). It doesn't seem to work either.

My code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnPortrait = (Button) findViewById(R.id.portraitButton);
    tvPortrait = (TextView) findViewById(R.id.portraitText);
    btnLandscape = (Button) findViewById(R.id.landscapeButton);
    tvLandscape = (TextView) findViewById(R.id.landscapeText);
}

And the onConfigurationChange():

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        btnPortrait.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                    Toast.makeText(getContext(), "Portrait",Toast.LENGTH_SHORT).show();
            }
        });
    }

    else {
        btnLandscape.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "Landscape", Toast.LENGTH_SHORT).show();
            }
        });
    }   
}

None of the Toasts work. Does somebody know what could be happening?

Thanks in advance.

Upvotes: 0

Views: 762

Answers (3)

Squonk
Squonk

Reputation: 48871

I'm not sure by what you mean in the comments by "they're not the same button" but a way of doing what you want with two different resource ids would be as follows...

public class MyActivity extends Activity {

    Button myButton = null;
    TextView myTextView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myButton = (Button) findViewById(R.id.portraitButton);
        if (myButton == null)
            myButton = (Button) findViewById(R.id.landscapeButton);
        myTextView = (TextView) findViewById(R.id.portraitText);
        if (myTextView == null)
            myTextView = (TextView) findViewById(R.id.landscapeText);
    }

    // The following method is your OnClickListener
    public void sayHello(View v) {
        switch (v.getId) {
            case R.id.portraitButton :
                // Do something for portrait
                break;
            case R.id.landscapeButton :
                // Do something for landscape
                break;
        }
    }
}

In your layout files you would then assign the OnClickListener using android:onClick as follows...

For portrait...

<Button
    android:id="@+id/portraitButton"
    ...
    android:onClick="sayHello" >
</Button>

For landscape...

<Button
    android:id="@+id/landscapeButton"
    ...
    android:onClick="sayHello" >
</Button>

In this way, the OnClickListener is set by the inflation process when setContentView(...) is called. It then simply comes down to it determining the resource id to decide what action needs to be taken.

Upvotes: 0

Developer So far
Developer So far

Reputation: 353

Just make sure that your button Id in layout-land is same Id in default layout

Update1 :

ok add not null check before using the widget like this

if( btnPortrait !=null ) { 
// do what you want 
}

Upvotes: 0

Kevin Coppock
Kevin Coppock

Reputation: 134704

Don't name them differently; the widgets for the same function should share the same ID whether it's in portrait or landscape.

If it's a widget that only exists or functions differently in landscape, just check if it's null after findViewById(), and if that returns null, you know you're not in a configuration with a layout that includes that button.

Upvotes: 1

Related Questions