Use Custom Layout in NavigationDrawer With Header And list - android

How to do add custom layout in NavigationView and design my create custom NavigationView use material design,i want put my drawer icon to right and text left of it something like this

I Search too much and this is my experience that works fine
at first create layout for header. its name is nav_header_main.xml then put it in layouts folders in res and put this code in it..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="#dimen/nav_header_height"
android:background="#drawable/header"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:gravity="top">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/cv_nave_profile_image"
android:layout_width="#dimen/nav_profile_image"
android:layout_height="#dimen/nav_profile_image"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/profile"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/cv_nave_profile_image"
android:layout_alignParentTop="true"
android:padding="#dimen/activity_horizontal_margin"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_nav_name"
android:textStyle="bold"
android:typeface="sans"
android:textColor="#ffffff"
android:gravity="right"
android:layout_gravity="right"
android:text="رخداد جدید"
android:paddingBottom="5dp"
android:textSize="#dimen/body"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:typeface="sans"
android:textColor="#ffffff"
android:id="#+id/tv_nav_phone"
android:layout_alignParentLeft="true"
android:text="0370077315"
/>
</RelativeLayout>
</LinearLayout>
then i include it as child of NavigationView and For menu item i use RecyclerView to show menu and icon so my NavigationView is
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="spydroid.ir.dorobar.Activities.SearchActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_search" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView android:id="#+id/nav_view"
android:layout_width="fill_parent" android:layout_height="match_parent"
android:layout_gravity="right" android:fitsSystemWindows="true"
android:layout_marginLeft="#dimen/nav_margin"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<include layout="#layout/nav_header_main" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/drawer_slidermenu"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"/>
</RelativeLayout>
</LinearLayout>
</android.support.design.widget.NavigationView>
just you have to remember put your NavigationView in DrawerLayout
then i create layout for menu item with ImageView and TextView this layout and this name is card_drawer_item.xml and its code is here
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">
<ImageView
android:id="#+id/drawer_icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="#drawable/ic_about"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/drawer_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/drawer_icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:typeface="sans"
android:paddingRight="40dp"/>
</RelativeLayout>
then i create ViewHolder folder for this layout.
public class DrawerItemHolder extends RecyclerView.ViewHolder {
public ImageView itemIcon;
public TextView itemText;
public DrawerItemHolder(View itemView) {
super(itemView);
itemIcon= (ImageView) itemView.findViewById(R.id.drawer_icon);
itemText= (TextView) itemView.findViewById(R.id.drawer_text);
}
}
now i define text of menu items as string array and array that contains menu icons in menu in strings.xml
<string-array name="drawer_items">
<item>setting</item>
<item>add record</item>
<item>ads</item>
<item>about</item>
<item>call</item>
<item>help</item>
<item>privacy</item>
</string-array>
<array name="drawers_icons">
<item>#drawable/ic_action_settings</item>
<item>#drawable/ic_plus</item>
<item>#drawable/ic_ads</item>
<item>#drawable/ic_about</item>
<item>#drawable/ic_phone</item>
<item>#drawable/ic_help</item>
<item>#drawable/ic_policy</item>
</array>
then we just need an Adapter like this
public class DrawerItemAdapter extends RecyclerView.Adapter<DrawerItemHolder> {
// slide menu items
private List<DrawerItem> items;
private List<Integer> drawerIcons;
public DrawerItemAdapter(List<DrawerItem> items) {
super();
this.items = items;
}
#Override
public DrawerItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.card_drawer_item, parent, false);
return new DrawerItemHolder(itemView);
}
#Override
public void onBindViewHolder(DrawerItemHolder holder, int position) {
holder.itemIcon.setImageResource(items.get(position).getIconId());
holder.itemText.setText(items.get(position).getText());
}
#Override
public int getItemCount() {
return items.size();
}
}
every thing is ok .. just now we have to set NavigationView in Activity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
recList = (RecyclerView) findViewById(R.id.drawer_slidermenu);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
String []itemsTitle=getResources().getStringArray(R.array.drawer_items);
TypedArray icons=getResources().obtainTypedArray(R.array.drawers_icons);
List<DrawerItem>drawerItems= new ArrayList<DrawerItem>();
for(int i=0;i<itemsTitle.length;i++){
drawerItems.add(new DrawerItem(icons.getResourceId(i,-1),itemsTitle[i]));
}
DrawerItemAdapter ad= new DrawerItemAdapter(drawerItems);
recList.setAdapter(ad);
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
return;
}
super.onBackPressed();
}

