Skip to content Skip to sidebar Skip to footer

Set Custom Dialog Once Into Activity

I have listactivity consist of multiple row each row open activity which contain text and two button one of them open infinite gallery the other one open dialog ,and each dialog ha

Solution 1:

You have a lot of duplicating code. I suggest change intent day indicating extra type to int, and put your text resources into arrays with index = day number - 1.

Also it is easier to read and maintain code if you implement onClickListener interface in your activity class instead of creating new object.

Here is refactored version of your code, much shorter and clearer:

publicclassMyDayextendsActivityimplementsView.OnClickListener {
    privateContextmContext=this;
    private Button mButton;
    private TextView tv1, tv2, tv3, tv4;
    privateint  dayNumber;
    private Dialog mDialog;
    // this string should be in resources, like other belowprivatefinal String[] tv1Resources = {"First day", "Second day", "Third day" , ...}; 
    privatefinalint[] tv3Resources = {R.string.day1, R.string.day2, R.string.day3 , ...};
    privatefinalint[] dialogTextResources = {R.string.torusim_places_1, R.string.torusim_places_2, R.string.torusim_places_3 , ...};

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        BooleancustomTitleSupported=       
                requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);     
        setContentView(R.layout.day);  

        if (customTitleSupported) {          
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);  } 

            // note, now you should put integer extra, //not String, this extra is just day number -1, //for day1 this is 0, for day2 this is 1 etc. //In your case, probably this number == item position in listview of 
        dayNumber = getIntent().getIntExtra("cheese", -1); previous activity
        if(dayNumber == -1){
            finish(); //this needed if somehow you get invalid extrareturn;
        }

        initializeTextViews();
    }

    @OverridepublicvoidonClick(View v) {       
        switch (v.getId()){
            //actually this switch isn't needed because you have //only one button in this activity, but I wanted to show,// how you can maintain many onClick events herecase R.id.city_button: 
            // custom dialog// we use class field for avoid making final dialog local variable
            mDialog = newDialog(mContext, R.style.cust_dialog); 

            mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            mDialog.setContentView(R.layout.custom_dialog);
            // set the custom dialog components - text, image and buttonTextViewtext= (TextView) mDialog.findViewById(R.id.dialog_text);
            text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
            text.setText(Html.fromHtml(getString(dialogTextResources[dayNumber])));

            ButtondialogButton= (Button) mDialog.findViewById(R.id.dialog_Button);
            dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(newOnClickListener() {
                publicvoidonClick(View v) {
                    if (mDialog != null) mDialog.dismiss(); // check for aviod dialog null pointer
                }
            });
            mDialog.show(); 
            break;
        }
    }

    privatevoidinitializeTextViews() {
        tv1 = (TextView) findViewById(R.id.title_tv1);
        tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));

        tv2 = (TextView) findViewById(R.id.day_tv1);
        tv2.setTypeface(FontFactory.getBFantezy(getBaseContext()));


        tv3 = (TextView) findViewById(R.id.day_tv3);
        tv3.setTypeface(FontFactory.getBFantezy(getBaseContext()));


        mButton = (Button) findViewById(R.id.city_button);
        mButton.setOnClickListener(this);

        tv1.setText(tv1Resources[dayNumber]);
         // due to code, this resource is the same for all days
        tv2.setText(Html.fromHtml(getString(R.string.beginning)));
        tv3.setText(Html.fromHtml(getString(tv3Resources[dayNumber])));
    }

    publicvoidhandleClick(View v) {
        // Create an intent to start the new activity.Intentintent=newIntent();
        intent.setClass(this, DayGallery.class);
        intent.putExtra("dayname", day);
        startActivity(intent);
    }
}

Hope this helps.

Solution 2:

You could put your dialog creation within a separate method and just call that method from your onClick of your buttons. something like the following:

if(day.equalsIgnoreCase("Day1")){
    tv1.setText("First Day");       
    tv2.setText(Html.fromHtml(getString(R.string.beginning)));  
    tv3.setText(Html.fromHtml(getString(R.string.day1))); 

    button = (Button) findViewById(R.id.city_button);        
    button.setOnClickListener(new OnClickListener() { 
        publicvoidonClick(View arg0) { 
            dialogCreation(Html.fromHtml(getString(R.string.torusim_places_1)).toString());
        }

 elseif(day.equalsIgnoreCase("Day2")){.... }

Then your dialogCreation() could look like:

publicvoiddialogCreation(String arg0) {
     // custom dialogfinalDialogdialog=newDialog(context,R.style.cust_dialog);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);           
    dialog.setContentView(R.layout.custom_dialog); 
    // set the custom dialog components - text, image and buttonTextViewtext= (TextView) dialog.findViewById(R.id.dialog_text);
    text.setTypeface(FontFactory.getBFantezy(getBaseContext()));                
    text.setText(arg0);

     ButtondialogButton= (Button) dialog.findViewById(R.id.dialog_Button);             
            dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(newOnClickListener() {
        publicvoidonClick(View v) {
            dialog.dismiss();}
                     });
            dialog.show(); }
                     }); 
}

Post a Comment for "Set Custom Dialog Once Into Activity"