Extended navigation drawer can't change activity. What's going on? - android

I have created a BaseActivity class with navigation drawer and the other class CheckList extends the BaseActivity to have a navigation drawer.
In BaseActivity, I can change the activity to CheckList through navigation drawer.
But in CheckList. Even I can see the drawer, it is not work for changing class.
BaseActivity:
public class BaseActivity extends AppCompatActivity{
protected DrawerLayout drawerLayout;
protected ListView drawerList;
protected ActionBarDrawerToggle drawerToggle;
protected CharSequence mDrawerTitle;
protected CharSequence mTitle;
protected String[] drawer_menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initActionBar();
initDrawer();
initDrawerList();
}
protected void initActionBar() {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);
}
protected void initDrawer() {
setContentView(R.layout.drawer);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerList = (ListView)findViewById(R.id.left_drawer);
mTitle = mDrawerTitle = getTitle();
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getSupportActionBar().setTitle(mTitle);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(mDrawerTitle);
}
};
drawerToggle.syncState();
drawerLayout.setDrawerListener(drawerToggle);
}
protected void initDrawerList() {
drawer_menu = this.getResources().getStringArray(R.array.drawer_menu);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawer_menu);
drawerList.setAdapter(adapter);
drawerList.setOnItemClickListener(new DrawerItemClickListener());
}
//respond when click the ic_drawer
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
protected class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
protected void selectItem(int position) {
//Fragment fragment = null;
switch (position) {
case 0:
break;
case 1:
Intent main = new Intent();
main.setClass(getApplicationContext(), MainActivity.class);
startActivity(main);
finish();
break;
case 2:
Intent checklist = new Intent();
checklist.setClass(getApplicationContext(), CheckList.class);
startActivity(checklist);
finish();
break;
default:
break;
}
/*FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();*/
drawerList.setItemChecked(position, true);
setTitle(drawer_menu[position]);
drawerLayout.closeDrawer(drawerList);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
CheckList:
public class CheckList extends BaseActivity implements OnClickListener {
Button addBtn;
ListView listView;
ArrayList<String> checkListItem;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.checklist, drawerLayout);
addBtn = (Button)findViewById(R.id.addBtn);
listView = (ListView)findViewById(R.id.listItems);
addBtn.setOnClickListener(this);
checkListItem = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, checkListItem);
listView.setAdapter(adapter);
checkListItem.add(getString(R.string.cl_passport));
checkListItem.add(getString(R.string.cl_charger));
checkListItem.add(getString(R.string.cl_camera));
registerForContextMenu(listView);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.checklist_contextmenu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
switch (item.getItemId()) {
case R.id.cl_contextmenu_delete:
checkListItem.remove(info.position);
adapter.notifyDataSetChanged();
return true;
case R.id.cl_contextmenu_edit:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(CheckList.this);
alertDialog.setTitle(getString(R.string.cl_editItem));
final EditText edit = new EditText(this);
edit.setText(checkListItem.get(info.position));
edit.setSelectAllOnFocus(true);
alertDialog.setView(edit);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
checkListItem.set(info.position, edit.getText().toString());
adapter.notifyDataSetChanged();
}
});
alertDialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.create();
alertDialog.show();
return true;
default:
return super.onContextItemSelected(item);
}
}
#Override
public void onClick(View v) {
if(v==this.addBtn) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(CheckList.this);
alertDialog.setTitle(getString(R.string.cl_input));
final EditText checkListInput = new EditText(this);
alertDialog.setView(checkListInput);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
checkListItem.add(checkListInput.getText().toString());
}
});
alertDialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.create();
alertDialog.show();
}
}
}
I can't simply change the class to MainActivity in CheckList. But in BaseActivity, I can change to both MainActivity and CheckList.
Many thanks !!
Updated
This is my drawer.xml file
<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/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Add content here -->
</FrameLayout>
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#e5e5e5"
android:dividerHeight="1dp"
android:background="#d6d6d6"/>
</android.support.v4.widget.DrawerLayout>

