Android NavigationDrawer items don't click - android

I'm using Android NavigationDrawer without Listview items into slide menu, after creating and changing that slide menu items don't click and don't work to handle to click, for example this below code is my XML layout:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/create_new_waybill"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_margin="5dp"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_add_waybill"
android:layout_gravity="center"/>
<com.sample.app.Widgets.TextViewStyle
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="#dimen/normal_text_size"
android:text="#string/slidemenu_waybill"
app:fonttype="mjbeirut"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/mainViewFragments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
In this XML I want to show Toast after clicking on create_new_waybill id.
my Activity
public class ActivityMain extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private LinearLayout mSlideMenuContainer;
private ActionBarDrawerToggle mDrawerToggle;
private String[] items;
private UiFiller uiFiller;
private int currentSelectedSlideMenuItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
G.currentActivity = this;
/* Getting reference to the DrawerLayout */
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mSlideMenuContainer = (LinearLayout) findViewById(R.id.slide_menu);
LinearLayout create_new_waybill = (LinearLayout) findViewById(R.id.create_new_waybill);
create_new_waybill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("ffffffffffff","aaaaaaa");
}
});
/* Initials UI widgets */
uiFiller = new UiFiller(getWindow().getDecorView());
/* Setting default fragment */
currentSelectedSlideMenuItem = 0;
updateFragment(currentSelectedSlideMenuItem);
}
}

Try setting these 3 attributes for ImageView and TextViewStyle:
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
and replace Log.e("ffffffffffff","aaaaaaa");
with:
Toast.makeText(view.getContext(), "Some text", Toast.LENGTH_SHORT).show();

Related

Show/Hide Layout onTouch in framelayout

I have a full image fragment. There is a RelativeLayout and LinearLayout in framelayout. LinearLayout contain viewpager.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayoutViewpager">
<com.bogdwellers.pinchtozoom.view.ImageViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relativelayoutforbuttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/save_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
</RelativeLayout>
I want When user first open it it should only show image(Viewpager layout) and if user click on it that show other option like - share and save(RelativeLayout).
We can find this type of feature in facebook and whatsapp.
You can simply hide or unhide the relativeLayout's instance using setVisibility.(..) method on linearLayout or imageviewpager click.
layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayoutViewpager">
<ImageViewPager
android:id="#+id/viewpager"
android:onClick="onPagerClick"
android:layout_width="200dp"
android:layout_height="200dp" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relative_layout"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/save_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onSave"
android:text="Save" />
</RelativeLayout>
</FrameLayout>
Activity:
public class TestActivity extends AppCompatActivity {
private boolean isButtonShown = false;
ImageViewPager viewPager;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
viewPager = findViewById(R.id.viewpager);
layout = findViewById(R.id.relative_layout);
}
public void onPagerClick(View view) {
if (isButtonShown) {
isButtonShown = false;
layout.setVisibility(View.GONE);
} else {
isButtonShown = true;
layout.setVisibility(View.VISIBLE);
}
}
}
Try this type code for visiablity ..
private void share(){
relativeLayout.setVisibility(View.VISIBLE);
linearLayoutViewpager.setVisibility(View.GONE);
}
linearLayoutViewpager=findViewById(R.id.linearLayoutViewpager);
linearLayoutViewpager.setVisibility(View.VISIBLE);
relativeLayout=findViewById(R.id.relativelayoutforbuttons);
relativeLayout.setVisibility(View.GONE);
it only sample code but mange according your needs.
final boolean flag=true;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (flag){
relativeLayout.setVisibility(View.VISIBLE);
flag=false;
}
else{
relativeLayout.setVisibility(View.GONE);
}
}
});

Common Navigation Menu drawer In Android

