When I change the layout of a fragment within an activity, the menu options in the action bar seem to stop working. The buttons (Search icon and NavDrawer icon) become unresponsive.
This is the layout file of the fragment with the working menu options:
activity_login.xml (working)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LoginFragment"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Login."
android:id="#+id/login_title_text"
android:layout_alignParentTop="true"
android:textSize="40sp"
android:layout_marginTop="100dp"
android:fontFamily="sans-serif-light"
android:textColor="#color/colorAlt"
android:layout_centerHorizontal="true" />
<!-- E-mail section -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="E-mail address"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:ems="10"
android:id="#+id/login_emailField"
android:layout_above="#+id/login_passwordField" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/register_email_icon"
android:layout_alignBottom="#id/login_emailField"
android:layout_marginLeft="15dp"
android:layout_marginBottom="10dp"/>
<!-- Password section -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/register_password_icon"
android:layout_alignBottom="#id/login_passwordField"
android:layout_marginLeft="15dp"
android:layout_marginBottom="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginTop="10dp"
android:hint="Password"
android:id="#+id/login_passwordField"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="25dp" />
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/login_login_button"
android:layout_gravity="center_vertical"
android:layout_below="#+id/login_passwordField"
android:layout_marginBottom="29dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
card_view:cardBackgroundColor="#E91E63"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="6dp">
<TextView
android:id="#+id/material_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:layout_gravity="center_horizontal"
android:textColor="#FFF"
android:textSize="19sp"
android:fontFamily="sans-serif-medium"
android:text="Login" />
</android.support.v7.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Forgotten your username or password?"
android:layout_below="#id/login_login_button"
android:gravity="center"
android:layout_centerHorizontal="true"
android:id="#+id/login_footer_text"
android:layout_marginTop="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tap here"
android:textColor="#000000"
android:layout_below="#id/login_footer_text"
android:gravity="center"
android:layout_centerHorizontal="true"
android:id="#+id/loginForgottenTapText"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</LinearLayout>
This is the layout file that seems to break the buttons:
activity_login.xml (non-working)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LoginFragment"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Login."
android:id="#+id/login_title_text"
android:layout_alignParentTop="true"
android:textSize="40sp"
android:layout_marginTop="100dp"
android:fontFamily="sans-serif-light"
android:textColor="#color/colorAlt"
android:layout_centerHorizontal="true" />
<!-- E-mail section -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="E-mail address"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:ems="10"
android:id="#+id/login_emailField"
android:layout_below="#+id/login_title_text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/register_email_icon"
android:layout_alignBottom="#id/login_emailField"
android:layout_marginLeft="15dp"
android:layout_marginBottom="10dp"/>
<!-- Password section -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginTop="10dp"
android:hint="Password"
android:layout_below="#id/login_emailField"
android:id="#+id/login_passwordField"
android:layout_marginLeft="50dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="25dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/register_password_icon"
android:layout_alignBottom="#id/login_passwordField"
android:layout_marginLeft="15dp"
android:layout_marginBottom="10dp"/>
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/login_login_button"
android:layout_gravity="center_vertical"
android:layout_below="#+id/login_passwordField"
android:layout_marginBottom="29dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
card_view:cardBackgroundColor="#E91E63"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="6dp">
<TextView
android:id="#+id/material_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:layout_gravity="center_horizontal"
android:textColor="#FFF"
android:textSize="19sp"
android:fontFamily="sans-serif-medium"
android:text="Login" />
</android.support.v7.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Forgotten your username or password?"
android:layout_below="#id/login_login_button"
android:gravity="center"
android:layout_centerHorizontal="true"
android:id="#+id/login_footer_text"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tap here"
android:textColor="#000000"
android:layout_below="#id/login_footer_text"
android:gravity="center"
android:layout_centerHorizontal="true"
android:id="#+id/loginForgottenTapText"
android:layout_marginTop="10dp"/>
</RelativeLayout>
</ScrollView>
MainActivity.java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, LoginFragment.OnLoginCallback {
MenuItem register;
MenuItem login;
MenuItem logout;
MenuItem completeListing;
MenuItem expiredListing;
MenuItem activeListing;
NavigationView navigationView;
UserCredentialHandler userStatus;
protected static final String KEY_USER_STATUS = "USER_STATUS";
protected static final String USER_PREFS = "userNamePrefs";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userStatus = new UserCredentialHandler();
final HomeFragment homeFragment = new HomeFragment();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), CreateListingActivity.class);
startActivity(i);
}
});
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
getMenuItems(navigationView);
if(userStatus.checkIfUserIsLoggedIn(getApplicationContext())){
userStatus.setNavHeaderOnLogin(getApplicationContext(), navigationView);
updateNavDrawer("login", register, login, logout, expiredListing, completeListing, activeListing);
} else {
userStatus.setNavHeaderOnLogout(navigationView);
updateNavDrawer("logout", register, login, logout, expiredListing, completeListing, activeListing);
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.main_container,homeFragment);
fragmentTransaction.commit();
}
#Override
public void onLoginSuccess() {
HomeFragment homeFragment = new HomeFragment();
userStatus.setNavHeaderOnLogin(getApplicationContext(), navigationView);
updateNavDrawer("login",register,login,logout, expiredListing, completeListing, activeListing);
register.setVisible(false);
login.setVisible(false);
logout.setVisible(true);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.main_container,homeFragment);
fragmentTransaction.commit();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
getSupportFragmentManager().popBackStack();
}
}
#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_activity_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;
} else if (id == R.id.search_mag_icon){
SearchFragment searchFragment = new SearchFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_container, searchFragment)
.addToBackStack(null)
.commit();
}
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_register) {
final RegisterFragment registerFragment = new RegisterFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_container, registerFragment)
.addToBackStack(null)
.commit();
} else if (id == R.id.nav_login) {
final LoginFragment loginFragment = new LoginFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_container, loginFragment)
.addToBackStack(null)
.commit();
} else if(id == R.id.nav_logout) {
userStatus.logoutUser(getApplicationContext());
userStatus.setNavHeaderOnLogout(navigationView);
updateNavDrawer("logout",register,login,logout, expiredListing, completeListing, activeListing);
} else if (id == R.id.nav_my_listings) {
// navigate to my listings
} else if (id == R.id.nav_how) {
// navigate to how it works page
} else if (id == R.id.nav_help) {
// navigate to help page
} else if (id == R.id.nav_contact_us) {
// navigate to contact page,
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void updateNavDrawer(String action, MenuItem register, MenuItem login, MenuItem logout, MenuItem expiredListing, MenuItem completeListing, MenuItem activeListing){
if(action=="login"){
register.setVisible(false);
login.setVisible(false);
logout.setVisible(true);
completeListing.setVisible(true);
activeListing.setVisible(true);
expiredListing.setVisible(true);
} else {
register.setVisible(true);
login.setVisible(true);
logout.setVisible(false);
completeListing.setVisible(false);
activeListing.setVisible(false);
expiredListing.setVisible(false);
}
}
private void getMenuItems(NavigationView nv){
register = nv.getMenu().getItem(0);
login = nv.getMenu().getItem(1);
logout = nv.getMenu().getItem(2);
activeListing = nv.getMenu().getItem(3);
completeListing = nv.getMenu().getItem(4);
expiredListing = nv.getMenu().getItem(5);
}
}
I cannot seem to workout why changing the layout is breaking the ActionBar menu options, any help would be appreciated.
Edit:
Changing the <ScrollView> element to <LinearLayout> seems to solve it but I need a ScrollView. The ScrollView seems to overlap the ActionBar and is on the top.
Try setting android:layout_height on the ScrollView to "match_parent" instead of "wrap_content"
Related
I tried to create some button on my home activity, but I do not know what is the problem with the button that I created. It does not appear in my home activity.
I am totally new to Android Studio, so I don´t know how to write my code that it will work the way I want it. Can you help me please...
This is the XML code (design)
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sos"
android:layout_centerHorizontal="true"
android:layout_marginStart="50dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
android:textStyle="bold"
android:backgroundTint="#df2c14"
android:textColor="#color/teal_200"
android:textSize="18dp"
android:text="Emergency"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/danger"
android:layout_centerHorizontal="true"
android:layout_marginStart="50dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
android:textStyle="bold"
android:backgroundTint="#df2c14"
android:textColor="#color/teal_200"
android:textSize="18dp"
android:text="Emergency danger"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/accident"
android:layout_centerHorizontal="true"
android:layout_marginStart="50dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
android:textStyle="bold"
android:backgroundTint="#df2c14"
android:textColor="#color/teal_200"
android:textSize="18dp"
android:text="Emergency accident"
/>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
app:title="SOS"
app:titleTextColor="#color/material_dynamic_neutral_variant20"
app:titleMarginStart="150dp"
app:titleMarginBottom="600dp"
android:background="#color/material_dynamic_neutral_variant20"
/>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/nav_view"
app:headerLayout="#layout/header"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
/>
And this is Home code :
public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer_Layout;
NavigationView navigationView;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
drawer_Layout = findViewById(R.id.drawerLayout);
navigationView = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView.bringToFront();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer_Layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer
_close);
drawer_Layout.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menuHome:
break;
case R.id.menuPhone:
Intent intent = new Intent(Home.this, phone.class);
startActivity(intent);
break;
case R.id.menuMsos:
Intent intent1 = new Intent(Home.this, Msos.class);
startActivity(intent1);
break;
}
return true;
}
}
I want to add ScrollView to my navigation drawer, I first tried to but the linear layout as a child under Scrollview, but when I opened my fragment, the navigation drawer shows under my fragment. I want to have my fragments untouched, whereas the navigation drawer does now show behind the fragments, and the navigation drawer has scrollview.
drawer code:
public class drawer_home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
Toolbar toolbar;
TextView mStartDate, mEndDate, mPlan, mDays;
FirebaseAuth firebaseAuth;
FirebaseUser user;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_home);
toolbar = findViewById(R.id.tb);
mStartDate = findViewById(R.id.startDateTV);
mEndDate = findViewById(R.id.endDateTV);
mPlan = findViewById(R.id.planTV);
mDays = findViewById(R.id.daysTV);
setSupportActionBar(toolbar);
/*TextView textView = (TextView)toolbar.findViewById(R.id.toolbartv);
textView.setText("No Excusas");
getSupportActionBar().setDisplayShowTitleEnabled(false);*/
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new androidx.fragment.app.Fragment()).commit();
navigationView.setCheckedItem(R.id.nav_homeMain);
}
firebaseAuth = FirebaseAuth.getInstance();
user = firebaseAuth.getCurrentUser();
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("Users");
Query query = databaseReference.orderByChild("email").equalTo(user.getEmail());
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
//checkc until requiered data get
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String startdate = "Fecha de Inicio = " + ds.child("Start Date").getValue();
String enddate = "Fecha de Salida = " + ds.child("End Date").getValue();
String plan = "Plan = " + ds.child("Plan").getValue();
String days = "Tu plan acaba en " + ds.child("Days Left").getValue() +" dias ";
mStartDate.setText(startdate);
mEndDate.setText(enddate);
mPlan.setText(plan);
mDays.setText(days);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_homeMain:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new androidx.fragment.app.Fragment()).commit();
break;
case R.id.nav_qr:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new QRcodeFragment()).commit();
break;
case R.id.nav_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new UserProfile()).commit();
break;
case R.id.nav_order:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new OrdersActivity()).commit();
break;
case R.id.nav_nutrition:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new NutritionActivity()).commit();
break;
case R.id.nav_chat:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ContactActivity()).commit();
break;
case R.id.nav_coach:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new CoachesActivity()).commit();
break;
case R.id.nav_information:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AboutUsActivity()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
xml:
<androidx.drawerlayout.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"
android:background="#drawable/homebcgself"
tools:context=".drawer_home"
tools:openDrawer="start">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/tb"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:background="#drawable/toolbar_bg"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<TextView
android:id="#+id/toolbartv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:layout_gravity="center"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
android:text="No Excusas"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#ffffff"
android:textSize="40sp"
android:textStyle="bold" />
</androidx.appcompat.widget.Toolbar>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/startDateTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="200dp"
android:textColor="#color/colorWhite"
android:textSize="30sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorWhite" />
<TextView
android:id="#+id/endDateTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginStart="5dp"
android:textColor="#color/colorWhite"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorWhite" />
<TextView
android:id="#+id/planTV"
android:layout_width="match_parent"
android:textSize="30sp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginStart="5dp"
android:textColor="#color/colorWhite"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorWhite" />
<TextView
android:id="#+id/daysTV"
android:layout_width="match_parent"
android:textSize="30sp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginStart="5dp"
android:textColor="#color/colorWhite"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorWhite" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
example of scrolling I want for the main content:
when the main content is scrollable, it shows under the first fragment
I think you have to set
<com.google.android.material.navigation.NavigationView
...
android:fitsSystemWindows="true"
android:isScrollContainer="true"
... />
Had a lot of help and figured it out.
Add scrollview to separate fragment and incorporate in the onCreate.
'''
ScrollView
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/startDateTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="200dp"
android:textColor="#color/colorWhite"
android:textSize="30sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorWhite" />
<TextView
android:id="#+id/endDateTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginStart="5dp"
android:textColor="#color/colorWhite"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorWhite" />
<TextView
android:id="#+id/planTV"
android:layout_width="match_parent"
android:textSize="30sp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginStart="5dp"
android:textColor="#color/colorWhite"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorWhite" />
<TextView
android:id="#+id/daysTV"
android:layout_width="match_parent"
android:textSize="30sp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginStart="5dp"
android:textColor="#color/colorWhite"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorWhite" />
</LinearLayout>
I'm trying to make a navigation drawer that opens a new activity when the user intent an item.
I don't know how to do this?
There is 5 items in my drawer and I want each of them to open a different activity using intent not fragment .
when i click to the item anythink do , i need to go in other activity using intent
My code is the following
public class menu extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
TextView tt1 , txtnom , txtadress;
Typeface ty;
ViewPager viewp;
TabLayout tab;
ImageView img;
private DrawerLayout mdrawer;
private ActionBarDrawerToggle mtoggle;
private int[] tabIcons = {
R.drawable.ic_tablayout_tache,
R.drawable.ic_ic_tablayout_rendez,
R.drawable.ic_tablayout_eve,
R.drawable.ic_projettab
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Toolbar tol = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tol);
getSupportActionBar().setTitle("");
tt1 = (TextView) findViewById(R.id.textbar);
ty = Typeface.createFromAsset(getAssets(), "fonts/BALOOBHAI-REGULAR.TTF");
tt1.setTypeface(ty);
mdrawer = (DrawerLayout) findViewById(R.id.drawerlayout);
mtoggle = new ActionBarDrawerToggle(this, mdrawer, R.string.ouvrir, R.string.fermer);
mdrawer.addDrawerListener(mtoggle);
mtoggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewp = (ViewPager) findViewById(R.id.viewpage);
tab = (TabLayout) findViewById(R.id.tablayout);
setupViewPager(viewp);
tab.setupWithViewPager(viewp);
setupTabIcons();
NavigationView navigationView = (NavigationView)findViewById(R.id.navview);
View header = LayoutInflater.from(this).inflate(R.layout.header_menu , null);
navigationView.addHeaderView(header);
navigationView.setNavigationItemSelectedListener(this);
img = (ImageView)header.findViewById(R.id.pic);
txtnom = (TextView)header.findViewById(R.id.textnom);
txtadress = (TextView)header.findViewById(R.id.textadr);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null){
Picasso.with(getApplicationContext()).load(user.getPhotoUrl()).transform(new CircleTransform()).into(img);
String name = user.getDisplayName();
String email = user.getEmail();
txtnom.setText(name);
txtadress.setText(email);
} else {
gopagelogin();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mtoggle.onOptionsItemSelected(item)) {
return true;
}
int id = item.getItemId();
if ( id == R.id.dec) {
alertmsg();
}
return super.onOptionsItemSelected(item);
}
public void runThis(View v) {
Intent ii = new Intent(menu.this, partietache.class);
startActivity(ii);
}
public void Rendezvous(View v) {
Intent intent = new Intent(menu.this, partierendezvous.class);
startActivity(intent);
}
public void Evenement(View v) {
Intent intent = new Intent(menu.this, partieevenement.class);
startActivity(intent);
}
public void Projet(View v) {
Intent intent = new Intent(menu.this, partieprojet.class);
startActivity(intent);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tachefragment(), "Tâches");
adapter.addFragment(new RendezvousFragment(), "Rendez-vous");
adapter.addFragment(new Evenementfragment(), "Événements");
adapter.addFragment(new ProjetFragment(), "Projets");
viewPager.setAdapter(adapter);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.para) {
Intent i = new Intent(menu.this, settingmenu.class);
startActivity(i);
} else if (id == R.id.prof) {
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
}
mdrawer.closeDrawer(GravityCompat.START);
return true;
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String taches) {
mFragmentList.add(fragment);
mFragmentTitleList.add(taches);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
private void setupTabIcons() {
tab.getTabAt(0).setIcon(tabIcons[0]);
tab.getTabAt(1).setIcon(tabIcons[1]);
tab.getTabAt(2).setIcon(tabIcons[2]);
tab.getTabAt(3).setIcon(tabIcons[3]);
}
private void gopagelogin() {
Intent ii = new Intent(this , login.class);
ii.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ii);
}
private void alertmsg() {
AlertDialog.Builder alert = new AlertDialog.Builder(menu.this);
alert.setTitle("Déconnexion ?");
alert.setMessage("voulez-vous déconnecter?");
alert.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
FirebaseAuth.getInstance().signOut();
gopagelogin();
}
});
alert.setNegativeButton("Non", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
alert.show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.deconnexion , menu);
return true;
}}
My xml code :
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawerlayout"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/mytoolbar" />
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/AppTabLayout"
app:tabTextAppearance="#style/AppTabTextAppearance"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tablayout" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="start"
android:background="#ededed"
app:itemIconTint="#000000"
app:itemTextColor="#000000"
app:menu="#menu/menudeb">
</android.support.design.widget.NavigationView>
<RelativeLayout xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignEnd="#+id/material_design_android_floating_action_menu"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/material_design_android_floating_action_menu">
<TextView
android:id="#+id/textbar"
android:layout_width="125dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginBottom="100dp"
android:layout_marginLeft="125dp"
android:layout_marginTop="11dp"
android:gravity="center"
android:text="WACKTY"
android:textColor="#01b698"
android:textSize="24dp" />
<RelativeLayout
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textmenu">
<com.github.clans.fab.FloatingActionMenu
android:id="#+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
fab:menu_animationDelayPerItem="55"
fab:menu_backgroundColor="#android:color/transparent"
fab:menu_buttonSpacing="0dp"
fab:menu_colorNormal="#ffffff"
fab:menu_colorPressed="#ffffff"
fab:menu_colorRipple="#99d4d4d4"
fab:menu_fab_size="normal"
fab:menu_icon="#drawable/ic_action_plus"
fab:menu_labels_colorNormal="#c4c0c0"
fab:menu_labels_colorPressed="#444"
fab:menu_labels_colorRipple="#66efecec"
fab:menu_labels_cornerRadius="0dp"
fab:menu_labels_ellipsize="none"
fab:menu_labels_hideAnimation="#anim/fab_slide_out_to_right"
fab:menu_labels_margin="0dp"
fab:menu_labels_maxLines="-1"
fab:menu_labels_padding="1dp"
fab:menu_labels_position="left"
fab:menu_labels_showAnimation="#anim/fab_slide_in_from_right"
fab:menu_labels_showShadow="false"
fab:menu_labels_singleLine="false"
fab:menu_labels_textColor="#000000"
fab:menu_labels_textSize="14sp"
fab:menu_openDirection="up">
<com.github.clans.fab.FloatingActionButton
android:id="#+id/projet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_projeticon"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Projet"
android:onClick="Projet"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/événement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_eventicon"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Événement"
android:onClick="Evenement"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/rendezvous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Rendezvous"
android:src="#drawable/ic_action_rendez"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Rendez-Vous"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/tache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="runThis"
android:src="#drawable/ic_action_task"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Tâches"
fab:fab_size="mini" />
</com.github.clans.fab.FloatingActionMenu>
</RelativeLayout>
</RelativeLayout>
Menu xml :
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/prof"
android:icon="#drawable/ic_user"
android:title="Profile"
/>
<item
android:id="#+id/proj"
android:icon="#drawable/ic_project"
android:title="Projets"/>
<item
android:id="#+id/para"
android:icon="#drawable/ic_action_set"
android:title="Parametres" />
</group>
<item android:title="Compte">
<menu>
<item
android:id="#+id/dec"
android:icon="#drawable/ic_action_set"
android:title="Déconnexion"/>
</menu>
</item>
</menu>
In your overrided onNavigationItemSelected(MenuItem item) method you have to getItemId and on the selected Item you have to open activity through intent.
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.ItemId) {
Intent i = new Intent(YourActivityName.this, OtherActivity.class);
startActivity(i);
}
mdrawer.closeDrawer(GravityCompat.START);
return true;
}
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawerlayout"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/mytoolbar" />
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/AppTabLayout"
app:tabTextAppearance="#style/AppTabTextAppearance"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tablayout" />
</LinearLayout>
<RelativeLayout xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignEnd="#+id/material_design_android_floating_action_menu"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/material_design_android_floating_action_menu">
<TextView
android:id="#+id/textbar"
android:layout_width="125dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginBottom="100dp"
android:layout_marginLeft="125dp"
android:layout_marginTop="11dp"
android:gravity="center"
android:text="WACKTY"
android:textColor="#01b698"
android:textSize="24dp" />
<RelativeLayout
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textmenu">
<com.github.clans.fab.FloatingActionMenu
android:id="#+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
fab:menu_animationDelayPerItem="55"
fab:menu_backgroundColor="#android:color/transparent"
fab:menu_buttonSpacing="0dp"
fab:menu_colorNormal="#ffffff"
fab:menu_colorPressed="#ffffff"
fab:menu_colorRipple="#99d4d4d4"
fab:menu_fab_size="normal"
fab:menu_icon="#drawable/ic_action_plus"
fab:menu_labels_colorNormal="#c4c0c0"
fab:menu_labels_colorPressed="#444"
fab:menu_labels_colorRipple="#66efecec"
fab:menu_labels_cornerRadius="0dp"
fab:menu_labels_ellipsize="none"
fab:menu_labels_hideAnimation="#anim/fab_slide_out_to_right"
fab:menu_labels_margin="0dp"
fab:menu_labels_maxLines="-1"
fab:menu_labels_padding="1dp"
fab:menu_labels_position="left"
fab:menu_labels_showAnimation="#anim/fab_slide_in_from_right"
fab:menu_labels_showShadow="false"
fab:menu_labels_singleLine="false"
fab:menu_labels_textColor="#000000"
fab:menu_labels_textSize="14sp"
fab:menu_openDirection="up">
<com.github.clans.fab.FloatingActionButton
android:id="#+id/projet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_projeticon"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Projet"
android:onClick="Projet"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/événement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_eventicon"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Événement"
android:onClick="Evenement"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/rendezvous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Rendezvous"
android:src="#drawable/ic_action_rendez"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Rendez-Vous"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/tache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="runThis"
android:src="#drawable/ic_action_task"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Tâches"
fab:fab_size="mini" />
</com.github.clans.fab.FloatingActionMenu>
</RelativeLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="start"
android:background="#ededed"
app:itemIconTint="#000000"
app:itemTextColor="#000000"
app:menu="#menu/menudeb">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
I'm developing an application (as my first one) and In this app I've declared my navigationView like this:
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/navigation_drawer"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/drawer_menu"
app:headerLayout="#layout/drawer_header">
In my mainactivity.java I have:
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{
...
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Log.d("Navigation","Item selected : "+ item.getItemId());
int id = item.getItemId();
switch (id){
case R.id.about:
Toast.makeText(getApplicationContext(),"About Us
elected",Toast.LENGTH_SHORT).show();
break;
case R.id.help:
Toast.makeText(getApplicationContext(),
"Help",Toast.LENGTH_SHORT).show();
break;
case R.id.setting:
Toast.makeText(getApplicationContext(),
"Settings",Toast.LENGTH_SHORT).show();
break;
case R.id.EditProfile:{
Intent intent= new Intent(this , EditProfile.class);
startActivity(intent);
}
default:
break;
}
DrawerLayout drawerLayout= (DrawerLayout)
findViewById(R.id.drawer_layout);
drawerLayout.closeDrawer(GravityCompat.START);
return false;
}
And according to this I've extended ActionBarDrawerToggle and in it's onDrawerOpened function I've don
drawerView.bringToFront();
drawerView.getParent().requestLayout();
But still when I click on navigation view Items onNavigationItemSelected function doesn't fire.
Is it because of header view I have added??
This method worked before I add header view to navigationView( I didn't test it just before adding header to navigationView.
And also I think it's useful to note that I have some buttons and TextViews in navigationView header.
<?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:orientation="vertical"
android:id="#+id/drawer_header"
android:background="#drawable/drawer_bg"
android:layout_width="match_parent"
android:padding="4dp"
android:layout_height="200dp">
<RelativeLayout
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/avatar"
android:id="#+id/AvatarImageView"
android:layout_centerHorizontal="true" />
<Button
android:text="Signup"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignParentRight="true"
android:id="#+id/header_signup"
android:textSize="12sp"
android:background="#drawable/signup_button"
android:textStyle="normal|bold" />
</RelativeLayout>
<TextView
android:textColor="#color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:id="#+id/header_username"
android:textAlignment="center" />
<TextView
android:text="Logout"
android:textColor="#color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:layout_height="wrap_content"
android:id="#+id/header_logout"
/>
</LinearLayout>
You should add following line of code in onCreate for click listener for menu item,
NavigationView navigationView = (NavigationView) findViewById(R.id. navigation_drawer);
navigationView.setNavigationItemSelectedListener(this);
You can set click listener to the header of the NavigationView like this:
View headerLayout = navigationView.getHeaderView(0);
headerLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent= new Intent(this , EditProfile.class);
startActivity(intent);
});
and for the elements inside the header
CircleImageView userPicture = (CircleImageView) headerLayout.findViewById(R.id.AvatarImageView);
Edit
navigationView.setNavigationItemSelectedListener(this);
This is the solution:
path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/WhatsApp/Media/.Statuses/";
File f= new File(path);
I made a drawer navigation based on a tutorial on the internet and I followed everything but when I open my app everything is working correctly except my onclick event. I'm pretty new to android (2 weeks) and tried to figure it out by myself but it didn't work out. I tries onclicklistener but that one didn't give any possitive feedback for me.
How do I make a click event that will take me to another activity?
my code:
public class LayoutOneActivity extends ActionBarActivity {
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_one);
Resources res = getResources();
String [] menu_items = res.getStringArray(R.array.menu_items); // String array where the menu items will be stored
menu = menu_items; // Variable for the menu items
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // Looking for the id "drawer_layout" and apply as layout
dList = (ListView) findViewById(R.id.left_drawer); // Looking for the listview where the items will be stored
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);// Making a new adapter
dList.setAdapter(adapter);// Give the list-layout the variable "adapter" which is an adapter (obviously)
dList.setSelector(R.drawable.back);// Sets the colour of the list-layout
dList.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
dLayout.closeDrawers();// The layout will be clossed when clicked outside the layout
Bundle args = new Bundle();// New bundle which will parse the data between various activities
args.putString("Menu", menu[position]);
Fragment detail = new DetailFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
}
});
}
layout_one
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="48dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal">
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="#drawable/button"
android:onClick="openNewActivity1"
android:text="#string/clickActivity1" />
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="#drawable/button"
android:onClick="openNewActivity2"
android:text="#string/clickActivity2" />
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="#drawable/button"
android:onClick="openNewActivity3"
android:text="#string/clickActivity3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal">
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="#drawable/button"
android:onClick="openNewActivity4"
android:text="#string/clickActivity4"/>
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="#drawable/button"
android:onClick="openNewActivity5"
android:text="#string/clickActivity5"/>
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="#drawable/button"
android:onClick="openNewActivity6"
android:text="#string/clickActivity6"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout> </FrameLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#fff"/> </android.support.v4.widget.DrawerLayout>
menu_detail_fragment
<?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:gravity="center"
android:background="#5ba4e5"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40px"
android:textColor="#ffffff"
android:layout_gravity="center"
android:id="#+id/detail"/> </LinearLayout>
To set a navigation menu onClickListener you have to implement the NavigationView.OnNavigationItemSelectedListener interface on the Activity.
On the onCreate method you have to define the navigation onClickListener to the context Activity like this:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Then you have to Override the method
#Override
public boolean onNavigationItemSelected(MenuItem item)
I write an example of how to implement the method below:
#Override
public boolean onNavigationItemSelected(MenuItem item){
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
EventFragment eventFragment = new EventFragment();
Bundle bundle = new Bundle();
bundle.putString("selectedEvent", "Urban");
eventFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame,eventFragment).commit();
} else if (id == R.id.nav_gallery) {
EventFragment eventFragment = new EventFragment();
Bundle bundle = new Bundle();
bundle.putString("selectedEvent", "Cosmos");
eventFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame,eventFragment).commit();
} else if (id == R.id.nav_slideshow) {
EventFragment eventFragment = new EventFragment();
Bundle bundle = new Bundle();
bundle.putString("selectedEvent", "Sink");
eventFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame,eventFragment).commit();
} else if (id == R.id.nav_share) {
ShopListFragment intent = new ShopListFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame, intent).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(START);
return true;
}