Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Android Devlopment- Get Overflow menu to open a new activity

Options
  • 30-06-2015 11:55pm
    #1
    Registered Users Posts: 2,827 ✭✭✭


    Hey lads,

    Fairly new to android development and have no java experiences so im really just bashing at it. Basically, In my app i have a activity main.xml , MainActivity.java and a menu_main.xml
    I want to have it so when the user touches the overflow, 3 vertical dots menu , he will switch to a new activity window. How can i implement this and explain what each step does?

    Using android studio.

    Cheers
    Tagged:


Comments

  • Registered Users Posts: 7,859 ✭✭✭The_B_Man


    Do you want the 3 vertical dots menu to open up and display options, which you then want to open a new activity window, or do you want the touch of the dots to trigger the new activity, without opening the menu?

    If you want the activity to open directly from the user tapping the 3 dots, then consider the more "Android" way of doing things, which is to add another Action Bar icon to the Action Bar (The Action Bar is the bar at the top on which the 3 dots menu is located). Once you add the new action bar icon beside the 3 dots, then you can simply intercept a tap on that, using standard Android practices (and using the action bar the way its meant to be used) and display your new activity very easily.

    You can use whatever icon you want, or you can make one from this site:
    https://romannurik.github.io/AndroidAssetStudio/

    Just to give you an example, you could define your main.xml as follows:
    <menu 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"
        tools:context=".MainActivity">
        <item
            android:id="@+id/action_example"
            android:title="@string/action_example"
            app:showAsAction="withText|ifRoom" />
        <item
            android:id="@+id/action_settings"
            android:orderInCategory="100"
            android:title="@string/action_settings"
            app:showAsAction="never" />
    </menu>
    

    then in your MainActivity.java, have something like this:
        @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();
            
            switch(id) {
                case R.id.action_settings:
                    //Do work to load settings screen
                    return true;
                case R.id.action_example:
                    //Launch new intent
                    Intent i = new Intent(this, NewActivity.class);
                    startActivity(i);
                    return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    

    In this case, I am using the menu item "action_example" as the menu item that launches the new screen.

    This code should probably work for you, if you just change "NewActivity" to the name you gave to your own Java class for the new screen.


  • Registered Users Posts: 2,827 ✭✭✭bpb101


    Yeah, thanks for the reply,
    I done a similar thing in the end on which i had only one option.
    in the ... icon menu. I may change it to the "if fits" idea.

    The next thing i wanted to get going was to have a slide out menu working , except the template dosent have that code in it by default , How would i would go about that would you think.


    Another question i have is the overflow menu is linking to a settings activity template.
    <[COLOR=#000080][B]resources[/B][/COLOR]>
        <[COLOR=#000080][B]string [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"title_activity_settings"[/B][/COLOR]>Settings</[COLOR=#000080][B]string[/B][/COLOR]>
        [COLOR=#808080][I]<!-- Strings related to Settings -->
    [/I][/COLOR][COLOR=#808080][I]    <!-- Example General settings -->
    [/I][/COLOR]<[COLOR=#000080][B]string [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_header_general"[/B][/COLOR]>General</[COLOR=#000080][B]string[/B][/COLOR]>[COLOR=Cyan]
    [COLOR=DarkOrange]
    [/COLOR][/COLOR][SIZE=2][COLOR=DarkOrange][B]
    
        <string name="pref_title_display_name">Enter Team 1</string>
        <string name="pref_default_display_name">John Smith</string>
    [/B][/COLOR]
    [/SIZE]
    
        <[COLOR=#000080][B]string [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_title_add_friends_to_messages"[/B][/COLOR]>Add friends to messages</[COLOR=#000080][B]string[/B][/COLOR]>
        <[COLOR=#000080][B]string-array [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_example_list_titles"[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>Always</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>When possible</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>Never</[COLOR=#000080][B]item[/B][/COLOR]>
        </[COLOR=#000080][B]string-array[/B][/COLOR]>
        <[COLOR=#000080][B]string-array [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_example_list_values"[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>1</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>0</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>-1</[COLOR=#000080][B]item[/B][/COLOR]>
        </[COLOR=#000080][B]string-array[/B][/COLOR]>
    
        [COLOR=#808080][I]<!-- Example settings for Data & Sync -->
    [/I][/COLOR]<[COLOR=#000080][B]string [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_header_data_sync"[/B][/COLOR]>Data [COLOR=#0000ff][B]&amp; [/B][/COLOR]sync</[COLOR=#000080][B]string[/B][/COLOR]>
    
        <[COLOR=#000080][B]string [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_title_sync_frequency"[/B][/COLOR]>Sync frequency</[COLOR=#000080][B]string[/B][/COLOR]>
        <[COLOR=#000080][B]string-array [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_sync_frequency_titles"[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>15 minutes</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>30 minutes</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>1 hour</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>3 hours</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>6 hours</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>Never</[COLOR=#000080][B]item[/B][/COLOR]>
        </[COLOR=#000080][B]string-array[/B][/COLOR]>
        <[COLOR=#000080][B]string-array [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_sync_frequency_values"[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>15</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>30</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>60</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>180</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>360</[COLOR=#000080][B]item[/B][/COLOR]>
            <[COLOR=#000080][B]item[/B][/COLOR]>-1</[COLOR=#000080][B]item[/B][/COLOR]>
        </[COLOR=#000080][B]string-array[/B][/COLOR]>
    
        <[COLOR=#000080][B]string [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_title_system_sync_settings"[/B][/COLOR]>System sync settings</[COLOR=#000080][B]string[/B][/COLOR]>
    
        [COLOR=#808080][I]<!-- Example settings for Notifications -->
    [/I][/COLOR]<[COLOR=#000080][B]string [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_header_notifications"[/B][/COLOR]>Notifications</[COLOR=#000080][B]string[/B][/COLOR]>
    
        <[COLOR=#000080][B]string [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_title_new_message_notifications"[/B][/COLOR]>New message notifications</[COLOR=#000080][B]string[/B][/COLOR]>
    
        <[COLOR=#000080][B]string [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_title_ringtone"[/B][/COLOR]>Ringtone</[COLOR=#000080][B]string[/B][/COLOR]>
        <[COLOR=#000080][B]string [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_ringtone_silent"[/B][/COLOR]>Silent</[COLOR=#000080][B]string[/B][/COLOR]>
    
        <[COLOR=#000080][B]string [/B][/COLOR][COLOR=#0000ff][B]name=[/B][/COLOR][COLOR=#008000][B]"pref_title_vibrate"[/B][/COLOR]>Vibrate</[COLOR=#000080][B]string[/B][/COLOR]>
    </[COLOR=#000080][B]resources[/B][/COLOR]>
    
    I have a Plain text view on main activity that i would like changed by entering data on the settings menu. (See code highlighted)How can i go about this. I heard somthing about not being able to change strings at runtime in android , How can i work around this to get a users input.

    Appreciate the help


  • Registered Users Posts: 7,859 ✭✭✭The_B_Man


    If you want to capture user input, you'll have to build a view containing the necessary fields, using EditTexts.
    Then have a Submit button that does the work for you (either passing the values back to the main screen or storing in SharedPreferences and retrieving from the main screen).


    As for the slide out menu, in Android Studio, create a new project and choose the DrawerLayout. I always start from that and code on top of it. Can't beat it really, for starting off.
    Just remove the PlaceHolder Fragment and create your own Fragments (File->New-> Blank Fragment) with your intended data etc.


Advertisement