You're unable to click on the drawer's list items because you're inflating the CheckList Activity's layout directly into the DrawerLayout of the BaseActivity. This is causing it to be "on top" of all of the DrawerLayout's child Views, and your clicks won't propagate through to the drawer ListView underneath. Instead, you want to inflate CheckList's layout into the content_frame FrameLayout within the DrawerLayout.
In the BaseActivity class add a protected field for the FrameLayout, and initialize it just like the other Views.
public class BaseActivity extends AppCompatActivity {
protected DrawerLayout drawerLayout;
protected ListView drawerList;
protected FrameLayout contentFrame;
...
protected void initDrawer() {
setContentView(R.layout.drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.left_drawer);
contentFrame = (FrameLayout) findViewById(R.id.content_frame);
...
}
}
Then change the inflate() call in CheckList as follows:
getLayoutInflater().inflate(R.layout.checklist, contentFrame);

Related

Navigation Drawer does not respond on touch

I am writing an app, where I am combining activivties and navigation drawer, which I have never tried before. Here is the problem: when I am trying to access menu elements by clicking from MainActivity, everything works perfect and clicked elements pass on to other activities. But when I am trying to access drawer from EventActivity, which is opened with one of the fields of the drawer, it just does not respond and closes. This happens whatever field I am trying to press on drawer from EventActivity. Here is the code of both
MainActivity:
public class MainActivity extends AppCompatActivity {
public DrawerLayout drawerLayout;
public ActionBarDrawerToggle actionBarDrawerToggle;
MenuItem event,remind,setting;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.my_drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
event=findViewById(R.id.nav_events);
remind=findViewById(R.id.nav_reminders);
setting=findViewById(R.id.nav_settings);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
if(item.getItemId()==R.id.nav_events){
Intent intentEv=new Intent(MainActivity.this,EventActivity.class);
startActivity(intentEv);
}
else if(item.getItemId()==R.id.nav_reminders){
Intent intentRem=new Intent(MainActivity.this,RemindersActivity.class);
startActivity(intentRem);
}
else if(item.getItemId()==R.id.nav_settings){
Intent intentSet=new Intent(MainActivity.this,SettingsActivity.class);
startActivity(intentSet);
}
return super.onOptionsItemSelected(item);
}
}
EventActivity
public class EventActivity extends AppCompatActivity {
public DrawerLayout drawerLayout;
public ActionBarDrawerToggle actionBarDrawerToggle;
MenuItem event,remind,setting;
FloatingActionButton fab;
FragmentManager fm;
OpenHelper openHelper;
SQLiteDatabase db;
ArrayList<ObjectItem> maps;
ArrayList<String> listItem;
ArrayAdapter adapter;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event);
event=findViewById(R.id.nav_events);
remind=findViewById(R.id.nav_reminders);
setting=findViewById(R.id.nav_settings);
fab=findViewById(R.id.fab);
list=findViewById(R.id.list);
openHelper=new OpenHelper(getBaseContext());
db=openHelper.getReadableDatabase();
listItem=new ArrayList<>();
viewData();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selected= (String) list.getItemAtPosition(position);
//Убрать элемент
String desc = null,time = null,date=null;
Cursor csr=db.rawQuery("SELECT description FROM event WHERE header =\"" +selected + "\";",null);
if(csr!=null)
if(csr.moveToFirst())
desc=csr.getString(0);
Cursor csr2=db.rawQuery("SELECT time FROM event WHERE header = \"" +selected + "\";",null);
if(csr2!=null)
if(csr2.moveToFirst())
time=csr2.getString(0);
Cursor csr3=db.rawQuery("SELECT date FROM event WHERE header = \"" +selected + "\";",null);
if(csr3!=null)
if(csr3.moveToFirst())
date=csr3.getString(0);
Dialog dialog = new Dialog(EventActivity.this);
dialog.setContentView(R.layout.itemdialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
TextView textHeader = dialog.findViewById(R.id.ItemHeader);
textHeader.setText(selected);
TextView textDesc = dialog.findViewById(R.id.ItemDesc);
textDesc.setText(desc);
TextView textTime = dialog.findViewById(R.id.ItemTime);
textTime.setText(time);
TextView textDate = dialog.findViewById(R.id.ItemDate);
textDate.setText(date);
ImageButton imageButton=dialog.findViewById(R.id.imgBut);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openHelper.deleteData(selected);
maps.clear();
viewData();
dialog.dismiss();
}
});
dialog.show();
}
});
drawerLayout = findViewById(R.id.my_drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
event=findViewById(R.id.nav_events);
remind=findViewById(R.id.nav_reminders);
setting=findViewById(R.id.nav_settings);
fm=getSupportFragmentManager();
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(EventActivity.this,EventBuilderActivity.class);
startActivity(intent);
}
});
}
private void viewData() {
list.setAdapter(null);
Cursor cursor=openHelper.viewData();
maps = new ArrayList<ObjectItem>();
if(cursor.getCount()==0){
Toast.makeText(this,"No info",Toast.LENGTH_SHORT).show();
} else{
while (cursor.moveToNext()){
maps.add(new ObjectItem(cursor.getString(1),
cursor.getString(2),
cursor.getString(3),cursor.getString(4)));
}
adapter=new MeinAdapter(this, maps);
list.setAdapter(adapter);
}
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
if(item.getItemId()==R.id.nav_events){
Intent intentMain=new Intent(EventActivity.this,MainActivity.class);
startActivity(intentMain);
}
else if(item.getItemId()==R.id.nav_reminders){
Intent intentRem=new Intent(EventActivity.this,RemindersActivity.class);
startActivity(intentRem);
}
else if(item.getItemId()==R.id.nav_settings){
Intent intentSet=new Intent(EventActivity.this,SettingsActivity.class);
startActivity(intentSet);
}
return super.onOptionsItemSelected(item);
}
}

