ListView in Navigation Drawer DrawerLayout not populating - android

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.

Related

How to change color of text inside ListView?

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).

adding an icon for each row of listView into drawer

i want to be very clear on explain everything,as title says, how i can insert icon next to each row,but an image not taken from drawable but by URL
i know to achieve this task with a drawable, but i'm not figuring out on how to do this with an URL image like:
URL url = new URL(stringURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
overlay = BitmapFactory.decodeStream(input);
connection.disconnect();
so it's a bitMap, not a drawable eg.(.png,.jpg...),
my activity:
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ListView mDrawerListR;
private ActionBarDrawerToggle mDrawerToggle;
private LinearLayout rightDrawerLinearLayout;
private String[] drawerItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerItems = getResources().getStringArray(R.array.array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawerItems));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
rightDrawerLinearLayout = (LinearLayout) findViewById(R.id.right_drawer_ll);
mDrawerListR = (ListView) findViewById(R.id.right_drawer);
mDrawerListR.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawerItems));
mDrawerListR.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar1);
seekBar.setOnTouchListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close );
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
[...]
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" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#123456"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
<LinearLayout
android:id="#+id/right_drawer_ll"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#123456"
android:orientation="vertical"
android:visibility="visible" >
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="14dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="10dp"
android:max="5"
android:progress="0" />
<ListView
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#123456"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</LinearLayout>
</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:background="?android:attr/activatedBackgroundIndicator"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#fff" />
thanks for any help.
If you want to load a image from a URL, you can use a library like ...
https://github.com/nostra13/Android-Universal-Image-Loader
You can just load the image asynchronously via the adapter..

How separating Lists with Headers in android?

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>

Java Illegal Argument Exception android

I am having this kind of error, after searching for possible resources still does not work.
Error:
java.lang.IllegalArgumentException: View android.widget.ExpandableListView{416b0bc8 VFED.VC. ......ID 0,0-480,0 #7f080005 app:id/lvExp} is not a drawer
Heres my code:
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
private DrawerLayout drawer;
private LinearLayout linear;
Button btn;
HashMap<String, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepareListData();
btn = (Button)findViewById(R.id.button1);
linear = (LinearLayout)findViewById(R.id.linear);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(!drawer.isDrawerOpen(expListView))
{
drawer.openDrawer(expListView);
}
else
{
drawer.closeDrawer(expListView);
}
}
});
// get the listview
// preparing list data
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
FOR XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#f4f4f4" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#000000"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="47dp"
android:layout_height="45dp"
android:background="#drawable/ic_home" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drawer"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<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 -->
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000" >
</ExpandableListView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
</LinearLayout>
you are getting exception on if(!drawer.isDrawerOpen(expListView)) expListView list is not a drawer. thats why you are getting java.lang.IllegalArgumentException:
try this post you will get some idea.
for more information link

The ListView Items in my navigation drawer are there but I can't see the list

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</>

Categories

Resources