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!!