Search code examples
androidandroid-layoutandroid-fragments

Switching views dynamically on a fragment


How can I achieve this?

No view is showing...

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.details, container, false);
       
        InitAlerts();
        return v;
    }
    
    private void InitAlerts()
    {
        //TODO Find step number
        //Temp: already set to 1
        
        if(stepNum == 1) {
            startStepOne();
        } else if(stepNum == 2) {
            startStepTwo();
        } else {
            startStepSummary();
        }
    }
    
    private void startStepOne() {
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View v = inflater.inflate( R.layout.usage_alerts_step_one, null, false);
        alertsStepOneConfirm = (Button) v.findViewById(R.id.alertsStepOneConfirm);
        alertsStepOneConfirm.setOnClickListener(this);
        v.bringToFront();
    }
    
    private void startStepTwo() {
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View v = inflater.inflate( R.layout.usage_alerts_step_two, null, false);
        stepTwoConfirm = (Button) v.findViewById(R.id.stepTwoConfirm);
        stepTwoConfirm.setOnClickListener(this);
        v.bringToFront();
    }

Solution

  • 1) Call the addView method on the R.layout.details layout with the corresponding step view as parameter. You need to return the inflated view from each of the step* methods.

    2) If you don't need the R.layout.details, you can just make onCreateView return the view that corresponds to the step