Fragment is occupying full screen in activity - android

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>

Related

Android Data Binding cannot id inside <merge> for zxing-android-embedded

Dependency reference: journeyapps/zxing-android-embedded
Description of the problem:
I'm new to Android Data binding & cannot bind the resource id zxing_viewfinder_view which is inside <merge> </merge>
Here is my code for understanding.
activity_scan_qr.xml
<LinearLayout
android:id="#+id/ll_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#+id/ll_header"
android:background="#color/color_white"
android:gravity="center"
android:orientation="vertical">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="#+id/zxing_barcode_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:zxing_scanner_layout="#layout/view_custom_qr_scanner"/>
</LinearLayout>
view_custom_qr_scanner.xml
<?xml version="1.0" encoding="utf-8"?>
<merge 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">
<com.journeyapps.barcodescanner.BarcodeView
android:id="#+id/zxing_barcode_surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:zxing_framing_rect_height="250dp"
app:zxing_framing_rect_width="250dp" />
<com.journeyapps.barcodescanner.ViewfinderView
android:id="#+id/zxing_viewfinder_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:zxing_possible_result_points="#color/zxing_custom_possible_result_points"
app:zxing_result_view="#color/zxing_custom_result_view"
app:zxing_viewfinder_laser="#color/zxing_custom_viewfinder_laser"
app:zxing_viewfinder_laser_visibility="true"
app:zxing_viewfinder_mask="#color/zxing_custom_viewfinder_mask" />
<TextView
android:id="#+id/zxing_status_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/zxing_transparent"
android:text="Place a QR code inside the scan area."
android:textColor="#color/zxing_status_text" />
</merge>
ScanQRActivity.java
public class ScanQRActivity extends AppCompatActivity implements DecoratedBarcodeView.TorchListener {
public static final String TAG = ScanQRActivity.class.getSimpleName();
Context mContext;
Activity mActivity;
ActivityScanQrBinding binding;
ActionBar actionBar;
private CaptureManager capture;
boolean bFlashLight = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityScanQrBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
mContext = this;
mActivity = this;
capture = new CaptureManager(this, binding.zxingBarcodeScanner);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.setShowMissingCameraPermissionDialog(true);
capture.decode();
changeLaserVisibility(true);
}
public void changeLaserVisibility(boolean visible) {
binding.zxingViewfinderView.setLaserVisibility(visible);
}
}
To use data binding you need to place your layout inside tag.
Your activity.xml
<layout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/ll_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#+id/ll_header"
android:background="#color/color_white"
android:gravity="center"
android:orientation="vertical">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="#+id/zxing_barcode_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:zxing_scanner_layout="#layout/view_custom_qr_scanner"/>
</LinearLayout>
</layout>
Your view_custom_qr_scanner.xml file should be
<layout>
<merge 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">
<com.journeyapps.barcodescanner.BarcodeView
android:id="#+id/zxing_barcode_surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:zxing_framing_rect_height="250dp"
app:zxing_framing_rect_width="250dp" />
<com.journeyapps.barcodescanner.ViewfinderView
android:id="#+id/zxing_viewfinder_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:zxing_possible_result_points="#color/zxing_custom_possible_result_points"
app:zxing_result_view="#color/zxing_custom_result_view"
app:zxing_viewfinder_laser="#color/zxing_custom_viewfinder_laser"
app:zxing_viewfinder_laser_visibility="true"
app:zxing_viewfinder_mask="#color/zxing_custom_viewfinder_mask" />
<TextView
android:id="#+id/zxing_status_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/zxing_transparent"
android:text="Place a QR code inside the scan area."
android:textColor="#color/zxing_status_text" />
</merge>
</layout>
In your activity, create a new variable of type 'binding' that would be
private ViewCustomQrScannerBinding bindingCustomScanner
, now in your "OnCreate" under "binding = ActivityScanQrBinding.inflate (getLayoutInflater ());"
add this line
bindingCustomScanner = ViewCustomQrScannerBinding.bind (binding.root)
now when you call the properties of the new view, you call the new 'bindingCustomScanner'

