Use seekbars in dialog/Alertdialog to change settings in activity

305 views Asked by At

I am trying to create an app that shows positions where i have been in googlemaps. Then I have some seekbars that changes what days and during which time I want to show where i have been for example. I got a code working when I have the googlemaps fragment and the seekbar (using crystal seekbar) in that same acitivy sharing the space on the screen. But now I want to increase the size of the googlemap and instead have the seekbars in a other layout showed by a dialog when hitting a button called settings. I cannot get that to work because it complains about Views and I do not want to remove the view because then all my parameters and stuff changes back to sarting values and stops working. Below I show my code for the class and the error message.

Here declaration of the seekbar in the mainactivity: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    layout_settings = (LinearLayout)inflater.inflate(R.layout.dialog_settings, null);

    //Initialize
    InitializeUI();

    //Makes buttons work
    onClickListener();

    //UpdateGuiStart
    updateStartGUI();

    //Seekbars
    SeekBarCheckBoxModeController();

}

And here is where i initilize all my seekbars:

 public void InitializeUI() {
    GPSButton = (Button) findViewById(R.id.GPSButton);
    mapsButton = (Button) findViewById(R.id.mapsButton);
    TextSpeed = (TextView) findViewById(R.id.TextSpeed);
    check_view = (TextView) findViewById(R.id.textView_check);
    deleteData = (Button) findViewById(R.id.button_delete);
    settings = (Button) findViewById(R.id.button_settings);
    TextSpeed.setText("Speed : " + speed);
    mModeSpinner = (Spinner) layout_settings.findViewById(R.id.spinner);
    mSeekBarTime = (CrystalSeekbar) layout_settings.findViewById(R.id.rangeSeekbarTime);
    mSeekBarDate = (CrystalRangeSeekbar) layout_settings.findViewById(R.id.rangeSeekbarDate);
    mSeekBarTimeLength = (CrystalSeekbar) layout_settings.findViewById(R.id.rangeSeekbarTimeLength);
    mSeekBarTransparency = (CrystalSeekbar) layout_settings.findViewById(R.id.rangeSeekbarTransparency);
    tvMinTime = (TextView) layout_settings.findViewById(R.id.textViewMinTime);
    tvMaxTime = (TextView) layout_settings.findViewById(R.id.textViewMaxTime);
    tvMinDate = (TextView) layout_settings.findViewById(R.id.textViewMinDate);
    tvMaxDate = (TextView) layout_settings.findViewById(R.id.textViewMaxDate);
    tvMaxDates = (TextView) layout_settings.findViewById(R.id.textViewMaxDates);
    tvTransparency = (TextView) layout_settings.findViewById(R.id.textViewTransparency);


    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.fragment_map);
    mapFragment.getMapAsync(this);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    //Initialize database helper
    mDbHelper = new LocationDbHelper(this);
}

and here is my alertdialog for showing the seekbars where I want to do my changes that should be done on the map and when i click there again i want my seekbars to stay the same way i left the last time I changed something...

 settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            minday = current_minday;
            maxday = current_maxday;
            mintime = current_time;
            maxtime = maxTime;
            trans = current_trans;
            mode = mModeSpinner.getSelectedItemPosition();

            final AlertDialog.Builder settings_alert = new AlertDialog.Builder(MainActivity.this);
            settings_alert.setView(layout_settings);
            settings_alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    show();
                    Toast.makeText(MainActivity.this, "Settings changed", Toast.LENGTH_LONG).show();
                    dialogInterface.dismiss();
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    mSeekBarDate.setMaxStartValue(maxday);
                    mSeekBarDate.setMinStartValue(minday);
                    mSeekBarTime.setMinStartValue(mintime);
                    mSeekBarTimeLength.setMinStartValue(maxtime);
                    mModeSpinner.setSelection(mode);
                    dialogInterface.dismiss();

                }
            }).show();

        }
    });

Error message:

FATAL EXCEPTION: main
                                                                                        Process: com.example.smartcities.smartcitiesproject, PID: 30988
                                                                                        java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                                            at android.view.ViewGroup.addViewInner(ViewGroup.java:4654)
                                                                                            at android.view.ViewGroup.addView(ViewGroup.java:4490)
                                                                                            at android.view.ViewGroup.addView(ViewGroup.java:4462)
                                                                                            at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:613)
                                                                                            at android.support.v7.app.AlertController.setupView(AlertController.java:452)
                                                                                            at android.support.v7.app.AlertController.installContent(AlertController.java:215)
                                                                                            at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
                                                                                            at android.app.Dialog.dispatchOnCreate(Dialog.java:578)
                                                                                            at android.app.Dialog.show(Dialog.java:314)
                                                                                            at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:953)
                                                                                            at com.example.smartcities.smartcitiesprojects.MainActivity$4.onClick(MainActivity.java:313)
                                                                                            at android.view.View.performClick(View.java:5697)
                                                                                            at android.widget.TextView.performClick(TextView.java:10814)
                                                                                            at android.view.View$PerformClick.run(View.java:22526)
                                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:158)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

picture of seekbar dialog

1

There are 1 answers

5
Roljhon On BEST ANSWER
    LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    layout_settings = (LinearLayout)inflater.inflate(R.layout.dialog_settings, null);

The above code must be local to the onClick event.

It should be like this:

  settings.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
        layout_settings = (LinearLayout)inflater.inflate(R.layout.dialog_settings, null);

        minday = current_minday;
        maxday = current_maxday;
        mintime = current_time;
        maxtime = maxTime;
        trans = current_trans;
        mode = mModeSpinner.getSelectedItemPosition();

        final AlertDialog.Builder settings_alert = new AlertDialog.Builder(MainActivity.this);
        settings_alert.setView(layout_settings);
        settings_alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                show();
                Toast.makeText(MainActivity.this, "Settings changed", Toast.LENGTH_LONG).show();
                dialogInterface.dismiss();
            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                mSeekBarDate.setMaxStartValue(maxday);
                mSeekBarDate.setMinStartValue(minday);
                mSeekBarTime.setMinStartValue(mintime);
                mSeekBarTimeLength.setMinStartValue(maxtime);
                mModeSpinner.setSelection(mode);
                dialogInterface.dismiss();
            }
        }).show();
    }
});