Make Drawer Navigation Menu Item Open new activity when clicked - android

I'm new to android app development and am trying to build an app. I have successfully built my navigation drawer but I want the menu items to open another activity when clicked. I have tried various answers on SO but my app seems to crash each time i run my app after applying any of the answers. Please what could be the cause of this.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/my_account"
android:title="My Account"
android:icon="#mipmap/ic_person_white_24dp"
android:onClick="settings"></item>
<item
android:id="#+id/my_settings"
android:title="Settings"></item>
<item
android:id="#+id/Logout"
android:title="Log Out"
android:icon="#mipmap/ic_exit_to_app_white_24dp"></item>
</menu>
activity.java
package com.example.orume.export;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class WorkActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work);
mToolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.open,
R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final Button bExplore = (Button) findViewById(R.id.bExplore);
bExplore.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent exploreIntent = new Intent(WorkActivity.this,
RegisterActivity.class);
WorkActivity.this.startActivity(exploreIntent);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
return true;
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="#+id/drawerLayout"
android:background="#d8dadc">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
android:background="#color/colorPrimary"
app:headerLayout="#layout/navigation_header">
</android.support.design.widget.NavigationView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#d8dadc">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="false"
android:padding="10dp">
<Button
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="0.5"
android:background="#mipmap/bg_tool"
android:id="#+id/bExplore"
android:layout_marginRight="5dp"/>
<Button
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="0.5"
android:background="#mipmap/bg_tool_2"
android:id="#+id/bPrice"
android:layout_marginLeft="5dp"
android:text="Explore"
android:textAllCaps="false"
android:textColor="#191919"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="false"
android:padding="10dp"
android:layout_marginTop="75dp">
<Button
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="0.5"
android:background="#mipmap/bg_tool"
android:id="#+id/bS"
android:layout_marginRight="5dp"/>
<Button
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="0.5"
android:background="#mipmap/bg_tool_2"
android:id="#+id/bA"
android:layout_marginLeft="5dp"
android:text="Explore"
android:textAllCaps="false"
android:textColor="#191919"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>

In your menu.xml you have added this line android:onClick="settings". remove that line or add the following code to your MainActivity.java
public boolean settings(MenuItem item) {
// actions
return true;
}

Related

When click on the menu icon to open NavigationDrawer it crashes