Related

Fragment is occupying full screen in activity

I am stuck at this point where I am adding a Fragment in a NavigationDrawer activity. When the activity displays the fragment, the fragment bumps up the whole screen as shown in the image below:
The expected behaviour is as shown in the image below.
I am using databinding with a RecyclerView. StockListItem is the POJO class used for accessing data in RecyclerView and stock_list_item_layout is the layout used for displaying list items in RecylerView.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private RecyclerViewAdapter adapter;
DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
getSupportFragmentManager().beginTransaction().add(R.id.flContainer,new
WatchlistFragment()).commit();
}
}
public class WatchlistFragment extends Fragment {
private FragmentWatchlistBinding watchlistBinding;
private RecyclerViewAdapter adapter;
private List<StockListItem> lstStockItems = new ArrayList<>();
public WatchlistFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_watchlist, container,
false);
Toast.makeText(getActivity(), "Reached WatchlistFragment.",
Toast.LENGTH_SHORT).show();
watchlistBinding =
DataBindingUtil.setContentView(getActivity(),R.layout.fragment_watchlist);
watchlistBinding.rvMyRecyclerView.setLayoutManager(new
LinearLayoutManager(getActivity()));
watchlistBinding.rvMyRecyclerView.setHasFixedSize(false);
lstStockItems.add(new StockListItem("Vedanta
Ltd","174.45","+5.35","+3.45"));
lstStockItems.add(new StockListItem("Eicher Motors
Ltd","19974.45","+545.35","+13.45"));
lstStockItems.add(new StockListItem("Adani Green Energy
Ltd","34.45","+5.35","+3.45"));
lstStockItems.add(new StockListItem("Federal Bank
Ltd","84.45","+5.35","+3.45"));
lstStockItems.add(new StockListItem("Hindustan Zinc
Ltd","274.45","+5.35","+3.45"));
lstStockItems.add(new StockListItem("Indian Oil Corporation
Ltd","154.45","+5.35","+3.45"));
lstStockItems.add(new StockListItem("Hindustan Petroleum Corporation
Ltd","154.45","+5.35","+3.45"));
lstStockItems.add(new StockListItem("ITC
Ltd","274.45","+5.35","+3.45"));
adapter = new RecyclerViewAdapter(getActivity(),lstStockItems);
watchlistBinding.rvMyRecyclerView.setAdapter(adapter);
return v;
}}
This is what my fragment_watchlist.xml looks like.
<?xml version="1.0" encoding="utf-8"?>
<layout >
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".WatchlistFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:text="Watchlist"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rvMyRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</FrameLayout>
</layout>
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Context ctx;
private List<StockListItem> lstUsers = new ArrayList<>();
public RecyclerViewAdapter(Context ctx, List<StockListItem> lstUsers) {
this.ctx = ctx;
this.lstUsers = lstUsers;
}
#NonNull
#Override
public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(#NonNull
ViewGroup viewGroup, int i) {
StockListItemLayoutBinding stockListItemLayoutBinding =
DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()),
R.layout.stock_list_item_layout,viewGroup,false);
MyViewHolder myViewHolder = new
MyViewHolder(stockListItemLayoutBinding);
return myViewHolder;
}
#Override
public void onBindViewHolder(#NonNull RecyclerViewAdapter.MyViewHolder
viewHolder, int i) {
StockListItem stockListItem = lstUsers.get(i);
viewHolder.stockListItemLayoutBinding.setStock(stockListItem);
}
#Override
public int getItemCount() {
return lstUsers.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder
{
StockListItemLayoutBinding stockListItemLayoutBinding;
public MyViewHolder(#NonNull StockListItemLayoutBinding
stockListItemLayoutBinding) {
super(stockListItemLayoutBinding.getRoot());
this.stockListItemLayoutBinding = stockListItemLayoutBinding;
}
}
}
public class StockListItem
{
private String StockName;
private String StockPrice;
private String StockPriceChange;
private String StockPriceChangePercent;
public String getStockPriceChange() {
return StockPriceChange;
}
public String getStockPriceChangePercent() {
return StockPriceChangePercent;
}
public void setStockPriceChange(String stockPriceChange) {
StockPriceChange = stockPriceChange;
}
public void setStockPriceChangePercent(String stockPriceChangePercent) {
StockPriceChangePercent = stockPriceChangePercent;
}
public void setStockName(String stockName) {
StockName = stockName;
}
public void setStockPrice(String stockPrice) {
StockPrice = stockPrice;
}
public String getStockName() {
return StockName;
}
public String getStockPrice() {
return StockPrice;
}
public StockListItem(String stockName, String stockPrice, String
stockPriceChange, String stockPriceChangePercent) {
this.StockName = stockName;
this.StockPrice = stockPrice;
this.StockPriceChange = stockPriceChange;
this.StockPriceChangePercent = stockPriceChangePercent;
}
}
This is what stock_list_item_layout.xml looks like.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
type="com.example.mvp1stockmeter.StockListItem"
name="Stock"/>
</data>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?attr/selectableItemBackground"
android:layout_margin="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvStockName"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{Stock.StockName}"
android:layout_alignParentLeft="true"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right">
<TextView
android:id="#+id/tvStockPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="#{Stock.StockPrice}"
android:textSize="20sp"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvStockPriceChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="#{Stock.StockPriceChange}"
android:textSize="16sp"
android:layout_alignParentRight="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="("
android:textSize="16sp"/>
<TextView
android:id="#+id/tvStockPriceChangePercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="#{Stock.StockPriceChangePercent}"
android:textSize="16sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="%)"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</layout>
This is what my activity_main.xml looks like. -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_main"
android:orientation="vertical">
<FrameLayout
android:visibility="visible"
android:id="#+id/flContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
app_bar_main.xml layout file looks like-
<android.support.design.widget.CoordinatorLayout
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=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
The content_main.xml file looks like this-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_main"
android:orientation="vertical">
<FrameLayout
android:visibility="visible"
android:id="#+id/flContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize">
</FrameLayout>
</LinearLayout>
This is how my nav_header_main.xml file looks like-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#mipmap/ic_launcher_round" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="#string/app_name"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/website" />
</LinearLayout>
This is how my activity_main_drawer.xm file looks like-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_watchlist"
android:icon="#drawable/ic_menu_camera"
android:title="Watchlist" />
<item
android:id="#+id/nav_profile"
android:icon="#drawable/ic_menu_manage"
android:title="Profile" />
<item
android:id="#+id/nav_suggestions_or_complaints"
android:icon="#drawable/ic_menu_manage"
android:title="Suggestions / Complaints" />
<item
android:id="#+id/nav_upcoming_features"
android:icon="#drawable/ic_menu_manage"
android:title="Upcoming Features" />
</group>
</menu>
This is how my main.xml file looks like -
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
Just add android:layout_marginTop="?attr/actionBarSize" in your activity_main's FrameLayout, like this:
<FrameLayout
android:visibility="visible"
android:layout_marginTop="?attr/actionBarSize"
android:id="#+id/flContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>

cardview extra space in recycleview android app

I want my cards be one to another,but they spawn with extra space,i don't know how to fix this.
I thought that my problem with recycleView.
//My RVadapter
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
List<Bus> abstractlistbus;
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView bus_name;
TextView controller_Status;
Context mContext;
PersonViewHolder(final View itemView, final Context context) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
mContext=context;
bus_name = (TextView)itemView.findViewById(R.id.name);
controller_Status = (TextView)itemView.findViewById(R.id.Status);
}
}
RVAdapter(List<Bus> abstractlist){
this.abstractlistbus = abstractlist;
}
#Override
public int getItemCount() {
return abstractlistbus.size();
}
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards_layout, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v,v.getContext());
return pvh;
}
#Override
public void onBindViewHolder(/*final*/ PersonViewHolder personViewHolder, final int i) {
personViewHolder.bus_name.setText("Номер автобуса:"+abstractlistbus.get(i).number);
personViewHolder.controller_Status.setText(abstractlistbus.get(i).controller+"");
}
}
//My activity_main.xml
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
</RelativeLayout>
//My cards_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:tag="cards main container">
<android.support.v7.widget.CardView
android:id="#+id/cv"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Android Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/Status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Android Version"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
//And piece of code im MainActivity.java
RecyclerView rv = (RecyclerView) findViewById(R.id.my_recycler_view);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
extra space between cards
enter image description here
enter image description here
enter image description here
in your card linear layout make **android:layout_height="match_parent"** to **android:layout_height="wrap_content"**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:tag="cards main container">
In your cards_layout.xml - add these margins to your linear layout.
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
This will solve your problem.
in you adapter view change CardView's height match_parent to wrap_content
Try it with this tag in your CardView:
app:cardUseCompatPadding="true"

Adding a toolbar causes recyclerview to have a whitespace at bottom (AppBarLayout)

I'm using AppBarLayout and CollapsingToolbarLayout with relativelayout(my hiding-on-scroll content) and Toolbar which includes layout(my pinned item). To show some items I'm using RecyclerView( GridLayoutManager on it) and items which are shown in RecyclerView are CardView(with linearlayout) items.
Problem is that whitespace appears at bottom of RecyclerView when i have my toolbar, and if i delete toolbar whitespace also dissappear. Problem looks like this on device, and it looks normal in editor.
I tried changing both,cardview and recyclerview height and width but nothing helped.
My xml code for cardview which is used for items in recyclerview:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/white_background_rounded_corners"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="156dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/white_background"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/ivCategory"
android:layout_width="156dp"
android:layout_height="100dp"
android:layout_marginTop="8dp"
android:background="#null"
android:clickable="true"
android:contentDescription="#string/category"
android:focusable="true"
android:scaleType="fitCenter"
android:src="#mipmap/android_icon" />
<TextView
android:id="#+id/tvCategoryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="#string/text"
android:textColor="#color/mainColour"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView
And this is my xml code for whole form(form where recyclerview is used):
<android.support.design.widget.CoordinatorLayout
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"
android:background="#color/GrayColour"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax">
<include
android:id="#+id/Header"
layout="#layout/header_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="53dp"/>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="pin"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp">
<include
android:id="#+id/TopBar"
layout="#layout/top_navigation_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/Categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
tools:listitem="#layout/list_item"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.design.widget.CoordinatorLayout>
Also here is my adapter for filling recyclerview with items:
public class Adapter extends FirestoreAdapter<Adapter.ViewHolder> {
private static final String TAG = "Adapter";
public CategoryAdapter(Query query) {
super(query);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new Adapter.ViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false));
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(getSnapshot(position).toObject(Category.class));
}
public class ViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.ivCategory)
ImageView ivCategory;
#BindView(R.id.tvCategoryName)
TextView tvCategoryName;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void bind(final Category category) {
StorageReference storageReference = FirebaseStorage.getInstance().getReference();
StorageReference imageRef = storageReference.child(category.getImage());
Glide.with(ivCategory)
.load(imageRef)
.into(ivCategory);
tvCategoryName.setText(category.getName());
ivCategory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "test");
SecondActivity.start(v.getContext(), category.getName());
}
});
}
}
}
Thank you

Android toolbar is covering a fragment

I have 2 layouts in an app that are being covered by the toolbar. In each layout I've tried using android:layout_below="#id/app_bar_layout or android:layout_below="#id/toolbar, with no luck. How can I fix this?
The recycler view is called by a touch on an menu item and, inflated by rv adpater and view holder classes. So far, I have no other problems with the app except the toolbar covering the views.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.aaron.walkingtourtest09feb.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/app_bar_layout"
android:theme="#android:style/Theme.Material">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:background="#607D8B"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"/>
</android.support.design.widget.CoordinatorLayout>
content_main.xml:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
tools:context="com.example.test09feb.MainActivity"
tools:showIn="#layout/app_bar_main"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<fragment
android:id="#+id/listFragment"
android:tag="unique_tag"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.test09feb.MainActivity$ExampleFragment" >
</fragment>
Recyclerview_activity.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/cardview_light_background"
android:animateLayoutChanges="true"
android:padding="6dp">
<android.support.v7.widget.RecyclerView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rv">
</android.support.v7.widget.RecyclerView>
cards.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cv"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/card_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#EEEEEE"
android:orientation="vertical"
android:padding="0dp">
<ImageView
android:id="#+id/subject_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
<TextView
android:id="#+id/subject_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/subject_photo"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:textColor="#android:color/black"
android:textSize="16sp"/>
<TextView
android:id="#+id/subject_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/subject_name"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#777777"
/>
<Button
android:id="#+id/card_button_left"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/subject_text"
android:layout_toLeftOf="#+id/card_button_right"
android:text="#string/watch"
/>
<Button
android:id="#id/card_button_right"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/subject_text"
android:text="#string/read"/>
<TextView
android:id="#+id/expanded_subject_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card_button_right"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#777777"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
RVAdapter:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.SubjectViewHolder> {
List<Subject> subjects;
static SubjectViewHolder svh;
View v = null;
Context cxt;
private static MyListener listener;
public interface MyListener {
void onClick(View itemView, int viewPosition);
}
RVAdapter(List<Subject> subjects, Context cxt) {
this.subjects = subjects;
this.cxt = cxt;
}
public void setOnClickListener(MyListener listener) {
this.listener = listener;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public SubjectViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards, viewGroup, false);
svh = new SubjectViewHolder(v);
return svh;
}
String[] localLinks = {
"http://192.168.11.111:8000/pic1.png",
"http://192.168.11.111:8000/pic2.jpg",
"http://192.168.11.111:8000/pic3.png",
"http://192.168.11.111:8000/pic4.jpg",
"http://192.168.11.111:8000/pic5.png",
"http://192.168.11.111:8000/pic6.jpg",
"http://192.168.11.111:8000/pic7.png",
"http://192.168.11.111:8000/pic8.jpg",
"http://192.168.11.111:8000/pic9.png",
"http://192.168.11.111:8000/pic10.jpg",
"http://192.168.11.111:8000/pic11.png",
"http://192.168.11.111:8000/pic12.png",
"http://192.168.11.111:8000/pic13.png",
"http://192.168.11.111:8000/pic14.png",
};
#Override
public void onBindViewHolder(SubjectViewHolder subjectViewHolder, int i) {
subjectViewHolder.subjectName.setText(subjects.get(i).subjectName);
subjectViewHolder.subjectText.setText(subjects.get(i).subjectText);
Picasso.with(cxt).load(localLinks[i]).into(subjectViewHolder.subjectPhoto);
subjectViewHolder.expandedSubjectText.setText(subjects.get(i).expandedSubjectText);
subjectViewHolder.expandedSubjectText.setVisibility(View.GONE);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemCount() {
return subjects.size();
}
public static class SubjectViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView subjectName;
TextView subjectText;
TextView expandedSubjectText;
ImageView subjectPhoto;
Button leftButton;
Button rightButton;
SubjectViewHolder(final View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
subjectName = (TextView) itemView.findViewById(R.id.subject_name);
subjectText = (TextView) itemView.findViewById(R.id.subject_text);
expandedSubjectText = (TextView) itemView.findViewById(R.id.expanded_subject_text);
subjectPhoto = (ImageView) itemView.findViewById(R.id.subject_photo);
leftButton = (Button) itemView.findViewById(R.id.card_button_left);
rightButton = (Button) itemView.findViewById(R.id.card_button_right);
rightButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (expandedSubjectText.getVisibility() == View.GONE) {
expandedSubjectText.setVisibility(View.VISIBLE);
} else {
expandedSubjectText.setVisibility(View.GONE);
}
// Triggers click upwards to the adapter on click
if (listener != null)
v.findViewById(R.id.card_container);
listener.onClick(rightButton, svh.getAdapterPosition());
}
});
leftButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Triggers click upwards to the adapter on click
if (listener != null)
listener.onClick(leftButton, svh.getAdapterPosition());
}
});
}
}}
result of adding appbar_scrolling_view_behavior as suggested. A blank bar covers the textview that has just animated itself visible.
You have to add following attribute to the parent view in content_main.xml
app:layout_behavior="#string/appbar_scrolling_view_behavior"
Like this:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test09feb.MainActivity"
tools:showIn="#layout/app_bar_main"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</FrameLayout>
Your Toolbars height is defined by: android:layout_height="?attr/actionBarSize"
so I added a top margin with those values to the list view:
android:layout_marginTop="?attr/actionBarSize"
Worked for me in the app I'm working on.

