How to get the value of dynamic created EditText in android? - java

I am building an android application where I am creating dynamic EdittextView. I need to display the sum of integer enter in it by the user. Below is my code to create Dynamic EdittextView:
for (int i = 1; i < ZipRunApplication.ConfigLeg; i++){
LayoutInflater inflater = null;
inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView = inflater.inflate(R.layout.drop_money, null);
TextView droxTextView = (TextView) mLinearView.findViewById(R.id.dropTextView);
final TextView position = (TextView) mLinearView.findViewById(R.id.position);
final TextView Amount = (TextView) mLinearView.findViewById(R.id.Amount);
final EditText dropEditTextView = (EditText) mLinearView.findViewById(R.id.dropEditext);
dropEditTextView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
Amount.setText(dropEditTextView.getText().toString()); //
}
});
droxTextView.setText("Amount to be pick From Drop " + String.valueOf(i));
position.setText(String.valueOf(i));
droxTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
container.addView(mLinearView);
}
Could any one help me getting the sum of the all the EdittextView created Dynamicly.

Answer for:
Could any one help me getting the sum of the all the EdittextView
created Dynamically.
You can maintain an ArrayList of EditText and then can iterate through them and get the text entered in each of them and find the sum.
As an example I have the following snippet:
LinearLayout layout;
List<EditText> concernedEditTexts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (LinearLayout)findViewById(R.id.base_layout); // Base layout defined in xml
concernedEditTexts = new ArrayList<EditText>();
// Creating five EditTexts
for(int i= 0; i< 5; i++){
EditText text = new EditText(getApplicationContext());
layout.addView(text);
concernedEditTexts.add(text); // Adding dynamically created EditText in the ArrayList
}
Button button = new Button(getApplicationContext());
button.setText("Get Sum");
layout.addView(button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int sum = 0;
// Iterate through the List and find the sum
for(EditText editText : concernedEditTexts){
sum+= Integer.parseInt(editText.getText().toString());
}
Log.d("SUM","Sum is "+sum);
}
});
}
Note: This code is kind of raw and needs a lot more validations, but should be enough to explain the concept.
In your example you will need to store every dropEditTextView in the List and then iterate through the list as shown in my example will give you the desired result.

you can use
Integer.parseInt(dropEditTextView.getText().toString())

setTag() to each EditText as i variable and take an arrarylist of size ZipRunApplication. when you write in edittext then in textchnage listener get tag value and convert to int. This value will tell you for which position in arraylist you are writing . then in that position of arraylist set Integer.parseInt(editTextString).

Four tips:
1 - Here we get the childs by the parent:
for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
if (mLinearLayout.getChildAt(i) instanceof LinearLayout) {
LinearLayout ll = (LinearLayout) mLinearLayout.getChildAt(i);
for (int j = 0; j < ll.getChildCount(); j++) {
if (ll.getChildAt(j) instanceof EditText) {
ll.getChildAt(j).setOnFocusChangeListener(this);
}
}
}
}
2 - Don't forget to parse the data you try to receive:
Integer.parseInt(mFocusedEditText.getText().toString());
3 - note:
view.setTag() <-> View.getTag()
4 - And last but not least: In terms of readability, maintainability and performance you will get to a point soon, where a ListView- or RecyclerView will fit your needs MUCH better (so keep in mind: the above coding isn't a proper solution, even if it works).
ListView and
RecyclerView

Related

Plus/Minus button with a counter but counter does not work properly

I have 2 buttons and a TextView to update the counter based on how many times the plus or minus button was pressed.
But, the issue is that: (for example) When I press the "+" button to 4 and goes down to 3 after pressing "-" button. Then, when I try to press "+"(add) button again it jumps up to 5 instead of 4. (i.e. the counter continues adding 1 from when the last time "+" button was pressed.
This is the adapter class where the ImageButtons and TextView listeners are implemented
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
//inflate layout flavor_item.xml
View view = LayoutInflater.from(context).inflate(R.layout.flavor_item, container, false);
//initialize UID views from flavor_item.xml
ImageView imageIv = view.findViewById(R.id.imageIv);
TextView flavorTv = view.findViewById(R.id.flavorTv);
TextView quantityTv = view.findViewById(R.id.quantityTv);
ImageButton minusbutton = (ImageButton) view.findViewById(R.id.minusbutton);
ImageButton plusbutton = (ImageButton) view.findViewById(R.id.plusbutton);
//getting data
DashboardFlavorModel model = modelArrayList.get(position);
String title = model.getTitle();
int image = model.getImage();
String qty = model.getQuantity();
//setting data
imageIv.setImageResource(image);
flavorTv.setText(title);
quantityTv.setText(qty);
//plusbutton listener
plusbutton.setOnClickListener(new View.OnClickListener() {
int count = Integer.parseInt(model.getQuantity());
#Override
public void onClick(View view) {
count++;
model.setQuantity(""+count);
quantityTv.setText(""+count);
}
});
//listener
minusbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int i = Integer.parseInt(model.getQuantity());
if (i > 0) {
i--;
model.setQuantity(""+i);
quantityTv.setText(""+i);
} else{
Snackbar.make(view,"Cannot have < 0 QTY",Snackbar.LENGTH_SHORT).setAction("RETRY", new View.OnClickListener() {
#Override
public void onClick(View view) {
model.setQuantity("0");
quantityTv.setText(model.getQuantity());
}[![enter image description here][1]][1]
}).show();
}
}
});
(Note***) I tried checking the counter using getter and setter to check whether it worked and it did so I have no idea why when pressing "+" after "-" it wouldn't just +1 from the value after "-" button.
try putting
int count = Integer.parseInt(model.getQuantity());
inside onClick for plusbutton onclicklistener