Navigation Drawer in multiple activities does not work

I'm trying to use NavigationDrawer on my activity (Home), but the content of activity is not shown.
home_activity.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="match_parent"
android:orientation="vertical"
android:id="#+id/home_activity">
<Button
android:id="#+id/like_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="likeCounter"
android:background="#drawable/likecountgreen" />
</LinearLayout>
activity_drawer.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/nav_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:choiceMode="singleChoice" />
HomeActivity.class
public class HomeActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
super.onCreateDrawer();
}
}
BaseActivity.class
public class BaseActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {
private ListView drawerList;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private String[] options;
private TextView titleActionBar;
protected void onCreateDrawer() {
setContentView(R.layout.activity_drawer);
setupActionBar();
options = getResources()
.getStringArray(R.array.drawer_options);
drawerLayout =
(DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.nav_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, options));
drawerList.setOnItemClickListener(this);
setupDrawer();
}
private void setupActionBar() {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);
titleActionBar = (TextView) findViewById(R.id.title_action_bar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
titleActionBar.setText("App");
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
titleActionBar.setText("Options");
invalidateOptionsMenu();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 2:
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
drawerLayout.closeDrawer(drawerList);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
return true;
}
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
What I've discovered is that drawer_activity is overlaying home_activity.
How can I solve it?
In onCreateDrawer you are setting a new layout, so your layout that you set in your MainActivity is not shown anymore. Here is how you can solve this:
HomeActivity:
public class HomeActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreateDrawer(R.layout.home_activity);
}
}
And in your BaseActivity:
protected FrameLayout content;
protected void onCreateDrawer(final int layoutResID) {
setContentView(R.layout.activity_drawer);
content = (FrameLayout) findViewById(R.id.content_frame);
getLayoutInflater().inflate(layoutResID, content, true);
setupActionBar();
options = getResources()
.getStringArray(R.array.drawer_options);
drawerLayout =
(DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.nav_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, options));
drawerList.setOnItemClickListener(this);
setupDrawer();
}
Now the FrameLayout in your activity_drawer layout contains the layout of your HomeActivity.

handling click event of ActionBarDrawerToggle and ActionBar

