android - Trying to add a RelativeLayout programmatically but seems like it's off screen or too big -
i have xml screen @ top have spinner list of search conditions, button next (this call control area) , each time pressed, group of views added search. group comprises of spinner user specify operator (and, or), text box title of search condition , edit text user put input (this call filter area). used build search query database search later on.
the xml below have far group hardcoded can show add programatically.
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillviewport="true"> <!--important otherwise backgrnd img. not fill whole screen --> <relativelayout android:id="@+id/searchlayout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context="com.kadis.materialref.search"> <spinner android:id="@+id/attributeselector" android:layout_width="wrap_content" android:layout_height="@dimen/controlelementheight" android:layout_alignparentleft="true" android:layout_alignparentstart="true"/> <button android:id="@+id/addattributebutton" android:layout_width="@dimen/controlelementheight" android:layout_height="@dimen/controlelementheight" android:layout_alignparentend="true" android:layout_alignparentright="true" android:layout_torightof="@+id/attributeselector" android:layout_toendof="@+id/attributeselector" android:onclick="addsearchcondition" android:text="@string/addsearchattributestring"/> <view android:id="@+id/dividercontrol1" style="@style/divider" android:layout_below="@+id/attributeselector"/> <view android:id="@+id/dividercontrol2" style="@style/divider" android:layout_below="@+id/dividercontrol1"/> <relativelayout android:layout_width="match_parent" android:id="@+id/filterarea0" android:layout_height="@dimen/controlelementheight" android:layout_below="@+id/dividercontrol2" android:layout_alignparentleft="true" android:layout_alignparentstart="true"> <spinner android:id="@+id/filteroperator0" android:layout_width="@dimen/controlelementheight" android:layout_height="match_parent" android:layout_alignparentleft="true" android:layout_alignparentstart="true"/> <textview style="@style/searchformtitle" android:id="@+id/filtertitle0" android:text="@string/column1" android:layout_torightof="@id/filteroperator0" android:layout_toendof="@id/filteroperator0" android:layout_alignparentend="true" android:layout_alignparentright="true"/> <edittext style="@style/searchformfieldtext" android:layout_below="@+id/filtertitle0" android:layout_torightof="@id/filteroperator0" android:layout_toendof="@id/filteroperator0" android:layout_alignparentend="true" android:layout_alignparentright="true"/> </relativelayout> </relativelayout> </scrollview>
the screen
the function called when button pressed add filter area following. function generateviewid same after api 17, added in class api < 17.
public void addsearchcondition(view view) { // selection spinner in control area; attribute add in search conditions. spinner attributeselector = (spinner) findviewbyid(r.id.attributeselector); string attributeselected = attributeselector.getselecteditem().tostring(); // layout add stuff. relativelayout searchlayout = (relativelayout) findviewbyid(r.id.searchlayout); // last id of last child know under add new layout int bottomchildid = searchlayout.getchildat(searchlayout.getchildcount()-1).getid(); // // new relative layout added. // relativelayout filterlayout = new relativelayout(this); filterlayout.setid(generateviewid()); // define width , height parameters. relativelayout.layoutparams rlp = new relativelayout.layoutparams( relativelayout.layoutparams.match_parent, r.dimen.controlelementheight); // define position rlp.addrule(relativelayout.below, bottomchildid); rlp.addrule(relativelayout.align_parent_start); rlp.addrule(relativelayout.align_parent_left); filterlayout.setlayoutparams(rlp); // // add operator spinner. // spinner operatorspinner = new spinner(this); operatorspinner.setid(search.generateviewid()); // options spinner. string[] operators = getresources().getstringarray(r.array.operators); // add items on spinner. arrayadapter<string> spinnerarrayadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, operators); spinnerarrayadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); operatorspinner.setadapter(spinnerarrayadapter); // define width , height parameters. relativelayout.layoutparams rlpsp = new relativelayout.layoutparams( r.dimen.controlelementheight, r.dimen.controlelementheight); // define position rlpsp.addrule(relativelayout.align_parent_left); rlpsp.addrule(relativelayout.align_parent_start); // set layout parameters operatorspinner.setlayoutparams(rlpsp); // add spinner on layout filterlayout.addview(operatorspinner); // // add text view title // final textview tv = (textview)getlayoutinflater().inflate(r.layout.search_attribute_title, null); tv.setid(search.generateviewid()); // set text display tv.settext(attributeselected); // define width , height parameters. relativelayout.layoutparams rlptv = new relativelayout.layoutparams( relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); // define position rlptv.addrule(relativelayout.align_parent_right); rlptv.addrule(relativelayout.align_parent_end); rlptv.addrule(relativelayout.right_of, operatorspinner.getid()); rlptv.addrule(relativelayout.end_of, operatorspinner.getid()); // set layout parameters tv.setlayoutparams(rlptv); // add spinner on layout filterlayout.addview(tv); // add edit text placeholder user input final edittext et = (edittext)getlayoutinflater().inflate(r.layout.search_attribute_input, null); et.setid(search.generateviewid()); // set text display et.settext(attributeselected); // define width , height parameters. relativelayout.layoutparams rlpet = new relativelayout.layoutparams( relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); // define position rlpet.addrule(relativelayout.align_parent_right); rlpet.addrule(relativelayout.align_parent_end); rlpet.addrule(relativelayout.right_of, operatorspinner.getid()); rlpet.addrule(relativelayout.end_of, operatorspinner.getid()); rlpet.addrule(relativelayout.below, tv.getid()); // set layout parameters et.setlayoutparams(rlpet); // add spinner on layout filterlayout.addview(et); // adding whole filter layout search layout (whole screen) searchlayout.addview(filterlayout); }
i not sure if problem might layoutparameters textview , edittext add rules have have layoutparameters , have pass arguments. maybe should pass wrap_content since in xml hardcoded have don't pass them?
i have debugged , layout created , added. ids generated , when press on screen white space below hardcoded xml reacts it's spinner operator choices. guessing goes wrong dimensions maybe? after pressing button , filter area "added" if press white area supposed spinner of (with operators) triggered
the dimensions have far following defined in case
<dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <!-- fonts sizes --> <dimen name="titlebig">48sp</dimen> <dimen name="titlemed">36sp</dimen> <dimen name="titlesmall">24sp</dimen> <dimen name="searchtitle">10sp</dimen> <dimen name="searchfield">15sp</dimen> <!-- padding --> <dimen name="titlepadding">20dp</dimen> <!-- element heights --> <dimen name="controlelementheight">40dp</dimen>
also, when choose choice in spinner attributes (the search filters) button disappears, think gets shoved off screen right? how can fix that?
so in code create layoutparams
relativelayout
:
relativelayout.layoutparams rlp = new relativelayout.layoutparams( relativelayout.layoutparams.match_parent, r.dimen.controlelementheight);
you not passing dimension height, rather unique identifier of dimension resource. can see value if open r.java
project , search controlelementheight
. passing huge integer value in height ids values 0x7f0a0047
.
what want this:
relativelayout.layoutparams rlp = new relativelayout.layoutparams( relativelayout.layoutparams.match_parent, getresources().getdimensionpixelsize( r.dimen.controlelementheight) );
this request actual value of defined dimension resource, scaled device's screen density.
Comments
Post a Comment