I need to add navigation drawer menu in my android application. my application has many modules so i can't code each activity to display navigation menu. so i decided to put the navigation menu code in my base activity. so each activity extends this base activity. navigation drawer menu's are working properly but the problem is activity components are not working. i don't know what is happening. is there any changes needed? Thanks in advance.
baseActivity.java
public class BaseActivity extends ActionBarActivity {
RelativeLayout fullLayout;
DrawerLayout dLayout;
#Override
public void setContentView(int layoutResID) {
fullLayout = (RelativeLayout) getLayoutInflater().inflate(
R.layout.activity_base, null);
FrameLayout frameLayout = (FrameLayout) fullLayout
.findViewById(R.id.content_frame);
ListView dList = (ListView) fullLayout.findViewById(R.id.left_drawer);
dLayout = (DrawerLayout) fullLayout.findViewById(R.id.drawer_layout);
getLayoutInflater().inflate(layoutResID, frameLayout, true);
setContentView(fullLayout);
String[] menu = new String[] { "Home", "Android", "Windows", "Linux",
"Raspberry Pi", "WordPress", "Videos", "Contact Us" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, menu);
dList.setAdapter(adapter);
dList.setSelector(android.R.color.holo_blue_dark);
dList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
dLayout.closeDrawers();
}
});
}};
activity_base.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="sas.mobi.lakshmi.main.BaseActivity" >
<FrameLayout
android:id="#+id/content_frame"
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" >
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
homeActivity.java
public class HomeAndSettingActivity extends BaseActivity {
private Button btnAccount;
private Button btnCollection;
private Button btnOthers;
private Button btnTempExit;
private Button btnExit;
private AdminDS adminDS;
private AdminDO adminDO;
private FinanceDS financeDS;
private PartnerPaymentDS paymentDS;
private GoogleCloudMessaging gcm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_and_setting);
initializeComponents();
btnAccount.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
AccountRegHomeActivity.class);
startActivity(intent);
finish();
}
});
btnCollection.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
CollectionHomeActivity.class);
startActivity(intent);
finish();
}
});
btnOthers.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
OthersHomeActivity.class);
startActivity(intent);
finish();
}
});
btnTempExit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
btnExit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
financeDS = new FinanceDS(getApplicationContext());
boolean isUps = financeDS.closeActiveCurrentFinances();
if (isUps) {
finish();
}
}
});
}
/**
* method to initialize components
*/
private void initializeComponents() {
btnAccount = (Button) findViewById(R.id.btnAccount);
btnCollection = (Button) findViewById(R.id.btnCollection);
btnOthers = (Button) findViewById(R.id.btnOthers);
btnTempExit = (Button) findViewById(R.id.btnTempExit);
btnExit = (Button) findViewById(R.id.btnExit);
}};
acitivity_home.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<Button
android:id="#+id/btnAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="?android:attr/dividerVertical"
android:text="#string/btnAccount"
android:textSize="14sp" />
<Button
android:id="#+id/btnCollection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="?android:attr/dividerVertical"
android:text="#string/btnCollection"
android:textSize="14sp" />
<Button
android:id="#+id/btnOthers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="?android:attr/dividerVertical"
android:text="#string/btnOthers"
android:textSize="14sp" />
<Button
android:id="#+id/btnTempExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="?android:attr/dividerVertical"
android:text="#string/btnTempExit"
android:textSize="14sp" />
<Button
android:id="#+id/btnExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="?android:attr/dividerVertical"
android:text="#string/btnExit"
android:textSize="14sp" />
this home activity and common navigation drawer menu's showing properly, but the component inside the home activity is not working.but the drawer components are working.
Change the main xml into this way and load the sidemenu into side menu frame:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="sas.mobi.lakshmi.main.BaseActivity" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<FrameLayout
android:id="#+id/side_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
Hi use fragments to Achive the Navigation Draw use cannot do so good with activities as its very simple you need replace fragment on each call of on your menu please find the below link http://www.androidhive.info/2015/04/android-getting-started-with-material-design/
or you can create new project in android studio by selecting nav ui from a new project
As mencioned here you need to move your FrameLayout with id content_frame inside DrawerLayout something like that
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="sas.mobi.lakshmi.main.BaseActivity" >
<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: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:background="#fff"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

Button beneath navigation drawer not working when pressed

Yet, when the button is placed in the hierarchy so that it can be seen on top of the navigation drawer, the button functions properly. However, the button should be hidden behind the navigation drawer when it is slid out, so this is not desirable.
Below is the code from MainActivity.java
public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks {
public ProgressDialog progBar;
public final static boolean DEBUG = false;
public final static String TAG = "AppGetter";
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
ImageButton cart_button = (ImageButton) findViewById(R.id.button2);
cart_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
start_request();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onNavigationDrawerItemSelected(int position) {
Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
}
#Override
public void onBackPressed() {
if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
super.onBackPressed();
}
public void start_request()
{
String pkg = getPackageName();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(pkg,pkg+".RequestActivity"));
startActivity(intent);
if(DEBUG)Log.v(TAG,"Intent intent: "+intent);
}
}
I am assuming the issue lies within the class above, but for completion's sake, I pasted the two XML files of interest below.
activity_main.xml As you can see, the ImageButton is currently "above" the navigation drawer in hierarchy so as to make it be covered by the navigation drawer. Moving the ImageButton "below" makes the button work properly, but causes it to appear on top of the navigation drawer (and not tinted like the rest of the layout).
<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">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fontify="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#color/myPrimaryColor">
<View
android:id="#+id/block1"
android:layout_height="240dp"
android:layout_width="match_parent"
android:layout_below="#+id/toolbar_actionbar"
android:background="#drawable/block_primary"
/>
<TextView
android:id="#+id/title1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/toolbar_actionbar"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:text="#string/title"
android:textColor="#color/white"
android:textSize="34sp"
android:fontFamily="sans-serif"
/>
<include
android:id="#+id/toolbar_actionbar"
layout="#layout/toolbar_default"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<ImageButton
android:id="#+id/button2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="268dp"
android:src="#drawable/ic_chevron_up"
android:background="#drawable/fab_simple"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="#+id/toolbar_actionbar"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment
android:id="#+id/fragment_drawer"
android:name="com.onepersonco.iconrequestbase.NavigationDrawerFragment"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
fragment_navigation_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- Provides a margin at the top of the navigation drawer. -->
<View
android:id="#+id/navWhiteSpace1"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#color/myNavigationDrawerBackgroundColor"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_below="#+id/navWhiteSpace1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:scrollbars="vertical"
android:scrollbarDefaultDelayBeforeFade="0"
android:scrollbarFadeDuration="0"
android:overScrollMode="never"
android:focusable="true"
android:background="#color/myNavigationDrawerBackgroundColor"/>
</RelativeLayout>
I ran into the same issue when attempting to follow various navigation drawer tutorials online.
What worked for me was using Mike Penz's MaterialDrawer library on GitHub. He has a very simple tutorial in the "readme" file found on the bottom of the page.
Hopefully someone else with a better understanding of Java can explain why your code failed.

