1

In my application i should use Vertical Stepper and for this i added below library : VerticalStepperForm.
I should use this stepper into service and for this i write below codes.
But when use this codes into Service, when click on EditText not open soft keyboard (not show me any keyboard), but when use this codes into activity, it's not bug and show me keyboard.
But into service not show any keyboard!

My service codes:

public class FloatingLayoutService extends Service implements StepperFormListener {

    private WindowManager windowManager;
    private ConstraintLayout floatingLayout, floatingLay_root, floatingLay_main;
    private IBinder binder = new ServiceBinder();
    private LinearLayout floatingLay_headerLay;
    private List<Step> steps = new ArrayList();
    private List<Integer> intList = new ArrayList<>();
    private List<TestStepsListResponse.Datum> testPlans = new ArrayList<>();
    //Timer
    private boolean running;
    private int stepIndex = 1;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        //Inflate the layout using LayoutInflater
        LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        floatingLayout = (ConstraintLayout) li.inflate(R.layout.service_floating_layout, null);
        //Init view
        initUi();
        //Set layout params to display the controls over any screen.
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        // From API26, TYPE_PHONE deprecated. Use TYPE_APPLICATION_OVERLAY for O
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
        //Initial position of the floating controls
        params.gravity = Gravity.BOTTOM | Gravity.START;
        params.x = 0;
        params.y = 0;
        //Add the controls view to windowManager
        windowManager.addView(floatingLayout, params);
        //Steps
        testPlans = App.testPlansList;
        for (int i = 0; i < testPlans.size(); i++) {
            if (testPlans.get(i).getItemType().equals(ConstKeys.TEST_STEP_TEXT_RESPONSE)) {
                StepDynamicEdt stepDynamicTxt = new StepDynamicEdt(testPlans.get(i).getTitle(), testPlans.get(i).getDesc());
                // add to list
                steps.add(stepDynamicTxt);
                intList.add(i);
            } else {
                StepDynamicTxt stepDynamicEdt = new StepDynamicTxt(testPlans.get(i).getTitle(), testPlans.get(i).getDesc());
                // add to list
                steps.add(stepDynamicEdt);
            }
        }

        floatingLay_stepper.setup(this, steps)
                .allowNonLinearNavigation(false)
                .displayCancelButtonInLastStep(false)
                .displayBottomNavigation(false)
                .confirmationStepTitle("OK")
                .stepNextButtonText("Next")
                .lastStepNextButtonText("Finish")
                .includeConfirmationStep(false)
                .init();

        return START_STICKY;
    }

StepDynamicEdt class codes:

public class StepDynamicEdt extends Step<String> {

    private static final int MIN_CHARACTERS = 1;
    private EditText editText;
    private String errorMessage;

    public StepDynamicEdt(String title) {
        this(title, "");
    }

    public StepDynamicEdt(String title, String subtitle) {
        super(title, subtitle);
    }

    @NonNull
    @Override
    protected View createStepContentLayout() {
        //Create view programmatically
        editText = new EditText(getContext());
        editText.setHint("User name");
        editText.setSingleLine(true);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                markAsCompletedOrUncompleted(true);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                getFormView().goToNextStep(true);
                return false;
            }
        });

        errorMessage = "Insert 1 character";

        return editText;
    }

    @Override
    public String getStepData() {
        Editable text = editText.getText();
        if (text != null) {
            return text.toString();
        }
        return "";
    }

    @Override
    public String getStepDataAsHumanReadableString() {
        String name = getStepData();
        return name == null || name.isEmpty() ? "Empty" : name;
    }

    @Override
    public void restoreStepData(String data) {
        if (editText != null) {
            editText.setText(data);
        }
    }

    @Override
    protected IsDataValid isStepDataValid(String stepData) {
        if (stepData.length() < MIN_CHARACTERS) {
            return new IsDataValid(false, errorMessage);
        } else {
            return new IsDataValid(true);
        }
    }

    @Override
    protected void onStepOpened(boolean animated) {
        // No need to do anything here
    }

    @Override
    protected void onStepClosed(boolean animated) {
        // No need to do anything here
    }

    @Override
    protected void onStepMarkedAsCompleted(boolean animated) {
        // No need to do anything here
    }

    @Override
    protected void onStepMarkedAsUncompleted(boolean animated) {
        // No need to do anything here
    }

}

Update : I added below code into StepDynamicEdt class, but again not open keyboard!

InputMethodManager imm = (InputMethodManager) getContext().getSystemService(
        Context.INPUT_METHOD_SERVICE);
Objects.requireNonNull(imm).showSoftInput(editText, 0);

How can i fix it and open keyboard into service ?

Shambooli
  • 75
  • 7
  • Is there any particular reason you're not just putting that in an `Activity`? I mean, it's fullscreen, right? – Mike M. Aug 05 '19 at 11:35
  • @MikeM. , i should show this VerticalStepper in overlay other app. for this i should use service! – Shambooli Aug 05 '19 at 11:37
  • @MikeM., can you help me my friend? – Shambooli Aug 05 '19 at 11:48
  • "for this i should use service!" – Hmm, not necessarily. Anyway, it's `FLAG_NOT_FOCUSABLE` that's preventing that. You can pass `0` there for no flags. Be careful with this, though. From experience, if something goes wrong, it's easy to get stuck with a focusable window on top that interferes with everything, to the point where you almost have to reboot to get rid of it. – Mike M. Aug 05 '19 at 11:53
  • @MikeM. , thanks but can you send to me code with my above codes? because i am amateur and i really need your help i want see code for understand this. can you send to me code with my above codes? please – Shambooli Aug 05 '19 at 11:57
  • There is no new code, really. Just replace `WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE` with `0`. – Mike M. Aug 05 '19 at 12:00
  • @MikeM., yes yes your answer help me. thaaaaaaaanks my dear friend. can you send your answer in another post for accept it and perhaps help to other developers? – Shambooli Aug 05 '19 at 12:04
  • 1
    No problem. This is a rather common question, though, and it's been answered here several times already, so I'll just close this as a duplicate. Thanks, though. I appreciate the offer. Cheers! – Mike M. Aug 05 '19 at 12:26

0 Answers0