shrinidhi joshi
shrinidhi joshi

Reputation: 246

Changing the margin in a custom adapter onClick callback not working, android

I am displaying a ListView using a custom adapter. In the view i have a textView and a button in a layout.

What is supposed to happen -> As soon as i click on the text view the onClick callback in the custom adapter class for the TextView sets the margin for the button_layout like so

View button_layout = (((View)(View)v.getParent()).getParent()).findViewById(R.id.button_layout));
MarginLayoutParams margins=(MarginLayoutParams)button_layout.getLayoutParams();
margins.bottomMargin=-100; 

But this is not happening.I am able to change the background color. But not able to change the bottom margin. The customadapter is a part of the larger code which i cannot disclose.

The app doesnt crash but doesnt work either. If i see in the debugger the value of bottomMargin for the layout has changed but it is not reflected in the UI :( I have put some part of the code here. assume that the onClickListener has been set. It is working because as i said i am able to change the background color of the layout on Clicking the Text View.

public class MyCustomAdapter extends ArrayAdapter<someClass> implements OnClickListener{
   public View getView(int position, View convertView, ViewGroup parent) {
       }
        public void onClick(View v){
            View row_to_hide = (((View        ((View)v.getParent()).getParent()).findViewById(R.id.row_to_hide));`
            MarginLayoutParams margins=(MarginLayoutParams)row_to_hide.getLayoutParams();`
              margins.bottomMargin=-100;`
                                        }
   }

I am new to android and would like to know if there something conceptually wrong with this approach. Also the textView and the button are in a Relative layout and a Linear layout respectively.

I am trying to change the margins of the linear layout in which the button is situated so that i can hide the button.

Upvotes: 3

Views: 567

Answers (1)

Koso
Koso

Reputation: 3199

You changed layout params but you have to set them back to view. So method setLayoutParams is missing there. Code shold look like this:

View button_layout = (((View)(View)v.getParent()).getParent()).findViewById(R.id.button_layout));
MarginLayoutParams margins=(MarginLayoutParams)button_layout.getLayoutParams();
margins.bottomMargin=-100; 
button_layout.setLayoutParams(margins);

Side note: You should replace ((View)(View)v.getParent()).getParent()) with convertView parameter from getView method.

Upvotes: 1

Related Questions