Reputation: 164
I would like to change the size of my linear layout in my program not in the xml file itself. I'm a beginner and also would like to declare my variable outside a method (public) and initialize it in the method, like I did it with EditText and the LinearLayout.
I tried it that way, but it says:
Error:(47, 81) error: incompatible types: android.view.ViewGroup.LayoutParams cannot be converted to android.widget.LinearLayout.LayoutParams
But I dont know what to do with this error :(
My Code so far:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public EditText input_chat;
public LinearLayout layout_chat_input;
public LinearLayout.LayoutParams layout_chat_input_params;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(),"onCreate",Toast.LENGTH_SHORT).show();
input_chat = (EditText) findViewById(R.id.input_chat);
layout_chat_input = (LinearLayout) findViewById(R.id.layout_chat_input);
layout_chat_input_params = new LinearLayout.LayoutParams(layout_chat_input.getLayoutParams());
input_chat.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if (input_chat.getLineCount() > 0 && input_chat.getLineCount() < 5)
{
layout_chat_input_params = layout_chat_input.getLayoutParams();
layout_chat_input.setLayoutParams();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void afterTextChanged(Editable s) {}
});
}
}
Upvotes: 2
Views: 84
Reputation: 2111
Try:
public void onTextChanged(CharSequence s, int start, int before, int count){
if (input_chat.getLineCount() > 0 && input_chat.getLineCount() < 5){
layout_chat_input.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
OR
layout_chat_input.setLayoutParams(layout_chat_input_params);
Upvotes: 2