Change layout with Navigation Drawer - android

When I start a new "Navigation Drawer" project in Android Studio there is already some code. How can I change the layout by using the Buttons in the Drawer?
The Navigation Drawer looks like this

Try this code:
activity_main.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:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout >
app_bar_main.xml
<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.orgName.myName.projectName.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
android:id="#+id/main_container"
layout="#layout/content_main" />
<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:visibility="gone"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab2"
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_input_add"
android:visibility="gone"/>
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<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"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.orgName.myName.projectName.MainActivity"
tools:showIn="#layout/app_bar_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
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:orientation="vertical">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(0).setChecked(true);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_main, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
} else if (id == R.id.nav_gallery) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_gallery, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
} else if (id == R.id.nav_slideshow) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_slideshow, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
} else if (id == R.id.nav_manage) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_manage, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
} else if (id == R.id.nav_share) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_share, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
} else if (id == R.id.nav_send) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_send, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

look your MainActivity XML, you will find the structure likes below:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- you can custom your layout here in LinearLayout-->
<LinearLayout android:id="#+id/navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="#layout/fragment_navigation_drawer" >
<!-- you can add a button here-->
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mButton"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
you can custom your view in fragment, or instead with a Linearlayout here and fill button in your layout.
mLinearLayout = (LinearLayout) findViewById(R.id.navigation_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mButton = (DrawerLayout) findViewById(R.id.Button1);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//close DrawerLayout here
mDrawerLayout.closeDrawer(mLinearLayout);
}
});
You can use a Fragment to replace FrameLayout:
FragmentTransaction fragmentTransaction1 = fragmentManager
.beginTransaction();
fragmentTransaction1.replace(R.id.content_frame,
overviewFragment);
fragmentTransaction1.commit();

Related

How to make Navigation bar above the fragment page [duplicate]

im new in android im creating an app and i have a problem with a listfragment, because the list its showing but its overlapping the title bar(i had to add margin top to change that), and also im using a drawer and when im trying to open the list it shows over the options drawer too, letme paste you the code and the image so you can understan more:
The xml code:
<?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:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="420dp"
android:id="#android:id/list"
android:layout_weight="0.82"
android:layout_marginTop="60dp" />
</LinearLayout>
The listFregment code:
public class FragmentoPrincipalChofer extends ListFragment {
private List<ParseObject> mViaje;
private ListView mLista;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.fragmento_principal_chofer, container, false);
mLista = (ListView)x.findViewById(android.R.id.list);
return x;
}
#Override
public void onResume() {
super.onResume();
//obtener pedido de taxi
ParseQuery<ParseObject> query = ParseQuery.getQuery("Viaje");
query.whereEqualTo("chofer", "prueba3");
query.addDescendingOrder("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> viajes, com.parse.ParseException e) {
if (e == null) {
mViaje=viajes;
String[] nombreUsuarios = new String[mViaje.size()];
int i=0;
for (ParseObject viaje : mViaje){
nombreUsuarios[i] = viaje.getString("cliente");
i++;
}
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,nombreUsuarios);
mLista.setAdapter(adaptador);
} else {
}
}
});
}
}
the main drawer code:
public class DrawerPrincipal extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,FragmentoPrincipalUsuario.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_principal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//cargar fragment principal usuario
FragmentoPrincipalChofer fragmento=(FragmentoPrincipalChofer) getSupportFragmentManager().findFragmentByTag("fragmento");
if (fragmento==null){
fragmento=new FragmentoPrincipalChofer();
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
}
/*
FragmentoPrincipalUsuario principal = new FragmentoPrincipalUsuario();
android.support.v4.app.FragmentTransaction FragmentTransaction =
getSupportFragmentManager().beginTransaction();
FragmentTransaction.replace(R.id.contenedor_principal,principal);
FragmentTransaction.commit();
*/
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pantalla_principal_usuario, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.cuenta) {
// Handle the camera action
} else if (id == R.id.historial_viajes) {
} else if (id == R.id.contacto) {
} else if (id == R.id.compartir) {
} else if (id == R.id.version) {
} else if (id == R.id.salir) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
list fragment overlapping
here its the activity_drawer_principal.xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_drawer_principal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_drawer_principal"
app:menu="#menu/activity_pantalla_principal_usuario_drawer" />
</android.support.v4.widget.DrawerLayout>
here its the app_bar_drawer_principal:
<?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=".DrawerPrincipal">
<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" />
<FrameLayout
android:id="#+id/contenedor_principal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<include layout="#layout/content_drawer_principal" />
</android.support.design.widget.CoordinatorLayout>
your problem is your setting your list fragment to the whole view Id with this line
// android.R.id.content is the WHOLE screen of your Activity
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
Create a FrameLayout in your content_drawer_principal.xml:
<FrameLayout android:id="#+id/list_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
then do:
transaction.add(R.id.list_content,fragmento,"fragmento");
transaction.commit();
UPDATE
The real problem here is that your telling your FragmentTransaction to load your FragmentoPrincipalChofer into android.R.id.content which is a reserved id
android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID.
Revers the order of your activity layout, put list view brfore any other main content

