I have been working on adding the new appcompat-v7:21.0.0 to my project. For some reason, my overflow menu on my actionbar decides to overlap my actionbar when opening. It should be opening below the actionbar.
Here is my code for base layout:
<?xml version="1.0" encoding="utf-8"?>
<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">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"/>
<RelativeLayout
android:id="#+id/top_status"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_gravity="top|center"
android:background="#color/message_bottom_background"
android:gravity="center_horizontal"
tools:ignore="UselessParent"
android:visibility="gone">
<ImageView
android:id="#+id/top_warning_thumb"
android:src="#drawable/ic_action_warning"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/top_warning_thumb_desc"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:visibility="gone"/>
<TextView
android:id="#+id/top_warning_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/test_text_shortest"
android:layout_toEndOf="#id/top_warning_thumb"
android:layout_toRightOf="#id/top_warning_thumb"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:textSize="20sp"
android:textColor="#color/theme_primary_background"
android:layout_marginTop="1sp"
/>
</RelativeLayout>
</FrameLayout>
<!-- The navigation drawer -->
<LinearLayout
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical"
android:background="#color/card_background">
<TextView
android:id="#+id/left_status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:textSize="25sp"
android:lineSpacingExtra="5sp"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/test_text_shortest"
android:paddingLeft="0sp"
android:paddingRight="0sp"
android:textColor="#color/card_background"
/>
<ListView android:id="#+id/left_drawer_child"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/transparent"
android:dividerHeight="1dp"
android:paddingLeft="0sp"
android:paddingRight="0sp" />
</LinearLayout>
<!--android:divider="#666"-->
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Here is my code from my main activity that pertains to menus:
public class MainActivity extends ActionBarActivity implements WifiDiagnosticFragment
.OnFragmentInteractionListener,DispatchFragment.OnFragmentInteractionListener,
TextMessageViewerFragment.OnFragmentInteractionListener,
SupervisorFragment.OnFragmentInteractionListener,
WorklistFragment.OnFragmentInteractionListener, Observer {
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.base_menu, menu);
if (BuildConfig.BUILD_TYPE.equals("debug")){
menu.findItem(R.id.device_admin).setVisible(true);
}
return super.onCreateOptionsMenu(menu);
}
#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 (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
switch (item.getItemId()) {
case R.id.help:
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
HelpDialog helpDialog = new HelpDialog();
helpDialog.show(fm,"Help");
return true;
case R.id.device_admin:
fm = getSupportFragmentManager();
AdminDialog adminDialog = new AdminDialog();
adminDialog.show(fm,"Device Admin");
return true;
default:
return super.onOptionsItemSelected(item);
}
//return super.onOptionsItemSelected(item);
}
}
As rciovati said, this is the desired behavior: google.com/design/spec/components/menus.html
Related
This question already has answers here:
Android: how to load fragment into FrameLayout
(3 answers)
Closed 4 years ago.
I have created a bottom navigation, when icon is clicked fragment does not load on frame layout instead shows on bottom navigation
Here's the MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_drawer"
tools:context="com.safarpar.safarpar.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.BottomNavigationView
android:id="#+id/main_nav"
android:layout_width="match_parent"
android:layout_height="#dimen/_56sdp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
app:menu="#menu/bottom_nav_menu">
</android.support.design.widget.BottomNavigationView>
<FrameLayout
android:id="#+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/main_nav">
</FrameLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/nav_view"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
And the MainActivity.java
mFrame=(FrameLayout)findViewById(R.id.main_frame);
mbottomNavigationView=(BottomNavigationView)findViewById(R.id.main_nav);
mDrawerLayout=(DrawerLayout)findViewById(R.id.main_drawer);
mToggle = new
ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
//navigationdrawer
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//OnclickHandeler();
homeFragment= new HomeFragment();
bookingFragment= new BookingFragment();
mbottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
setFragment(homeFragment);
return true;
case R.id.nav_booking:
setFragment(bookingFragment);
default:
return true;
}
}
private void setFragment(android.support.v4.app.Fragment Fragment) {
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_nav,Fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
And the Fragment.xml
<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"
tools:context="com.safarpar.safarpar.HomeFragment">
<LinearLayout
android:id="#+id/Linear1"
android:layout_width="match_parent"
android:layout_height="#dimen/_200sdp"
android:background="#drawable/front_slider"
android:gravity="top"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/linear_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/Linear1"
android:layout_marginTop="#dimen/_10sdp"
android:orientation="horizontal"
android:padding="#dimen/_10sdp">
<Button
android:id="#+id/main_flight"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_marginLeft="#dimen/_19sdp"
android:layout_marginRight="#dimen/_7sdp"
android:background="#drawable/flight_button_state" />
<Button
android:id="#+id/main_hotel"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_marginLeft="#dimen/_10sdp"
android:layout_marginRight="#dimen/_7sdp"
android:background="#drawable/hotel_button_state" />
<Button
android:id="#+id/main_bus"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_marginLeft="#dimen/_7sdp"
android:layout_marginRight="#dimen/_10sdp"
android:background="#drawable/bus_button_state" />
<Button
android:id="#+id/main_cab"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_marginLeft="#dimen/_7sdp"
android:layout_marginRight="#dimen/_10sdp"
android:background="#drawable/cab_button_state" />
</LinearLayout>
</RelativeLayout>
Change the code for setting the Fragment like the following.
private void setFragment(android.support.v4.app.Fragment Fragment) {
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame,Fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
You need to use main_frame instead of main_nav here.
I had toolbar with options in activity_one toolbar and everything worked well it was created and also clickable. All buttons were working good. But after I moved it to another activity it doesn't work. I am talking about this button
I have checked another topics with the same questions but at that topics there were mistakes in code. But I can't find any mistakes in my code. Because it was working well in another activity. Maybe I do something wrong but I can't solve this problem with that information which I have.
The code of XML
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.borisruzanov.social.ui.ChatActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolbar"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
<ListView
android:id="#+id/messageListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/linearLayout"
android:stackFromBottom="true"
android:divider="#android:color/transparent"
android:transcriptMode="alwaysScroll"
tools:listitem="#layout/item_message"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="horizontal">
<ImageButton
android:id="#+id/photoPickerButton"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#android:drawable/ic_menu_gallery" />
<EditText
android:id="#+id/messageEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:enabled="false"
android:text="#string/send_button_label"/>
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
The code of class
Toolbar toolbar;
in OnCreate
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
And two methods
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
Log.v("========>", "In oncreateMenu");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.v("========>", "In item selected");
int id = item.getItemId();
if (id == R.id.menu_log_out){
mAuth.signOut();
}else if (id == R.id.menu_chat){
Intent intent = new Intent(this, ChatActivity.class);
startActivity(intent);
}
return true;
i have added a navigation drawer to an activity and now the space where normaly the navigation bar is set (in my case it should be hidden by a fullscreen call) is occupied by a blank space. without the navigation drawer the activity works just fine, fullscreen and no blank space. i also recognized that it works fine on devices with hardwarebuttons, there is no blankspace, like on a google pixel i.e.
Layout XMLs:
Activity xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:id="#+id/webViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button_panel">
<ScrollView
android:id="#+id/scrollViewContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</FrameLayout>
<RelativeLayout
android:id="#+id/button_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FFF"
android:orientation="horizontal"
android:padding="5dp">
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_to_the_left"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_margin="2dp"
android:alpha="0.2"
android:background="#drawable/arrowcircle"
tools:mockup_opacity="50%" />
<Button
android:id="#+id/button_Decision1"
style="#style/Widget.AppCompat.Button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="3dp"
android:layout_weight="1"
android:background="#color/colorDecision1"
android:text="#string/action_decision1"
android:textAppearance="?android:textAppearanceInverse" />
<Button
android:id="#+id/button_Decision2"
style="#style/Widget.AppCompat.Button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="3dp"
android:layout_weight="1"
android:background="#color/colorDecision2"
android:text="#string/action_decision2"
android:textAppearance="?android:textAppearanceInverse" />
<Button
android:id="#+id/button_Decision3"
style="#style/Widget.AppCompat.Button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="3dp"
android:layout_weight="1"
android:background="#color/colorDecision3"
android:text="#string/action_decision3"
android:textAppearance="?android:textAppearanceInverse" />
<Button
android:id="#+id/button_to_the_right"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_margin="2dp"
android:alpha="0.2"
android:background="#drawable/arrowcircle"
android:rotation="180" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer_text_act" />
following are the xml-files from the AS template fro navigation drawer
app_bar_main.xml:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.max.mybai.controller.MbaiStartActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
content_main.xml:
<android.support.constraint.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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.max.mybai.controller.MbaiStartActivity"
tools:showIn="#layout/app_bar_main">
nav_header_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#color/colorPrimary"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:background="#mipmap/ic_launcher"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:paddingLeft="5dp"
android:text="Menu"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Code from the Activity (which i took from the AS template for navigation drawer) except the fullscreen()
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_save_story) {
// Save current reading progress to Shared Preferences
...
Snackbar.make(getCurrentFocus(), "Geschichte wurde gespeichert", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void FullScreencall() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (!(ViewConfiguration.get(this).hasPermanentMenuKey())) {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
}
}
I created custom Toolbar.I added two images,one in center position and second left position.in xml view,everything is perfect but when i added menu elements in right side in toolbar,toolbar margined right side and image is not left position
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/md_white_1000"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/u_main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"/>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_below="#+id/statusBar"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView8"
android:background="#mipmap/logo_header"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/openDrawable"
android:background="#mipmap/ic_menu_white"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="#+id/statusBar"
android:layout_width="match_parent"
android:layout_height="#dimen/statusBarHeight"
android:background="#color/u_color" />
</RelativeLayout>
</LinearLayout>
<LinearLayout android:id="#+id/drawer_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">
<LinearLayout
android:id="#+id/drawer_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical" >
<include layout="#layout/activity_slider_menu" />
</LinearLayout>
</LinearLayout>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
is it a possible to add image center position when i added menu in toolbar?
I have an activity that displays a drawer layout when a menu item is clicked thus its direction is from right to left. I manage to display the navigation menu using the following code.
public class Forecast_details extends ActionBarActivity implements OnChartValueSelectedListener {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//return false;
// 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();
if (id == R.id.action_settings) {
return true;
}
if(id==R.id.Options){
if (mDrawerlayout.isDrawerOpen(Gravity.END)){
mDrawerlayout.closeDrawer(Gravity.END);
}else
mDrawerlayout.openDrawer(Gravity.END);
}
return super.onOptionsItemSelected(item);
}
My Navigation drawer contains a static list so I did not bother to make a listview out of it as seen below,
However I cannot click my items inside the Drawer Layout, or anything in the view even v4.widget.DrawerLayout in my xml is unclickable. Below is the two xml file that I used.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
></include>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout_right"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<include layout="#layout/nav_drawer_right" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.36" >
....
</LinearLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
nav_drawer_right.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView_right"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#2c3e50"
android:paddingTop="20dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container_fragment2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="1dp" >
</FrameLayout>
<LinearLayout
android:id="#+id/linearLayout_menuconatiner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="2dp"
android:paddingRight="2dp" >
<ImageView
android:id="#+id/imageView_forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:clickable="true"
android:onClick="SendClick"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_forward" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_request" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_reloadvnet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_updatevnet" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_reloadvsms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_updatevsms" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_feedback" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
Please help.. I tried a lot of things to make it work like using a listview instead but I still have no luck.. please do help, Im new at using drawer layout so there may be some more things I need to know.. Thank you in advance..
To prevent return OnClickListener to parent layout require set android:clickable="true" in drawerLayout container.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout_right"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.36" >
....
</ScrollView>
<include layout="#layout/nav_drawer_right" />
</android.support.v4.widget.DrawerLayout>
nav_drawer_right.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView_right"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#2c3e50"
android:paddingTop="20dp" >
....
</ScrollView>
in your Activity
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawerLayout mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout_right);
ScrollView sV = (ScrollView) findViewById(R.id.scrollView1);
mDrawerLayout.openDrawer(Gravity.RIGHT);
sV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("drawer click >>>>>>>>>>>>>>>>>>");
}
});
}
}