I have created layout through code which consist of LinearLayout in which I have one TextView one EditText three Buttons namely add, remove and set respectively and a TextView when I click on add button new EditText is added in layout. When I click on remove button remove the EditTextfrom layout and when I click on set button set the text from various edittext to TextView. I am unable to append the string from various EditText to TextView.
Here is my code
package com.example.rushikesh.assignment3;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView textView;
EditText edt;
Button btnAdd, btnRemove,btnSet;
LinearLayout linearLayout;
EditText editText;
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textView = new TextView(this);
textView.setLayoutParams(layoutParams);
edt = new EditText(this);
edt.setLayoutParams(layoutParams);
btnAdd = new Button(this);
btnAdd.setLayoutParams(layoutParams);
btnAdd.setText("+");
btnRemove = new Button(this);
btnRemove.setLayoutParams(layoutParams);
btnRemove.setText("-");
btnSet = new Button(this);
btnSet.setLayoutParams(layoutParams);
btnSet.setText("SET");
linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(layoutParams);
linearLayout.addView(textView);
linearLayout.addView(edt);
linearLayout.addView(btnAdd);
linearLayout.addView(btnRemove);
linearLayout.addView(btnSet);
setContentView(linearLayout);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText = new EditText(MainActivity.this);
editText.setLayoutParams(layoutParams);
linearLayout.addView(editText);
count++;
}
});
btnRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count > 0)
{
linearLayout.removeViewAt(5);
count--;
}else
{
Toast.makeText(MainActivity.this, "No edit text found", Toast.LENGTH_SHORT).show();
}
}
});
btnSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
Have a reference to all the EditTexts in a List.
List<EditText> editTexts = ...
editTexts.add(editText);
And onClick of the btnSet, get the text from the EditTexts and set it to TextView.
String fullText;
for (EditText editText in editTexts) {
fullText += editText.getText().toString();
}
textView.setText(fullText);
Related
I'm learning how to make UI programatically in Android. I'm trying to make an app with just one button, which when clicked, a toast pops up saying how many times the button has been clicked.
package com.example.android.dynamicui;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private int mCount=0;
float den = getResources().getDisplayMetrics().density;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Linear Layout
LinearLayout mLayout = new LinearLayout(this);
mLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
mLayout.setOrientation(LinearLayout.VERTICAL);
mLayout.setGravity(Gravity.CENTER);
//Button
AppCompatButton mButton = new AppCompatButton(this);
mButton.setText("Click me!");
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(0,(int)(15*den + 0.5f),0,0);
mButton.setLayoutParams(buttonParams);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCount++;
Toast.makeText(getBaseContext(), "You pressed button for "+mCount+" times!" , Toast.LENGTH_SHORT ).show();
}
});
//Adding button to Linear Layout
mLayout.addView(mButton,buttonParams);
setContentView(mLayout);
}
}
Please set the on click on button after the layout has been set i.e.:-
public class MainActivity extends AppCompatActivity {
private int mCount=0;
float den = getResources().getDisplayMetrics().density;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Linear Layout
LinearLayout mLayout = new LinearLayout(this);
mLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
mLayout.setOrientation(LinearLayout.VERTICAL);
mLayout.setGravity(Gravity.CENTER);
//Button
AppCompatButton mButton = new AppCompatButton(this);
mButton.setText("Click me!");
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(0,(int)(15*den + 0.5f),0,0);
mButton.setLayoutParams(buttonParams);
//Adding button to Linear Layout
mLayout.addView(mButton,buttonParams);
setContentView(mLayout);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCount++;
Toast.makeText(getBaseContext(), "You pressed button for "+mCount+" times!" , Toast.LENGTH_SHORT ).show();
}
});
}
}
It was crashing because I was using getResources() method, before onCreate(), which passed a null value to the 'den' variable.
i've created an android application which creates 50 button dynamically,which works perfectly, but the problem is when i press one button which is defined statically which results in changing the button (text named as 5) background color to yellow.
can anyone please tell me some solution for this
my code is as given below
my Android Platform is 2.3.3
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyMain extends Activity {
Button change;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
change= (Button) findViewById(R.id.change);
setContentView(R.layout.mymain);
createCalender();
change.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// what to write here in order to change the color of button titled as 5 to yellow as its background color
}
});
}
public void createCalender()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
param.setMargins(10, 10, 10, 10);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[10][5];
int count=51;
int tab=1;
for (int i = 0; i<10; i++)
{
if(count%5==1)
{
rowLayout = new LinearLayout(this);
rowLayout.setBackgroundColor(Color.BLACK);
rowLayout.setWeightSum(5);
layoutVertical.addView(rowLayout,param);
count=count-5;
}
for(int j=0;j<5;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setText(""+tab);
buttons[i][j].setHeight(55);
buttons[i][j].setWidth(80);
buttons[i][j].setTextColor(Color.BLACK);
buttons[i][j].setBackgroundColor(Color.GREEN);
tab++;
rowLayout.addView(buttons[i][j],param);
}
}
}
}
Try something like:
if("5".equals(v.getText())){
v.setBackgroundColor(Color.YELLOW);
}
i've created an android application which creates 50 button dynamically,which works perfectly, but the problem is when i put some background color for these buttons dynamically the press effect of the buttons is being lost
can anyone please tell me some solution for retaining the press effect of the button click
my code is as given below
my Android Platform is 2.3.3
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyMain extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mymain);
createCalender();
}
public void createCalender()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
param.setMargins(10, 10, 10, 10);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[10][5];
int count=51;
int tab=1;
for (int i = 0; i<10; i++)
{
if(count%5==1)
{
rowLayout = new LinearLayout(this);
rowLayout.setBackgroundColor(Color.BLACK);
rowLayout.setWeightSum(5);
layoutVertical.addView(rowLayout,param);
count=count-5;
}
for(int j=0;j<5;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setText(""+tab);
buttons[i][j].setHeight(55);
buttons[i][j].setWidth(80);
buttons[i][j].setTextColor(Color.BLACK);
buttons[i][j].setBackgroundColor(Color.GREEN);
tab++;
rowLayout.addView(buttons[i][j],param);
}
}
}
}
After buttons[i][j].setBackgroundColor(Color.GREEN);
buttons[i][j].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundColor(Color.GRAY); // Choose whichever color
new Handler().postDelayed(new Runnable() {
public void run() {
v.setBackgroundColor(Color.GREEN);
// Button Click Code Here
}
}, 100L); // Change this value to whatever is suitable
}
});
I know this is easy but i m just not able to debug it.
I am dynamically creating 3 LinearLayouts and adding them to a scroll View . I am adding a Button "ins" to the top of 3 linearlayout.
The problem is that on clicking the ins Button , its on clicklistener is being called 3 times .
code:
package com.integrated.mpr;
import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class Page1 extends Activity implements OnClickListener{
static int pos = new Choose().n;
static String partname;
int i;
int[][] id = new int[pos][5];
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int p =0;
for(int i =0;i<3;i++){
for(int j =0;j<5;j++){
p++;
}
}
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
int resid = getResources().getIdentifier("background", "drawable", getPackageName());
ll.setBackgroundResource(resid);
Button ins = new Button(this);
ins.setText("Instructions");
ins.setOnClickListener(this);
ins.setId(5000);
ll.addView(ins);
for(i =0;i<3;i++){
LinearLayout llay = new LinearLayout(this);
llay.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText("enter the " +(i+1)+" position name");
EditText et = new EditText(this);
et.setId(id[i][0]);
Button starta = new Button(this);
starta.setText("Record 1");
starta.setId(id[i][1]);
starta.setOnClickListener(this);
Button startb = new Button(this);
startb.setText("Record 2");
startb.setId(id[i][2]);
startb.setOnClickListener(this);
Button startc = new Button(this);
startc.setText("Record 3");
startc.setId(id[i][3]);
startc.setOnClickListener(this);
Button stop = new Button(this);
stop.setText("Submit");
stop.setId(id[i][4]);
stop.setOnClickListener(this);
TextView tv1 = new TextView(this);
tv1.setVisibility(llay.INVISIBLE);
llay.addView(tv);
llay.addView(et);
llay.addView(starta);
llay.addView(startb);
llay.addView(startc);
llay.addView(stop);
llay.addView(tv1);
ll.addView(llay);
}
Button bcon = new Button(this);
bcon.setText("Continue");
bcon.setId(10000);
bcon.setOnClickListener(this);
ll.addView(bcon);
sv.addView(ll);
this.setContentView(sv);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==5000){
Log.d("ins", "called");
Context mContext = Page1.this;
Dialog dialog = new Dialog(mContext);
dialog.setTitle("Instructions");
dialog.setContentView(R.layout.instructiondialog);
dialog.show();
}
}
}
}
I know definitely , there is some problem in the code of OnCreate but what it is ? Help please
Can anyone further explain how to set the button aligned o the right of screen , that is how to set its layout gravity?
I am not sure about what exactly you want to achieve.but you can set the flag.if flag value is 1 then only process onClick else not.
Below snippet will help you.
Yes but you will have to somehow reset that flag to 1.
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(v.getId()==5000)
{
if(flag)
{
Log.d("ins", "called");
Context mContext = Page1.this;
Dialog dialog = new Dialog(mContext);
dialog.setTitle("Instructions");
dialog.setContentView(R.layout.instructiondialog);
dialog.show();
}
flag=0;
}
}
You can also call button click event like this
Button ins = new Button(this);
ins.setText("Instructions");
ins.setOnClickListener(this);
ins.setId(5000);
ins.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
Context mContext = Page1.this;
Dialog dialog = new Dialog(mContext);
dialog.setTitle("Instructions");
dialog.setContentView(R.layout.instructiondialog);
dialog.show();
}
});
In main.xml I made a row containing a TextView, an EditText and a "+" and "-" button.
Underneath that I made an "Add" button that will help you create a new row When you click the add button, you get an EditText and a Submit and Cancel button.
On "Submit" it outputs the EditText value to the TextView and creates the same row as the first one.
The numeric value "NewValueBox" should +1 when the "+" button is pressed.
But because I call it in another function it is not recognized by createNewAddButton() function in which the button is set up.
So in short:
"How do I change the value of NewValueBox when I click NewAddButton?"
Here's the code:
package com.lars.MyApp;
import com.google.ads.*;
import com.lars.MyApp.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
public class DrinkRecOrderActivity extends Activity {
int currentValue1 = 0;
int currentValueNew = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText firstValue = (EditText) findViewById(R.id.firstValue);
Button valuePlus = (Button) findViewById(R.id.valuePlus);
Button valueMinus = (Button) findViewById(R.id.valueMinus);
final Button addValue = (Button) findViewById(R.id.add);
final TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
final LinearLayout addValueRow = (LinearLayout) findViewById(R.id.addValueRow);
final EditText addNewValue = (EditText) findViewById(R.id.addNewValue);
final Button submitNewValue = (Button) findViewById(R.id.submitNewValue);
final Button cancelNewValue = (Button) findViewById(R.id.cancelNewValue);
// BEGIN ONCLICKLISTENERS
valuePlus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
plusValue();
firstValue.setText("" + currentValue1);
}
});
valueMinus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
minValue();
firstValue.setText("" + currentValue1);
}
});
addValue.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addValueRow.setVisibility(View.VISIBLE);
addValue.setVisibility(View.GONE);
}
});
cancelNewValue.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addValueRow.setVisibility(View.GONE);
addValue.setVisibility(View.VISIBLE);
}
});
submitNewValue.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tableLayout1.addView(createnewRow());
addValueRow.setVisibility(View.GONE);
addValue.setVisibility(View.VISIBLE);
addNewValue.setText("");
}
});
// END ONCLICKLISTENERS
// Look up the AdView as a resource and load a request.
AdView adView = (AdView) this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
}
public TableRow createNewRow() {
final TableRow newRow = new TableRow(this);
final EditText addNewValue = (EditText) findViewById(R.id.addNewValue);
newRow.addView(createNewTextView(addNewValue.getText().toString()));
newRow.addView(createNewValueBox());
newRow.addView(createNewAddButton());
newRow.addView(createNewMinusButton());
return newRow;
}
public TextView createNewTextView(String text) {
final TextView textView = new TextView(this);
textView.setText(text);
return textView;
}
public EditText createNewValueBox() {
EditText NewValueBox = new EditText(this);
NewValueBox.setHint("0");
NewValueBox.setInputType(InputType.TYPE_CLASS_NUMBER);
return NewValueBox;
}
public Button createNewAddButton() {
final Button NewAddButton = new Button(this);
NewAddButton.setText("+");
NewAddButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
plusNew();
//NewValueBox.setText("" + currentValueNew);
}
});
return NewAddButton;
}
public Button createNewMinusButton() {
final Button NewMinusButton = new Button(this);
NewMinusButton.setText("-");
return NewMinusButton;
}
// BEGIN PLUS AND MIN FUNCTIONS
public void plusNew() {
if (currentValueNew <= 999) {
currentValueNew = currentValueNew + 1;
}
}
public void plusValue() {
if (currentValue1 <= 999) {
currentValue1 = currentValue1 + 1;
}
}
public void minValue() {
if (currentValue1 >= 1) {
currentValue1 = currentValue1 - 1;
}
}
// END PLUS AND MIN FUNCTIONS
}
Add IDs for your Views so you can later reference them. Make 3 private static int field in your activity(the ID for NewValueBox, NewAddButton and NewMinusButton):
private static int edt = 1;
private static int add = 1001;
private static int minus = 2001;
Then in your createNewValueBox() method set the ID:
NewValueBox.setId(edt);
edt++;
Do the same for the NewAddButton and the NewMinusButton:
NewAddButton.setId(add);
add++;
NewMinusButton.setId(minus);
minus++;
Then in your listener for the buttons find out exactly which add button has been clicked and set the text in the corresponding EditText:
NewAddButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
plusNew();
int tmp = v.getId();
EditText temp = (EditText) findViewById(1 + (tmp - 1001));
temp.setText("" + currentValueNew);
}
Kind of hackish method.