Can someone please explain why the first piece of code here works but not the last piece? The only difference is the index of which i insert my view, i-1 and i+1. Is i+1 just not possible with index? I can write any other number in there and it works.
upButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < drawerViewGroup3.getChildCount(); i++) {
View view = drawerViewGroup3.getChildAt(i);
TextView tv = (TextView) view.findViewById(R.id.drawer_name);
if (tv.getText().toString().equals(drawerName.getText().toString()) && i != 0) {
drawerViewGroup3.removeView(view);
drawerViewGroup3.addView(view, i - 1);
}
}
}
});
downButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < drawerViewGroup3.getChildCount(); i++) {
View view = drawerViewGroup3.getChildAt(i);
TextView tv = (TextView) view.findViewById(R.id.drawer_name);
if (tv.getText().toString().equals(drawerName.getText().toString())) {
drawerViewGroup3.removeView(view);
drawerViewGroup3.addView(view, i + 1);
}
}
}
});
Some context on the app. I have a vertical oriented LinearLayout with multiple LinearLayouts inside. When clicking the two Buttons in the code, one of the children is to move up or down, switching their positions.
EDIT: Ok so I figured it out.
downButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View viewToMove = null;
int viewToMovePos = drawerViewGroup3.getChildCount();
for (int i = 0; i < drawerViewGroup3.getChildCount(); i++) {
View view = drawerViewGroup3.getChildAt(i);
TextView tv = (TextView) view.findViewById(R.id.drawer_name);
if (tv.getText().toString().equals(drawerName.getText().toString()) && i != drawerViewGroup3.getChildCount() - 1) {
viewToMovePos = i;
viewToMove = view;
}
}
alert.dismiss();
if (viewToMovePos != drawerViewGroup3.getChildCount()) {
drawerViewGroup3.removeView(viewToMove);
drawerViewGroup3.addView(viewToMove, viewToMovePos + 1);
}
}
});
Not the prettiest code and probably not gonna help others as it's very specific, but that was the answer.
When you remove a view from your LinearLayout, the view at index i is now the view which was after the one which you removed. Then you add a view at i - 1 which is before this view (the one which was previously after the removed view). The final result is that you remove a view and insert back where it used to be.
Instead, you need to add the view at i - 2.
I suggest that you look at RecyclerView. It is specifically designed to efficiently create a dynamic list of views for given data. You only have to manipulate the data and RecyclerView does all the hard work for you.
I have a screen that add dynamically a row with two textView and one CheckBox. My problem is that I only want that the user could check one, and the others have to be uncheck. If the user push another checkbox, enable this and disable all the other checkbox. I've test some things but I don't get the functionality that I want.
I can't change the configuration from checkbox to radioButton and I have to use the method that I'm using:
private selected_position = -1;
protected void addGreetingToListView(final GreetingListJson greetings) {
View row = inflater.inflate(R.layout.main_greetings_settings_row, greetingListView, false);
row.setTag(greetings);
playButton = (ToggleButton) row.findViewById(R.id.detail_play_greeting);
TextView greetingName = (TextView)row.findViewById(R.id.greetingTitle);
greetingName.setDuplicateParentStateEnabled(true);
TextView greetingDescription = (TextView)row.findViewById(R.id.greetingDescription);
greetingDescription.setDuplicateParentStateEnabled(true);
checkBox = (CheckBox) row.findViewById(R.id.checkBox_greeting_list);
checkBox.setTag(greetings);
checkBox.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View view) {
if (((CheckBox) view).isChecked())
{
selected_position= greetingListView.getChildCount();
}
else
{
selected_position=-1;
}
}
});
greetingName.setText(greetings.Name);
greetingDescription.setText(greetings.Description);
row.setOnClickListener(this);
row.setOnLongClickListener(this);
row.setFocusable(true);
greetingListView.addView(row);
}
The XML of the Checkbox:
<CheckBox
android:id="#+id/checkBox_greeting_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_centerVertical="true"/>
Firstly, declare an ArrayList in the class:
ArrayList<CheckBox> mCheckBoxes = new ArrayList<CheckBox>();
Then in addGreetingToListView add every new checkbox to mCheckBoxes and modify the click listener of the checkbox:
checkBox.setTag(greetings);
mCheckBoxes.add(checkBox);
checkBox.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View view) {
if (((CheckBox) view).isChecked())
{
for (int i = 0; i < mCheckBoxes.size(); i++) {
if (mCheckBoxes.get(i) == view)
selected_position = i;
else
mCheckBoxes.get(i).setChecked(false);
}
}
else
{
selected_position=-1;
}
}
});
I have a TableLayout with TableRows and CheckBox and TextView inside. When I push a CheckBox I need to know in wich TableRow the CheckBox is placed to get the TextView "associated" to this CheckBox. For example:
TableLayout:
TableRow 1: X TextView1
TableRow 2: X TextView2
...
I'm trying something like this:
cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (cb.isChecked())
{
int id_cb = cb.getId();
boolean encontrado = false;
for (int i = 0; i < tabla_tareas.getChildCount() && !encontrado; i++)
{
TableRow aux_tr = (TableRow) tabla_tareas.getChildAt(i);
CheckBox aux_cb = (CheckBox) aux_tr.getChildAt(0);
if (id_cb == aux_cb.getId())
encontrado = true;
}...
But it doesn't work.
Utilizing what Elior has said, why don't you just use the folling methods to achieve what you want:
cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (cb.isChecked())
{
cb.getText().toString(); //This will retrieve the text associated with the Checkbox
cb.setText("Your new string here"); //This will set the text associated with the CheckBox
}...
With the above two methods you should be able to do everything you need to without the need for the TextView, and probably without the TableLayout as well since I am assuming you were using that to align the CheckBoxes and the TextViews.
How can I fold all ChildView Group in Expandable List. Now I have two buttons to control Expand All and Fold All on Activity. Expandable button is OK and it is working work but I don't know how to fold all ChildView Group. How can i do this ?
Thanks
**expandableListView.expandGroup(position, true); = OK<Br>
expandableListView.foldGroup(position) = ? or ?**
try this,
int count = (new ExpAdapter(this)).getGroupCount();
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < count; i++)
{
expList.collapseGroup(i);
}
}
});
How do I clear all the EditText fields in a layout with a Clear Button. I have a registration Activity that has about 10 different EditTexts. I know I could go and grab a reference to each specifically and then set.Text(""); But I am looking for a more dynamic elegant way. Possibly grab the Layout and loop through all the items in there looking for EditText types and then setting those to "". Not sure how to do that though and tried searching on the web for it but no luck. Any sample code?
The answer by #Pixie is great but I would like to make it much better.
This method works fine only if all the EditText are in a single(one) layout but when there are bunch of nested layouts this code doesn't deal with them.
After scratching my head a while I've made following solution:
private void clearForm(ViewGroup group) {
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
if(view instanceof ViewGroup && (((ViewGroup)view).getChildCount() > 0))
clearForm((ViewGroup)view);
}
}
To use this method just call this in following fashion:
clearForm((ViewGroup) findViewById(R.id.sign_up));
Where you can replace your R.id.sign_up with the id of root layout of your XML file.
I hope this would help many people as like me.
:)
You can iterate through all children in a view group and clear all the EditText fields:
ViewGroup group = (ViewGroup)findViewById(R.id.your_group);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
}
Use editText.getText().clear();
after onclick of any action do below step
((EditText) findViewById(R.id.yoursXmlId)).setText("");
or Clear all EditText fields by iterating all childrens:
ViewGroup group = (ViewGroup)findViewById(R.id.your_group);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
}
or use simple below step :
editText.getText().clear();
Might be helpful.
It's very simple.Type this in your function of button-
finish();
startActivity(this,YourCurrentActivity.class);
As Simple as that.
Welcome.
In my case I've done this.
public static void resetForm(ViewGroup group) {
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText) view).getText().clear();
}
if (view instanceof RadioGroup) {
((RadioButton)((RadioGroup) view).getChildAt(0)).setChecked(true);
}
if (view instanceof Spinner) {
((Spinner) view).setSelection(0);
}
if (view instanceof ViewGroup && (((ViewGroup) view).getChildCount() > 0))
resetForm((ViewGroup) view);
}
}
I used this for nested LinearLayout to clear EditTexts and RadioButtons
//method clear
private void clear(ViewGroup group)
{
for(int i=0,count=group.getChildCount();i
if(view instanceof LinearLayout)
{
clear((ViewGroup) view);
}
else if(view instanceof EditText)
{
((EditText) view).getText().clear();
}
if (view instanceof RadioButton)
{
((RadioButton) view).setChecked(false);
}
}//end for
}//end method clear
You can always do this...it works for me:
mClearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mEditName.getText().clear();
mEditSummary.getText().clear();
mEditPrice.getText().clear();
mEditQuantitiy.getText().clear();
}
});
this way you have one fat button that clears all the fields for you once
I created a reset button and write a reset onclick method as follow:
public void reset(View View){
Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
It works for me and also it works without addflags part. But I would like to know whether it is a good approach or not.
// Update answer
private void clearEditTextGroup(ViewGroup group){
for(int i=0 ; i< group.getChildCount(); i++){
View view = group.getChildAt(i);
if(view instanceof EditText){
// use one of clear code
}
if(view instanceof ViewGroup && (((ViewGroup)view).getChildCount() > 0))
clearEditTextGroup((ViewGroup)view);
}
}
use one of this code to clear your edittext
edittext.getText().clear();
or
edittext.setText(null);
or
edittext.setText("");
use
editText.getText().clear();
or setText as Empty using this below code
editText.setText(");