Getting a view's index in a LinearLayout

So I have a LinearLayout set up in my XML file, and I dynamically add a bunch of CardViews to it through code upon startup of my activity. How can I make it so that when I click on any of the CardViews, I am able to obtain its position?
I tried setting up on an onClickListener for the LinearLayout, as well as an OnClickLIstener for each individual card, but couldn't find a way to obtain the index of the card that was clicked.
I'd really like to know where to put the onClickListener, and how to obtain the position that was clicked so that I can start a new activity based on what was clicked.
Here is my code:
private LinearLayout sLinearLayout;
private CardView[] cardViews;
private Workout[] workouts;
private ViewGroup.LayoutParams params;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.saved_workouts);
Intent intent = getIntent();
Parcelable[] parcelableArrayExtra = intent.getParcelableArrayExtra(MainActivity.EXTRA_WORKOUTS);
workouts = new Workout[parcelableArrayExtra.length];
System.arraycopy(parcelableArrayExtra, 0, workouts, 0, parcelableArrayExtra.length);
sLinearLayout = findViewById(R.id.viewList);
// Set the CardView layoutParams
params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
populateCardViews();
populateLayout();
}
//TODO: this method will populate the linear layout, filling the list.
private void populateLayout() {
for (CardView cardView : cardViews) {
sLinearLayout.addView(cardView);
}
}
//TODO: this method will fill up the CardViews array with an array of workouts.
private void populateCardViews() {
cardViews = new CardView[workouts.length];
for(int i = 0; i < workouts.length; i++) {
CardView card = new CardView(this);
card.setClickable(true);
card.setLayoutParams(params);
// Set CardView corner radius
card.setRadius(9);
// Set cardView content padding
card.setContentPadding(15, 15, 15, 15);
// Set a background color for CardView
card.setCardBackgroundColor(Color.BLUE);
// Set the CardView maximum elevation
card.setMaxCardElevation(50);
// Set CardView elevation
card.setCardElevation(25);
// Initialize a new TextView to put in CardView
TextView tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("Workout WorkTime: " + workouts[i].getWorkTime());
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
tv.setTextColor(Color.WHITE);
card.setTag(workouts[i].getWorkTime());
card.addView(tv);
cardViews[i] = card;
}
}
I think you can setOnClickListener for each individual card. The index of clicked card is i
for (int i = 0; i < workouts.length; i++) {
...
final int finalI = i;
card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("TAG", "You click at card: " + finalI);
}
});
}
I not good to add so much layout programmatically. You can use a recycler view and an adapter to show a list of items. You can read this: https://developer.android.com/guide/topics/ui/layout/recyclerview
By the way, in your case: when you create a Cardview, just add onClickListener to it and the index of your Cardview is the index of it in Cardview list.
CardView cardView = new CardView(this);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//handle click here
}
});
See if this works.
cardview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int index = linearLayout.indexOfChild(v);
}
});

Get the values from GridView items

