Wednesday 23 July 2014

Dynamic Generation of Widgets

I Would like to discuss about dynamic creation of widgets in android on clicking button



MainActivity.java


package com.example.dynamicwidgets;



import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.LinearLayout;

import android.widget.RadioGroup;

import android.widget.TextView;

import android.widget.Toast;

import android.widget.LinearLayout.LayoutParams;





public class MainActivity extends Activity {





      LinearLayout demoLayout;



       private LinearLayout mLayout;

       private EditText mEditText;

       private Button mButton;

       int k = 0;



      public static TextView textView[] = new TextView[8];

      public static EditText locationEditText[] = new EditText[8];

      Button addWidgetBtn,deleteMoreAddedButtons;



    @Overrride

   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

    demoLayout = (LinearLayout)findViewById(R.id.demoLayout);



     addWidgetBtn = (Button)findViewById(R.id.addWidgetBtn);
     addWidgetBtn.setOnClickListener(new View.OnClickListener() { 
      @Override
     public void onClick(View v) {



    try{

              k++;

            final LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT, 50);

            lparams.setMargins(5, 0, 0, 0);

       

            textView[k] = new TextView(MainActivity.this);

            textView[k].setLayoutParams(lparams);

            textView[k].setId(k);

            textView[k].setText("Location");

 

           final LayoutParams lplocEdt = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);



       lplocEdt.setMargins(5, -15, 10, 15);

       locationEditText[k] = new EditText(MainActivity.this);

       locationEditText[k].setLayoutParams(lplocEdt);

       locationEditText[k].setHint("Specify Location");

       locationEditText[k].setId(k);

   

     }catch(Exception e){

        e.printStackTrace();

     }



      demoLayout.addView(textView[k]);

      demoLayout.addView(locationEditText[k]);

 }



 });



 deleteMoreAddedButtons = (Button)findViewById(R.id.deleteMoreLocations);

deleteMoreAddedButtons.setOnClickListener(new View.OnClickListener() {



 @Override
public void onClick(View v) {

// TODO Auto-generated method stub



try{

       if(k!=0){

                   k--;

       demoLayout.removeView(textView[k+1]);

       demoLayout.removeView(locationEditText[k+1]);



   }else{

   

      Toast.makeText(getApplicationContext(), "Nothing To Delete", Toast.LENGTH_SHORT).show();

     }



}catch(Exception e){

               e.printStackTrace();

}

}

});



}



}



     activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content" >



<LinearLayout

  android:id="@+id/linearLayout1"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_alignParentTop="true"

  android:layout_marginTop="105dp"

  android:orientation="vertical" >



 <ScrollView

        android:id="@+id/scrollView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" >



     <LinearLayout

        android:layout_width="271dp"

         android:layout_height="wrap_content"

         android:layout_alignParentLeft="true"

         android:layout_alignParentTop="true"

         android:layout_marginLeft="25dp"

         android:orientation="vertical" >



            <LinearLayout

                         android:id="@+id/demoLayout"

                         android:layout_width="match_parent"

                         android:layout_height="wrap_content"

                         android:orientation="vertical" >



                       <TextView

                                  android:id="@+id/textView5"

                                   android:layout_width="wrap_content"

                                    android:layout_height="wrap_content"

                                   android:text="Location" />



            <EditText

                          android:id="@+id/securinSpecifyTheLocation"

                          android:layout_width="match_parent"

                          android:layout_height="wrap_content" />



                       </LinearLayout>

                      </LinearLayout>

                    </ScrollView>

                          </LinearLayout>



         <Button

                    android:id="@+id/addWidgetBtn"                                                       

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_alignParentLeft="true"

                    android:layout_alignParentTop="true"

                    android:layout_marginTop="27dp"

                    android:text="Add Locations" />



          <Button

                      android:id="@+id/deleteMoreLocations"

                      android:layout_width="wrap_content"

                      android:layout_height="wrap_content"

                     android:layout_alignBaseline="@+id/addWidgetBtn"

                     android:layout_alignBottom="@+id/addWidgetBtn"

                     android:layout_alignParentRight="true"

                     android:text="Delete Locations" />



</RelativeLayout>