title: How to add Progress Bar dynamically to the layout
body: <p>I am rolling a dice and using a random function i am counting the no. of 1s,2s,3s,4s,5s,6s and printing it out .If it takes too much time then it will show a progress bar.Why my android code is not showing progress bar although i have used progress bar in my java code?
  Plz figure out the mistakes.</p>
  
  <p>Java Code</p>
  
  <pre><code>package dead.asynctaskprogressbar;
  
  import android.app.ProgressDialog;
  import android.os.AsyncTask;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.EditText;
  import android.widget.LinearLayout;
  import android.widget.ProgressBar;
  import android.widget.TextView;
  
  import java.util.Random;
  
  public class MainActivity extends AppCompatActivity {
  
      EditText editText;
      TextView textView;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          textView=(TextView)findViewById(R.id.textView);
          editText=(EditText)findViewById(R.id.editText);
  
      }
      public class Asynctask extends AsyncTask&lt;Integer,Integer,String&gt;{
          ProgressBar bar;
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              bar=new ProgressBar(MainActivity.this,null,android.R.attr.progressBarStyleHorizontal);
  
              bar.setVisibility(View.VISIBLE);
  
              bar.setMax(Integer.parseInt(editText.getText().toString().trim()));
  
          }
  
          @Override
          protected String doInBackground(Integer... integers) {
              Random random=new Random();
              int ones=0,twos=0,threes=0,fours=0,fives=0,sixes=0,a;
  
              double currentProgress=0;
              double previousprogress=0;
  
              for (int i=0;i&lt;integers[0];i++){
                  currentProgress=(double)i/integers[0];
  
                  if (currentProgress-previousprogress&gt;=0.02){
                      previousprogress=currentProgress;
                      publishProgress(i);
                  }
  
                   a=  random.nextInt(6)+1;
                  switch (a){
                      case 1:
                          ones++;
                          break;
                      case 2:
                          twos++;
                          break;
                      case 3:
                          threes++;
                          break;
                      case 4:
                          fours++;
                          break;
                      case 5:
                          fives++;
                          break;
                      default:
                          sixes++;
  
                  }
              }
              String results="Ones :"+ones +"\n"+"Twos :"+twos +"\n"+"Threes :"+threes +"\n"+"Fours :"+fours +"\n"+"Fives :"+fives +"\n"+"Sixes :"+sixes +"\n";
  
  
              return results;
          }
  
          @Override
          protected void onProgressUpdate(Integer... values) {
  
              super.onProgressUpdate(values);
              bar.setProgress(values[0]);
          }
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
  
                  textView.setText(s);
  
          }
      }
  
      public void Dice(View view){
  
          int not=Integer.parseInt(editText.getText().toString().trim());
          new  Asynctask().execute(not);
      }
  }
  </code></pre>
  
  <p>Xml code</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      tools:context="dead.asynctaskprogressbar.MainActivity"&gt;
  
      &lt;EditText
          android:id="@+id/editText"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:ems="10"
          android:gravity="center_horizontal"
          android:hint="Dice"
          android:inputType="textPersonName"
          android:padding="10dp" /&gt;
  
      &lt;Button
          android:id="@+id/button"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:onClick="Dice"
          android:text="Roll Dice" /&gt;
  
      &lt;TextView
          android:id="@+id/textView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="TextView"
          android:textSize="20dp" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>