I've implemented ActionBarDrawerToggle for NavigationDrawer and also used ActionBar.
How can I handle both click events because both require onOptionsItemSelected(MenuItem item) method?
Is there any other way around?
public class A1 extends AppCompatActivity implements OnItemClickListener {
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter < String > adapter;
ActionBarDrawerToggle drawListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a1);
menu = new String[] {
"Home", "Android", "Windows", "Linux", "Raspberry Pi", "WordPress", "Videos", "Contact Us"
};
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
adapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, menu);
dList.setAdapter(adapter);
dList.setOnItemClickListener(this);
dList.setSelector(android.R.color.holo_blue_dark);
drawListener = new ActionBarDrawerToggle(this, dLayout, R.drawable.ic_draw, R.string.dopen) {
#Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Menu");
}
#Override
public void onDrawerClosed(View drawerView) {
// TODO Auto-generated method stub
super.onDrawerClosed(drawerView);
}
};
dLayout.setDrawerListener(drawListener);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onCreateOptionsMenu(Menu menu) //for actionbar
{
getMenuInflater().inflate(R.menu.actionbar, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.copy:
return showToast("Copy");
default:
return false;
}
// if(drawListener.onOptionsItemSelected(item))
// {
// return true;
// }
// return super.onOptionsItemSelected(item);
}
public boolean showToast(String message) {
Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT).show();
return true;
}
#Override
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawListener.onConfigurationChanged(newConfig);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawListener.syncState();
}
#Override
public void onItemClick(AdapterView <? > arg0, View v, int position, long id) {
selectitem(position);
dLayout.closeDrawers();
Bundle args = new Bundle();
args.putString("Menu", menu[position]);
Fragment detail = new DetailFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
}
public void selectitem(int position) {
dList.setItemChecked(position, true);
setTitle(menu[position]);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
}
I see you're using the latest AppCompat and stuff. Then why complicate the drawer?
Please go through this. You can make a drawer VERY quickly and easily, handle its open and close quite neatly. You don't even need the ActionBarDrawerToggle.
Cheers.

Set database object to be deleted in android