When i click on the icon to open the NavigationDrawer my app suddenly crashes and i am trying to solve this about 3 days. Can anyone help me?
I am using Android Studio and this is my first app.
My problem is to open the NavigationDrawer by clicking on the icon on app bar.
Codes:
MainActivity.java
package com.example.chatapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;
FirebaseAuth mAuth;
FirebaseUser mUser;
DatabaseReference mUserRef;
String profileImageUriV, usernameV;
CircleImageView profileImageHeader;
TextView usernameHeader;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Chat App");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
mAuth=FirebaseAuth.getInstance();
mUser=mAuth.getCurrentUser();
mUserRef= FirebaseDatabase.getInstance().getReference().child("Users");
drawerLayout=findViewById(R.id.drawerLayout);
navigationView=findViewById(R.id.navView);
View view=navigationView.inflateHeaderView(R.layout.drawer_header);
profileImageHeader=view.findViewById(R.id.profileImage_Header);
usernameHeader=view.findViewById(R.id.usernameHeader);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
protected void onStart() {
super.onStart();
if (mUser==null)
{
SendUserToLoginActivity();
}
else{
mUserRef.child(mUser.getUid()).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
profileImageUriV=dataSnapshot.child("profileImage").getValue().toString();
usernameV=dataSnapshot.child("username").getValue().toString();
Picasso.get().load(profileImageUriV).into(profileImageHeader);
usernameHeader.setText(usernameV);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(MainActivity.this, "Sorry! Something going wrong", Toast.LENGTH_SHORT).show();
}
});
}
}
private void SendUserToLoginActivity() {
Intent intent=new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.home:
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
break;
case R.id.profile:
Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
break;
case R.id.friend:
Toast.makeText(this, "Friend", Toast.LENGTH_SHORT).show();
break;
case R.id.findFriend:
Toast.makeText(this, "Find Friend", Toast.LENGTH_SHORT).show();
break;
case R.id.chat:
Toast.makeText(this, "Chat", Toast.LENGTH_SHORT).show();
break;
case R.id.logout:
Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (item.getItemId()==android.R.id.home){
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return true;
}
}
Drawer_Menu.xml:
<group android:id="#+id/first">
<item android:id="#+id/home"
android:title="Home"
android:icon="#drawable/ic_home"/>
<item android:id="#+id/profile"
android:title="Profile "
android:icon="#drawable/ic_profile"/>
<item android:id="#+id/friend"
android:title="Friend "
android:icon="#drawable/ic_friend"/>
<item android:id="#+id/findFriend"
android:title="Find Friend "
android:icon="#drawable/ic_find_friend"/>
<item android:id="#+id/chat"
android:title="Chat "
android:icon="#drawable/ic_chat"/>
</group>
<group android:id="#+id/second">
<item android:id="#+id/logout"
android:title="Logout "
android:icon="#drawable/ic_logout"/>
</group>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/app_bar"
layout="#layout/main_appbar" />
<EditText
android:id="#+id/inputAddPost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/input_add_post_bg"
android:hint="Post something here :)"
android:paddingLeft="50dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/app_bar" />
<ImageView
android:id="#+id/addImagePost"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="#+id/inputAddPost"
app:layout_constraintStart_toStartOf="#+id/inputAddPost"
app:layout_constraintTop_toTopOf="#+id/inputAddPost"
app:srcCompat="#drawable/ic_add_post_image" />
<ImageView
android:id="#+id/send_post_imageView"
android:layout_width="53dp"
android:layout_height="48dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="#+id/inputAddPost"
app:layout_constraintEnd_toEndOf="#+id/inputAddPost"
app:layout_constraintTop_toTopOf="#+id/inputAddPost"
app:srcCompat="#drawable/send_post" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputAddPost"
android:background="#color/colorGray"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
build.gradle:
implementation 'com.google.firebase:firebase-auth:19.3.0'
implementation 'com.google.firebase:firebase-core:17.3.0'
implementation 'com.google.firebase:firebase-storage:19.1.1'
implementation 'com.firebaseui:firebase-ui-database:6.2.1'
implementation 'com.google.android.material:material:1.0.0'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'com.squareup.picasso:picasso:2.71828'
I think You would have missed mentioning the id of the DrawerLayout in your 'activity_main.xml' through which our activity finds the Layout in the XML file
android:id="#+id/drawerLayout"
Below is the code after updating.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/app_bar"
layout="#layout/main_appbar" />
<EditText
android:id="#+id/inputAddPost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/input_add_post_bg"
android:hint="Post something here :)"
android:paddingLeft="50dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/app_bar" />
<ImageView
android:id="#+id/addImagePost"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="#+id/inputAddPost"
app:layout_constraintStart_toStartOf="#+id/inputAddPost"
app:layout_constraintTop_toTopOf="#+id/inputAddPost"
app:srcCompat="#drawable/ic_add_post_image" />
<ImageView
android:id="#+id/send_post_imageView"
android:layout_width="53dp"
android:layout_height="48dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="#+id/inputAddPost"
app:layout_constraintEnd_toEndOf="#+id/inputAddPost"
app:layout_constraintTop_toTopOf="#+id/inputAddPost"
app:srcCompat="#drawable/send_post" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputAddPost"
android:background="#color/colorGray"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>

why BottomSheetBehavior in androidx error The view is not a child of CoordinatorLayout?

