I am trying to create a Left Navigation for a Fragment, using instruction from this link http://developer.android.com/training/implementing-navigation/nav-drawer.html#top. The problem is in this line, but I don't know how to fix it
mDrawerList.setAdapter(adapter);
Here is the full code:
public class BattleFieldFragment extends Fragment {
private String[] mLeftDrawerTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.activity_battle_field, parent, false);
mLeftDrawerTitles = getResources().getStringArray(R.array.bar_left_drawer);
mDrawerLayout = (DrawerLayout) v.findViewById(R.id.layout_battle_field_drawer);
mDrawerList = (ListView) v.findViewById(R.id.layout_battle_field_left_drawer);
// Set the adapter for the list view
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.drawer_battle_field, mLeftDrawerTitles);
mDrawerList.setAdapter(adapter);
return v;
}
I did not put on xml codes; please let me know if more information is needed.
Here is activity_battle_field.xml as requested:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_battle_field_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/layout_battle_field"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<!-- should not be larger than 320 to show content -->
<ListView android:id="#+id/layout_battle_field_left_drawer"
android:layout_width="180dp"
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>
You're working on the example built for the Activity and now trying to implement it in Fragment. I will give you a short example how I did it in my app, you just have to change layout names to fit yours.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.skoric.tdm.app.MainActivity" >
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.skoric.tdm.drawer.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
fragment_navigation_drawer.xml
<?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">
<ListView
android:id="#+id/drawerList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:listSelector="#drawable/drawer_list_selector"
android:background="#color/white" />
</android.support.v4.widget.DrawerLayout>
NavigationDrawerFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
{
View view = inflater.inflate(R.layout.fragment_navigation_drawer, container);
drawerListView = (ListView) view.findViewById(R.id.drawerList);
...
drawerListView.setAdapter(adapter);
return view;
}
But that's just a part of the code, I personally suggest that you start an empty project in Eclipse or Android Studio and select to automatically create an activity with the navigation drawer implemented and then follow their implementation to create one for your own app.
Related
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>
having a trouble here with the sliding tabs. So I have completed the tutorial but when i had a ListView to my Pager it only shows on the bottom of the screen.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.ambidata.innovway.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#android:color/white"/>
</LinearLayout>
And my XML from ListView:
<?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" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/issue_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_row_selector"/>
</LinearLayout>
Fragment Code:
public class TabIssues extends Fragment {
private ListView mainListView ;
private CustomListAdapter_Issues listAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle b = getArguments();
int user_id = b.getInt("user_id");
boolean all = b.getBoolean("all");
WSConnectionIssues runner = new WSConnectionIssues(user_id, all);
runner.execute();
while (runner.download == AsyncTask.Status.RUNNING)
{
try
{
Thread.sleep(1);
} catch (Exception ex)
{
ex.printStackTrace();
}
}
View view = inflater.inflate(R.layout.activity_issues, container, false);
// Find the ListView resource.
mainListView = (ListView) view.findViewById(R.id.issue_list);
registerForContextMenu(mainListView);
// Create ArrayAdapter using the planet list.
View convertView = inflater.inflate(R.layout.row_issues, null);
listAdapter = new CustomListAdapter_Issues(this, runner.listIssues, convertView);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter(listAdapter);
return view;
}
Im using the phone for debug...as you can see blank...then the first item of my listview and if you scroll the others are there!
Could you try the listview layout as:
<?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" >
<ListView
android:id="#+id/issue_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_row_selector"/>
</RelativeLayout>
I have a main Activity DrawerActivity containes 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">
<!-- The main content view -->
<FrameLayout
android:id="#+id/frame_container"
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="right"
android:choiceMode="singleChoice"
android:divider="#666"
android:dividerHeight="1dp"
android:background="#333"
android:paddingLeft="15sp"
android:paddingRight="15sp"
/>
</android.support.v4.widget.DrawerLayout>
Fragment class loaded from my DrawerActivity.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater
.inflate(R.layout.welcome_activity, container, false);
initTabber();
return rootView;
}
private void initTabber() {
tabbar = (LinearLayout)rootView.findViewById(R.id.tabbar);
tabLayout = LayoutInflater.from(activity.getApplicationContext()).inflate(
R.layout.tabbar_layout, null);
tabbar.addView(tabLayout);
tabbarManager = new TabbarManager(context, tabLayout, activity);
tabbarManager.initTabbar(false, false, false, false);
ViewGroup tabGroup = (ViewGroup)rootView.findViewById(R.id.tabberContainer);
FontsHandler.ApplyFontToGroup(tabGroup, AppConstants.EXT_MEDIUM);
}
When i inflate tabber layout in initTabber methods my DrawerActivity gets slower. I mean DrawerLayout menu is not opening smoothly.Help need pls
I work on my android application and I would like to create a view with sliding sub-view. The sub-view will contains some widgets such as EditText. Here is my layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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 android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:background="#0000ff">
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
I can't switch between EditText widgets. When I click some widget the sliding layout begins to collapse. I thought that is the cause my problem, but I use the following code.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
DrawerLayout drawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
return view;
}
Left side layout is locked but I still cannot switch between widgets. I tried to change clickable and focusable attribute too.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
DrawerLayout drawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
drawerLayout.setClickable(true);
drawerLayout.setFocusable(true);
return view;
}
Any idea?
I can't switch between EditText widgets. When I click some widget the
sliding layout begins to collapse.
because your widgets is on the main layout and those are not on the sliding menu. so when you click on them it means you click outside of your drawerlayout, your layout is incorrect try this:
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
</FrameLayout>
<LinearLayout android:layout_width="250dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:background="#0000ff">
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
I have designed navigation drawer app. All the other features are working properly except Fragment navigation.
When i try to select an item from the side menu, its displays that selected layout on top of the activity_main layout.I think transition.replace() method is not working or it does not replacing the current layout with selected layout.
Here is my code
public class MainActivity extends FragmentActivity {
ActionBarDrawerToggle icon;
final String[] listContent ={"Account Setup","Fragment Two",
"Fragment Three","Main Page"};
final String[] fragments ={
"com.strawberryapps.accountsetup.AccountSetup",
"com.strawberryapps.backupnotes.BackupNotes",
"com.strawberryapps.notes.AddNote",
"com.strawberryapps.myapp.MainPage"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> ad = new ArrayAdapter<String> (getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, listContent);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView list = (ListView) findViewById(R.id.drawer);
list.setAdapter(ad);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
#Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
android.support.v4.app.FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
transition.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[position]));
transition.commit();
}
});
}
});
}
Here is my 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"
android:background="#FFFFFF"
>
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#454545"
android:choiceMode="singleChoice"/>
</android.support.v4.widget.DrawerLayout>
Im developing with android 4.3
Kindly change color of background of fragment's layout .xml file....
Use this code in fragment's layout file in parent layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_home"
android:background="#FFF"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
</RelativeLayout>