I can\'t get my elements, for example ListView
, to resize properly.
I got a ListActivity
for searching, with a dashboard, an editbox and a
This could be a long shot...
Have you tried playing with the android:layout_height=""
setting for the ListView
?
Maybe using match_parent
or wrap_content
could force it to work...
As I said... a long shot!
You can force the ListView to redraw by changing its height by 1px and calling
requestLayout()
Adding the following at the end of onSizeChanged()
will solve it:
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
requestLayout();
}
});
See the answer to this question: Views inside a custom ViewGroup not rendering after a size change
It's possible to do what you want by registering a setOnFocusChangeListener
and a setOnClickListener
to the EditText
.
There are a lot of different scenarios to consider when it comes to navigation and things might need to be changed to work with a certain layout.
Anyway, start by overriding onSizeChanged to show the hidden element when the back button is touched.
public class MyLinearLayout extends LinearLayout {
private MyListActivity mMyListActivity;
public MyLinearLayout(Context context) {
super(context);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setMyListActivity(MyListActivity mla) {
mMyListActivity = mla;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// show the element when we get more room
if (h > oldh) {
if (mMyListActivity != null) {
mMyListActivity.showBar();
}
}
super.onSizeChanged(w, h, oldw, oldh);
}
}
In the ListActivity we grab the MyLinearLayout and pass this
to it. Then a setOnFocusChangeListener is registered to handle things when the EditText's focus changes. The setOnClickListener is used to hide the element when the EditText already has focus.
public class MyListActivity extends ListActivity {
private ArrayList<MyData> mDataList = new ArrayList<MyData>();
private MyLinearLayout mMyLinearLayout;
private LinearLayout mHideMeLinearLayout;
private EditText mEditText;
public void showBar() {
if (mHideMeLinearLayout != null) {
mHideMeLinearLayout.setVisibility(View.VISIBLE);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get MyLinearLayout and pass this to it.
mMyLinearLayout = (MyLinearLayout) findViewById(R.id.myLinearLayout);
mMyLinearLayout.setMyListActivity(this);
// the LinearLayout to be hidden
mHideMeLinearLayout = (LinearLayout) findViewById(R.id.LinearLayoutToHide);
mEditText = (EditText) findViewById(R.id.editText);
mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Service.INPUT_METHOD_SERVICE);
if (hasFocus) {
imm.showSoftInput(mEditText, 0);
mHideMeLinearLayout.setVisibility(View.GONE);
} else {
imm.hideSoftInputFromWindow(mMyLinearLayout.getWindowToken(), 0);
mHideMeLinearLayout.setVisibility(View.VISIBLE);
}
}
});
mEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mHideMeLinearLayout.setVisibility(View.GONE);
}
});
.....
}
.....
}
I'll provide a working example later, but gotta run now.