I use androidx and material Bottom Sheet Modal persistent. but my code is error like this :
The view is not a child of CoordinatorLayout
bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="72dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical"
android:weightSum="3">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_margin"
android:layout_weight="2"
android:text="Order Details"
android:textColor="#444"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:gravity="right"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="15dp"
android:text="₹435.00"></TextView>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chicken Fried Rice 1x1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paneer Tikka 1x2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_margin"
android:text="Delivery Address"
android:textColor="#444"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flat No 404, Skyline Apartments, Vizag - 500576" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="#000"
android:foreground="?attr/selectableItemBackground"
android:text="PROCEED PAYMENT"
android:textColor="#fff" />
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efefef"
tools:context="com.app.MainActivity">
<include layout="#layout/content_main" />
<!-- Adding bottom sheet after main content -->
<include layout="#layout/bottom_sheet" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.app;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
#BindView(R.id.btn_bottom_sheet)
Button btnBottomSheet;
#BindView(R.id.bottom_sheet)
LinearLayout layoutBottomSheet;
BottomSheetBehavior sheetBehavior;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet);
/**
* bottom sheet state change listener
* we are changing button text when sheet changed state
* */
sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_HIDDEN:
break;
case BottomSheetBehavior.STATE_EXPANDED: {
btnBottomSheet.setText("Close Sheet");
}
break;
case BottomSheetBehavior.STATE_COLLAPSED: {
btnBottomSheet.setText("Expand Sheet");
}
break;
case BottomSheetBehavior.STATE_DRAGGING:
break;
case BottomSheetBehavior.STATE_SETTLING:
break;
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
}
});
}
/**
* manually opening / closing bottom sheet on button click
*/
#OnClick(R.id.btn_bottom_sheet)
public void toggleBottomSheet() {
if (sheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
btnBottomSheet.setText("Close sheet");
} else {
sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
btnBottomSheet.setText("Expand sheet");
}
}
}
where I wrong, and how to solve this, I already google about this problem, but I got no clue.
error in sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet);
thanks
Your parent layout is a ConstraintLayout not a CoordinatorLayout

Clash Of Clan like NavigationView Pull Arrow

I want to create a custom view which stays visible while side drawer is closed, but by pulling that, the drawer will slide.
I had seen like this in very popular game Clash Of Clan like shown in below images:
Now I wonder if there is any way to achieve same by using DrawerLayout and NavigationView?
Finally I achieved this by adding addDrawerListener and translating the HANDLE accordingly.
Here is my ACTIVITY:
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
/**
* #author Meet Vora
*/
public class PullSliderActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private View viewPuller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pull_slider);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
viewPuller = findViewById(R.id.viewPuller);
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
viewPuller.setTranslationX(drawerView.getX() + drawerView.getWidth());
}
#Override
public void onDrawerOpened(View drawerView) {
}
#Override
public void onDrawerClosed(View drawerView) {
}
#Override
public void onDrawerStateChanged(int newState) {
}
});
}
}
Here is LAYOUT XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#style/MyHelpActivityTheme">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#49fcea"
android:gravity="center"
android:text="This is MAIN Screen"
android:textColor="#color/black"
android:textSize="20sp"
android:textStyle="bold"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#223344"
android:gravity="center"
android:text="This is NAVIGATION View"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
<TextView
android:id="#+id/viewPuller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#5f97cf"
android:clickable="false"
android:gravity="center"
android:padding="4dp"
android:text="N\n.\nV\nI\nE\nW"
android:textColor="#color/white"
android:textSize="10sp"
android:textStyle="bold"/>
</RelativeLayout>

How to reference ID from other layout

