Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After struggling with it myself quite some time, here's the solution (tested it - working good):</p> <p>The general steps are:</p> <ol> <li>Create a wrapper view</li> <li>Detach the screen view children, place the wrapper, and attach the children</li> <li>Inflate the content to the children</li> <li>Controling the wrapper will help you control exactly the action bar and the content below it all together.</li> <li>Now, using the wrapper, you can add "brothers" to the actionbar/main area. That brother is exactly what you described in your image.</li> </ol> <p>Let's see some code.</p> <p>First, create a method to help create a wrapper view. the wrapper will be placed between the entire screen and the content of your app. being a <a href="http://developer.android.com/reference/android/view/ViewGroup.html">ViewGroup</a> you can later on fully control it's content.</p> <pre><code>private ViewGroup setContentViewWithWrapper(int resContent) { ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView(); ViewGroup decorChild = (ViewGroup) decorView.getChildAt(0); // Removing decorChild, we'll add it back soon decorView.removeAllViews(); ViewGroup wrapperView = new FrameLayout(this); // You should set some ID, if you'll want to reference this wrapper in that manner later // // The ID, such as "R.id.ACTIVITY_LAYOUT_WRAPPER" can be set at a resource file, such as: // &lt;resources xmlns:android="http://schemas.android.com/apk/res/android"&gt; // &lt;item type="id" name="ACTIVITY_LAYOUT_WRAPPER"/&gt; // &lt;/resources&gt; // wrapperView.setId(R.id.ACTIVITY_LAYOUT_WRAPPER); // Now we are rebuilding the DecorView, but this time we // have our wrapper view to stand between the real content and the decor decorView.addView(wrapperView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); wrapperView.addView(decorChild, decorChild.getLayoutParams()); LayoutInflater.from(this).inflate(getActivityLayout(), (ViewGroup)((LinearLayout)wrapperView.getChildAt(0)).getChildAt(1), true); return wrapperView; } </code></pre> <p>Now, interfere with the regular <a href="http://developer.android.com/reference/android/app/Activity.html">Activity</a> creation, and instead of using <a href="http://developer.android.com/reference/android/app/Activity.html#setContentView%28int%29">setContentView</a>, use the method we've created.</p> <pre><code> @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // DON'T CALL `setContentView`, // we are replacing that line with this code: ViewGroup wrapperView = setContentViewWithWrapper(R.layout.activity_layout); // Now, because the wrapper view contains the entire screen (including the notification bar // which is above the ActionBar) I think you'll find it useful to know the exact Y where the // action bar is located. // You can use something like that: ViewGroup actionBar = (ViewGroup)((LinearLayout)wrapperView.getChildAt(0)).getChildAt(0); int topOffset = actionBar.getTop(); // Now, if you'll want to add a view: // 1. Create new view // 2. Set padding top - use "topOffset" // 3. Add the view to "wrapperView" // 4. The view should be set at front. if not - try calling to "bringToFront()" } </code></pre> <p>That's about it.</p> <p>Notes</p> <ul> <li>I've used <a href="http://developer.android.com/tools/help/hierarchy-viewer.html">Android's hierarchy-viewer</a> to understand what's the right hierarchy. (didn't guess those <code>0</code> and <code>1</code> indexes)</li> <li>If you are using some kind of a menu drawer in your activity, you might have to configure it a little bit different since drawers are already creating that wrapper for you</li> <li>I've learned a lot by looking at <a href="https://github.com/SimonVT/android-menudrawer">this great library</a></li> </ul> <p><strong>EDIT:</strong> Refer to @CristopherOyarzúnAltamirano Answer for further support on newer Android versions</p> <p>Good luck!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload