Open Actionbar Menu on Longclick - Android - java

I'm trying to make the actionbar menu (onCreateOptionsMenu) open ONLY on a long-click. How would I achieve this? At the moment I have it working for just a single short press/click using the following code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// TODO: Only onlongclick should the menu be revealed
getMenuInflater().inflate(R.menu.my_menu_id, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home:
open_home();
return true;
case R.id.menu_how_to:
open_how_to();
return true;
case R.id.menu_rate:
open_rate();
return true;
case R.id.menu_about:
open_about();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I'd like the menu to ONLY open for a long click (sort of like a hidden/trick feature). I've looked into onLongClickListener but can't get it to work. After the menu is opened it can behave normally (no more longclicks needed). Thanks for your help! Really appreciate it.

This is not possible. LongClick on the ActionBar buttons is not overridable.
It will always show a toast with the name of the button you long pressed. This is a help and discovery feature.

Okay so there's 2 answers to this depending on whether you would like icons in the menu:
Create a context menu, however each row of the menu cannot have icons next to it. This is the simpler method.
Create an alert dialog with a menu. This is much more complex but you can add icons and make the menu much more customisable.
I personally prefer method 2 because of the icons. I'll run through these methods below. First I'll just setup the action bar and button for the menu.
Sp first create an image button to open the menu somewhere. Also open up the custom action bar. This code is for BOTH methods:
// This goes somewhere in the activity's onCreate
ImageButton menu_home_button = (ImageButton) findViewById(R.id.action_bar_menu_home);
registerForContextMenu(menu_home_button); //ONLY USE THIS FOR METHOD1
menu_home_button.setLongClickable(true);
menu_home_button.setClickable(true);
// Custom action bar
actionBar = getActionBar();
actionBar.setCustomView(R.layout.my_custom_actionbar);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
My ImageButton was overlayed on top of the action bar on the right and looked like this in my_custom_actionbar.xml:
<ImageButton
android:background="?android:selectableItemBackground"
android:id="#+id/action_bar_menu_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/menu_icon"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="4dp"
android:clickable="true"
android:focusable="true"
android:longClickable="true" />
--------------------------------------
METHOD 1:
Add this to onCreate in WebViewActivity.java:
// Do the long press stuff
menu_home_button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(getApplicationContext(), "Long Press works...", Toast.LENGTH_SHORT).show();
// Do whatever you want on the longclick
}
});
The following code goes where onCreateOptionsMenu and onOptionsItemSelected would go (at the end of the java class -- mine was called WebViewActivity.java).
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu_id, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.menu_home:
open_home();
return true;
case R.id.menu_how_to:
open_how_to();
return true;
case R.id.menu_rate:
open_rate();
return true;
case R.id.menu_about:
open_about();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Then you create your menu in my_menu_id.xml. Including icons in the XML won't work so don't bother trying:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.yourapp.WebViewActivity" >
<item
android:id="#+id/menu_home"
android:title="#string/menu_home"
android:showAsAction="never"/>
<item
android:id="#+id/menu_how_to"
android:title="#string/menu_how_to"
android:showAsAction="never"/>
<item
android:id="#+id/menu_rate"
android:title="#string/menu_rate"
android:showAsAction="never"/>
<item
android:id="#+id/menu_about"
android:title="#string/menu_about"
android:showAsAction="never"/>
</menu>
-----------------------------------------
METHOD 2:
Add this to onCreate in WebViewActivity.java:
menu_home_button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// Menu text
final String[] items = {"Home", "How To", "Rate", "About"};
// Menu Icons in Drawable
final Drawable[] item_icons = {
getResources().getDrawable(R.drawable.home_icon),
getResources().getDrawable(R.drawable.how_to_icon),
getResources().getDrawable(R.drawable.rate_icon),
getResources().getDrawable(R.drawable.about_icon),
};
ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.custom_menu_dialog, items) {
ViewHolder holder;
class ViewHolder {
ImageView icon;
TextView title;
}
public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.custom_menu_dialog, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
// view already defined, retrieve view holder
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(items[position]);
holder.icon.setImageDrawable(item_icons[position]);
return convertView;
}
};
Toast.makeText(getApplicationContext(), "Long Press works...", Toast.LENGTH_SHORT).show();
AlertDialog.Builder menu_dialog = new AlertDialog.Builder(WebViewActivity.this);
menu_dialog.setTitle(getResources().getString("My Menu Name");
menu_dialog.setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
//Toast.makeText(WebViewActivity.this, "You selected: " + items[item], Toast.LENGTH_LONG).show();
switch (item) {
case 0:
open_home();
break;
case 1: // HOW TO
open_how_to();
break;
case 2:
open_rate();
break;
case 3: // ABOUT
open_about();
break;
default: // Do Case 0
open_home();
break;
}
dialog.dismiss();
}
});
AlertDialog alert = menu_dialog.create();
alert.show();
return true;
}
});
Finally here is the custom_menu_dialog.xml file with the base info for the text and icon of the menu:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:paddingLeft="16dp"/>
<TextView
android:id="#+id/title"
android:text=""
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff6f6f6"
android:textSize="18sp"
android:paddingLeft="16dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"/>
</LinearLayout>
Hopw this helps at least one other person out. It certainly works for me.

Related

Navigation Menu Fragment not being able to initialize its layout's components

I've created a navigation menu fragment for my application so that i can use it across all of my activities. However, when i run the app the menu does not work neither when i click on the hamburger icon nor when i pull from left to right. So i decided to use the debugger and i found out that none of my fragment's layout components get initialized within the fragment's class. Can you help me figure out why this happens and how to fix it? Here is my fragment.java class and my fragment_layout.xml file:
Fragment.java:
public class NavMenuFragment extends Fragment {
// NavMenu member vars
private DrawerLayout mDrawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle mToggle; // Button for toggling the side menu
// Keeps the position of the previously selected menu item(0 : Home)
int position = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_nav_menu,null);
mDrawerLayout = view.findViewById(R.id.drawerLayout);
navigationView = view.findViewById(R.id.nav_view);
mToggle = new ActionBarDrawerToggle(getActivity(),mDrawerLayout,R.string.drawer_open,R.string.drawer_closed); // Instantiating our button
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Sets the default selected menu item, to the Home item
navigationView.getMenu().findItem(R.id.nav_home).setChecked(true);
// Used to help on check and uncheck menu items when the user clicks on them
final List<MenuItem> items = new ArrayList<>();
Menu menu;
menu = navigationView.getMenu();
// Fill the list with all the menu items
for(int i=0; i<menu.size();i++) {
items.add(menu.getItem(i));
}
Toast.makeText(getActivity(), "size:" + items.size(), Toast.LENGTH_SHORT).show();
// When an item inside the NavView gets clicked, then handle the event...
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
mDrawerLayout.closeDrawer(Gravity.START);
break;
case R.id.nav_UserBoxGLB:
break;
case R.id.nav_UserBoxJP:
break;
case R.id.nav_settings:
break;
case R.id.nav_feedback:
break;
case R.id.nav_contact_us:
break;
case R.id.nav_donate:
// Open the website's URL in a browser window
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);
break;
case R.id.nav_about:
break;
default:
return onNavigationItemSelected(item);
}
items.get(position).setChecked(false);
item.setChecked(true);
mDrawerLayout.closeDrawers();
return false;
}
});
mDrawerLayout.addDrawerListener(mToggle);
// Set the hamburger icon's color
mToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.NavActionBarTextColor));
mToggle.syncState();
}
// When an item from the Action Bar gets tapped, then...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return mToggle.onOptionsItemSelected(item) || onOptionsItemSelected(item);
}
}
Fragment_layout.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:id="#+id/drawerLayout">
<!-- The actual side menu Nav View -->
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_menu"
android:id="#+id/nav_view">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
You are doing it wrong
If you want to use same navigation drawer for all fragment then
You can make a navigation drawer in your main activity and open fragment from that navigation drawer.
Here is the tutorial

How to prevent Black Screen when switching activities?

I am working on a Project and testing the XML, however, whenever I try switching to my next activity, the app gets stuck on a black screen and does nothing, and I am forced to quit.
I tried switching the layouts for setContentView and that didn't work, I checked to make sure the code in the activity files was working, and now I am looking at other's questions and I am just confused.
Here is my MainActivity:
public class MainActivity extends android.app.Activity {
ImageButton startButton;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
startButton = (ImageButton)findViewById(R.id.startButton);
textView = (TextView)findViewById(R.id.textView);
textView.setTextColor(Color.WHITE);
View backgroundView = findViewById(R.id.spoopySkellington);
View root = backgroundView.getRootView();
root.setBackgroundColor(getResources().getColor(android.R.color.black));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void startClick(View view)
{
final Intent intent = new Intent(MainActivity.this, AdventureActivity.class);
startActivity(intent);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the activity I am trying to switch to.
public class AdventureActivity extends android.app.Activity {
Character mainCharacter;
Character skeleton;
TextView dialogueView;
EditText textField;
Button confirmButton;
String field;
ImageView enemy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_adventure);
confirmButton = (Button)findViewById(R.id.confirmButton);
dialogueView = (TextView)findViewById(R.id.dialogueView);
textField = (EditText)findViewById(R.id.inputText);
field = "";
skeleton = new Character("Skeleton", 10, 1);
String name = "";
while (field.equals("")) {
dialogueView.setText("What is your name?");
name = field;
}
mainCharacter = new Character(name);
}
public void onClick(View view)
{
field = textField.getText().toString();
textField.setText("");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Lastly, here is the activity xml for the one I want to switch to:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.jason.textadventure.AdventureActivity"
tools:showIn="#layout/activity_adventure">
<TextView
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/dialogueView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="..."
android:textSize="30dp"
tools:layout_editor_absoluteY="126dp"
tools:layout_editor_absoluteX="180dp"
/>
<EditText
app:layout_constraintBottom_toBottomOf="#id/dialogueView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/inputText"
android:layout_width="261dp"
android:layout_height="40dp"
tools:layout_editor_absoluteY="196dp"
tools:layout_editor_absoluteX="62dp"
android:inputType="textNoSuggestions"
/>
<Button
app:layout_constraintBottom_toBottomOf="#id/inputText"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#id/dialogueView"
android:id="#+id/confirmButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONFIRM"
android:onClick="onClick"
tools:layout_editor_absoluteY="184dp"
tools:layout_editor_absoluteX="148dp" />
When onCreate() is called, your code is stuck around this loop
while (field.equals("")) {
dialogueView.setText("What is your name?");
name = field;
}

Header ImageView in Navigation Drawer - Android Eclipse

I have created a Navigation Drawer and then I want to put an image header without having it to be part of the item resources. I have created a header image but it was clickable and the id of it was the id of the first item. And the header image become a big header in my whole app which can be seen in every page. How can I fix this.
This is the main activity
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// Boolean keepPlaying = true;
// MediaPlayer mp;
Item btnplay;
MediaPlayer mp;
private boolean isPaused=false;
private int length;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = new MediaPlayer();
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
isPlaying = false;
isPaused = false;
}
});
play(null);//you are calling play by launching
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Search
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// History
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Linux Distro
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// Dos Vs Linux
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
// Linux Commands
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
// Bash Commands
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
// Terminal
navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));
// About US
navDrawerItems.add(new NavDrawerItem(navMenuTitles[8], navMenuIcons.getResourceId(8, -1)));
// Help
navDrawerItems.add(new NavDrawerItem(navMenuTitles[9], navMenuIcons.getResourceId(9, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
//Header of the listview, go to header.xml to customize
View header = getLayoutInflater().inflate(R.layout.header_navi, null);
//addHeaderView is to add custom content into first row
mDrawerList.addHeaderView(header);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer_invisible, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(1);
}
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
//inflate items in actionbar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//Background sounds
boolean isPlaying = false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
mDrawerToggle.onOptionsItemSelected(item);
if (item.getItemId() == R.id.btnplay) //whatever you named in xml
{
invalidateOptionsMenu();
}
return super.onOptionsItemSelected(item);
}
public void play(MenuItem menuItem) {
if (!isPlaying && !isPaused) {
mp = new MediaPlayer();
/* Toast.makeText(getApplicationContext(), "play started",
Toast.LENGTH_SHORT).show();
*/
try {
AssetFileDescriptor afd = getAssets().openFd("bgmusic_mainactivity.ogg");
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getLength());
mp.prepare();
mp.start();// play sound
mp.setLooping(true);
} catch (Exception e) {
e.printStackTrace();
}
isPlaying = true;
} else if (isPlaying) {
/* Toast.makeText(getApplicationContext(), "play paused",
Toast.LENGTH_SHORT).show();
*/
mp.pause();
isPlaying = false;
isPaused = true;
length = mp.getCurrentPosition();
menuItem.setIcon(R.drawable.ic_action_volume_muted);
// and for resuming the player from the position where it stopped
// lately is done by:
} else if (isPaused) {
mp.seekTo(length);
mp.start();
isPlaying = true;
isPaused = false;
menuItem.setIcon(R.drawable.ic_action_volume_on);
}
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(isPlaying){
menu.getItem(0).setIcon(R.drawable.ic_action_volume_on);
}
else{
menu.getItem(0).setIcon(R.drawable.ic_action_volume_muted);
}
return super.onPrepareOptionsMenu(menu);
}
/**
* Displaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new CommandList();
break;
case 1:
fragment = new Home();
break;
case 2:
fragment = new History();
break;
case 3:
fragment = new LinuxDistro();
break;
case 4:
fragment = new DosLinux();
break;
case 5:
fragment = new LinuxCommands();
break;
case 6:
fragment = new BashCommands();
break;
case 7:
fragment = new Terminal();
break;
case 8:
fragment = new AboutUs();
break;
case 9:
fragment = new Help();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment!");
}
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
// exit dialog
#Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you really want to close the app?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Tux To Go Alert!")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mp.stop();
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
header_navi.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_header"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src="#drawable/header" />
</RelativeLayout>
activity_main
<?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/view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"
>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
There is A simple way to add a image or Header in the Navigation :
you can add any View or image view in the "DrawerLayout" just put your list-view in a "LinearLayout" and add a Image view and the list view in that Layout. It will show the Image View or any other view which you added in the "DrawerLayout" .
As Example :
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"
>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_header"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src="#drawable/header" />
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Just put your <ListView> and <ImageView> in a <RelativeLayout> and add this to <ListView>
android:layout_below="your image id"
Replace your image id with image id.

on item click in array of string

I made an android app in which the user click the favorite menu item button and the page name saves in the favorite.class activity. But when I click on any item in the favorite list it opens only one specific class and not others which I want. Here is my code please look at the code and tell me what should I do to make it first in ListView form and then to click the item which opens the correct activity from it.
favorite.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_marginLeft="17dp">
<TextView
android:id="#+id/empty_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/no_bookmark"
android:textSize="16sp"
android:textColor="#000000"
android:
android:textStyle="bold"/>
<TextView
android:id="#+id/bookmark_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textSize="16sp"
android:textColor="#000000"
android:textStyle="bold"/>
<!-- ScrollView is needed when adding views, or only the last view will show up -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/bookmark_insert_point"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
Favorite.Class activity:
public class Favorite extends Activity {
private TextView mEmptyText;
private LinearLayout mBookmarkLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite);
// this code is used for the action bar color change//
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6B8E23")));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mEmptyText = (TextView) findViewById(R.id.empty_textview);
mBookmarkLayout = (LinearLayout) findViewById(R.id.bookmark_insert_point);
getAllKeys();
}
private void getAllKeys()
{
SharedPreferences sp = this.getSharedPreferences("bookmarks", MODE_PRIVATE);
Map<String,?> keys = sp.getAll();
int count = 0;
for(Map.Entry<String,?> entry : keys.entrySet())
{
String value = entry.getValue().toString();
System.out.println("!!!!!!!!!!!!!!!!!value = "+value);
String delimiter = ",";
String[] values_array = value.split(delimiter);
addBookmark(values_array);
count++; //keep track of the number of bookmarks
}
//if there are no bookmarks, display a text view saying so. Otherwise, make the text view go away
if (count == 0)
{
mEmptyText.setVisibility(View.VISIBLE);
mEmptyText.setText(getString(R.string.no_bookmark));
}
else
mEmptyText.setVisibility(View.GONE);
}
#SuppressWarnings("deprecation")
private void addBookmark(final String[] values_array)
{
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.favorite, null);
final TextView text = (TextView) v.findViewById(R.id.bookmark_text);
text.setText(values_array[1]);
// insert into main view
mBookmarkLayout.addView(v, 0, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
System.out.println("!!!!!!!!!!!!!!!!!!!!Just added a view");
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(text.equals("Atherosclerosis"));
Intent i=new Intent(Favorite.this,Atherosclerosis.class);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.favorite, menu);
return true;
}
}
and finally the activity from where user click for add to favorite:
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.id_search:
Intent newActivity0 = new Intent(this,Search.class);
startActivity(newActivity0);
return true;
case R.id.id_favorit:
SharedPreferences sp = this.getSharedPreferences("bookmarks", MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("atherosclerosis", "com.kmcpesh.shortreviewofcardiology.Favorite,Atherosclerosis");
editor.commit();
Toast.makeText(getApplicationContext(), "Item Added to Favorite List!", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
well i guess what you have to do is setOnItemClickListener in everyone of the list items...
an example is
private void registerCallClickBack() {
// TODO Auto-generated method stub
ListView list = (ListView)findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
long id) {
// TODO Auto-generated method stub
//String message = "You have chosen the " + id + "item";
//Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
Intent intent = new Intent("package.Activity name");
startActivity(intent);
}
});
}
this will either display a toast message of every listview item clicked or start a new intent..

ListView OnItemLongClickListener() not triggered

I have a class that extends ListActivity where the list items respond to OnClick events. Adding an OnItemLongClickListener does not work. The onItemLongClick() function is not called (no log-output or Toast showing) but the normal OnClick() event is handled instead.
I want to display a contextual action bar upon long click. A minimum example using my code in a new project works fine. So my question is: What can possibly prevent the onItemLongClick() trigger from being triggered?
My minimum API is 11. I am also setting the listView to longClickable="true".
Activity code (selected functions):
public class EventListActivity extends ListActivity {
private ArrayList<Event> arrEvents = null;
private ArrayAdapter<Event> adpEvents = null;
private ActionMode mActionMode = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// only create list adapter and set it
arrEvents = new ArrayList<Event>();
adpEvents = new ArrayAdapter<Event>(this, android.R.layout.simple_list_item_activated_2, android.R.id.text1, arrEvents) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(arrEvents.get(position).getTitle());
text2.setText(arrEvents.get(position).getDateTimeFormatted());
return view;
}
};
setListAdapter(adpEvents);
// add CAB to ListView
setupCAB();
}
#Override
protected void onResume() {
super.onResume();
// populate list and refresh adapter
createEventList();
adpEvents.notifyDataSetChanged();
// if list empty show emtpy msg, otherwise hide it
setContentView(R.layout.activity_event_list);
TextView empty = (TextView) findViewById(R.id.text_empty);
if(arrEvents.isEmpty()) {
empty.setVisibility(View.VISIBLE);
} else {
empty.setVisibility(View.GONE);
}
}
private void setupCAB() {
// Important: to select single mode
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
// Called when the user long-clicks an item on the list
#Override
public boolean onItemLongClick(AdapterView<?> parent, View row, int position, long rowid) {
Log.w("EventListActivity", "Long click detected!");
Toast.makeText(EventListActivity.this, "Long click detected!", Toast.LENGTH_SHORT).show();
if (mActionMode != null) {
return false;
}
// Important: to mark the editing row as activated
getListView().setItemChecked(position, true);
// Start the CAB using the ActionMode.Callback defined above
mActionMode = EventListActivity.this.startActionMode(mActionModeCallback);
return true;
}
});
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.event_context, menu);
return true;
}
// Called when the user enters the action mode
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Disable the list to avoid selecting other elements while editing one
EventListActivity.this.getListView().setEnabled(false);
return true; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.mnu_share_event:
//TODO share event
mode.finish();
return true;
default:
return false;
}
}
// Called when the user exits the action mode
#Override
public void onDestroyActionMode(ActionMode mode) {
// Re-enable the list after edition
EventListActivity.this.getListView().setEnabled(true);
mActionMode = null;
}
};
}
activity_event_list.xml:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".EventListActivity" >
<TextView
android:id="#+id/text_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:text="#string/empty"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="gone" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:longClickable="true" >
</ListView>
</RelativeLayout>
If you have buttons responding to onClick() events inside your listview, you need to set the following in the container holding those buttons:
android:descendantFocusability="blocksDescendants"
If what you have are textviews, the problem is slightly trickier. See this: Focusable EditText inside ListView
This answer does not solve user1's question but the symptoms were similar my problem (i.e. OnItemClickListener was getting called but OnItemLongClickListener was not). I'm posting my answer here in case anyone else stumbles on this question like I did when trying to solve my problem.
I was using a ListView inside a Fragment and implemented the listener methods:
public class MyFragment extends Fragment implements OnClickListener,
OnLongClickListener, OnItemClickListener, OnItemLongClickListener {
Here is the onItemClick method that was working fine:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long rowId) {
Log.i("Chimee", "short click working");
}
And here is the onItemLongClick method that wasn't firing:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
long rowId) {
Log.i("Chimee", "Long click working");
return false;
}
Of course the simple answer was that I forgot to setOnItemLongClickListener. I added it after the setOnItemClickListener that I had all along and then it worked fine.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_fragment, container, false);
lvSuggestions = (ListView) v.findViewById(R.id.lvSuggestions);
lvSuggestions.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvSuggestions.setOnItemClickListener(this);
lvSuggestions.setOnItemLongClickListener(this); // Forgot this
...
}
When using a ListActivity or ListFragment there is no method you can override for the long-click, and getting access to the ListView is not possible in onCreateView(), since it is being controlled by the parent class.
So, to overcome this, I did this, since the getListView() command won't work until after the view is created:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecipeListView = this.getListView();
mRecipeListView.setOnItemLongClickListener(new ListView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long row_id) {
// Process the long-click
}
});
}

Categories