Code example for TableRow

Methods: addView, setId, setLayoutParams

0
 
		// Get the TableLayout 
		TableLayout tl = (TableLayout) findViewById(R.id.maintable);
 
		// Create a TableRow and give it an ID 
		TableRow tr = new TableRow(this);
		tr.setId(tablerowid);
		tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
 
		// create our delete button 
		Button delButton = new Button(this);
		delButton.setId(delbuttonId);
		delButton.setText("X");
		delButton.setTextColor(Color.BLACK);
		delButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		tr.addView(delButton);
 
		OnClickListener DeleteButtonListner = new OnClickListener() {
			public void onClick(View v) {
				String name = "put name here";
				yesnoDeleteHandler("Are you sure?", 
						"Are you sure you want to delete?", v.getId() + 4000);
			} 
		}; 
 
		delButton.setOnClickListener(DeleteButtonListner);
 
		// create our minus button 
		Button minusButton = new Button(this);
		minusButton.setId(minusbuttonid);
		minusButton.setText("-");
		minusButton.setTextColor(Color.BLACK);
		minusButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		tr.addView(minusButton);
 
		OnClickListener minusButtonListner = new OnClickListener() {
			public void onClick(View v) {
				EditText numtxt = (EditText) findViewById(v.getId() + 2000);
				float newval = Float.valueOf(numtxt.getText().toString()) - 1;
				mutex = 0;
				numtxt.setText(String.valueOf(newval));
 
				EditText nametxt = (EditText) findViewById(v.getId() + 3000);
 
				updateDatabaseValue(nametxt.getText().toString().trim(), newval);
			} 
		}; 
 
		minusButton.setOnClickListener(minusButtonListner);
 
		// create our editview numerical 
		// 
		EditText numericalEdit = new EditText(this);
		numericalEdit.setId(edittextid);
		numericalEdit.setText(String.valueOf(val));
		numericalEdit.setTextColor(Color.BLACK);
		numericalEdit.setInputType(InputType.TYPE_CLASS_NUMBER);
		numericalEdit.setLayoutParams(new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tr.addView(numericalEdit);
 
		// numerical edit listener 
		// name listener 
		numericalEdit.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable s) {
				if(mutex == 0)
				{ 
					mutex = 33;
					return; 
				} 
				// here we find the old name 
				EditText myNumEdit = (EditText) getCurrentFocus();
				int myNumEditId = myNumEdit.getId();
				EditText myNameEdit = (EditText) findViewById(myNumEditId + 1000);
				String name = myNameEdit.getText().toString().trim();
				// here we call the text changed update sql function 
				String floatString = s.toString();
				float value;
				try { 
					value = Float.valueOf(textChangedListenerText);
				} catch (Exception e) {
				} 
 
				try { 
					value = Float.valueOf(floatString); 
				} catch (Exception e) {
					myNumEdit.setText(textChangedListenerText);
					return; 
				} 
				updateDatabaseValue(name, value);
			} 
 
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				textChangedListenerText = String.valueOf(s);
 
			} 
 
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			} 
		}); 
 
		// create our plus button 
		Button plusButton = new Button(this);
		plusButton.setId(plusbuttotid);
		plusButton.setText("+");
		plusButton.setTextColor(Color.BLACK);
		plusButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		tr.addView(plusButton);
 
		OnClickListener plusButtonListner = new OnClickListener() {
			public void onClick(View v) {
				EditText numtxt = (EditText) findViewById(v.getId() + 1000);
				mutex = 0;
				float newval = Float.valueOf(numtxt.getText().toString()) + 1;
				numtxt.setText(String.valueOf(newval));
 
				EditText nametxt = (EditText) findViewById(v.getId() + 2000);
 
				updateDatabaseValue(nametxt.getText().toString().trim(), newval);
			} 
		}; 
 
		plusButton.setOnClickListener(plusButtonListner);
 
		// create our editview name 
		// 
		EditText nameEdit = new EditText(this);
		nameEdit.setId(nameid);
		nameEdit.setText(name);
		nameEdit.setTextColor(Color.BLACK);
		nameEdit.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		tr.addView(nameEdit);
 
		// name listener 
		nameEdit.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable s) {
				// here we call the text changed update sql function 
Experience pair programming with AI  Get Codota for Java