Home > front end >  Restrict an activities SetContentView to display inside an existing container
Restrict an activities SetContentView to display inside an existing container

Time:02-15

I have a complex imported package that creates an activity and then uses SetContentView on an R.layout file. This layout essentially inflates and covers the screen, but I would like it to only "inflate" inside an existing view.

The activity class looks something like this

public class ExampleActivity extends AppCompatActivity {
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    setTheme(R.style.Theme_AppCompat_NoActionBar);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.example_layout);
  }
}

And I would call it from something like a clickListener with a function like this (where activity is MainActivity)

public static void startNewActivity(Activity activity) {
    Intent navigationActivity = new Intent(activity, ExampleActivity.class);
    activity.startActivity(navigationActivity);
  }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#AFAFB1"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="viewToFill">
    </LinearLayout>
     

</androidx.coordinatorlayout.widget.CoordinatorLayout>

So the question I'm asking is would it be possible to set an activity up so that when SetContentView is called a predefined view is filled instead of the entire screen. In simple terms, have SetContentView put R.layout.example_layout into the linearlayout viewToFill in activity_main.xml.

Any help would be appreciated, I have ideas of working around it, it's just that this solution is by far the least involved if it's possible, but I'm very aware it may not be

CodePudding user response:

As per the official documentation in Android application components the Activity covers the entire screen / window which is the correct and desired behaviour.

If you want to start a view that only covers the partial screen then you should use a Fragment. A Fragment represents a reusable portion of your app's UI and provides the modularity that you want.

CodePudding user response:

Just define Theme.AppCompat.NoActionBar for that Activity in AndroidManifest.xml.

  • Related