1haker
1haker

Reputation: 134

NestedScrollView with Toolbar

So I set in styles.xml for AppTheme NoActionBar, because I want Toolbar only in a few Activites and also I create post_details_menu with two items. But toolbar not showing at all.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

Then in Manifest.xml, I have the theme: AppTheme

<application
    android:theme="@style/AppTheme"
    ...>
</application>

Then i created my_toolbar:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"/>
</LinearLayout>

And in im PostDetailsActivity i want use my Toolbar:

public class PostDetailActivity extends AppCompatActivity {

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

    Toolbar toolbar = findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.post_details_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()){
        case R.id.post_Edit:
            showMessage("post_edit clicked");
            return true;
        case R.id.post_Delete:
            showMessage("post_delete clicked");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

And finally this is my layout for ActivityPostDetails:

<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PostDetailActivity"
android:background="#fff">

   <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

       <ImageView ... />

       <TextView ... />

       <androidx.recyclerview.widget.RecyclerView .../>

   </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

Upvotes: 1

Views: 1603

Answers (2)

Manoj Mohanty
Manoj Mohanty

Reputation: 370

Xml structure something like this should solve the problem

<LinearLayout>
<Toolbar/>
<NestedScrollView>
//one parent view inside nestedscrollview
<AnylayoutType>
// other required views
</AnylayoutType>
</NestedScrollView>
<LinearLayout/>

With this approach your Toolbar won't move and menu option will always be available and nested sroll view will work as required.

And if you are sure your view hierarchy is not nested its better to use LinearLayout inside nested scrollview

Upvotes: 1

Kaushal Panchal
Kaushal Panchal

Reputation: 1825

It's too simple. set your app bar layout in the XML file where you want to show the toolbar.

check below XML code:-

<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PostDetailActivity"
android:background="#fff">

   <androidx.constraintlayout.widget.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">

         <com.google.android.material.appbar.AppBarLayout
              android:id="@+id/appBarLayout"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:theme="@style/AppTheme.AppBarOverlay"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent">

                 <androidx.appcompat.widget.Toolbar 
                     xmlns:android="http://schemas.android.com/apk/res/android"
                     xmlns:app="http://schemas.android.com/apk/res-auto"
                     android:id="@+id/toolbar"
                     android:layout_width="match_parent"
                     android:layout_height="?attr/actionBarSize"
                     android:background="@color/colorPrimary"
                     android:theme="@style/AppTheme.ActionBar"
                     app:navigationIcon="@drawable/ic_back"
                  app:subtitleTextAppearance="@style/CustomSubTitleTextAppearance"
                     app:titleTextColor="@color/black" />

                    </com.google.android.material.appbar.AppBarLayout>

                    <ImageView ... />

                    <TextView ... />

                    <androidx.recyclerview.widget.RecyclerView .../>

                </androidx.constraintlayout.widget.ConstraintLayout>

            </androidx.core.widget.NestedScrollView>

In your java code, you have to bind toolbar programmatically in PostDetailsActivity.java.

toolbar = findViewById(R.id.toolbar)
toolbar!!.setNavigationIcon(R.drawable.ic_back)
if (toolbar != null) {
   setSupportActionBar(toolbar)
}

Upvotes: 1

Related Questions