Example usage for android.widget HorizontalScrollView smoothScrollBy

List of usage examples for android.widget HorizontalScrollView smoothScrollBy

Introduction

In this page you can find the example usage for android.widget HorizontalScrollView smoothScrollBy.

Prototype

public final void smoothScrollBy(int dx, int dy) 

Source Link

Document

Like View#scrollBy , but scroll smoothly instead of immediately.

Usage

From source file:org.connectbot.ConsoleActivity.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        StrictModeSetup.run();/*from  w  ww.j  av a  2s.  c om*/
    }

    hardKeyboard = getResources().getConfiguration().keyboard == Configuration.KEYBOARD_QWERTY;

    clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);

    titleBarHide = prefs.getBoolean(PreferenceConstants.TITLEBARHIDE, false);
    if (titleBarHide && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // This is a separate method because Gradle does not uniformly respect the conditional
        // Build check. See: https://code.google.com/p/android/issues/detail?id=137195
        requestActionBar();
    }

    this.setContentView(R.layout.act_console);

    // hide status bar if requested by user
    if (prefs.getBoolean(PreferenceConstants.FULLSCREEN, false)) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    // TODO find proper way to disable volume key beep if it exists.
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    // handle requested console from incoming intent
    if (icicle == null) {
        requested = getIntent().getData();
    } else {
        String uri = icicle.getString(STATE_SELECTED_URI);
        if (uri != null) {
            requested = Uri.parse(uri);
        }
    }

    inflater = LayoutInflater.from(this);

    toolbar = (Toolbar) findViewById(R.id.toolbar);

    pager = (TerminalViewPager) findViewById(R.id.console_flip);
    pager.addOnPageChangeListener(new TerminalViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            setTitle(adapter.getPageTitle(position));
            onTerminalChanged();
        }
    });
    adapter = new TerminalPagerAdapter();
    pager.setAdapter(adapter);

    empty = (TextView) findViewById(android.R.id.empty);

    stringPromptGroup = (RelativeLayout) findViewById(R.id.console_password_group);
    stringPromptInstructions = (TextView) findViewById(R.id.console_password_instructions);
    stringPrompt = (EditText) findViewById(R.id.console_password);
    stringPrompt.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_UP)
                return false;
            if (keyCode != KeyEvent.KEYCODE_ENTER)
                return false;

            // pass collected password down to current terminal
            String value = stringPrompt.getText().toString();

            PromptHelper helper = getCurrentPromptHelper();
            if (helper == null)
                return false;
            helper.setResponse(value);

            // finally clear password for next user
            stringPrompt.setText("");
            updatePromptVisible();

            return true;
        }
    });

    booleanPromptGroup = (RelativeLayout) findViewById(R.id.console_boolean_group);
    booleanPrompt = (TextView) findViewById(R.id.console_prompt);

    booleanYes = (Button) findViewById(R.id.console_prompt_yes);
    booleanYes.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            PromptHelper helper = getCurrentPromptHelper();
            if (helper == null)
                return;
            helper.setResponse(Boolean.TRUE);
            updatePromptVisible();
        }
    });

    Button booleanNo = (Button) findViewById(R.id.console_prompt_no);
    booleanNo.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            PromptHelper helper = getCurrentPromptHelper();
            if (helper == null)
                return;
            helper.setResponse(Boolean.FALSE);
            updatePromptVisible();
        }
    });

    fade_out_delayed = AnimationUtils.loadAnimation(this, R.anim.fade_out_delayed);

    // Preload animation for keyboard button
    keyboard_fade_in = AnimationUtils.loadAnimation(this, R.anim.keyboard_fade_in);
    keyboard_fade_out = AnimationUtils.loadAnimation(this, R.anim.keyboard_fade_out);

    keyboardGroup = (LinearLayout) findViewById(R.id.keyboard_group);

    keyboardAlwaysVisible = prefs.getBoolean(PreferenceConstants.KEY_ALWAYS_VISIVLE, false);
    if (keyboardAlwaysVisible) {
        // equivalent to android:layout_above=keyboard_group
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.ABOVE, R.id.keyboard_group);
        pager.setLayoutParams(layoutParams);

        // Show virtual keyboard
        keyboardGroup.setVisibility(View.VISIBLE);
    }

    mKeyboardButton = (ImageView) findViewById(R.id.button_keyboard);
    mKeyboardButton.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            View terminal = adapter.getCurrentTerminalView();
            if (terminal == null)
                return;
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInputFromWindow(terminal.getApplicationWindowToken(),
                    InputMethodManager.SHOW_FORCED, 0);
            terminal.requestFocus();
            hideEmulatedKeys();
        }
    });

    findViewById(R.id.button_ctrl).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_esc).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_tab).setOnClickListener(emulatedKeysListener);

    addKeyRepeater(findViewById(R.id.button_up));
    addKeyRepeater(findViewById(R.id.button_up));
    addKeyRepeater(findViewById(R.id.button_down));
    addKeyRepeater(findViewById(R.id.button_left));
    addKeyRepeater(findViewById(R.id.button_right));

    findViewById(R.id.button_home).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_end).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_pgup).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_pgdn).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f1).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f2).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f3).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f4).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f5).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f6).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f7).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f8).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f9).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f10).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f11).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f12).setOnClickListener(emulatedKeysListener);

    actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        if (titleBarHide) {
            actionBar.hide();
        }
        actionBar.addOnMenuVisibilityListener(new ActionBar.OnMenuVisibilityListener() {
            public void onMenuVisibilityChanged(boolean isVisible) {
                inActionBarMenu = isVisible;
                if (!isVisible) {
                    hideEmulatedKeys();
                }
            }
        });
    }

    final HorizontalScrollView keyboardScroll = (HorizontalScrollView) findViewById(R.id.keyboard_hscroll);
    if (!hardKeyboard) {
        // Show virtual keyboard and scroll back and forth
        showEmulatedKeys(false);
        keyboardScroll.postDelayed(new Runnable() {
            @Override
            public void run() {
                final int xscroll = findViewById(R.id.button_f12).getRight();
                if (BuildConfig.DEBUG) {
                    Log.d(TAG, "smoothScrollBy(toEnd[" + xscroll + "])");
                }
                keyboardScroll.smoothScrollBy(xscroll, 0);
                keyboardScroll.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (BuildConfig.DEBUG) {
                            Log.d(TAG, "smoothScrollBy(toStart[" + (-xscroll) + "])");
                        }
                        keyboardScroll.smoothScrollBy(-xscroll, 0);
                    }
                }, 500);
            }
        }, 500);
    }

    // Reset keyboard auto-hide timer when scrolling
    keyboardScroll.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                autoHideEmulatedKeys();
                break;
            case MotionEvent.ACTION_UP:
                v.performClick();
                return (true);
            }
            return (false);
        }
    });

    tabs = (TabLayout) findViewById(R.id.tabs);
    if (tabs != null)
        setupTabLayoutWithViewPager();

    pager.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showEmulatedKeys(true);
        }
    });

    // Change keyboard button image according to soft keyboard visibility
    // How to detect keyboard visibility: http://stackoverflow.com/q/4745988
    contentView = findViewById(android.R.id.content);
    contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            contentView.getWindowVisibleDisplayFrame(r);
            int screenHeight = contentView.getRootView().getHeight();
            int keypadHeight = screenHeight - r.bottom;

            if (keypadHeight > screenHeight * 0.15) {
                // keyboard is opened
                mKeyboardButton.setImageResource(R.drawable.ic_keyboard_hide);
                mKeyboardButton.setContentDescription(
                        getResources().getText(R.string.image_description_hide_keyboard));
            } else {
                // keyboard is closed
                mKeyboardButton.setImageResource(R.drawable.ic_keyboard);
                mKeyboardButton.setContentDescription(
                        getResources().getText(R.string.image_description_show_keyboard));
            }
        }
    });
}