Reputation: 1110
I followed this tutorial on how to implement collapsing toolbar layout Link Here
And I successfully get the result what i want.
After some debugging I found my action bar search button is above the image which supposed to be inside the image.
What I'm doing is put the search icon inside the image just like any other collpsing toolbar layout.
And the image should occupy the top(colorPrimaryDark).
Here's my XML.(Based on the tutorial on the given link)
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAFAFA">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="270dp"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="28dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/img_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gameover2"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="16dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="24dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="@string/coordinatar_card"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textIsSelectable="false"
android:textSize="25sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/coordinatar_card_desc"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="24dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="@string/appBarLayout_card"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textIsSelectable="false"
android:textSize="25sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/appBarLayout_card_desc"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="24dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="@string/collapsing_toolbar_card"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textIsSelectable="false"
android:textSize="25sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/collapsing_toolbar_card_desc"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_search"
app:layout_anchor="@+id/appbarLayout"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
And my Activity..
public class selectedPOI extends AppCompatActivity {
private RecyclerView recyclerView;
private JuanAdapter adapter;
CollapsingToolbarLayout collapsingToolbar;
int mutedColor = R.attr.colorPrimary;
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selected_poi);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("Collapsing Toolbar");
fab=(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, "You clicked on the fab", Snackbar.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
}
EDIT:
On API 21 or higher, The ActionBar is above the image.
But in API lower than API21, Its working fine.
Upvotes: 0
Views: 1790
Reputation: 933
Ok, i think there can be two solution to this problem of yours.
1. You can add this to your style <item name="android:fitsSystemWindows">true</item>
in v21 style. This will allow each screen of your app to adjust to only the area which is meant for your app.
Other way could be by code , try this
toolbar.setPadding(0,getStatusBarHeight(this), 0, 0); here the toolbar is the your collapsing toolbar. Put this check for lollypop, if it is, then add top padding to your toolbar same as of height of status bar.
public static int getStatusBarHeight(Context ctx) { int result = 0; int resourceId = ctx.getResources().getIdentifier("status_bar_height", "dimen","android"); if (resourceId > 0) { result = ctx.getResources().getDimensionPixelSize(resourceId); } return result; }
Let us know, how it went, i think it should do it for you.
Upvotes: 1