2

When I start the application, the button works. Each time I rotate the cellphone, onConfigurationChanged is called. However onClick is no more called after I pressed the button. How can I to fix this? Thanks in advance.

PS: The actual program is a calculator with a lot of buttons.

package com.thomas.test.app;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity implements View.OnClickListener
{
    Button button1;

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

        if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
            setContentView(R.layout.activity_main_h);
        else if (newConfig.orientation==Configuration.ORIENTATION_PORTRAIT)
            setContentView(R.layout.activity_main_v);
    }

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

        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Log.d("XYZ", "onClick: IN");
        if (view.getId()==R.id.button1)
            button1.setText("PRESSED");
    }
}

This is the activity_main_h.xml (activity_main_v is identical except for displaying "vertical"):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.thomas.test.app.MainActivity">

    <TextView
        android:text="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button1"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
yes4me
  • 159
  • 3
  • 15
  • 1
    There is a more standard way to assign different layouts to different orientations, without having to check the orientation itself: see layout-land. If you are stuck with this implementation though, and have the CONFIG-CHANGES property set to your activity, it won't be recreated, your onCreate method is not called, so rather put the initialization and listener assignment to your onResume. – rekaszeru Apr 26 '14 at 06:05
  • is your problem solved...? kinly tell how did you do it . i'm also having a calculator but some buttons don't work in lansdscape mode... there is no configchanges attribute in my manisfest file. kindly help – mfaisalhyder Jul 24 '14 at 09:03

3 Answers3

6

You are created activity

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

        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);
    }

Your Listener is in onCreate so it is only clickable for

setContentView(R.layout.activity_main_v);

this view. Do one thing

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
        setContentView(R.layout.activity_main_h);
        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);
    }else if (newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){
        setContentView(R.layout.activity_main_v);
        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);
    }
}

So your event can register again on onConfigurationChanged

Tulsiram Rathod
  • 1,926
  • 2
  • 19
  • 29
1

I don't think changing the layout layout by setContentView on orientation change is a good practice. Anyway, to temporarily fix the bug in your code just try this

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

        if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.activity_main_h);
            button1 = (Button)findViewById(R.id.button1);
            button1.setOnClickListener(this);
         }
        else if (newConfig.orientation==Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.activity_main_v);
            button1 = (Button)findViewById(R.id.button1);
            button1.setOnClickListener(this);
        }

}

Stefan
  • 5,203
  • 8
  • 27
  • 51
Abhishek V
  • 12,488
  • 6
  • 51
  • 63
  • Oh that works! How do you deal with orientation in a "good" way? – yes4me Apr 26 '14 at 06:11
  • As @rekaszeru said android automatically handles orientation change if you put two separate layout files in `res/layout-land` and `res/layout-port` folder. You might want to refer this question http://stackoverflow.com/questions/12781449/different-design-for-landscape-and-portrait-orientation-android – Abhishek V Apr 26 '14 at 06:19
0

Are you using completely different layouts for landscape and portrait mode? If not, then you could simply remove the android:configChanges="orientation" line from this <Activity/> object in your AndroidManifest.xml

Having...

android:configChanges="orientation"

is telling the Android OS that you want to handle the orientation change manually. The default behavior is for the Activity to just recreate itself. Good luck.

Tamby Kojak
  • 2,129
  • 1
  • 14
  • 25