I have 80 EditText fields (cube[i]) and want to read what is in inside the text fields when the text field loses focus.
I can detect when any of the EditTexts (cube) loses focus but I cannot detect exactly which one, Im trying to find which cube is focused on.
the line "EditText cube = (EditText) v.getClass();" is giving me an error
Maybe I can use the View v?
for (int i = 0; i < cube.length; i++) {
cube[i].setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
EditText cube = (EditText) v.getClass();
String s = cube.getText().toString();
//cubecolor();
}
}
});
}
}
Any help is appreciated.
while creating set some tag to the editText like this (pseudo code)
EditText edit = new EditText(context);
edit.setTag(Integer.valueOf(i)); // i is within the for loop;
Now during the onFocus get the tag
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
EditText cube = (EditText) v;
Integer tag = (Integer)cube.getTag();
//code to sort out which cube based on tag
String s = cube.getText().toString();
//cubecolor();
}
}
This worked, Thanks Dante.
for (int i = 0; i < cube.length; i++) {
cube[i].setTag(Integer.valueOf(i)); // give cubes tags
}
for (int i = 0; i < cube.length; i++) {
cube[i].setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
Integer tag = (Integer) v.getTag();
String s = cube[tag].getText().toString();
Log(TAG, " Content" + s);
revert_cubecolor(tag);
}
}
});
}
Related
Hello I'm a student and My project is making a application by android studio.
I'm just a beginner so everything is difficult :(
Here is my problem.
I dynamically created checkbox depending on the current.
I want to get finalMemberTag if user click the btnEx button.
But In this code, When I click the btnEx, finalMemberTag is null.
And I want that If user click the checkbox, add the checkbox's text to the finalMemberTag.
and checkbox is unchecked, remove the checkbox's text to the finalMemberTag.
How can I do these?
(I already set the drawable things.)
Please Help me...
private String[] finalMemberTag;
Button btnEx;
btnEx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendData();
}
});
Switch(current){
case "A":
String[] memberA = {"all", "ab", "cd", "ef", "g", "hu", "hi", "dd"};
for (int i = 0; i < 8; i++) {
CheckBox chA = new CheckBox(getApplicationContext());
chA.setText(membersA[i]);
chA.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
chA.setTextColor(Color.parseColor("#8D8D8D"));
cHA.setButtonDrawable(android.R.color.transparent);
chA.setBackground(inactiveCb);
int index = i;
chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
chA.setBackground(activeCb);
chA.setTextColor(startextColor);
finalMemberTag[index] = buttonView.getText().toString();
}
if(!isChecked) {
chA.setBackground(inactiveCb);
chA.setTextColor(Color.parseColor("#8D8D8D")); }
}
});
chA.setPadding(36, 24, 36, 24);
chA.setLayoutParams(params);
placeInputMember.addView(chA);
}
break;
case "B":
String[] memberB = {"all", "abc", "Dcd", "e3f", "DSg", "DGu", "hE", "dV"};
for (int i = 0; i < 8; i++) {
CheckBox chB = new CheckBox(getApplicationContext());
chB.setText(memberB[i]);
chB.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
chB.setTextColor(Color.parseColor("#8D8D8D"));
chB.setButtonDrawable(android.R.color.transparent);
chB.setBackground(inactiveCb);
int index = i;
chB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
chB.setBackground(activeCb);
chB.setTextColor(startextColor);
finalMemberTag[index] = buttonView.getText().toString();
}
if(!isChecked) {
chB.setBackground(inactiveCb);
chB.setTextColor(Color.parseColor("#8D8D8D")); }
}
});
chB.setPadding(36, 24, 36, 24);
chB.setLayoutParams(params);
placeInputMember.addView(chB);
}
break;
}
public void sendData(){
Log.d("##", String.valueOf(finalMemberTag));
}
ANOTHER ERROR
String[] membersA = {"all", "ji", "ha", "bo", "re", "co", "zo", "pi"};
arrayMember = new ArrayList<>();
for (int i = 0; i < membersBts.length; i++) {
CheckBox chA = new CheckBox(getApplicationContext());
chA .setText(members[i]);
chA.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
chA.setTextColor(Color.parseColor("#8D8D8D"));
chA .setButtonDrawable(android.R.color.transparent);
chA.setBackground(inactiveCb);
int index = i;
chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
chA.setBackground(activeCb);
chA.setTextColor(startextColor);
arrayMember.add(index, buttonView.getText().toString());
}
if(!isChecked) {
chA.setBackground(inactiveCb);
chA.setTextColor(Color.parseColor("#8D8D8D"));
arrayMember.remove(index);
}
}
});
chA.setPadding(36, 24, 36, 24);
chA.setLayoutParams(params);
placeInputMember.addView(chA);
}
break;
ERROR LOGCAT
java.lang.IndexOutOfBoundsException: Index: 5, Size: 2
Ok, you have some issues with your code:
First, finalMemberTag is always null because you never initialized it. If you want to use a string array and you want to store the text on the checkboxes then we should assume that at most you'll have all of the checkboxes (16) selected. This means you'll want to initialize your array as:
private String[] finalMemberTag = new String[16]
Second, I think you'd be better off using a string set to avoid duplicates, or, if you need the information on a particular index, an arraylist.
With the Set:
private Set<String> finalMembersTag = new HashSet<>(16);
//onCreate...other stuff...for loop
//...setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final String buttonText = buttonView.getText().toString();
if (isChecked) {
// other stuff ...
finalMembersTag.add(buttonText);
} else {
// other stuff ...
finalMembersTag.remove(buttonText);
}
}
With the ArrayList:
private List<String> finalMembersTag = new ArrayList<>(16);
//onCreate...other stuff...for loop
int index = i;
chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final String buttonText = buttonView.getText().toString();
if (isChecked) {
// other stuff ...
finalMembersTag.add(buttonText); //this will add the text
} else {
// other stuff ...
finalMembersTag.remove(buttonText); //this will remove the element if found, or if you want an empty string you can do finalMembersTag.add(index, "");
}
}
I have created a programmatically TextView in for loop,on textView touch i have set border line on each textview.
I want to remove all textView border line on single button click.
But it remove only last created textView border Line.
Here is my code
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
final String text = c.getString(Constants.CARD_TEXT);
final AppCompatTextView textView = new AppCompatTextView(Card.this);
textView.setId(i);
textView.setText(text);
textView.setY((int) (hgt * yaxis / 100));
final int finalI1 = i;
txtNext.setOnClickListener(new View.OnClickListener() {
#SuppressLint("ResourceType")
#Override
onClick(View v) {
for (int j = 0; j <= finalI1; j++) {
Log.e("Clicked1221", String.valueOf(textView.getId() - j));
if (textView.getId() - j == j) {
;
}
textView.setBackgroundResource(0);
}
}
}
});
final int finalI = i;
textView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textView.setSelected(true);
if (gestureDetector.onTouchEvent(event)) {
if (textView.isSelected()) {
textView.setBackgroundResource(R.drawable.doted);
}
return true;
}
imgBackground.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textView.setBackgroundResource(0);
textView.setSelected(false);
return false;
}
});
return true;
}
});
layoutImage.addView(textView);
You are calling textView.setBackgroundResource(R.drawable.doted); on only one text view, if you want to change background of all created textviews, you have to store reference of each created textview somewhere, create a list of textviews, and add each new text view to that list and in onTouchListener do something like this
for(textView:textViews){
textView.setBackgroundResource(0);
textView.setSelected(false);
}
I have created Adapter to store Cart Items with there dynamic quantities.
Adding custom views which Contains Size, No. of Print, Price. In that I have to take No. of Print in EditText from User Input.
onBindViewHolder() of Adapter: In that I have added common TextWatcher for every EditText which is separated by ID passed in Constructor.
/*
* Add Custom View For Size and Price
* */
if (cart.getCartPhotoPrintSize() != null) {
try {
data = cart.getCartPhotoPrintSize().get(position);
if (data != null) {
// Remove the custom view
holder.cartPhotoSizrPriceCustome.removeAllViews();
holder.cartPhotoSizrPriceCustome.setVisibility(View.VISIBLE);
// Local Variable for Storing the size of StudioData Size
int size = cart.getCartPhotoPrintSize().size();
// Loop for set the custom layout
for (int i = 0; i < size; i++) {
// inner Loacl Variable
Studio.StudioSize studioSize = cart.getCartPhotoPrintSize().get(i);
// Layout Inflater For Targeting The RootView Of Context
LayoutInflater inflater = LayoutInflater.from(mContext);
View sizePriceView = inflater.inflate(R.layout.size_price_quantity_layout, null, false);
// Binding the id of TextView
studioSizeTextView = (TextView) sizePriceView.findViewById(R.id.studio_photo_album_size);
studioPriceTextView = (TextView) sizePriceView.findViewById(R.id.studio_photo_album_price);
photoQuantity = (EditText) sizePriceView.findViewById(R.id.photo_quantity);
studioSizeTextView.setId(i);
studioSizeTextView.setText(studioSize.getSize());
studioPriceTextView.setText("" + studioSize.getPrice());
holder.cartPhotoSizrPriceCustome.setTag(cart.getId());
holder.cartPhotoSizrPriceCustome.addView(sizePriceView);
//Add TextWacher For Every EditTextView
photoQuantity.addTextChangedListener(new GeneralTextWatcher(holder, i));
}
}
} catch (Exception e) {
Log.w("TAG", e.getMessage());
}
}
GeneralTextWatcher
private class GeneralTextWatcher implements TextWatcher {
ViewHolder viewHolder;
int id;
public GeneralTextWatcher(ViewHolder viewHolder, int i) {
this.viewHolder = viewHolder;
this.id = i;
Log.e(TAG, "Constructor Calling..");
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
totalAmount = 0;
int child = viewHolder.cartPhotoSizrPriceCustome.getChildCount();
int pos = viewHolder.getAdapterPosition();
Log.i("size position : ", "" + id);
Log.i("cart item position : ", "" + pos);
Cart tCart = tempValue.get(pos);
Studio.StudioSize ssTemp = tCart.getCartPhotoPrintSize().get(id);
ssTemp.setQuantity(!TextUtils.isEmpty(s) ? Integer.parseInt(s.toString()) : 0);
for (int j = 0; j < child; j++) {
View view = viewHolder.cartPhotoSizrPriceCustome.getChildAt(j);
EditText editText = (EditText) view.findViewById(R.id.photo_quantity);
TextView textView = (TextView) view.findViewById(R.id.studio_photo_album_price);
Double quantity = 0.0;
if (!editText.getText().toString().equals(""))
quantity = Double.parseDouble(editText.getText().toString());
Double price = Double.parseDouble(textView.getText().toString());
if (printAmount != 0)
tempTotal = printAmount;
printAmount = quantity * price;
totalAmount += printAmount;
}
tempValue.get(pos).setTotal(totalAmount);
((CartActivity) mContext).mValues = tempValue;
Log.e("Tag", "After tmpStr " + new Gson().toJson(
tempValue,
new TypeToken<ArrayList<Cart>>() {
}.getType()));
((CartActivity) mContext).findTotalAmount(tempValue);
}
#Override
public void afterTextChanged(Editable s) {
}
}
When I Change First Cart Item with all three EditText values and Saving new JSON is like Click to see JSON after 3rd EditText filled up Its Working Fine,
But
After changing Second Cart Item fourth EditText Value and Saving JSON like as Click to see after changing 4th EditText in Second Cart Its replacing old Values.
I am getting this problem since last two days. Any help would be appreciated.
Thank you.
hey how can i know that the below checkbox when clicked is from which row of the list.
is there any way of knowing that. Every time yo is returning false which is its default value.
checkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
callBlockOptions callBlockOptions = (callBlockOptions) cb.getTag();
callBlockOptions.setChecked( cb.isChecked() );
String yo;
if(callBlockOptions.getPosition()=="0")
{
callBlockOptions.setAllCalls();
}
if(callBlockOptions.getAllCalls() ==true)
yo="true";
else
yo="false";
Toast.makeText(getContext(), callBlockOptions.getPosition(), Toast.LENGTH_LONG).show();
}
});
}
After seeing your code, one thing you might want to try is setting listeners for the individual children within the ListView. You could do something like this:
ListView listView = (ListView)findViewById(R.id.listView);
int count = listView.getChildCount();
for (int x = 0; x < count; x++)
{
Class<? extends View> c = listView.getChildAt(x).getClass();
if (c == CheckBox.class)
{
CheckBox checkBox = (CheckBox)(listView.getChildAt(x));
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton compoutButton, boolean isChecked) {
// TODO Auto-generated method stub
callBlockOptions.isChecked = isChecked;
}
});
}
else if (c == TextView.class)
{
TextView textView = (TextView)(listView.getChildAt(x));
textView.setOnEditorActionListener(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView tView, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
callBlockOptions.name = tView.getText().toString();
return true;
}
});
}
You probably want your other class to be like this:
public class callBlockOptions {
public static String name;
public static Boolean isChecked;
}
Then you can get and set name and isChecked like this:
callBlockOptions.isChecked = false;
Boolean boolVal = callBlockOptions.isChecked;
public void changeCurrency(RelativeLayout layout) {
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
Class c = v.getClass();
if (c == EditText.class) {
// validate EditClass
} else if (c == TextView.class) {
//validate RadioButton
}
}
}
In the above code I'm trying to iterate through gui elements in a layout and validate their contents. I'm struggling at the commented parts.
I.e. getting access to the EditText's text value..
I can't figure out how to cast the c object to a EditText to check the values.
Ideas?
Try using the logic below
View v = layout.getChildAt(i);
if (v instanceof EditText) {
EditText et = (EditText) v;
//Do stuff
} else if (v instanceof TextView) {
//Do other stuff
}
Since EditText is a subclass of TextView, you need to check for EditText first. An EditText will test positive as an instance of TextView.
Most of the views that use text extend from TextView. This should be sufficient if all you are doing is validating text.
public void changeCurrency(RelativeLayout layout) {
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof TextView) {
TextView t = (TextView) v;
String text = t.getText().toString();
// ...
}
}
}