RecyclerView not showing anything, although there not seem to be any problems

I am a bit comfused at this moment. In order to save time I used Android Studio's template for Navigation Drawer Activity. I was planning to create a Navigation Drawer and pass it through the rest of the activities. The first Activity did go well. The problem is with the second one, as I want to have the Navigation Drawer and create a RecyclerView.
So my code is:
Content_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.sparrowred.sendcard.Main2Activity"
tools:showIn="#layout/activity_main2">
<android.support.v7.widget.RecyclerView
android:id="#+id/imageRecycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
app_bar_main2.xml:
<?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.sparrowred.sendcard.Main2Activity">
<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_main2" />
<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_menu_add" />
</android.support.design.widget.CoordinatorLayout>
nav_header_main2.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="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
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">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:src="#android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio#android.com" />
</LinearLayout>
activity_main2.xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
image_list_row:
<?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="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:contentDescription="#string/image_descrprtion"
android:layout_gravity="center_horizontal"
android:id="#+id/imageId"
android:src="#drawable/profile"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title"
android:textStyle="bold"
android:padding="8dp"
android:layout_gravity="center_horizontal"
android:text="dummy"/>
</LinearLayout>
imageAdapter.java:
package com.sparrowred.sendcard;
public class imageAdapter {
private String title;
private int imageId;
public imageAdapter() {
}
public imageAdapter(String title, int imageId) {
this.title = title;
this.imageId = imageId;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return this.title;
}
public void setImageId(int imageId){
this.imageId = imageId;
}
public int getImageId(){
return this.imageId;
}
}
imageDescriptionArray.java (I need to change the name :P):
package com.sparrowred.sendcard;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class imageDescriptionArray extends RecyclerView.Adapter<imageDescriptionArray.MyViewHolder> {
private LayoutInflater layoutInflater;
private List<imageAdapter> imageAdapterList;
public imageDescriptionArray(List<imageAdapter> imageAdapterList) {
this.imageAdapterList = imageAdapterList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.image_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
imageAdapter iva = imageAdapterList.get(position);
holder.title.setText(iva.getTitle());
holder.image.setImageResource(iva.getImageId());
}
#Override
public int getItemCount() {
return imageAdapterList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView image;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
image = (ImageView) view.findViewById(R.id.imageId);
}
}
}
and finally Main2Activity.java:
package com.sparrowred.sendcard;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import jp.wasabeef.recyclerview.animators.SlideInUpAnimator;
public class Main2Activity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private RecyclerView mImageRecycleView;
private List<imageAdapter> imageAdapterList;
private imageDescriptionArray mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mImageRecycleView = (RecyclerView) findViewById(R.id.imageRecycleView);
mImageRecycleView.setHasFixedSize(true);
imageAdapterList = new ArrayList<>(12);
mAdapter = new imageDescriptionArray(imageAdapterList);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mImageRecycleView.setLayoutManager(mLayoutManager);
mImageRecycleView.setItemAnimator(new SlideInUpAnimator());
mImageRecycleView.setAdapter(mAdapter);
prepareData();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void prepareData(){
List<imageAdapter> data = new ArrayList<>();
int[] images = {R.drawable.birthday_image_1,R.drawable.birthday_image_2,R.drawable.birthday_image_3,R.drawable.birthday_image_4,
R.drawable.birthday_image_5,R.drawable.birthday_image_6,R.drawable.birthday_i mage_7,R.drawable.birthday_image_8,
R.drawable.birthday_image_9,R.drawable.birthday_image_10,R.drawable.birthday_ image_11,R.drawable.birthday_image_12};
String[] description = {"ballons","a lot of cakes","ballons with cakes","presents","presents with hat","cup-cake",
"retro","perspective","flying presents","empty card","giving a present","tasty cake"};
for(int i = 0; i<images.length && i<description.length; i++){
imageAdapter current = new imageAdapter();
current.setTitle(description[i]);
current.setImageId(images[i]);
data.add(current);
}
mAdapter.notifyDataSetChanged();
}
}
This the code for my second activity.It's not fully implemented but for sure the recyclerView doesn't work. Can anyone help me out with this. I believe that the problem is within my xmls .... but I cannot find anything.
PS. My code, when compiled shows no errors....
Try with the following code. Replace your prepareData() with mine
private void prepareData(){
int[] images = {R.drawable.birthday_image_1,R.drawable.birthday_image_2,R.drawable.birthday_image_3,R.drawable.birthday_image_4,
R.drawable.birthday_image_5,R.drawable.birthday_image_6,R.drawable.birthday_i mage_7,R.drawable.birthday_image_8,
R.drawable.birthday_image_9,R.drawable.birthday_image_10,R.drawable.birthday_ image_11,R.drawable.birthday_image_12};
String[] description = {"ballons","a lot of cakes","ballons with cakes","presents","presents with hat","cup-cake",
"retro","perspective","flying presents","empty card","giving a present","tasty cake"};
for(int i = 0; i<images.length && i<description.length; i++){
imageAdapter current = new imageAdapter();
current.setTitle(description[i]);
current.setImageId(images[i]);
imageAdapterList.add(current);
}
mAdapter.notifyDataSetChanged();
}