Android toolbar change layout

I got an activity with many fragments. When i change fragments i need to change also and my toolbar declared in MainActivity.class. My problem is that i include a layout and didnt find a method to change this layout. How to change toolbars layout in this case?
MainActivity.class
#ContentView(R.layout.activity_main)
public class MainActivity extends RoboActionBarActivity {
#InjectView(R.id.tool_bar)
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setToolBar();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new SplashFragment())
.commit();
} else {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new HomeFragment())
.commit();
}
}
private void setToolBar() {
setSupportActionBar(toolbar);
}
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
String currentFragmentClass = fragment.getClass().getSimpleName();
if (currentFragmentClass.equals(getString(R.string.info_fragment))) {
//here i need to change and set onclicks
}
}
}
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"
tools:context=".activities.MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar" />
<View
android:id="#+id/shadow_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/tool_bar"
android:background="#ad8c22" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/shadow_view"
tools:context=".activities.MainActivity"
tools:ignore="MergeRootFrame" />
</RelativeLayout>
tool_bar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorToolBar"
>
<RelativeLayout
android:id="#+id/toolbar_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="toggleSlidingMenu"
android:src="#mipmap/icon_sliding_menu" />
<ImageView
android:id="#+id/app_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#mipmap/jammboree" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/app_logo"
android:padding="5dp">
<ImageView
android:id="#+id/hotlist_bell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="bell"
android:gravity="center"
android:padding="7dp"
android:src="#mipmap/icon_my_cart" />
<TextView
android:id="#+id/hotlist_hot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#id/hotlist_bell"
android:layout_alignTop="#id/hotlist_bell"
android:background="#drawable/rounded_square"
android:gravity="center"
android:minWidth="17sp"
android:padding="2dp"
android:text="2"
android:textColor="#ffffffff"
android:textSize="12sp" />
<TextView
android:id="#+id/myMoneyInMyPocket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/hotlist_bell"
android:text="2000$"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
If I understood right, you want to change the toolbar whenever you change the Fragment the user is currently seeing, isn´t that right?
There is a very simple way to achieve this trough your XML. Let's say you want three different styled toolbars.
1) First, you create XML files for every toolbar you need.
2) Then you include every toolbar in to your activity_main.xml, like this:
<?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"
tools:context=".activities.MainActivity">
<include
android:id="#+id/toolbar1"
layout="#layout/tool_bar1"
android:visibility="visible" />
<include
android:id="#+id/toolbar2"
layout="#layout/tool_bar2"
android:visibility="gone" />
<include
android:id="#+id/toolbar3"
layout="#layout/tool_bar3"
android:visibility="gone" />
// Here is the rest of your XML code
</RelativeLayout>
See how all three toolbar includes have the visibility property?
Well, now you can toy with this property, and show/hide the desired toolbar whenever you want/need to.
For example:
RelativeLayout mLayout = (RelativeLayout) getActivity().findViewById(R.id.my_main_layout);
Toolbar mToolbar1 = (Toolbar) mLayout.findViewById(R.id.toolbar1);
mToolbar1.setVisibility(View.GONE);
Toolbar mToolbar2 = (Toolbar) mLayout.findViewById(R.id.toolbar2);
mToolbar2.setVisibility(View.GONE);
Toolbar mToolbar3 = (Toolbar) mLayout.findViewById(R.id.toolbar3);
mToolbar3.setVisibility(View.VISIBLE);
And just change which one are you making visible, according to your needs.
Hope this helps.
I don`t think the method of #herrmartell is nice;
In my way,I have three Fragment in a Activity,I defined some different layout resources for a common toolbar xml,and load them in toolbar when I click different button; my code :
common_toolbar xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.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="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<!-- your custom layout -->
</android.support.v7.widget.Toolbar>
activity_main_toolbar_common_view.xml
<TextView
android:id="#+id/action_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="迪士尼...ˇ"
android:textColor="#color/white"
android:textSize="#dimen/larget_text_size"
android:paddingLeft="10dp"
android:layout_alignParentStart="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
activity_main_toolbar_mine_view.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mine"
android:textSize="#dimen/big_text_size"
android:textColor="#color/white"
android:gravity="center"
android:layout_centerInParent="true"/>
<ImageView
android:id="#+id/setting"
android:layout_width="#dimen/toolbar_icon_size"
android:layout_height="#dimen/toolbar_icon_size"
android:src="#drawable/toolbar_setting"
android:layout_alignParentRight="true"
/>
switchToolbar() method in Activity:
public void switchToolbar(int layout) {
if(currentToolbarLayout == layout){
return;
}
currentToolbarLayout = layout;
View v = getLayoutInflater().inflate(layout,null);
toolbar.removeAllViews();
toolbar.addView(v);
switch (layout){
case R.layout.activity_main_toolbar_common_view:
v.findViewById(R.id.action_location).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳到位置", Toast.LENGTH_SHORT).show();
}
});
break;
case R.layout.activity_main_toolbar_mine_view:
v.findViewById(R.id.setting).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳到设置", Toast.LENGTH_SHORT).show();
}
});
break;
}
}
I render my toolbar view in the onCreate() method of Activity:
ButterKnife.bind(this);
setSupportActionBar(toolbar);
switchToolbar(R.layout.activity_main_toolbar_common_view);
when I switch toolbar, I call:
switchToolbar(R.layout.activity_main_toolbar_mine_view);

Categories

Resources