Alert Dialog Box With Dynamic Content

Alert dialog box

In this post we are talking about creating dynamic content in alert dialog box.This is not the correct way but it is a way. It’s mostly for beginners in android developing. where we want to use dynamic content for alertdialog box.Like a sub catogery in categorys.managing and utilising space of mobile screen more effectively. First of all let me give you a hind what we are up to.

 

alert dialog box 1alert dialog box 2alert dialog box 3

 

 

 

 

 

 

 

 

 

First we need to create the Alertdialog box.Contents for Alert dialog box can be added in many ways. The first way is by code,it means write what we want directly in code like

EditText edittext=new EditText(this)

. For advanced programmers it’s works but it’s bit difficult to do for biginers. The second method is creating a custom xml file for the Alertdialog box. It’s easy to make and understand. We go with the second way.



First step

The first step is to initialise some contents globally,so it can be accessed everywhere from the class.

    public LayoutInflater inflater;
    public RadioButton checkBox0,checkBox1,checkBox2,checkBox2v,checkBox2n,checkBox3,checkBox4,checkBox5,checkBox6,checkBox7;
    public RadioButton radioButton0,radioButton1,radioButton2;
Creating the custom Alert dialog box

Android uses xml file for creating Ui.Our custom ui is created using an xml file.Create an xml file for the ui in the layout folder of android studio.Below is a sample code.
I am using

dialog.xml
<?xml version="1.0" encoding="utf-8"?>\n<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
	android:orientation="vertical"   
        android:layout_width="wrap_content"    
        android:id="@+id/test"    
        android:layout_height="wrap_content">  
<TextView        
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"        
        android:layout_width="wrap_content"     
	android:layout_height="wrap_content"
        android:textSize="20sp"        
        android:text="categories to include"/>          
<LinearLayout
        android:layout_marginLeft="16dp" 
        android:layout_width="wrap_content"             
	android:layout_height="wrap_content"        
        android:orientation="vertical">  
<CheckBox             
	android:layout_width="wrap_content"  
        android:layout_height="wrap_content"   
        android:id="@+id/checkbox_1" 
	android:onClick="onCheckboxClicked"       
        android:text="event management"/>   
<CheckBox                
	android:layout_width="wrap_content"  
        android:layout_height="wrap_content"         
        android:id="@+id/checkbox_2" 
	android:onClick="onCheckboxClicked"    
        android:text="catering"/> 
<CheckBox                       
	android:layout_width="wrap_content"        
        android:layout_height="wrap_content"  
        android:id="@+id/checkbox_2v"
	android:visibility="gone"                       
        android:layout_marginLeft="16dp"                      
        android:onClick="onCheckboxClicked"       
	android:text="vegetarian"/>                   
<CheckBox                       
        android:layout_width="wrap_content"                       
	android:layout_height="wrap_content"  
        android:id="@+id/checkbox_2n"  
        android:visibility="gone"      
	android:layout_marginLeft="16dp"   
        android:onClick="onCheckboxClicked"  
        android:text="non vegetarian"/>
<CheckBox                  
        android:layout_width="wrap_content"                 
        android:layout_height="wrap_content"    
	android:id="@+id/checkbox_3"                  
        android:onClick="onCheckboxClicked"                  
        android:text="videography"/>            
<CheckBox                   
        android:layout_width="wrap_content"                
        android:layout_height="wrap_content"                 
	android:id="@+id/checkbox_4"                 
        android:onClick="onCheckboxClicked"                 
        android:text="photography"/>       
<CheckBox                  
        android:layout_width="wrap_content"                 
        android:layout_height="wrap_content"                 
	android:id="@+id/checkbox_5"                    
        android:onClick="onCheckboxClicked"                 
        android:text="travels"/>           
<CheckBox                  
        android:layout_width="wrap_content"                
        android:layout_height="wrap_content"                  
	android:id="@+id/checkbox_6"               
        android:onClick="onCheckboxClicked"                
        android:text="decaration"/>       
<CheckBox                   
        android:layout_width="wrap_content"                 
        android:layout_height="wrap_content"                 
	android:id="@+id/checkbox_7"                    
        android:onClick="onCheckboxClicked"                
        android:text="lighting and sound"/>
	
</LinearLayout>
</LinearLayout>




Now for the main part

MainActivity.java contain all the codeing part of the Alert dialog box

public class MainActivity extends AppCompatActivity implements View.OnClickListener {   
 CoordinatorLayout coordinatorLayoutMain;  
 
	FloatingActionButton fab;
	View view;   
	LayoutInflater inflater;   
	CheckBox checkBox1,checkBox2,checkBox2v,checkBox2n,checkBox3,checkBox4,
	checkBox5,checkBox6,checkBox7;   
	Button buttonDialog; 
	
