-2

I want to send integer data from main activity to the custom dialog box,This integer will help select the text view and buttons

This code calls the custom dialog box in another activity

Main Activity

exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                counter=1;
                CustomDialogClass cdd=new CustomDialogClass(MainActivity.this,getApplicationContext(),counter);
                try{
                    Toast.makeText(MainActivity.this,"ExitCounter",Toast.LENGTH_LONG).show();
                }catch (Exception e){
                    Toast.makeText(MainActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
                }
                //cdd.show();
            }
        });
        hello = (Button) findViewById(R.id.test_hello);
        hello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                counter=2;
                CustomDialogClass cdd=new CustomDialogClass(MainActivity.this,getApplicationContext(),counter);
                try{
                    Toast.makeText(MainActivity.this,"HelloCounter",Toast.LENGTH_LONG).show();
                }catch (Exception e){
                    Toast.makeText(MainActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
                }
               // cdd.show();
            }
        });

This the code for the Custom Dialog box

package com.example.mhaad.test;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

class CustomDialogClass extends Dialog implements android.view.View.OnClickListener {

    public Activity c;
    public Button yes, no,ok;
    public TextView hello,exit;
    public int selection;
    Context context;

    public CustomDialogClass(Activity a, Context applicationContext,int con) {
        super(a);
        // TODO Auto-generated constructor stub
        this.c = a;
        this.context=applicationContext;
        selection=con;
        Toast.makeText(context,"Con",Toast.LENGTH_LONG).show();
        show();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(context,"onCreate",Toast.LENGTH_LONG).show();
        Toast.makeText(context,""+selection,Toast.LENGTH_LONG).show();
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_custom_dialog_box);
        yes = (Button) findViewById(R.id.btn_yes);
        no = (Button) findViewById(R.id.btn_no);
        ok=(Button)findViewById(R.id.btn_ok);
        exit = (TextView) findViewById(R.id.txt_dia);
        hello = (TextView) findViewById(R.id.txt_dia1);
        exit.setVisibility(View.INVISIBLE);
        hello.setVisibility(View.INVISIBLE);
        yes.setVisibility(View.INVISIBLE);
        no.setVisibility(View.INVISIBLE);
        ok.setVisibility(View.INVISIBLE);
        yes.setOnClickListener(this);
        no.setOnClickListener(this);
        ok.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        Toast.makeText(context,"Click",Toast.LENGTH_LONG).show();
        switch(selection){
            case 1:
                Log.e("asd",String.valueOf(selection));
                exit.setVisibility(View.VISIBLE);
                hello.setVisibility(View.INVISIBLE);
                yes.setVisibility(View.VISIBLE);
                no.setVisibility(View.VISIBLE);
                ok.setVisibility(View.INVISIBLE);
                switch (v.getId()) {
                    case R.id.btn_yes:
                        c.finish();
                        break;
                    case R.id.btn_no:
                        dismiss();
                        break;
                    case R.id.btn_ok:
                        dismiss();
                        break;
                    default:
                        break;
                }
                break;

            case 2:
                Log.e("asd",String.valueOf(selection));
                exit.setVisibility(View.INVISIBLE);
                hello.setVisibility(View.VISIBLE);
                yes.setVisibility(View.INVISIBLE);
                no.setVisibility(View.INVISIBLE);
                ok.setVisibility(View.VISIBLE);
                switch (v.getId()) {
                    case R.id.btn_yes:
                        c.finish();
                        break;
                    case R.id.btn_no:
                        dismiss();
                        break;
                    case R.id.btn_ok:
                        dismiss();
                        break;
                    default:
                        break;
                }
                break;

            default:
                break;
        }
        dismiss();
    }
}

Here is the Manifest

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

The integer is pass to the custom dialog box but the integer is passed to the onClick() after the the dialog box ends,I have tried everything but the int value is not accessed by the onClick() function . Need Help!!

  • What difference does it make while you already have an `Object` of `CustomDialogClass` ? Just pass the value with a setter method or you can pass it in constructor. Or to distinguish the call you can use a callback See [How to Define Callbacks in Android](https://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android). – ADM Mar 27 '18 at 13:47
  • So your Custom Dialog box is being shown by another activity, and even if that is the case, your custom dialog constructor has the parameter counter and should be able to use the variable con, as I can see that you have initialised selection with con, then what is the issue with this ? – Ashish Kumar Mar 27 '18 at 13:50
  • Not getting what do you want to achieve? According to the question you have passed the value in the constructor and assigned it to your variable. What else? – Aayush Thakur Mar 27 '18 at 13:51
  • as you can see I have set all the elements to invisible – Muhammad Haad Bin Zahid Mar 27 '18 at 13:55
  • @AayushThakur after debugging the dialog box is empty [Here is the Image](https://ibb.co/gqSfh7) – Muhammad Haad Bin Zahid Mar 27 '18 at 14:04

1 Answers1

0

Pass your integer through the bundle.

      //Create the bundle
   Intent i=new intent(MainActivity.this,yourSecondActivity)
   Bundle bundle = new Bundle();
   //Add your integer
   bundle.putInt("integer", your_integer);
   //Add the bundle to the intent
   i.putExtras(bundle);
   startActivity(i);

// in your second Activity

 Bundle bundle = getIntent().getExtras();
 //Extract the data…
 Integer int= bundle.getInt("integer",0);