Change layout in Navigation Drawer

I have a basic app with a sidebar view. Now I want to be able to change the layout by clicking on one of the items in the navigation drawer. How can I do that?
For example i want to get to the layout "layout_gallery" by clicking on "Gallery" in the navigation drawer.
Here's the code of the MainActivity.java:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Thanks in advance
Try this one:
activity_main.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:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_main.xml
<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.orgName.myName.projectName.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
android:id="#+id/main_container"
layout="#layout/content_main" />
<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:visibility="gone"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab2"
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_input_add"
android:visibility="gone"/>
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<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"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.orgName.myName.projectName.MainActivity"
tools:showIn="#layout/app_bar_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
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:orientation="vertical">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_main, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
} else if (id == R.id.nav_gallery) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_gallery, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
} else if (id == R.id.nav_slideshow) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_slideshow, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
} else if (id == R.id.nav_manage) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_manage, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
} else if (id == R.id.nav_share) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_share, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
} else if (id == R.id.nav_send) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id. main_container);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.content_send, null);
mainLayout.removeAllViews();
mainLayout.addView(layout);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Use fragment for multiple classes and layout.
You can find an example here

How to make Focus to FloatingActionButton Even if the Navigation drawer is open in Android

I want to make focus on FloatingActionButton Even if my Navigation Drawer is open.
I know when the navigation Drawer is open then rest of the screen is unfocusable by using setScrimColor this we can reduce the focus on Navigation Drawer. But I want to make FloatingActionButton is focusable.
Library is below for ActionButton
https://github.com/shell-software/fab
see the below screenshot for my problem.
Normal Screen :
Click on FloatingButton :
What I want like this Focusable :
activity_main.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:fab="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:layout_gravity="end"
android:fitsSystemWindows="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<com.example.softeng.animationf.fabdirectory.ActionButton
android:id="#+id/fab_activity_action_button"
style="#style/fab_action_button_style"
fab:type="MINI"/>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="end">
<RelativeLayout
android:id="#+id/right_navigation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#4D4D4D">
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigation_view;
int count = 1;
private boolean isOutSideClicked = false;
RelativeLayout relativeLayout;
private ActionButton actionButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
/* drawerLayout.setScrimColor(Color.parseColor("#00000000"));*/
navigation_view = (NavigationView)findViewById(R.id.navigation_view);
relativeLayout = (RelativeLayout)findViewById(R.id.right_navigation);
actionButton = (ActionButton) findViewById(R.id.fab_activity_action_button);
actionButton.setImageResource(R.drawable.fab_plus_icon);
actionButton.setRippleEffectEnabled(true);
actionButton.setShadowRadius(0);
actionButton.setShadowXOffset(0);
actionButton.setShadowYOffset(0);
actionButton.setButtonColor(Color.parseColor("#0072BA"));
actionButton.setButtonColorPressed(Color.parseColor("#004F80"));
actionButton.setShadowRadius(10);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count == 1) {
actionButton.moveLeft(200);
actionButton.setImageResource(R.drawable.close);
drawerLayout.openDrawer(Gravity.RIGHT);
count = count - 1;
}else if(count == 0){
closeFab();
}
else {
}
}
});
}
private void closeFab(){
actionButton.move(new MovingParams(MainActivity.this, 200 , 0));
actionButton.setImageResource(R.drawable.fab_plus_icon);
count = count + 1;
drawerLayout.closeDrawer(Gravity.RIGHT);
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (drawerLayout.isDrawerOpen(navigation_view)) {
View content = findViewById(R.id.drawer_layout);
int[] contentLocation = new int[2];
content.getLocationOnScreen(contentLocation);
Rect rect = new Rect(contentLocation[0],
contentLocation[1],
contentLocation[0] + content.getWidth(),
contentLocation[1] + content.getHeight());
if (!(rect.contains((int) event.getX(), (int) event.getY()))) {
isOutSideClicked = true;
} else {
isOutSideClicked = false;
this.closeFab();
}
}
}
return super.dispatchTouchEvent(event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The Rounded Image means FloatingActionButton i want to focusable. Any Help be Appreciated.
Found the solution to your problem. I have done some changes in your Layout as well as in Java code. Please refer and let me know if there are any queries.
MainActivity.java
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigation_view;
ImageView view;
int count = 1;
private boolean isOutSideClicked = false;
RelativeLayout relativeLayout;
private ActionButton actionButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (ImageView)findViewById(R.id.view);
if(getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerLayout.setScrimColor(Color.parseColor("#00000000"));
navigation_view = (NavigationView) findViewById(R.id.navigation_view);
relativeLayout = (RelativeLayout)findViewById(R.id.right_navigation);
actionButton = (ActionButton) findViewById(R.id.fab_activity_action_button);
actionButton.setImageResource(R.drawable.fab_plus_icon);
actionButton.setRippleEffectEnabled(true);
actionButton.setShadowRadius(0);
actionButton.setShadowXOffset(0);
actionButton.setShadowYOffset(0);
actionButton.setButtonColor(Color.parseColor("#0072BA"));
actionButton.setButtonColorPressed(Color.parseColor("#004F80"));
actionButton.setShadowRadius(10);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count == 1) {
actionButton.moveLeft(200);
actionButton.setImageResource(R.drawable.fab_plus_icon);
drawerLayout.openDrawer(Gravity.RIGHT);
view.setVisibility(View.VISIBLE);
actionButton.bringToFront();
count = count - 1;
}else if(count == 0){
closeFab();
}
else {
}
}
});
}
private void closeFab(){
view.setVisibility(View.GONE);
actionButton.move(new MovingParams(MainActivity.this, 200 , 0));
actionButton.setImageResource(R.drawable.fab_plus_icon);
count = count + 1;
drawerLayout.closeDrawer(Gravity.RIGHT);
}
// dispatchTouchEvent is as it was not putting code here..!!
}
activity_main.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:fab="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:layout_gravity="end"
android:fitsSystemWindows="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
android:visibility="gone" />
<com.example.softeng.animationf.fabdirectory.ActionButton
android:id="#+id/fab_activity_action_button"
style="#style/fab_action_button_style"
android:layout_gravity="bottom|right"
fab:type="MINI"/>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="end">
<RelativeLayout
android:id="#+id/right_navigation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#4D4D4D">
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Please use this same code. I recommend you do copy-paste because this is working exactly as you wanted..!!
use Frame layout as a parent ViewGroup as it uses z index so place
FAB floating action button at the end
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_search"
android:elevation="50dp"
android:layout_gravity="left|bottom" />
</FrameLayout>

