Reputation: 101
In My App i created Side Menu using Navigation Drawer.I need to change the menu list item background color based on clicking state.So i used <selector>
to achieve this.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" ><color android:color="@color/red"/></item>
<item android:state_focused="true"><color android:color="@color/yellow"/></item>
<item android:state_activated="true"><color android:color="@color/green"/></item>
<item><color android:color="#ddd"/></item>
</selector>
Above code was working fine. After clicking a item in navigation drawer background color is changing fine based on the above selector and also navigation drawer will be closed.
But My problem is if i open navigation drawer menu item background color goes to default. So i need to set the same background color for the menu item.
Then if i open the navigation drawer my menu item comes to default color.No Background color for an item.
But i need to set the background color for a menu item based on the current activity.
Background color disappear only when navigation drawer is closed.
Please help me.
Sorry for my poor english.
Thanks in advance.
package com.example.qh_test;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TechPageActivity extends ActionBarActivity implements
OnItemClickListener {
private DrawerLayout drawerlayout;
private ListView listview;
private String[] menus;
private ActionBarDrawerToggle drawerListener;
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tech_page);
drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);
drawerListener = new ActionBarDrawerToggle(this, drawerlayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
// TODO Auto-generated method stub
}
};
drawerlayout.setDrawerListener(drawerListener);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
menus = getResources().getStringArray(R.array.tech_menus);
listview = (ListView) findViewById(R.id.drawerList);
listview.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, menus));
listview.setOnItemClickListener(this);
webview = (WebView) findViewById(R.id.homeWebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new MyWebViewClient());
webview.loadUrl("http://stackoverflow.com/");
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListener.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tech_page, 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();
if (drawerListener.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListener.onConfigurationChanged(newConfig);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
selectItem(position);
//SelectItem.get(position).setColor(getResources().getColor("your color"));
switch (position) {
case 0:
webview.loadUrl("http://stackoverflow.com/");
listview.setItemChecked(position, true);
drawerlayout.closeDrawer(listview);
break;
case 1:
webview.loadUrl("http://stackoverflow.com/questions");
listview.setItemChecked(position, true);
drawerlayout.closeDrawers();
break;
case 2:
webview.loadUrl("http://stackoverflow.com/unanswered");
listview.setItemChecked(position, true);
drawerlayout.closeDrawers();
break;
}
}
public void selectItem(int position) {
// TODO Auto-generated method stub
listview.setItemChecked(position, true);
setTitle(menus[position]);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
}
Upvotes: 2
Views: 4674
Reputation: 2430
In your OnClickListener for NavigationDrawer menu call method setItemChecked(int position, true)
Upvotes: 0
Reputation: 806
You can call such a method in the onStart method of your activity (with ListView mDrawerList and your DrawerAdapter as mDrawerAdapter):
protected void selectItem(int position)
{
if (mDrawerList != null) {
mDrawerList.setItemChecked(position, true);
mDrawerAdapter.setSelected(position);
}
}
Upvotes: 1
Reputation: 91
check this sample custom navigation drawer
inside onclick
SelectItem.get(position).setColor(getResources().getColor("your color"));
http://www.tutecentral.com/android-custom-navigation-drawer/
Upvotes: 0