RecyclerView not displaying last item but is calling onBindView

I am trying a list of items using RecyclerView , everything works fine but the last item is not being displayed! The length of the list is 2 and onBindView is called twice and the when I used Log to print the items it aslo prints the last item. So the list is fine but the RecyclerView is not displaying it.
This is my code RecyclerView Adapter:
public class ModuleList extends RecyclerView.Adapter<ModuleList.ViewHolder> {
private List<Module> moduleList;
public ModuleList(List<Module> moduleList){
this.moduleList = moduleList;
}
public ModuleList(){
this.moduleList = new ArrayList<>();
}
public List<Module> getModuleList() {
return moduleList;
}
public void setModuleList(List<Module> moduleList) {
this.moduleList = moduleList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_modules,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Module module = moduleList.get(position);
// function is called twice and the all items are logged works fine
Log.d("ALL",module.getModuleName());
Log.d("ALL", String.valueOf(module.getNumberOfLevels()));
holder.moduleName.setText(String.valueOf(module.getModuleName()));
holder.numberOfLevels.setText(String.valueOf(module.getNumberOfLevels()));
}
#Override
public int getItemCount() {
//size is 2
return moduleList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView moduleName;
public TextView numberOfLevels;
public ViewHolder(View itemView) {
super(itemView);
moduleName = (TextView) itemView.findViewById(R.id.list_module_name);
numberOfLevels = (TextView) itemView.findViewById(R.id.list_no_of_levels);
}
}
}
This is my xml where recyclerview is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/all_modules_list"
android:scrollbars="vertical"
/>
</LinearLayout>
This is my xml where RecyclerView Fragment is displayed:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_screen"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_screen_container"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="#+id/main_screen_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
</LinearLayout>
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="#+id/navigation_items"
android:layout_gravity="start"
android:background="#android:color/white"
>
</ListView>
</android.support.v4.widget.DrawerLayout>
Edit:list_module.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_module_name"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_no_of_levels"
/>
</LinearLayout>
Hey can you try the solution below?
Change this in the list_module.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="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/list_module_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/list_no_of_levels"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I have replaced match_parent to wrap_content to parent layout.