	@Override    
	protected void onCreate(Bundle savedInstanceState) {     
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
	setSupportActionBar(toolbar);        
	coordinatorLayoutMain=(CoordinatorLayout)findViewById(R.id.coordinatorLayoutMain);       
	fab = (FloatingActionButton) findViewById(R.id.fab);
	buttonDialog=(Button)findViewById(R.id.buttonDialog);        
	fab.setOnClickListener(this);       
	buttonDialog.setOnClickListener(this);
	//inflater for the dialogbox     
	inflater = this.getLayoutInflater();        
	view=inflater.inflate(R.layout.dialog, null);        
	//check box for alertdialogbox START     
	checkBox1=(CheckBox)view.findViewById(R.id.checkbox_1);
	checkBox2=(CheckBox)view.findViewById(R.id.checkbox_2);      
	checkBox2v=(CheckBox)view.findViewById(R.id.checkbox_2v);
	checkBox2n=(CheckBox)view.findViewById(R.id.checkbox_2n);   
	checkBox3=(CheckBox)view.findViewById(R.id.checkbox_3);
	checkBox4=(CheckBox)view.findViewById(R.id.checkbox_4);	
	checkBox5=(CheckBox)view.findViewById(R.id.checkbox_5);   
    checkBox6=(CheckBox)view.findViewById(R.id.checkbox_6);     
	checkBox7=(CheckBox)view.findViewById(R.id.checkbox_7);      
	//check box for alertdialogbox  END
    }  
	
	
	// for viewng snack bar messages    
	public void SnackMessage(String message){     
	Snackbar.make(coordinatorLayoutMain,message, Snackbar.LENGTH_LONG).setAction("Action", null).show();   
	} 

	
	//handling clicks   
	@Override    
	public void onClick(View view) {        
	switch (view.getId()){       
	case R.id.fab:SnackMessage("fab click");break;
	case R.id.buttonDialog:onCreateDialog();break;        }    }
	
	// method for dialogbox view   
	public void onCreateDialog() {       
	final AlertDialog.Builder builder = new AlertDialog.Builder(this);      
	// Get the layout inflater
	// Inflate and set the layout for the dialog\n    
    // Pass null as the parent view because its going in the dialog layout        
	builder.setView(view)               
	// Add action buttons               
	.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
	@Override\n                    
	public void onClick(DialogInterface dialog, int id) {\n   
	// do something here  
	}}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {
	// do something here                    }
	}).setNeutralButton(R.string.report,new DialogInterface.OnClickListener() {
	public void onClick(DialogInterface dialog, int id) {
	// do something here
	}
	});
	builder.create().show();
	}
	
    //handling clicks from inside dialog box
    public void onCheckboxClicked(View view) {
	// Is the view now checked?
	boolean checked = ((CheckBox) view).isChecked();
	// Check which checkbox was clicked
	switch(view.getId()) {
	case R.id.checkbox_1:
	if (checked){                  
	SnackMessage("event management checked");
	} else{
	SnackMessage("event management unchecked");
	}break;
	case R.id.checkbox_2:
	if (checked){
	SnackMessage("catering checked");
	checkBox2v.setVisibility(View.VISIBLE);
	checkBox2n.setVisibility(View.VISIBLE);
	}else{
	SnackMessage("catering unchecked");
	checkBox2v.setVisibility(View.GONE);
	checkBox2n.setVisibility(View.GONE);
	}break;
	case R.id.checkbox_2n:
	if (checked){
	SnackMessage("non-veg checked");
	}else{
	SnackMessage("non-veg unchecked");
	}               
	break;          
	case R.id.checkbox_2v:
	if (checked){
	SnackMessage("veg checked");
	}else{
	SnackMessage("veg unchecked");
	}               break;
	case R.id.checkbox_3:
	if (checked){
	SnackMessage("video checked");
	}else{
	SnackMessage("video unchecked");
	}               
	break;
	case R.id.checkbox_4:
	if (checked){
	SnackMessage("photography checked");
	}else{
	SnackMessage("photography unchecked");
	}
	break;
	case R.id.checkbox_5:
	if (checked){
	SnackMessage("travel checked");
	} else{
	SnackMessage("travel unchecked");
	}
	break;
	case R.id.checkbox_6:
	if (checked){
	SnackMessage("decoration checked");
	} else{
	SnackMessage("decoration unchecked");               
	}               
	break;
	case R.id.checkbox_7:
	if (checked){
	SnackMessage("light and sound checked");
	} else{
	SnackMessage("light and sound unchecked");
	}               break;
	}    }  
	
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.menu_main, menu);
	return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	// Handle action bar item clicks here. The action bar will
	// automatically handle clicks on the Home/Up button, so long
	// as you specify a parent activity in AndroidManifest.xml.
	int id = item.getItemId();
	//noinspection SimplifiableIfStatement
	if (id == R.id.action_settings) {
	return true;
	}        
	return super.onOptionsItemSelected(item);
    }}

Leave a Reply

%d bloggers like this: