I've made a custom toolbar and its Right-To-Left. and it looks like this:
I want a navigation drawer appears when I click on the right side icon. But when I add the navigation drawer to app, it crashes. I've tried many things and also searched a lot, but it didn't work.
It'd be GREAT it someone would help me.
Here is my code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.playpersia.mycustomizedtoolbar.MainActivity"
>
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"/>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="end"
android:fitsSystemWindows="true"
android:id="#+id/right_drawer">
<!-- Main layout -->
<FrameLayout
android:id="#+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar"
android:text="Hello"/>
</FrameLayout>
<!-- Nav drawer -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right|end" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/header_drawer"
android:src="#drawable/my_image" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:id="#+id/list_view"
android:entries="#array/pages" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
right_drawer.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"
tools:openDrawer="end"
android:fitsSystemWindows="true"
android:id="#+id/right_drawer"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/header_drawer"
android:src="#drawable/my_image" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:id="#+id/list_view"
android:entries="#array/pages" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java
package com.playpersia.mycustomizedtoolbar;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
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.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.zip.Inflater;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
public ListView listView;
public String[] pages;
ArrayAdapter<String> arrayAdapter;
public ImageView menu_icon;
public ImageView back_arrow;
public boolean mSlideState;
private DrawerLayout drawerLayout;
public ActionBarDrawerToggle _mDrawerTg;
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
menu_icon = (ImageView) findViewById(R.id.menu_icon);
back_arrow = (ImageView) findViewById(R.id.back_button);
drawerLayout =(DrawerLayout) findViewById(R.id.right_drawer);
pages =getResources().getStringArray(R.array.pages);
_mDrawerTg =new ActionBarDrawerToggle(this, drawerLayout,
null, R.string.drawer_open, R.string.drawer_close){
public void onDrawerClosed(View view){
mSlideState=false;
}
public void onDrawerOpened (View drawerView){
super.onDrawerOpened(drawerView);
mSlideState=true;
} };
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item,pages));
drawerLayout.setDrawerListener(_mDrawerTg);
_mDrawerTg.syncState();
menu_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mSlideState){
drawerLayout.closeDrawer(GravityCompat.END);
}else{
drawerLayout.openDrawer(GravityCompat.END); } } }); }
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
_mDrawerTg.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
_mDrawerTg.onConfigurationChanged(newConfig);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (_mDrawerTg.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
}
UPDATE
logcat:
01-18 12:34:25.386 29949-29949/com.playpersia.mycustomizedtoolbar
E/AndroidRuntime: FATAL EXCEPTION: main Process:
com.playpersia.mycustomizedtoolbar, PID: 29949 java.lang.RuntimeException:
Unable to start activityComponentInfo{com.playpersia.mycustomizedtoolbar/com.playpersia.mycustomizedtool
bar.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)at
android.app.ActivityThread.access$800(ActivityThread.java:156)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)at
android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.playpersia.mycustomizedtoolbar.MainActivity.onCreate(MainActivity.java:53)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
UPDATE 2
now I have this toolbar when I click on the right side icon the drawer opens & closes,
right side drawer
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a
null object reference at
com.playpersia.mycustomizedtoolbar.MainActivity.onCreate(MainActivity.java:53)
In onCreate method, your listView object is null.
You need to add
listView = (ListView) findViewById(R.id.list_view);
before
listView.setAdapter(new ArrayAdapter<String (this,R.layout.drawer_list_item,pages));
UPDATE:
Change your activity_main.xml to:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"/>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="end"
android:fitsSystemWindows="true"
android:id="#+id/right_drawer">
<!-- Main layout -->
<FrameLayout
android:id="#+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar"
android:text="Hello"/>
</FrameLayout>
<!-- Nav drawer -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|start" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/header_drawer"
android:src="#drawable/my_image" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:id="#+id/list_view"
android:entries="#array/pages" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
This should be your MainLayout since you are using DrawerLayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Your contents, for example, CoordinatorLayout with material design -->
<!-- And after main Contents, here you can use NavigationDrawer also -->
</android.support.v4.widget.DrawerLayout>
And instead of commented code, use your contents.
You did it in wrong way, Take a look:
http://developer.android.com/training/implementing-navigation/nav-drawer.html#DrawerLayout
To add a navigation drawer, declare your user interface with a
DrawerLayout object as the root view of your layout. Inside the
DrawerLayout, add one view that contains the main content for the
screen (your primary layout when the drawer is hidden) and another
view that contains the contents of the navigation drawer.
For example, the following layout uses a DrawerLayout with two child
views: a FrameLayout to contain the main content (populated by a
Fragment at runtime), and a ListView for the navigation drawer.
Example:
<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">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<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="#111"/>
</android.support.v4.widget.DrawerLayout>
You may want to take a look at the Material Design Guidelines for this implementation of course:
https://www.google.com/design/spec/patterns/navigation-drawer.html
Related
I'm working on a drawer layout. I want to replace the main activity screen with a fragment when I click a menu item, but it doesn't show a toolbar when I set the background of a fragment on some color app. Work done so far ...
<!-- activity_main.xml-->
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/drawer"
tools:openDrawer="start"
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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.appcompat.widget.Toolbar
android:background="#ff9100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="News Time"
android:id="#+id/toolbar"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/replacer"
/>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/navmenu"
app:menu="#menu/menu"
app:headerLayout="#layout/navheader"
android:layout_gravity="start"
android:background="#ffaa00"
/>
</androidx.drawerlayout.widget.DrawerLayout>
MainActivity.java
package com.example.newsapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
NavigationView nav;
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nav = (NavigationView)findViewById(R.id.navmenu);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer);
toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
final FragmentManager fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
nav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment temp = null;
switch (menuItem.getItemId())
{
case R.id.sporticon:
Toast.makeText(getApplicationContext(),"This is sport section",Toast.LENGTH_SHORT).show();
temp = new sportfragment();
break;
}
fragmentTransaction.replace(R.id.replacer,temp);
fragmentTransaction.commit();
return true;
}
});
}
}
This is fine when there is no background attribute but toolbar disappears when I add it
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".sportfragment"
android:background="#00c3ff"
>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_gravity="center"
android:textSize="30sp"
android:textColor="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is sport section"
/>
</FrameLayout>
You are using relative layout so you have to position every single child in relative layout since you are not doing it. framelayout overlapping the toolbar when there is not color in framelayout toolbar visible to you and it does not go anywhere so whenever you add a color toolbar becomes invisible to you but not from screen it will be under the framelayout...
so add below line in framelayout
android:layout_below="#id/toolbar"
I have made an bottom app bar app with fab and bottom sheet and I have implemented the state expanded and state hidden from navigation on item selected listener.The question is how to make bottom sheet overlay on bottom app bar the following result I have got is.
This is main activity xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_CoordinatorLayout"
android:layout_width="match_parent"
android:background="#FFB7B7B7"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/Widget.MaterialComponents.BottomAppBar.Colored"
android:layout_gravity="bottom"
android:elevation="26dp"
app:navigationIcon="#drawable/ic_menu_black" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:elevation="26dp"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/ic_menu_add"
app:layout_anchor="#id/bottom_app_bar"
app:menu="#menu/menu_demo"
/>
<FrameLayout
android:id="#+id/bottom_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="16dp"
android:visibility="visible"
app:behavior_hideable="true"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/menu_demo" />
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Main activity class
package com.bab.BottomAppBar;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import android.view.View;
import com.google.android.material.navigation.NavigationView;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
BottomAppBar bab;
BottomSheetBehavior<View> bsb;
CoordinatorLayout cl;
NavigationView nv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nv=findViewById(R.id.navigation_view);
cl=findViewById(R.id.activity_CoordinatorLayout);
View bottomDrawer=cl.findViewById(R.id.bottom_drawer);
bab=findViewById(R.id.bottom_app_bar);
bsb=BottomSheetBehavior.from(bottomDrawer);
bsb.setState(BottomSheetBehavior.STATE_HIDDEN);
bab.setNavigationIcon(R.drawable.ic_menu_black);
bab.replaceMenu(R.menu.menu_demo);
bab.setNavigationOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View p1) {
bsb.setState(BottomSheetBehavior.STATE_HALF_EXPANDED);
}
});
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
#Override
public boolean onNavigationItemSelected(MenuItem p1) {
return false;
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
bsb.setState(BottomSheetBehavior.STATE_HIDDEN);
}
}
Result I got:
This is the result
Result I wanted:
See in this the frame layout as bottom sheet is overlaying on bottom app bar
Someone please help me
You need to set BottomSheet's elevation more than BottomAppBar's.
Elevation means how your views lay above each other
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.
I am working with Navigation Drawer in Android.I have code from some examples.In my code working to change the drawer open and drawer close title but the panel is not open.I not insert the ListView array. My main Problem is not open the panel.Is anything I missed in the code?
layout_mynavigation.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_height="150dp"
android:layout_width="match_parent"
android:id="#+id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/drawer_list"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
MyNavigation.java
import android.content.res.Configuration;
import android.os.Bundle;
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.MenuItem;
import android.view.View;
public class MyDrawer extends AppCompatActivity {
private ActionBarDrawerToggle mActionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_mydrawer);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
DrawerLayout mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.drawer_open,R.string.drawer_closed) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle( R.string.drawer_open );
invalidateOptionsMenu();
}
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle( R.string.drawer_closed );
invalidateOptionsMenu();
}
}
};
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged( newConfig );
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return mActionBarDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
}
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Customize your theme here. -->
</style>
DrawerLayout expects it's first child to be the actual layout and the second one to be the drawer. So, to make it work you should change your layout to (assuming that your entire drawer consists of only one ListView):
<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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_height="150dp"
android:layout_width="match_parent"
android:id="#+id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize"/>
</LinearLayout>
<ListView
android:id="#+id/drawer_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="#000000"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
It looks like your xml layout needs to be re-arranged. The DrawerLayout should wrap two items; 1) a View or ViewGroup to set as the page content (e.g., RelativeLayout containing a Toolbar and Fragment container); 2) a View or ViewGroup that holds your NavigationDrawer items (e.g., ListView).
<android.support.v4.widget.DrawerLayout
android:id="#+id/navigation_drawer_layout"
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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/your_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/your_toolbar_color"
android:minHeight="?attr/actionBarSize"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/your_toolbar"/>
</RelativeLayout>
<ListView
android:id="#+id/your_drawer_list"
android:layout_width="#dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#color/your_drawer_color"
android:divider="#null"/>
</android.support.v4.widget.DrawerLayout>
You can get a more detailed explanation and examples here:
https://developer.android.com/training/implementing-navigation/nav-drawer.html
If your list is empty and you have the height as wrap_content, the height will be 0. Changing it to match_parent will have it fill the screen regardless of the content.
<ListView
android:id="#+id/drawer_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="#000000"
android:layout_gravity="start"/>
I have a DrawerLayout, which has an EditText and a ListView in it.
The problem is, that I cannot touch my EditText or ListView items. Instead of it
DrawerLayout recieves all the touch events. Clicking on any item in the DrawerLayout causes DrawerLayout to close (which I guess is the default onTouch method in the drawerlayout class)
<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" >
<-- my main layouts here -->
</FrameLayout>
<LinearLayout
android:id="#+id/left_linear"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clickable="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:orientation="horizontal" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/noborder"
android:clickable="true"
android:hint="search box 1"
android:padding="8dp"
android:singleLine="true" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="5dp"
android:src="#android:drawable/ic_menu_search" />
</LinearLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Try drawerlayout.requestDisallowInterceptTouchEvent(true) in your oncreate
Set Both edit text and text view explicitly as clickable .. then implement something like this for list view
/******************************Listener for Drawer***************/
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//your code here based on posotion
}
}
you can set the listener using something like this
DrawerList.setOnItemClickListener(new DrawerItemClickListener());
i found a similar problem i have update sdk and specially Extra Folder and it work fine.
I've test your code and it's working fine on my device.
Thanks i hope it help.
i have done something like this,
i have updated your code.
edittext can now take focus
create main.xml
<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:background="#drawable/background"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<-- my main layouts here -->
</FrameLayout>
<FrameLayout
android:id="#+id/navigation_frame"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
create navigation.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/left_linear"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clickable="true"
android:layout_gravity="start">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:orientation="horizontal">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:background="#drawable/noborder"
android:hint="search box 1"
android:padding="8dp"
android:singleLine="true" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="5dp"
android:src="#android:drawable/ic_menu_search" />
</LinearLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
//create activity like
public class HomeActivity extends actionbaractivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// add menu to navigation drawer
addMenu();
}
private void addMenu()
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.navigation_frame,
new NavigationFragment());
fragmentTransaction.commit();
}
}
// create new fragment class that add menu to drawer layout
public class NavigationFragment extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.navigation_view, null);
return view;
}
}
// its working for me..
Try this one.
package com.example.drawersample;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);
// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawerlist, drawerListViewItems));
// 2. App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// 2.1 create ActionBarDrawerToggle
actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_launcher, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
// 2.2 Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);
// 2.3 enable and show "up" arrow
getActionBar().setDisplayHomeAsUpEnabled(true);
// just styling option
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns
// true
// then it has handled the app icon touch event
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this, ((TextView) view).getText(),
Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(drawerListView);
}
}
}
drawerlist.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="20sp"
android:gravity="center_vertical"
android:paddingStart="14.5sp"
android:paddingEnd="14.5sp"
android:minHeight="35sp"
/>
main.xml
<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">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#666"
android:dividerHeight="1dp"
android:background="#333"
android:paddingLeft="15sp"
android:paddingRight="15sp"
/>
</android.support.v4.widget.DrawerLayout>
strings.xml
<resources>
<string name="app_name">DrawerSample</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="items">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
<item>Item 5</item>
<item>Item 6</item>
</string-array>
<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>
</resources>
Try to add your Edittext and textview of the layout in below lines
android:focusableInTouchMode="true"
android:focusable="true"
or
oncreate methos you use requestfocus(your element id);
thank you.