Question

In my android application I have a spinner containing different calculators, and when a calculator is selected it shows up in a scrollview below. When the screen is set to horizontal, the layout changes so that the scrollview is to the right, and the spinner is to the left, and it all works the same way.

However, a listview would look better when the screen is in a horizontal position, but I can't seem to find a way to make the switch in the java code.

Can anyone help with this?

Here is the code for the initialize method, and I was wondering if there was a way to insert an if-statement where a spinner would be initialized if the screen was vertical, and a listview would be initialized if the screen was horizontal.

private void initialize() {

    valg_vertical = (Spinner) findViewById(R.id.sKalk);

    // Adds the custom look for the spinner
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.custom_spinner, getResources().getStringArray(
                    R.array.calculator));
    adapter.setDropDownViewResource(SpinnerItemLayout);
    valg_vertical.setAdapter(adapter);
    // Adds the custom look for the spinner

    lLayout = (LinearLayout) findViewById(R.id.lInSVKalk);
    valg_vertical.setOnItemSelectedListener(this);
}
Was it helpful?

Solution

You have 2 possible solutions:

  1. Create different layouts for portrait and landscape modes (they will be places under layout and layout-land folders respectively).
  2. Create a container in your layout (FrameLayout, for example). And add corresponding view to it in runtime depending on orientation.

Anyway, since both orientations use different elements, in your code you should add the following if statement:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){

} else {

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top