Following this tutorial I moved my ActionBar (Toolbar) from my main layout to another layout and I include the toolbar to my main layout like this:
<?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"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.marcelo.notemuevas.MainActivity">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/my_toolbar">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Servicios"
android:id="#+id/serviciosTxt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/colorPrimaryDark"
android:textColor="#FFFFFF"
android:gravity="center"
android:paddingTop="18dp"
android:paddingBottom="18dp"
/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/serviciosTxt">
</ListView>
</RelativeLayout>
</RelativeLayout>
And my toolbar layout is this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/CustomActionBarTheme"/>
</LinearLayout>
But I got the following error
Why I cant make reference of my toolbar if it has the same id? Can I include my toolbar and make reference so I can put elements below the toolbar?
Edit 1 - Activity Code
package com.marcelo.notemuevas;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] itemname ={
"Carpinteria"
};
String[] descripcion={
"Reparación"
};
Integer[] imgid={
R.mipmap.icono1
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
myToolbar.setLogo(R.mipmap.ic_launcher);
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid, descripcion);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem = itemname[position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MainActivity.this, carpinteria.class);
myIntent.putExtra("key", 5); //Optional parameters
MainActivity.this.startActivity(myIntent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
Edit 2 - Solution
Now I make it work, I added a new Id to the include then I make reference to it like James Lockhart and Phan Văn Linh suggest (dont know why the first time didnt work) so my new question is, the IDs works like variables? only exist where it has been declared?
This is the working 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"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.marcelo.notemuevas.MainActivity">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbarLayout"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolbarLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Servicios"
android:id="#+id/serviciosTxt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/colorPrimaryDark"
android:textColor="#FFFFFF"
android:gravity="center"
android:paddingTop="18dp"
android:paddingBottom="18dp"
/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/serviciosTxt">
</ListView>
</RelativeLayout>
</RelativeLayout>
In your XML
<include
android:id=#"+id/toolBarLayout" // set the id for layout that contains toolbar
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolBarLayout"> // change my_toolbar to toolBarLayout
Then you access to your Toolbar by change
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
to
LinerLayout myLinear = (LinerLayout) findViewById(R.id.toolBarLayout);
Toolbar myToolbar = (Toolbar) myLinear.findViewById(R.id.my_toolbar);
Please try this:
In your toolbar layout file, in the LinearLayout tag, set the 'android:id' attribute to a unique layout id name, for example: android:id="#+id/toolbarLayout"
Once you have completed (1), in your main layout refer to your toolbar layout file as follows: android:layout_below="#id/toolbarLayout"
Hope this helps.

Android ImageButton ClickEvent not working

here is the code, please can anyone simply tell me how to make the onClick event run?
xml file of the activity, which has a navigation-drawer fragment, An Image view, A TextView, An ImageButton
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/drawer_layout"
android:adjustViewBounds="true"
android:contentDescription="#string/ContentDescriptionProfileImage"
android:src="#drawable/profile_pic" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:background="#FFFFFF"
android:orientation="horizontal"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:padding="3dp"
android:text="#string/profileName"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/profileSettings"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#null"
android:contentDescription="#string/settingsIconDescription"
android:padding="8dp"
android:onClick="onSettingsIconPressed"
android:src="#drawable/settings32" />
</RelativeLayout>
</LinearLayout>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/app_bar">
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="studio25.materialdesign.NavigationDrawerFragment"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer"></fragment>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
The image of the Screen Looks like this
here is the activity class
package studio25.materialdesign;
import android.annotation.TargetApi;
import android.os.Build;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.Toast;
public class ProfileView extends ActionBarActivity {
private Toolbar toolbar;
String title = "";
ImageButton setttingsButton;
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_view);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
this.getSupportActionBar().setTitle(title);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp((DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
setttingsButton= (ImageButton) findViewById(R.id.profileSettings);
/*
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.primaryColor));
*/
}
public void onSettingsIconPressed(View v)
{
/*
PopupMenu popupMenu=new PopupMenu(this,v);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.profile_settings_popup_menu,popupMenu.getMenu());
popupMenu.show();
*/
Toast.makeText(this,"Event Click Occured",Toast.LENGTH_SHORT).show();
}
}
I basically wanted a menu popup on buttonclick, but on debugging realized that my button click is not itself working.
Some tutotials say for popUp menu , minSdkVersion should be 11, even changed that.
Nothing is helping me.
Is my xml erroneous or what?
I usually like to hook up my callbacks in code (just my personal preference).
setttingsButton= (ImageButton) findViewById(R.id.profileSettings);
setttingsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(this,"clicked!",Toast.LENGTH_SHORT).show();
}
});
With this you can remove the onClick attribute in the xml layout.
If you want a popup window then PopupWindow is a great option.
popupWindow = new PopupWindow(some_view_to_show, ViewGroup.LayoutParams.MATCH_PARENT, (int)popupHeight, false);
...
popupWindow.showAtLocation(view_to_inject_into, Gravity.NO_GRAVITY, 0, (size.y) - (int) popupHeight);
then you call popupWindow.dismiss(); to remove the popup
Your issue could also be that since you are using a RelativeLayout each element gets "stacked" on top of each other so you have essentially "buried" your ImageButton that you want to click on. So in other words some other view is blocking or consuming your clicks.
Try moving your ImageButton to be one of the last elements that you add to the xml file so that it is "on top" of everything else so that it can be clicked.
Alternatively you can try making the containing LinearLayout clickable instead of the ImageButton. So you would move the android:onClick="onSettingsIconPressed" attribute to the LinearLayout that contains the ImageButton.

Categories

Resources