listfragment overlapping my main drawer

im new in android im creating an app and i have a problem with a listfragment, because the list its showing but its overlapping the title bar(i had to add margin top to change that), and also im using a drawer and when im trying to open the list it shows over the options drawer too, letme paste you the code and the image so you can understan more:
The xml code:
<?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:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="420dp"
android:id="#android:id/list"
android:layout_weight="0.82"
android:layout_marginTop="60dp" />
</LinearLayout>
The listFregment code:
public class FragmentoPrincipalChofer extends ListFragment {
private List<ParseObject> mViaje;
private ListView mLista;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.fragmento_principal_chofer, container, false);
mLista = (ListView)x.findViewById(android.R.id.list);
return x;
}
#Override
public void onResume() {
super.onResume();
//obtener pedido de taxi
ParseQuery<ParseObject> query = ParseQuery.getQuery("Viaje");
query.whereEqualTo("chofer", "prueba3");
query.addDescendingOrder("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> viajes, com.parse.ParseException e) {
if (e == null) {
mViaje=viajes;
String[] nombreUsuarios = new String[mViaje.size()];
int i=0;
for (ParseObject viaje : mViaje){
nombreUsuarios[i] = viaje.getString("cliente");
i++;
}
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,nombreUsuarios);
mLista.setAdapter(adaptador);
} else {
}
}
});
}
}
the main drawer code:
public class DrawerPrincipal extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,FragmentoPrincipalUsuario.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_principal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//cargar fragment principal usuario
FragmentoPrincipalChofer fragmento=(FragmentoPrincipalChofer) getSupportFragmentManager().findFragmentByTag("fragmento");
if (fragmento==null){
fragmento=new FragmentoPrincipalChofer();
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
}
/*
FragmentoPrincipalUsuario principal = new FragmentoPrincipalUsuario();
android.support.v4.app.FragmentTransaction FragmentTransaction =
getSupportFragmentManager().beginTransaction();
FragmentTransaction.replace(R.id.contenedor_principal,principal);
FragmentTransaction.commit();
*/
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.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pantalla_principal_usuario, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.cuenta) {
// Handle the camera action
} else if (id == R.id.historial_viajes) {
} else if (id == R.id.contacto) {
} else if (id == R.id.compartir) {
} else if (id == R.id.version) {
} else if (id == R.id.salir) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
list fragment overlapping
here its the activity_drawer_principal.xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_drawer_principal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_drawer_principal"
app:menu="#menu/activity_pantalla_principal_usuario_drawer" />
</android.support.v4.widget.DrawerLayout>
here its the app_bar_drawer_principal:
<?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=".DrawerPrincipal">
<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" />
<FrameLayout
android:id="#+id/contenedor_principal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<include layout="#layout/content_drawer_principal" />
</android.support.design.widget.CoordinatorLayout>
your problem is your setting your list fragment to the whole view Id with this line
// android.R.id.content is the WHOLE screen of your Activity
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
Create a FrameLayout in your content_drawer_principal.xml:
<FrameLayout android:id="#+id/list_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
then do:
transaction.add(R.id.list_content,fragmento,"fragmento");
transaction.commit();
UPDATE
The real problem here is that your telling your FragmentTransaction to load your FragmentoPrincipalChofer into android.R.id.content which is a reserved id
android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID.
Revers the order of your activity layout, put list view brfore any other main content

Categories

Resources