I am working on a project where I have a let's say 5x5 grid of TextViews and I want to check if an entire row or column has equal elements. I am using an Adapter class to inflate my gridview with simply one textview element. Here is the code that I have tried but I cannot seem to make it work:
final int size = gridView.getCount(); //25
int temp = 0;
for (int i = 0; i < size; i++) {
ViewGroup gridChild = (ViewGroup) gridView.getChildAt(i);
childSize = gridChild.getChildCount();
for (int j = 0; j < childSize; j++) {
if (gridChild.getChildAt(j) instanceof TextView &&
((TextView) gridChild.getChildAt(j)).getText().toString().equals("x")) {
temp++;
}
The thing is when i tried to debug, debugger showed null values for childSize variable and could not properly get the value from getChildAt. Basically, what I am trying to do is get inside the if statement. Also this is the first time I am working with ViewGroup calss, and the methods that I call. Any help would be appreciated.
Edit:I am looking for a way to do this outside the getView method in the adapter class and not in a onClick method as well. (Code sample answers would be highly appreciated). Also, the getChildAt method call returns null so the code I have shown would not work because I am assigning a null value to the gridChild.
This is the onClick that I use for the TextViews:
`
public void numberFill(View view) {
if (((TextView) view).getText().toString().isEmpty()) {
((TextView) view).setText(String.valueOf(numbCounter + 1));
numbCounter++;
}
else if (!((TextView) view).getText().toString().isEmpty() && numbCounter >= 16) {
((TextView) view).setText("x");
}
}
This is my adapter class:
public class GridAdapter extends BaseAdapter {
private final Context mContext;
private String[] numbers;
public GridAdapter(Context context, String[] numbers) {
this.mContext = context;
this.numbers = numbers;
}
#Override
public int getCount() {
return numbers.length;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public Object getItem(int position) {
return numbers[position];
//return null;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(mContext);
gridView = inflater.inflate(R.layout.textview_layout, null);
TextView textView = (TextView) gridView.findViewById(R.id.cell);
textView.setText(numbers[position]);
} else {
gridView = (View) convertView;
}
return gridView;
}
}
numberFill reworked:
public void numberFill(View view) {
int index = (Integer) view.getTag();
if (numbers[index].toString().isEmpty()) {
numbers[index] = String.valueOf(numbCounter + 1);
numbCounter++;
}
else if (!numbers[index].toString().isEmpty() && numbCounter >= 25) {
numbers[index] = "x";
}
gridAdapter.notifyDataSetChanged();
}
`
When using an AdapterView – such as your GridView – you generally don't want to directly access and manipulate its child Views outside of its Adapter. Instead, the dataset backing the Adapter should be updated, and the GridView then refreshed.
In your case, you presumably have a setup similar to this in your Activity:
private GridAdapter gridAdapter;
private String[] numbers;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
numbers = new String[25];
gridAdapter = new GridAdapter(this, numbers);
}
Here, the numbers array is what you want to directly modify, rather than the text on the GridView's child TextViews. That array is then easily iterated over to do your row and column value checks.
Since the array will be modified in the Activity, we need a way to pass the clicked TextView's position in the Adapter to the Activity's click method, as we'll need it to access the correct array element. For this, we can utilize the tag property available on all View's, via the setTag() and getTag() methods. For example, in GridAdapter's getView() method:
...
TextView textView = (TextView) gridView.findViewById(R.id.cell);
textView.setText(numbers[position]);
textView.setTag(position);
...
In the click method, the position can be easily retrieved with getTag(), and used as the index to get the clicked TextView's text from the numbers array. You can then do the necessary processing or calculation with that text, set the modified value back to the array element, and trigger a refresh on the Adapter.
public void numberFill(View view) {
int index = (Integer) view.getTag();
// Do your processing with numbers[index]
numbers[index] = "new value";
gridAdapter.notifyDataSetChanged();
}
The notifyDataSetChanged() call will cause the GridView to update its children, and your new value will be set in the appropriate TextView. The numbers array now also has the current values, and is readily available in the Activity to perform the necessary checks there.

Compare the result of two TextViews Java/Android

I'm making a simple very simple android math game. But I cant manage to compare two TextViews to let the user know if they calculated correct or not???
Im not comparing them correctly as my if/else statement is only giving me the else output??
public class PlayActivity extends AppCompatActivity {
EditText number1;
EditText number2;
TextView result;
Button addNumbers;
TextView equalW;
TextView equalL;
TextView generate;
double num1,num2,sum;
Random r = new Random();
public View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
generate = (TextView)findViewById(R.id.textViewGenerate);
int generated = r.nextInt(101);
generate.setText(Integer.toString(generated));
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
number1 = (EditText)findViewById(R.id.editTextNumber1);
number2 = (EditText)findViewById(R.id.editTextNumber2);
result = (TextView)findViewById(R.id.textViewSum);
addNumbers = (Button)findViewById(R.id.buttonAdd);
equalL = (TextView)findViewById(R.id.textViewLose);
equalW = (TextView)findViewById(R.id.textViewWin);
Button buttonGenerate = (Button)findViewById(R.id.buttonGenerate);
buttonGenerate.setOnClickListener(listener);
addNumbers.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num1 = Double.parseDouble(number1.getText().toString());
num2 = Double.parseDouble(number2.getText().toString());
sum = num1 + num2;
result.setText(Double.toString(sum));
if (generate.getText().toString().equals(result))
{
equalW.setText("Answer is correct");
}
else {
equalL.setText("lose");
}
}
});
}
You have one obvious problem and another lurking problem.
The obvious one: You have to compare a String with a String and not with a TextView. Hence replace if (generate.getText().toString().equals(result)) with if (generate.getText().toString().equals(result.getText().toString())).
The lurking one: If you see closely, sum is set as String in result and generated is set as String in generate. sum is of data type double and generate is of data type int. Comparing both will cause problem. This is like comparing "10".equals("10.0"). This is error prone. You need to set both these fields to a common data type.
Change if condition as:
if (generate.getText().toString().equals(result.getText().toString()))
{
}
Because result is view so call getText method for comparing String values.
result is textview so you have to write
if (generate.getText().toString().equals(result.getText().toString))

