Inflate Toolbar in Stepper

Asked

Viewed 64 times

0

I need to create a Stepper in my application that I got the library on Github https://github.com/shivasurya/materialsteppers

I managed to implement the application, however I want to put a Toolbar to inflate a menu and have the back button in it, but I am not able to put in the application. Follows the codes:

Xml screen 1:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarSessao2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    </android.support.v7.widget.Toolbar>

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView"
        style="@style/FundoAPP"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbarSessao2"
        android:layout_gravity="right">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:weightSum="1">

            <TextView
                style="@style/titleHints"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/hint_sessao_1" />

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp">

                <EditText
                    android:id="@+id/JN_04_01"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:hint="@string/hintHora"
                    android:textColorHint="#ffa3a3a3" />

            </android.support.design.widget.TextInputLayout>

            <View style="@style/Divider" />

            <TextView
                android:id="@+id/textView4"
                style="@style/Textos"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp"
                android:text="@string/Repet" />


        </LinearLayout>
    </ScrollView>
</RelativeLayout>

Screen 2 is similar to the one above...

This is the Fragment of this screen above:

public class sessao_fragment1 extends stepperFragment implements View.OnClickListener{


    public sessao_fragment1(){

    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(
                R.layout.sessao1, container, false);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_jn_04_sessao, menu);
    }

    @Override
    public boolean onNextButtonHandler() {
        return true;
    }

    @Override
    public void onClick(View v) {

    }
}

The second screen is also the same...

And that’s the screen that controls Stepper:

public class stepper_sessao extends progressMobileStepper implements View.OnClickListener{


        List<Class> stepperFragmentList = new ArrayList<>();

        @Override
        public List<Class> init() {
            stepperFragmentList.add(sessao_fragment1.class);
            stepperFragmentList.add(sessao_fragment2.class);

            return stepperFragmentList;
        }

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarSessao2);
            toolbar.setTitle(R.string.title_activity_inserir_programacao);
            toolbar.setTitleTextColor(getResources().getColor(R.color.colorTextButton));
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            // Set an OnMenuItemClickListener to handle menu item clicks
            toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    // Handle the menu item
                    switch (item.getItemId()) {
                        case R.id.manutencao_sessoes:
                            startActivity(new Intent(getApplicationContext(), jn_14_manutencao_sessao.class));
                            break;

                        case R.id.cad_Remedio:
                            startActivity(new Intent(getApplicationContext(), jn_03_remedio.class));
                            break;

                        case R.id.salvarSessao:
                            //SALVAR();
                            break;
                    }
                    return true;
                }
            });

            toolbar.inflateMenu(R.menu.menu_jn_04_sessao);
            
        }

        @Override
        public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
            
            return super.onCreateView(parent, name, context, attrs);
        }

        @Override
        public View onCreateView(String name, Context context, AttributeSet attrs) {
            return super.onCreateView(name, context, attrs);

        }

        @Override
        public void onStepperCompleted() {
            showCompletedDialog();
        }


        protected void showCompletedDialog(){
            android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(
                    stepper_sessao.this);

            // set title
            /*alertDialogBuilder.setTitle("Hooray");
            alertDialogBuilder
                    .setMessage("We've completed the stepper")
                    .setCancelable(true)
                    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {

                        }
                    });
            */
            // create alert dialog
            android.support.v7.app.AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
            //finish();
        }
        @Override
        protected void onDestroy() {
            super.onDestroy();
        }


    }

The Toolbar I put this commented on this last screen, but it was not correct... if you can help!

that’s the mistake:

02-14 23:05:48.443 1354-1354/com.housepharm E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.housepharm, PID: 1354
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.housepharm/com.housepharm.Visao.Tela.stepper_sessao}: java.lang.NullPointerException
  • From what I understand of this library, the simplest way is to have an Activity that configure and control the Stepper, I don’t know if that’s what’s going on. But Toolbar should be set up in Activity and not in Fragment. But it seems to me that the stepper_sessao is a Fragment. You are using a Fragment to control the Stepper?

  • So, I looked at his code, the only thing he does in Mainactivity is to extend the progressiMobileStepper, however, his project has Darkactionbar style and I’m using Theme.AppCompat.Light.Noactionbar; I tried too, after you spoke, put the Toolbar inside onCreate instead of onCreateView, but it gave java.lang.Nullpointerexception error after creating the object

  • Where generates the error? Could change the question with the code that generated the error?

  • I completed the code as requested

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.