Search code examples
androidandroid-activityfullscreen

Android: Exit full screen and return to main activity


Main activity contains a spinner and a button. MainActivity.java code is as follows.

public class MainActivity extends AppCompatActivity implements LoadNew.VCallback {

    public Spinner dropdown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dropdown = (Spinner) findViewById(R.id.spinner1);

        //Create a list of items for the spinner.
        String[] items = new String[]{"select topic", "topic1", "topic2"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
        dropdown.setAdapter(adapter);
        dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // TODO Auto-generated method stub
                String sp1 = String.valueOf(dropdown.getSelectedItem());
                if (sp1.contentEquals("select topic")) {
                    loadFun("");
                }
                if (sp1.contentEquals("topic1")) {
                    loadFun("topic1");
                }
                if (sp1.contentEquals("topic2")) {
                    loadFun("topic2");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView){
                // TODO Auto-generated method stub
            }
        });

        Button x = (Button) findViewById(R.id.full_screen);
        x.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadFun("topic2", 0);
            }
        });
    }

    public void loadFun(String topicName, int i) {
        LoadNew st = new LoadNew(this);
        st.display(topicName, i);
    }
}

The xml layout for main activity is as follows (activity_main.xml).

<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <Spinner
                android:id="@+id/spinner1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:drawable/btn_dropdown"
                android:spinnerMode="dropdown" />
        </LinearLayout>
        <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal" >
             <Button
                android:id="@+id/full_screen"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Full Screen"
                style="?android:attr/borderlessButtonStyle"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/full_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

There is another java class LoadNew.java as follows.

public class LoadNew {

    public interface VCallback {
        //To access methods of MainActivity class
    }

    public Activity activity;

    public LoadNew(Activity _activity) {
        this.activity = _activity;
        VCallback callerActivity = (VCallback) activity;

    }

    //Display PDF
    public void display(String topicName, int i) {
        LinearLayout fullScreen = (LinearLayout) this.activity.findViewById(R.id.full_view);
        fullScreen.removeAllViews();//Clear field
        LayoutInflater inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = inflater.inflate(R.layout.activity_load, null);
        if(i ==0) {
         this.activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            this.activity.setContentView(view);
        }
        TextView tv= (TextView) this.activity.findViewById(R.id.tv1);
        tv.setText(topicName);
        tv.setTextSize(100);
    }
}

The activity_load.xml file is as follows.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout> xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/tv1"
    android:layout_below="@+id/bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</RelativeLayout>

When an item is selected in the spinner, the corresponding text is displayed in the textview. Upon pressing the button, the textview appears in full screen mode. Now I want to return to the previous view (by pressing back button or by single tab on screen), i.e., the view before the button was clicked. How can I achieve this? Thanks in advance for the patience and the help.


Solution

  • Simplest would be to have the spinner and the non-fullscreen TextView on one activity and open a second activity with the fullscreen mode on button click.