I've setup a database in android for my app however I'm stuck trying to delete a object within the database.
I'd like the button within BasicButtonsCard to delete the item that has been clicked on.
For example cardX holds data ABCD while cardY hold EFGH and on clicking the delete button on cardX, it deletes the card and its data.
Help is appreciated.
Code for input field:
public class note extends DialogFragment {
EditText memoryText, memoryTitle;
public interface NewPersonDialogListener{
void onFinishAddPersonDialog(String text, String title);
}
public note(){}
public Dialog onCreateDialog(Bundle savedInstanceState){
LayoutInflater i = getActivity().getLayoutInflater();
View rootview = i.inflate(R.layout.activity_note, null);
memoryText = (EditText)rootview.findViewById(R.id.memoryText);
memoryTitle = (EditText)rootview.findViewById(R.id.memoryTitle);
AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
b.setTitle("Add New Memory");
b.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String text = memoryText.getText().toString();
String title = memoryTitle.getText().toString();
NewPersonDialogListener activity = (NewPersonDialogListener)getActivity();
activity.onFinishAddPersonDialog(text, title);
}
});
b.setNegativeButton("Cancel", null);
b.setView(rootview);
return b.create();
}
}
Code that handles MainActivity in the app:
public class MainActivity extends AppCompatActivity implements note.NewPersonDialogListener{
ImageButton runCommand;
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
String[] mDrawerListItems;
MaterialListView mListView;
MemoryDataSource dataSource;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
mDrawerList = (ListView)findViewById(android.R.id.list);
dataSource = new MemoryDataSource(this);
mListView = (MaterialListView) findViewById(R.id.material_listview);
generateList();
mListView.addOnItemTouchListener(new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(CardItemView cardItemView, int i) {
}
#Override
public void onItemLongClick(CardItemView cardItemView, int i) {
}
});
mDrawerListItems = getResources().getStringArray(R.array.drawer_list);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerListItems));
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
break;
case 1:
intent = new Intent(MainActivity.this, bands.class);
startActivity(intent);
break;
case 2:
intent = new Intent(MainActivity.this, memory_vault.class);
startActivity(intent);
break;
}
mDrawerLayout.closeDrawer(mDrawerList);
}
});
runCommand = (ImageButton)findViewById(R.id.runCommand);
runCommand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*
Intent i = new Intent(MainActivity.this, note.class);
startActivity(i);
*/
note noteFragment = new note();
noteFragment.show(getFragmentManager(), "tag");
}
});
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
toolbar,
R.string.drawer_open,
R.string.drawer_close){
public void onDrawerClosed(View v){
super.onDrawerClosed(v);
invalidateOptionsMenu();
syncState();
}
public void onDrawerOpened(View v){
super.onDrawerOpened(v);
invalidateOptionsMenu();
syncState();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle.syncState();
}
public void generateList(){
dataSource.open();
List<memoryModel> list = dataSource.findMemory();
for (memoryModel model : list){
BasicButtonsCard card = new BasicButtonsCard(this);
card.setTitle(model.getmemoryTitle());
card.setDescription(model.getmemoryText());
card.setBackgroundColorRes(R.color.background_material_dark);
card.setDescriptionColorRes(R.color.abc_secondary_text_material_dark);
card.setTitleColorRes(R.color.abc_secondary_text_material_dark);
card.setLeftButtonText("LEFT");
card.setRightButtonText("RIGHT");
card.setDismissible(true);
card.setOnRightButtonPressedListener(new OnButtonPressListener() {
#Override
public void onButtonPressedListener(View view, Card card) {
Intent intent = new Intent(MainActivity.this, bands.class);
startActivity(intent);
}
});
card.setOnLeftButtonPressedListener(new OnButtonPressListener() {
#Override
public void onButtonPressedListener(View view, Card card) {
}
});
mListView.add(card);
}
}
#Override
public void onResume(){
super.onResume();
}
#Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case android.R.id.home: {
if (mDrawerLayout.isDrawerOpen(mDrawerList)){
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
return true;
}
default: return super.onOptionsItemSelected(item);
}
}
#Override
public void onFinishAddPersonDialog(String text, String title) {
dataSource.open();
memoryModel model = new memoryModel();
model.setmemoryText(title);
model.setmemoryTitle(text);
dataSource.create(model);
dataSource.close();
generateList();
}
}
I will assume that you already have a RecyclerView with a button on each row since you didn't mention that as a problem. You should have a view holder class, and in the constructor of that class you can set up your listeners.
class Holder extends RecyclerView.ViewHolder{
public Holder(View itemView){
super(itemView);
View button = itemView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int rowNumber = getPosition();
// call delete method here
}
});
}
public void bind(int index){
// ...
}
}
Solved the issue of deleting data from the database. Since it was contained in a cardview layout with a button all I needed to do was add a onclick listener which would do the following
dataSource.deleteMemory(model.getmemoryTitle());
dataSource.deleteMemory(model.getmemoryText());
mListView.remove(card);

Navigation drawer not opening with extended BaseActivity

I'm having an issue where my navigation drawer will not open, and for the life of me, I can't figure out what's causing it. Could someone take a look and possibly see something I'm missing?
public class BaseActivity extends Activity
{
public DrawerLayout drawerLayout;
public ListView drawerList;
public String[] layers;
private ActionBarDrawerToggle drawerToggle;
Intent twitch = new Intent(this, TwitchActivity.class);
Intent community = new Intent(this, CommunityActivity.class);
Intent esports = new Intent(this, ESportsActivity.class);
Intent home = new Intent(this, MainActivity.class);
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
final ActionBar actionBar = getActionBar();
drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_launcher, 0, 0)
{
public void onDrawerClosed(View view)
{
actionBar.setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView)
{
actionBar.setTitle(R.string.menu);
}
};
drawerLayout.setDrawerListener(drawerToggle);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
layers = getResources().getStringArray(R.array.layers);
drawerList = (ListView) findViewById(R.id.left_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text1,
layers));
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
String selected = arg0.getItemAtPosition(pos).toString();
if(selected.equals("Twitch"))
startActivity(twitch);
if(selected.equals("Community"))
startActivity(community);
if(selected.equals("ESports"))
startActivity(esports);
if(selected.equals("Home"))
startActivity(home);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
My MainActivity
public class MainActivity extends BaseActivity {
private Spinner spinner;
public static String region;
public static String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
//..........
My drawer_layout.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">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Nav 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="#111111"/>
</android.support.v4.widget.DrawerLayout>

Categories

Resources