In the below snippet, how could I get mClickable to be the same height as mTextArea?
public class EditSpinner extends LinearLayout {
EditText mTextArea;
ImageButton mClickable;
public EditSpinner(Context context, String[] options) {
super(context);
setOrientation(HORIZONTAL);
setGravity(Gravity.CENTER_VERTICAL);
setBackgroundResource(android.R.drawable.edit_text);
setAddStatesFromChildren(true);
setPadding(0, 0, 0, 0);
mTextArea = new EditText(context);
mTextArea.setSingleLine(true);
mTextArea.setBackgroundResource(0);
addView(mTextArea, new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
mClickable = new ImageButton(context);
mClickable.setBackgroundResource(0);
mClickable.setImageDrawable(
context.getResources().getDrawable(
android.R.drawable.btn_default_small));
//Scale to mTextArea.getHeight()... except getHeight doesn't work
addView(mClickable,
new LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
}
}
Its a bit difficult to tell exactly what you want, but it sounds like you are asking for two different things, which (generally) isn’t possible with only one layout. Basically from what I interpret, you are saying I want the button to be the same size as the image, and then saying I don’t want the button to be the same size as the image (when its too big).
My suggestion is to incorporate a way to decipher which layout style you want. Perhaps it is by checking the image size vs the current screen size. Then apply the type of parameters that will work for that particular layout.
Setting the two the same size is easy via use of the XML
android:layout_weightparameter, and you are already familiar withwrap_contentfor use with a larger image. While you can use these two parameters together you won’t get what you want in all cases (again assuming I understand your post). You’ll need one layout using weights to make the views the same size and another using wrap_content to allow for different sizes.Also, just a side note, I would really recommend using XML for layouts unless there is something specific you can’t accomplish with XML.
Edit: Here is an example for you.