Android DrawerLayout not responding to button click event

I have a DrawerLayout component in my activity, but I'm not using ActionBar.
This is my main_activity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/drawer"
android:layout_width="650dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:divider="#null"
android:choiceMode="singleChoice"
android:visibility="gone"/>
</android.support.v4.widget.DrawerLayout>
<ImageButton
android:id="#+id/categories_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="20dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="#drawable/button_category"/>
In my MainActivity I try to open/close the DrawerLayout by clicking on my Button.
Here is my Activity:
public class MainActivity extends Activity {
private ImageButton categoriesButton;
private DrawerLayout drawerLayout;
private ListView drawerMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
categoriesButton = (ImageButton)findViewById(R.id.categories_button);
categoriesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(drawerLayout.isDrawerOpen(drawerMenu)) {
drawerLayout.closeDrawer(drawerMenu);
}else {
drawerLayout.openDrawer(drawerMenu);
}
}
});
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerMenu = (ListView) findViewById(R.id.drawer);
MyAdapter myAdapter = new MyAdapter(context, getCategories());
drawerMenu.setAdapter(myAdapter);
}
}
When I click the Button the first time it does not respond, but after I open the DrawerLayout manually it works perfect.
Does anybody know what is wrong here?
Thanks!
I found the solution! In the xml, the Listview visibility needs to be set to visible. It was set to gone (I took this code from the oficial documentation).
<ListView
android:id="#+id/drawer"
android:layout_width="650dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:divider="#null"
android:choiceMode="singleChoice"
android:visibility="visible"/>
Anyway now it works. Thanks for your help!

Navigation Drawer not occupying the entire screen height

I have a drawer layout with drawer on left.
Drawer has a button on click of which I want to replace the view of the drawer with a new view. The problem is , when new view gets replaced it doesn’t occupy the entire screen height.
To illustrate I have given a background colour to my navigation drawer views.
I want the Green view to occupy the entire screen height. Copy-paste the code below to get going:
MainActivity.java
public class MainActivity extends Activity {
LayoutInflater inflater;
DrawerLayout drawer;
RelativeLayout parentRL;
RelativeLayout viewToBeRemovedRL;
View viewToBeAdded;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_activity);
inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
parentRL = (RelativeLayout) findViewById(R.id.parentRL);
viewToBeRemovedRL = (RelativeLayout) findViewById(R.id.viewToBeReplaced);
}
public void onRemoveView(View view) {
viewToBeAdded = inflater.inflate(R.layout.add_view, null);
viewToBeRemovedRL.setVisibility(View.GONE);
parentRL.addView(viewToBeAdded);
}
public void onOpenDrawer(View view) {
drawer.openDrawer(parentRL);
}
}
main_activity.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" >
<RelativeLayout
android:id="#+id/leftRL"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_width="wrap_content"
android:layout_height="75dp"
android:onClick="onOpenDrawer"
android:text="Open Drawer" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/parentRL"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="left" >
<RelativeLayout
android:id="#+id/viewToBeReplaced"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000" >
<Button
android:layout_width="100dp"
android:layout_height="75dp"
android:onClick="onRemoveView"
android:text="Change View" />
</RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
add_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="290dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="New view"
/>
</RelativeLayout>
Usually when this happens, the drawer actually DOES occupy the entire screen height, it's just that the default background colour of the view is clear.
Set you RelativeLayout#id/parentRL 's background to a solid colour or some drawable, e.g.
android:background="#android:color/black"
and your drawer should cover the entire screen height.

Categories

Resources