I'm trying to implement android.support.v7.widget.SearchView with a collapseIcon on the Toolbar. Everything works fine i.e, the SearchView is working as expected but I'm unable to remove/ reduce the padding between the SearchView and collapseIcon, as visible in the attached screenshot.
Any ideas on how can I do that?
I've tried :
app:contentInsetStartWithNavigation="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
but this doesn't work.
The problem can be solved by setting the leftMargin = 0 in the layout parameters associated with the following LinearLayout in the SearchView:
(To get the layout parameters use the getLayoutParams)
LinearLayout searchEditFrame = (LinearLayout) searchView.findViewById(R.id.search_edit_frame); // Get the Linear Layout
// Get the associated LayoutParams and set leftMargin
((LinearLayout.LayoutParams) searchEditFrame.getLayoutParams()).leftMargin = 0;
I managed to solve the problem by looking at the xml file that's used by SearchView, so the padding can be removed/reduced by changing the leftMargin value:
layoutParams.leftMargin = 0;
for the following LinearLayout inside the SearchView:
LinearLayout searchEditFrame = (LinearLayout) searchView.findViewById(R.id.search_edit_frame);
Related
I am adding a View in a LinearLayout, this way:
LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayout);
TextInputLayout til=new TextInputLayout(MainActivity.this);
til.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
Log.i("TAG","BEFORE: "+ll.getChildCount());
ll.addView(til);
Log.i("TAG","AFTER: "+ll.getChildCount());
This way, the til object is not shown, but it IS added, because in the second log I see the number has incremented by one, the view I just added.
Why can't I see it? how to show the new view in the layout?
Thank you.
I think what you are forgetting is to add an EditText or a TextInputEditText to the TextInputLayout.
The android documentation says:
[TextInputLayout is a] Layout which wraps an EditText (or descendant) to show a floating label when the hint is hidden due to the user inputting text.
EditText editText = new EditText(this);
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(editTextParams);
editText.setTextSize(16);
editText.setTextColor(getResources().getColor(R.color.black));
editText.setHint("hint");
editText.setHintTextColor(getResources().getColor(R.color.gray));
textInputLayout.addView(editText, editTextParams);
TextInputLayout is nothing without having EditText as child.
Create a layout file text_input_layout.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
Now, to add, simply inflate this layout and addView() to LinearLayout as below:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
TextInputLayout textInputLayout = (TextInputLayout) LayoutInflator.from(this).inflate(R.layout.text_input_layout, null);
linearLayout.addView(textInputLayout);
Hope this helps...
Note: You can also have TextInputEditText as Child of TextInputLayout
The til has zero height. Use this instead:
til.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
I have added layout_anchor property on fab in layout xml. After clicking a button the fab should position on end of the screen. To achieve that I am thinking to add gravity property. But how do i remove layout_anchor property programmatically.
layout.xml
<com.samsoft.sam.Fab
android:id="#+id/fab"
app:layout_anchor="#id/asd"
app:layout_anchorGravity="top|end"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The above is my fab code and it positions perfectly, now I need to replace layout anchor property with android:gravity="top|end" programmatically. Please help me.
I assume you mean you want the FloatingActionButton's layout_gravity to end up as top|end.
You can do this by getting the FloatingActionButton's CoordinatorLayout.LayoutParams, clearing the anchor's ID, setting the gravity field as needed, and then setting the LayoutParams back on the FloatingActionButton.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
lp.setAnchorId(View.NO_ID);
lp.gravity = Gravity.TOP | GravityCompat.END;
fab.setLayoutParams(lp);
((CoordinatorLayout.LayoutParams) floatingActionButton.getLayoutParams()).setAnchorId(View.NO_ID);
This is the code you are looking for. Happy coding :)
I am having trouble adding a view in the scrollview. The structure of my xml is
// xml_layout.xml
<ScrollView>
<LinearLayout
android:id="#+id/container"
>
<LinearLayout>
<ListView
...
/>
</LinearLayout>
******Here I am trying to add an item *********
</LinearLayout>
</ScrollView>
I am trying to add a textview at the bottom programmatically as follows.
TextView v = new TextView(getActivity());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setText("TEST View");
v.setLayoutParams(params);
LinearLayout container = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.xml_layout, null).findViewById(R.id.container);
container.addView(v, 1);
The goal I am trying to achieve is making a tablelayout that contains two columns with the title and listview in the second column. Before I do this, I tried to display a simple textview so that I know I can add a widget. I have tried many different ways but did not work. Is there something wrong with my code??
you must first verify that your LinearLayout 'container' is 'vertical ' orientation, and...
LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(v);
I'm dynamically injecting a content view into the CoordinatorLayout and would now like to apply a layout_below property to the injected view so that it isn't hidden behind the AppBar.
Is there any way to do this at runtime from code instead of xml properties of the annotation?
Taking one step back and building the entire view in plain xml, I realized that layout_below is not the property I needed for my use case: placing the content view below the app bar. I did not make this clear in my question though, as I assumed layout_below would be the proper option for that.
In fact, to insert a non scrolling view into the CoordinatorLayout it should first be wrapped with a android.support.v4.widget.NestedScrollView. Then, to avoid its content being hidden behind the app bar, it is necessary to update its behavior to android.support.design.widget.AppBarLayout.ScrollingViewBehavior. Otherwise a default behavior is used which will hide behind the app bar.
val viewToInsert = getLayoutInflater.inflate( id, coordinatorWrapper, false )
val p = viewToInsert.getLayoutParams.asInstanceOf[Coordinator.LayoutParams]
p.setBehavior( new ScrollingViewBehavior )
coordinatorWrapper.addView( viewToInsert, 1, p )
You could try setting an anchor.
ContentView view = getContentView(); //your view
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
params.setAnchorId(R.id.app_bar_layout);
params.anchorGravity = Gravity.BOTTOM; //we will anchor to the bottom line of the appbar
params.gravity = Gravity.BOTTOM; //we want to be BELOW that line
view.setLayoutParams(params);
In my a LinearLayout I have set android:layout_marginBottom in my XML. I just want to retrieve that value.
Just LayoutParams doesn't give me access to bottomMargin . I tried to typecast :
row1 = (LinearLayout) findViewById(R.id.mainrow1);
LinearLayout.LayoutParams row1Params = (android.widget.LinearLayout.LayoutParams) row1.getLayoutParams();
but this gives me Runtime exception ClassCastException.
The main aim is I got buttons in nested LinearLayouts & I want to set height/width of LinearLayout programmatically that will inturn auto set height of buttons. I have added bottomMargin after 1st layout - so there is space between 2 rows.
Change Your layout of Button according to following logic,
set Layout_Width to 0dip
set Layout_Weight to 1
set Layout_Height to Wrap_Content
try this. When you get layout, R.layout. is best option. It is working for me.
row1 = (LinearLayout) findViewById(R.layout.mainrow1);