Android Open Source - Dynamic_Layout Starting Activity






From Project

Back to project page Dynamic_Layout.

License

The source code is released under:

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> Everyone is permitted to copy and distribute v...

If you think the Android project Dynamic_Layout listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.droidoholics.xfinity;
/*w w  w .  java  2s.  c  o m*/
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;

public class StartingActivity extends Activity {

  //the linear layout which holds rows
  private LinearLayout rowHolder;

  //the button which adds single row to rowHolder
  private Button btnAdd;

  //the button which reads each row in rowHolder
  private Button btnRead;

  //a layout inflater which will use to inflate layouts
  private LayoutInflater layoutInflater;

  //the ArrayList which will be hold ViewHolders
  private ArrayList<ViewHolder> viewHolders = new ArrayList<ViewHolder>();

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.starting_activity);

    //initializing rowHolder
    rowHolder = (LinearLayout) findViewById(R.id.rowHolder);

    //initializing btnAdd
    btnAdd = (Button) findViewById(R.id.btnAdd);

    //initializing btnRead
    btnRead = (Button) findViewById(R.id.btnRead);

    //initializing layout inflater
    layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

    //setup an OnClickListener for handle click events
    btnAdd.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        btnAddClicked(view);
      }
    });

    //setup an OnClickListener for handle click events
    btnRead.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        btnReadClicked(view);
      }
    });
  }

  private void btnAddClicked(View view) {

    final ViewHolder viewHolder = new ViewHolder();

    //inflating view which is defined in res\layout\single_row.xml
    viewHolder.parentView = layoutInflater.inflate(R.layout.single_row, null);
    viewHolder.btnRemove = (Button) viewHolder.parentView.findViewById(R.id.btnRemoveRow);
    viewHolder.editText = (EditText) viewHolder.parentView.findViewById(R.id.editText);

    //setup an OnClickListener for remove row
    viewHolder.btnRemove.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        viewHolders.remove(viewHolder);
        rowHolder.removeView(viewHolder.parentView);
      }
    });

    viewHolders.add(viewHolder);

  }

  private void btnReadClicked(View view) {
    int row = 0;
    for (ViewHolder viewHolder : viewHolders) {
      Toast.makeText(StartingActivity.this, "the entered value at row position" + ++row + " is " + viewHolder.editText.getText().toString(), Toast.LENGTH_SHORT).show();
    }
  }

  private static class ViewHolder {
    View parentView;
    EditText editText;
    Button btnRemove;

    //optional - use these if you want to store other values
    double price;
    int itemId;
    int quantity;
  }
}




Java Source Code List

org.droidoholics.xfinity.StartingActivity.java