Onurcan Ari Onurcan Ari - 5 months ago 14
Android Question

How can I make a dialog ? How can I fix this?

First of all, sorry about my English. I Am trying to create a dialog with 2 buttons and textview. When I click a button the program skips the first part and jumps to the second.
How can I fix this? My goal is to do a dialog like:


  • +Hi, how are you?

  • -Good

  • -Bad

  • (Clicked to bad)

  • +Why do you feel bad ?

  • -Because he bited me

  • -Because I Am wounded

    Thanks for the help :| !

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnT = (Button) findViewById(R.id.btnT);
    btnF = (Button) findViewById(R.id.btnF);
    tv=(TextView)findViewById(R.id.textView);
    yazdir(R.array.metin1,R.array.secim1,0);

    //first part
    btnT.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    yazdir(R.array.metin2,R.array.secim2,0);
    }
    });
    btnF.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    yazdir(R.array.metin2,R.array.secim2,1);
    }
    });

    //second part


    btnT.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    yazdir(R.array.metin3,R.array.secim3,0);
    }
    });
    btnF.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    yazdir(R.array.metin3,R.array.secim3,1);
    }
    });

    }





    private Handler mHandler = new Handler();
    private Runnable characterAdder = new Runnable() {
    @Override
    public void run() {
    tv.setText(mText.subSequence(0, mIndex++));
    if(mIndex <= mText.length()) {
    mHandler.postDelayed(characterAdder, 20);
    }
    else {
    btnT.setVisibility(View.VISIBLE);
    btnF.setVisibility(View.VISIBLE);
    }
    }
    };

    public void yazdir(int metinGirisi,int secimGirisi,int metinIndex) {
    // Yaz─▒

    text=getResources().getStringArray(metinGirisi);
    mText = text[metinIndex];
    mIndex = 0;
    tv.setText("");
    mHandler.removeCallbacks(characterAdder);
    mHandler.postDelayed(characterAdder, 20);

    // Buton

    secim=getResources().getStringArray(secimGirisi);
    btnT.setText(secim[0]);
    btnF.setText(secim[1]);
    btnF.setVisibility(View.INVISIBLE);
    btnT.setVisibility(View.INVISIBLE);


    }


Answer

You need to use this library

According to your requirement, your code should look like:

    MaterialDialog dialog = new MaterialDialog.Builder(this)
        .content("Hi, how are you?")
        .positiveText("Good")
        .negativeText("Bad")
        .show();

    // Handle Good click
    dialog.onPositive(new MaterialDialog.SingleButtonCallback() {
        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
             new MaterialDialog.Builder(this)   
            .content("You clicked Good")
            .positiveText("OK")              
            .show();
        }
    })

    // Handle Bad click
    dialog.onPositive(new MaterialDialog.SingleButtonCallback() {
        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
             new MaterialDialog.Builder(this)   
            .content("Why feeling bad?")
            .positiveText("Because he bit me")
            .negativeText("Because I am wounded")
            .show();   
        }
    })

Hope that helps!!