Friday, 7 July 2017

Android Studio Keyboard Shortcuts

Keyboard Shortcuts


Android Studio includes keyboard shortcuts for many common actions. Table 1 shows the default keyboard shortcuts by operating system.

Table 1. Default keyboard shortcuts for Windows/Linux and Mac operating systems.
DescriptionWindows/LinuxMac
General
Save allControl + SCommand + S
SynchronizeControl + Alt + YCommand + Option + Y
Maximize/minimize editorControl + Shift + F12Control + Command + F12
Add to favoritesAlt + Shift + FOption + Shift + F
Inspect current file with current profileAlt + Shift + IOption + Shift + I
Quick switch schemeControl + ` (backquote)Control + ` (backquote)
Open settings dialogueControl + Alt + SCommand + , (comma)
Open project structure dialogControl + Alt + Shift + SCommand + ; (semicolon)
Switch between tabs and tool windowControl + TabControl + Tab
Navigating and Searching Within Studio
Search everything (including code and menus)Press Shift twicePress Shift twice
FindControl + FCommand + F
Find nextF3Command + G
Find previousShift + F3Command + Shift + G
ReplaceControl + RCommand + R
Find actionControl + Shift + ACommand + Shift + A
Search by symbol nameControl + Alt + Shift + NCommand + Option + O
Find classControl + NCommand + O
Find file (instead of class)Control + Shift + NCommand + Shift + O
Find in pathControl + Shift + FCommand + Shift + F
Open file structure pop-upControl + F12Command + F12
Navigate between open editor tabsAlt + Right/Left ArrowControl + Right/Left Arrow
Jump to sourceF4 / Control + EnterF4 / Command + Down Arrow
Open current editor tab in new windowShift + F4Shift + F4
Recently opened files pop-upControl + ECommand + E
Recently edited files pop-upControl + Shift + ECommand + Shift + E
Go to last edit locationControl + Shift + BackspaceCommand + Shift + Backspace
Close active editor tabControl + F4Command + W
Return to editor window from a tool windowEscEsc
Hide active or last active tool windowShift + EscShift + Esc
Go to lineControl + GCommand + L
Open type hierarchyControl + HControl + H
Open method hierarchyControl + Shift + HCommand + Shift + H
Open call hierarchyControl + Alt + HControl + Option + H
Writing Code
Generate code (getters, setters, constructors, hashCode/equals, toString, new file, new class)Alt + InsertCommand + N
Override methodsControl + OControl + O
Implement methodsControl + IControl + I
Surround with (if...else / try...catch / etc.)Control + Alt + TCommand + Option + T
Delete line at caretControl + YCommand + Backspace
Collapse/expand current code blockControl + minus/plusCommand + minus/plus
Collapse/expand all code blocksControl + Shift + minus/plusCommand + Shift + minus/plus
Duplicate current line or selectionControl + DCommand + D
Basic code completionControl + SpaceControl + Space
Smart code completion (filters the list of methods and variables by expected type)Control + Shift + SpaceControl + Shift + Space
Complete statementControl + Shift + EnterCommand + Shift + Enter
Quick documentation lookupControl + QControl + J
Show parameters for selected methodControl + PCommand + P
Go to declaration (directly)Control + B or Control + ClickCommand + B or Command + Click
Go to implementationsControl + Alt + BCommand + Alt + B
Go to super-method/super-classControl + UCommand + U
Open quick definition lookupControl + Shift + ICommand + Y
Toggle project tool window visibilityAlt + 1Command + 1
Toggle bookmarkF11F3
Toggle bookmark with mnemonicControl + F11Option + F3
Comment/uncomment with line commentControl + /Command + /
Comment/uncomment with block commentControl + Shift + /Command + Shift + /
Select successively increasing code blocksControl + WOption + Up
Decrease current selection to previous stateControl + Shift + WOption + Down
Move to code block startControl + [Option + Command + [
Move to code block endControl + ]Option + Command + ]
Select to the code block startControl + Shift + [Option + Command + Shift + [
Select to the code block endControl + Shift + ]Option + Command + Shift + ]
Delete to end of wordControl + DeleteOption + Delete
Delete to start of wordControl + BackspaceOption + Backspace
Optimize importsControl + Alt + OControl + Option + O
Project quick fix (show intention actions and quick fixes)Alt + EnterOption + Enter
Reformat codeControl + Alt + LCommand + Option + L
Auto-indent linesControl + Alt + IControl + Option + I
Indent/unindent linesTab/Shift + TabTab/Shift + Tab
Smart line joinControl + Shift + JControl + Shift + J
Smart line splitControl + EnterCommand + Enter
Start new lineShift + EnterShift + Enter
Next/previous highlighted errorF2 / Shift + F2F2 / Shift + F2
Build and Run
BuildControl + F9Command + F9
Build and runShift + F10Control + R
Apply changes (with Instant Run)Control + F10Control + Command + R
Debugging
DebugShift + F9Control + D
Step overF8F8
Step intoF7F7
Smart step intoShift + F7Shift + F7
Step outShift + F8Shift + F8
Run to cursorAlt + F9Option + F9
Evaluate expressionAlt + F8Option + F8
Resume programF9Command + Option + R
Toggle breakpointControl + F8Command + F8
View breakpointsControl + Shift + F8Command + Shift + F8
Refactoring
CopyF5F5
MoveF6F6
Safe deleteAlt + DeleteCommand + Delete
RenameShift + F6Shift + F6
Change signatureControl + F6Command + F6
InlineControl + Alt + NCommand + Option + N
Extract methodControl + Alt + MCommand + Option + M
Extract variableControl + Alt + VCommand + Option + V
Extract fieldControl + Alt + FCommand + Option + F
Extract constantControl + Alt + CCommand + Option + C
Extract parameterControl + Alt + PCommand + Option + P
Version Control / Local History
Commit project to VCSControl + KCommand + K
Update project from VCSControl + TCommand + T
View recent changesAlt + Shift + COption + Shift + C
Open VCS popupAlt + ` (backquote)Control + V

Configure Custom Keymaps


You can choose from a number of preset keymaps or modify a preset keymap to create a new custom keymap in the keymap settings for Android Studio.
To open the keymap settings, click File > Settings > Keymap (on Mac, File > Properties > Keymap).
Figure 1. The Android Studio keymap settings window.
  1. Keymaps dropdown: Delect the desired keymap from this menu to switch between preset keymaps.
  2. Actions list: Right click on an action to modify it. You can add additional keyboard shortcuts for the action, add mouse shortcuts to associate an action with a mouse click, or remove current shortcuts. If you are using a preset keymap, modifying an action’s shortcuts will automatically create a copy of the keymap and add your modifications to the copy.
  3. Copy button: Select a keymap from the dropdown menu to use as a starting point, and click Copy to create a new custom keymap. You can modify the keymap name and shortcuts.
  4. Reset button: Select a keymap from the dropdown menu and click Reset to revert it to its original configuration.
  5. Search box: Type here to search for a keyboard shortcut by the action name.
  6. Search by Shortcut: Click Find Actions by Shortcut and type a shortcut to search for actions by shortcut.

Wednesday, 5 October 2016

Android TV - Dialog


Android TV - Dialog




The leanback library for Android TV introduced the concept of guided step fragments. These have an intuitive way of presenting prompts to a user. I made a simple class that uses a GuidedStepFragment to create a simple yes/no dialog for TV. check out the code for this below:

Add declaration of this Activity to AndroidManifest.
<activity  
android:name="ATVDialog"  
android:theme="@style/Theme.Example.Leanback.GuidedStep" />

So we can start implementing GuidedStepFragment from here. Before real implementation, I will tell you basic structure of this GuidedStepFragment in brief :

GuidedStepFragment – Structure

As mentioned in official_doc, It is composed of a guidance view on the left and a view on the right containing a list of possible actions.

Overriding Method

To use GuidedStepFragment, we need to override at least these 3 methods.

  • onCreateGuidance(Bundle) – To create guidance view (left side).  – Attributes of guidance (title, description etc) are specified here.
  • onCreateActions(List, Bundle) – To define list of possible actions (right side). – Attributes of action are specified here.
  • onGuidedActionClicked(GuidedAction) – This is onClick listener. – Behaviors after clicking action buttons can be specified here.

At least if you know this, you can use GuidedStepFragment. But you may want to modify the design layout of this Guidance. You can use “Theme” & “Stylist” to customize visual styling

Implementation (Overriding method)

To attach instance of GuidedStepFragment, we can use GuidedStepFragment.add function. Here instance of ConfirmationStepFragment class, which is a subclass of GuidedStepFragment, is added at onCreate of ATVDialog.
Please also check the sample implementation of 3 overriding methods such as, onCreateGuidance, onCreateActions and onGuidedActionClicked.


public class ATVDialog extends Activity {
    private static final int CONTINUE = 0;
    private static final int BACK = 1;
   
    @Override  
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (null == savedInstanceState) {
           
                GuidedStepFragment.addAsRoot(this, new ConfirmationStepFragment(),
             android.R.id.content);
           
        }
    
    }

    private static void addAction(List<GuidedAction> actions, long id, 
     String title, String desc) {
        actions.add(new GuidedAction.Builder()
                .id(id)
                .title(title)
                .description(desc)
                .build());
    }

public static class ConfirmationStepFragment extends GuidedStepFragment {

        @Override
        @NonNull     
        public Guidance onCreateGuidance(@NonNull Bundle savedInstanceState) {
            String title = "Confirm";
            String description = "Dialog description message";
            return new Guidance(title, description, null, null);
        }

        @Override       
      public void onCreateActions(@NonNull List<GuidedAction> actions,
          Bundle savedInstanceState) {
            addAction(actions, CONTINUE,
                    "Confirm",
                    null);
            addAction(actions, BACK,
                    "Cancel",
                    null);
        }

        @Override   
        public void onGuidedActionClicked(GuidedAction action) {
            FragmentManager fm = getFragmentManager();
            if (action.getId() == CONTINUE) {
               //Do action on confirm Click
            } else {
                getActivity().finish();
                  }
        }
}
}