Android RecyclerView Cards not showing`

I am new to Android. I have moved heaven and earth to get my app to show RecyclerView with Cards but am not able to,no matter what. Tried a lot of searching on Google and StackOverflow and also on a lot of recommended sites.
My code is as follows:
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
...
private RecyclerView mRecyclerView;
protected void onCreate(Bundle savedInstanceState) {
...
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
TransactionRecyclerViewAdapter adapter=new TransactionRecyclerViewAdapter(this, getDataSet());
mRecyclerView.setAdapter(adapter);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
...
}
private ArrayList<FavoriteTransaction> getDataSet() {
ArrayList results = new ArrayList<FavoriteTransaction>();
FavoriteTransaction favoriteTransaction1 = new FavoriteTransaction();
favoriteTransaction1.setAmount("11");
favoriteTransaction1.setBody("11 ka recharge");
results.add(favoriteTransaction1);
FavoriteTransaction favoriteTransaction2 = new FavoriteTransaction();
favoriteTransaction2.setAmount("12");
favoriteTransaction2.setBody("12 ka recharge");
results.add(favoriteTransaction2);
FavoriteTransaction favoriteTransaction3 = new FavoriteTransaction();
favoriteTransaction3.setAmount("13");
favoriteTransaction3.setBody("13 ka recharge");
results.add(favoriteTransaction3);
FavoriteTransaction favoriteTransaction4 = new FavoriteTransaction();
favoriteTransaction4.setAmount("14");
favoriteTransaction4.setBody("14 ka recharge");
results.add(favoriteTransaction4);
FavoriteTransaction favoriteTransaction5 = new FavoriteTransaction();
favoriteTransaction5.setAmount("15");
favoriteTransaction5.setBody("15 ka recharge");
results.add(favoriteTransaction5);
return results;
}
}
activity_home.xml
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorgray">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<include layout="#layout/content_home" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/activity_home_drawer"
app:headerLayout="#layout/nav_header"/>
</android.support.v4.widget.DrawerLayout>
content_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:paddingBottom="55dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<include
android:id="#+id/card_list_fav_transact"
layout="#layout/card_list_fav_transact"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
card_list_fav_transact.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/info_text_sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/hdr_lbl_fav"
android:layout_marginBottom="5dp"/>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:scrollbars="vertical"
/>
</LinearLayout>
card_transaction_row.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorwhite">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorwhite">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:text="190"
android:textColor="#color/colorPrimary"
android:id="#+id/tv_fav_amt"
android:background="#drawable/stl_tv_blue_circle"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:layout_toRightOf="#+id/tv_fav_amt"
android:layout_toEndOf="#+id/tv_fav_amt">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Body"
android:id="#+id/fav_body"
android:clickable="true"
android:background="#drawable/stl_btn_underline"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="SubText"
android:id="#+id/fav_subtext"
android:clickable="true"
android:background="#drawable/stl_btn_underline"/>
</LinearLayout>
<Button
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy"
android:id="#+id/btn_tr_buy"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:background="#drawable/stl_btn_blue"
android:textColor="#android:color/white"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
TransactionRecyclerViewAdapter.java
public class TransactionRecyclerViewAdapter extends RecyclerView.Adapter<TransactionViewHolder> {
private ArrayList<FavoriteTransaction> transactionList;
private Context context;
private LayoutInflater inflater;
public TransactionRecyclerViewAdapter(Context context, ArrayList<FavoriteTransaction> myDataset) {
this.transactionList = new ArrayList<>(myDataset);
this.context = context;
this.inflater = LayoutInflater.from(context);
}
#Override
public int getItemCount() {
return this.transactionList.size();
}
#Override
public void onBindViewHolder(TransactionViewHolder contactViewHolder, int i) {
FavoriteTransaction favTransaction = transactionList.get(i);
contactViewHolder.tvAmount.setText(favTransaction.getAmount());
}
#Override
public TransactionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v=inflater.inflate(R.layout.card_transaction_row, parent, false);
TransactionViewHolder viewHolder=new TransactionViewHolder(v);
return viewHolder;
}
}
TransactionViewHolder.java
public class TransactionViewHolder extends RecyclerView.ViewHolder {
TextView tvAmount, tvBody, tvSubText;
public TransactionViewHolder(View itemView) {
super(itemView);
tvAmount = (TextView) itemView.findViewById(R.id.tv_fav_amt);
tvBody = (TextView) itemView.findViewById(R.id.fav_body);
tvSubText = (TextView) itemView.findViewById(R.id.fav_subtext);
}
}
FavoriteTransaction.java
public class FavoriteTransaction {
private int transactionId;
private String amount;
private String subText, body;
public int getTransactionId() {
return transactionId;
}
public void setTransactionId(int transactionId) {
this.transactionId = transactionId;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getSubText() {
return subText;
}
public void setSubText(String subText) {
this.subText = subText;
}
}
When I execute my app, I am not able to see the recycler with the cards. Can someone help me where I am going wrong. Just point me in the right direction.
Thanks in advance.
You wrote findViewById to the Activity class. However RecyclerView is not in your activity_home.xml, it is in nested layout.
Try defining the included layout and apply findViewById on that view, or simply define RecyclerView in your activity_home.xml
Why So?
Activity.findViewById(int id) method finds the id inside that particular Activity's layout. If you have nested layout, for example, like this activity_home.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<include
android:id="#+id/view_card_list_fav_transact"
layout="#layout/card_list_fav_transact"/>
</android.support.v4.widget.DrawerLayout>
and your card_list_fav_transact.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:scrollbars="vertical"/>
</LinearLayout>
Code inside your Activity will be
View includedView = findViewById(R.id.view_card_list_favt_transact);
RecyclerView rv = includedView.findViewById(R.id.my_recycler_view);
And you are all set!

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.

Use Custom Layout in NavigationDrawer With Header And list

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();
}

Categories

Resources