Getting the id from dynamic custom table, rows

I am creating a table with dynamic custom table rows. I need to get the id in the activity class.
main_Activity-->MyTableLayoutView-->MyTableRow
My Question is, how do i get the id of the cell that was clicked in the table (TextViews) to the main_Activiy.
MyTableRowView:
public void addRow(String[] data, int[] rowId) {
for (int i = 0; i < data.length; i++) {
TextView tv = parseTextView(data[i]);
tv.setId(rowId[i]);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv2 = (TextView) v;
clickedText = tv2.getText().toString();
Debug.debugMsg(clickedText + " " + tv2.getId());
}
});
this.addView(tv);
}
}
As far as I can get is getting the id from the TablerowView class, but I need it to be in Activity class, Please if anyone can enlighten me. Thanks in advance.
Activity class:
private void showTables() {
db.open();
db.importDb();// TODO
table = new TableLayoutView(this,Converter.toArrayListStringArray(db.getDbTablesForChoose()));
table.addDataListArray(Converter.toArrayListStringArray(db.getDbTablesForChoose()),true);
llChooseSQLTable.addView(table);
}
TableLayoutView:
public void addDataListArray(ArrayList<String[]> data, boolean header) {
for (int i = 0; i < ROW_NUMBERS; i++) {
TableRowView tableRow = new TableRowView(context);
tableRow.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tableRow.addRow(data.get(i),idHandler.getNextIdsRow());//idHandles passes unique id.
this.addView(tableRow);
}
}
It seems like you are able to get the ID from your code above. If your primary concern is propagating that id from the view back up to the parent class then you could do it in multiple ways. One way is to get the context of the view then cast it to the specific activity and call a function within that activity, e.g. within the onClick() operation:
public void onClick(View v) {
((MyActivity)v.getContext()).insertNotificationMethod(v.getId());
}
Where your activity is called MyActivity and you implement a method called insertNotificationmethod which takes an integer. This would work only if this view is always within this function, and even then it is a rather crude way to do it.
You could also just directly call it within the onClick() method using:
MyActivity.this.insertNotificationMethod(v.getId());
However, this way of doing things would limit this view to only be useful within this particular activity.
For a more generic way, you could BroadcastReceivers which may be a little more complicated that you would like. For more information about those you could check out the Android documentation here: http://developer.android.com/reference/android/content/BroadcastReceiver.html
Hopefully that helps.
The only solution that I can think of is to move your addRow() and addDataListArray() methods to the Activity with some modefication:
public void addRow(String[] data, int[] rowId, TableRowView tableRow) {
for (int i = 0; i < data.length; i++) {
TextView tv = parseTextView(data[i]);
tv.setId(rowId[i]);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv2 = (TextView) v;
clickedText = tv2.getText().toString();
clickedId = tv2.getId());
}
});
tableRow.addView(tv);
}
}
public void addDataListArray(ArrayList<String[]> data, boolean header, TableLayoutView table) {
for (int i = 0; i < ROW_NUMBERS; i++) {
TableRowView tableRow = new TableRowView(context);
tableRow.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addRow(data.get(i),idHandler.getNextIdsRow(), tableRow);//idHandles passes unique id.
table.addView(tableRow);
}
}
And in your Activity change this line:
//table.addDataListArray(Converter.toArrayListStringArray(db.getDbTablesForChoose()),true);
addDataListArray(Converter.toArrayListStringArray(db.getDbTablesForChoose()),true, table);
Although I don't know if other part of your app will be affected by this huge modification I made.
Good luck!

Categories

Resources