I am trying that add section separator header for ListView in drawerLayout but i can not see separators and i just see titles.
This my code :
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public Cursor cursor;
public Cursor cursorsex;
public ArrayList<String> array;
public ArrayList<String> arraysex;
private ArrayAdapter<String> listAdapter ;
private ArrayAdapter<String> listAdaptersex ;
private ListView mDrawerList;
private ListView mDrawerListsex;
.....
Connection to sqlite
.....
sql = db.openDataBase();
cursor = sql.rawQuery("SELECT Title FROM WebSite_CategoryBack WHERE ParentID=0;", null);
array = new ArrayList<String>();
while(cursor.moveToNext()){
Titel_Drawer = cursor.getString(cursor.getColumnIndex("Title"));
array.add(Titel_Drawer);
}
cursor.close();
cursorsex = sql.rawQuery("SELECT Title FROM WebSite_CategoryBack WHERE ParentID=0;", null);
arraysex = new ArrayList<String>();
while(cursorsex.moveToNext()){
Titel_Drawer = cursorsex.getString(cursorsex.getColumnIndex("Comment"));
arraysex.add(Titel_Drawer);
}
cursorsex.close();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerListsex = (ListView) findViewById(R.id.sex);
listAdapter = new ArrayAdapter<String>(this, R.layout.drawer_list,R.id.textView, array);
mDrawerList.setAdapter( listAdapter );
listAdaptersex = new ArrayAdapter<String>(this, R.layout.xxx,R.id.xxx, arraysex);
mDrawerListsex.setAdapter( listAdaptersex );
}
}
This is xxx.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/xxx"/>
This is drawer_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"/>
This is activity_main.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="#+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start">
<TextView
android:layout_width="240dp"
android:layout_height="25dp"
android:textSize="20sp"
android:gravity="center"
android:text="#string/prof"
android:background="#drawable/drawer_title_shape" />
<!-- The navigation drawer -->
<ir.makarem.extentions.ClearableEditText
android:id="#+id/SearchEdittxt"
android:drawableLeft="#drawable/ic_action_search"
android:layout_width="240dp"
android:layout_height="35dip"
android:background="#drawable/textlines"
android:gravity="right"
android:hint="#string/action_search" />
<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="#FFDDDDDD"/>
<ListView
android:id="#+id/sex"
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="#FFDDDDDD"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
For example i need to see look like this photo.
I add two TextView for Title and two ListView.
you have to use section header listview to achieve this functionality
Refrence link Section header listview
You need to set a divider for your ListView: (code example)
// white color for example
int[] clr = {0, 0xFFFFFFFF, 0};
listView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, clr));
listView.setDividerHeight(1); // set your desired height
Or set the divider in .xml:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#FFFFFF"
android:dividerHeight="2px" >
</ListView>
Related
How can i change color of ListView Text(in drawer layout) in my code?
In my code listview is in various other layout.
I have tried various other code available in stackoverflow but none seemed to work for me
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/lltoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/lltoolbar"
android:text="#string/hello_world" />
</RelativeLayout>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_marginTop="55dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/drawerList"
android:entries="#array/abcd"
android:background="#FFFFFF"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left" />
</android.support.v4.widget.DrawerLayout>
private Toolbar toolbar;
TextView textView;
ImageView iv;
DrawerLayout drawerLayout;
private ListView listView;
private String[] names;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
textView = (TextView) findViewById(R.id.toolbar_title);
iv = (ImageView) findViewById(R.id.ivClick);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
listView = (ListView) findViewById(R.id.drawerList);
names = getResources().getStringArray(R.array.abcd);
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names));
listView.setOnItemClickListener(this);
}
you should create an xml layout for each row of your listview then create a class extend BaseAdapter and inside of getview inflate xml layout then set color for textview
Follow these steps:
Create an xml layout which will be shown in each row of your listview. (currently you are using the default android.R.layout.simple_list-item_1 layout) something like this:my_listrow_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sample text"
android:text_color="#006699"
android:id="#+id/list_item"/>
</LinearLayout>
2. in your activity code, change your list adapter code to this:
listView.setAdapter(new ArrayAdapter<>(this, R.layout.my_listrow_layout,
R.id.list_item, names));
my_listrow_layout is your custom made layout for the listview, R.id.list_item is the id of your textview in the listview layout. To change the color of the text add android:text_color="your desired color" value in the layout you just created. ( I have set it to #006699 which a dark blue color).
I'm trying to create a navigation drawer, but I keep getting a NullPointer error. When I debugged it, the ListView and DrawerLayout values were null, even though I assign them their respective values. Can anyone tell me what's wrong?
public class MainActivity extends ListActivity {
private ArrayList<String> mTitles = new ArrayList<String>();
private ArrayList<String> mUrls = new ArrayList<String>();
private ArrayList<String> mAbstract = new ArrayList<String>();
public ArrayList<String> mDrawerTitles = new ArrayList<String>();
public DrawerLayout mDrawerLayout;
public ListView mDrawerList;
ProgressDialog progressDialog;
public final String TOPSTORYKEY = "878029f7fee45ecd61ca3f52ea027186:19:72302140";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
mDrawerTitles.add("Politics");
mDrawerTitles.add("Technology");
mDrawerTitles.add("Sports");
mDrawerTitles.add("Books");
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
//DrawerListAdapter drawerAdapter = new DrawerListAdapter(this, mDrawerTitles);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
//new HttpJSONLoader().execute();
}
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
EDIT: here is my activity_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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/empty"
android:text="No results"
android:layout_centerInParent="true"/>
<!-- 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>
EDIT:
Solution was moving DrawerLayout from drawer_layout.xml to activity_main.xml and making layout have only two children, where first is MainActivity UI, and second is navigation drawer.
<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="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="No results" />
</RelativeLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
You need to set proper id in your XML for your ListView - android:id="#+id/left_drawer", which is the same as id are you referring in OnCreate, it causes your error.
Anyway, your implementation is wrong. Please read how to do it properly.
Here it is great explained how to do this - link
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/left_drawer"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No results"
android:layout_centerInParent="true"/>
i am trying to add on click listener in a listview but when clicked, no action. can you help me find what i did wrong?
This is adapted from one of the example in android tutorial
public class Main extends Activity {
private ListView menuListView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Inflate the menu items for use in the action bar
//getMenuInflater().inflate(R.menu.menu_main,menu);
//Get the Menu List from resources
menu = getResources().getStringArray(R.array.menu);
//get the drawer Layout
menuDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ArrayAdapter<String> itemAdapter = new ArrayAdapter<String>(this,R.layout.custom_menu_list,menu);
//get the list view
menuListView = (ListView) findViewById(R.id.listview);
menuListView.setAdapter(itemAdapter);
menuListView.setOnItemClickListener(new menuItemClickListener());
}
private class menuItemClickListener implements ListView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("Check","clicked");
}
}
}//
}
This is the xml of the listview textview
<?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="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:focusable="false"
android:focusableInTouchMode="false"
/>
This is the 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"
android:background="#FFFFFF">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"/>
<!-- The navigation drawer -->
<ListView android:id="#+id/listview"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toastMsg"
android:text="Hello"/>
</android.support.v4.widget.DrawerLayout>
Modify you're main.xml file, remove the TextView and run the code. It works
<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"
android:background="#FFFFFF">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"/>
<!-- The navigation drawer -->
<ListView android:id="#+id/listview"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"/>
</android.support.v4.widget.DrawerLayout>
DrawerLayout activity_register.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">
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal|center"
android:orientation="vertical" >
<TextView
android:id="#+id/msgView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<LinearLayout
android:id="#+id/sitrep_buttons_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal|center"
android:orientation="vertical">
<ImageView
android:id="#+id/sitrep_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone"
android:contentDescription="#string/desc"/>
</LinearLayout>
</LinearLayout>
<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="#999"/>
</android.support.v4.widget.DrawerLayout>
In my oncreate(Bundle savedInstanceState)
private ListView mDrawerList;
private String[] mActivities;
private String username = "";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// mDrawerList = new ListView(RegisterActivity.this);
mActivities = getResources().getStringArray(R.array.menu_item_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setBackgroundColor(0xFF00FF00);
Log.d(DTAG, "mDrawerList.getContentDescription: "+mDrawerList.getContentDescription());
Log.d(DTAG, "mDrawerList "+mDrawerList.equals(null));
Log.d(DTAG, "mActivities.length: "+mActivities.length);
for(int i = 0; i < mActivities.length; i++){
Log.d(DTAG, "mActivities: "+mActivities[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, mActivities);
Log.d(DTAG, "adapter.getCount: "+adapter.getCount());
mDrawerList.setAdapter(adapter);
The Log shows that my array is populated properly.
The Navigation Drawer left_drawer (ListView) shows but is not populated. width changes if I change layout_width, and background color is matches setting as well. I've been looking at multiple posts to compare my code, but I can't see a problem with the code.
Any Ideas would be great!
Ok, here's something. I can't change any attributes of the ListView programmatically.
mDrawerList.setBackgroundColor(0xFF00FF00);
Has no effect. Background remains set to that set in the layout file.
The list is there and it goes to whatever activity I send it to but the list the names on the list don't show. I've changed the color of the font and the drawer background and it doesn't show. The item gets highlighted alright and it redirects to the proper activity as it should but there's nothing written in the drawer.
activity_main:
<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/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/menu_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>
drawer_list_item.xml:
<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:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
MainActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listMenu = getResources().getStringArray(R.array.menuArray);
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
lv1 = (ListView) findViewById(R.id.menu_drawer);
lv1.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, listMenu));
I had set my array like this:
<item name="section" value="Section"/>
when I should have done this:
<item name="section">Section</>