AutoCompleteTextView creates a margin for dropdown list - android

Dropdown list comes with right and left margin even though I didn't set margin in my dropdown list item layout. I want it to match to the autocompletetextview's width. How can I achieve that?
Here is my AutoCompleteTextView xml code:
<LinearLayout
android:id="#+id/headerlogo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
android:layout_weight="1"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="9" >
<AutoCompleteTextView
android:id="#+id/autocomplete_name"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="7"
android:background="#drawable/edittextback"
android:ems="10"
android:textSize="15sp"
android:hint="#string/codehint"
android:textColorHint="#color/hintgrey"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:singleLine="true"
android:ellipsize="end"
android:completionThreshold="1">
</AutoCompleteTextView>
<Button
android:id="#+id/btnsearch"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#drawable/buttondarkblue"
android:text="X"
android:textColor="#color/white" />
</LinearLayout>
And item layout for dropdown list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:id="#+id/route"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:singleLine="true"
android:ellipsize="end"
android:padding="5dp"
android:textColor="#color/loginblue"
android:text="ASD"></TextView>
</LinearLayout>

You can use 2 methods
setDropDownHorizontalOffset(int) and setDropDownWidth(int)
For example, you can do something like this to match to autocompletetextview:
input.setDropDownHorizontalOffset(0)
input.setDropDownWidth(input.getWidth())
FYI 1st method can use negative values. So you can set a dropdown layout width more the autocompletetextview width:
int offset = 64
input.setDropDownHorizontalOffset(-1 * offset)
input.setDropDownWidth(input.getWidth() + offset * 2)

I don't know, you've got your answer. But may helps.
If you assign a layout for dropdown list in the custom adapter and set margins for the TextView or paddings for the root view (for example, LinearLayout), it doesn't work. I did as follow:
I set background for the dropdown list in style.xml file:
<item name="android:popupBackground">#drawable/dropdown_list_bg</item>
And then in dropdown_list_bg.xml file i wrote these lines:
<item android:right="1dip" android:left="1dip">
<shape>
<solid android:color="#color/dropdownListBg"/>
<corners android:radius="#dimen/corner"/>
</shape>
</item>
As you can see i set "padding right" and "padding left" with "android:right" and "android:left" attributes in the drawable xml file.

Related

Add Padding top in a Linear Layout

This is my linear layout:
It is too close to the top. I am trying to add some space to the top. I have tried using android:paddingTop but that does not work. How do I add space at top in linear layout?
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingTop="200dp"
android:paddingRight="20dp"
android:paddingBottom="20dp">
<TextView
android:id="#+id/pNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/comfortaa_bold"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="#+id/pDescriptionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/comfortaa_bold"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="#+id/pTypeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/comfortaa_bold"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="#+id/pPriceTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/comfortaa_bold"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="#+id/pQuantityTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/comfortaa_bold"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>
Given what you show in your image, it looks likely that you've already specified android:padding to give padding to all four edges. If that is the case, adding android:paddingTop won't do anything; the system will ignore that attribute in favor of the generic android:padding.
If, for example, you currently have android:padding="16dp", you'll have to delete that and replace it with something like this:
android:paddingTop="32dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
If you are using ListView and want to add only some spacing above the very first item, then the situation is different. Instead of adding paddingTop to your item view, you have some choices.
Padding on the ListView
Add these two attributes to your ListView or RecyclerView tag:
android:paddingTop="32dp"
android:clipToPadding="false"
This is by far the easiest solution, but it will affect how the scrollbar is drawn, which you may not like.
Add a header view to the ListView
Create a layout file for a header view. I suggest using a <Space> tag if you just want some extra space at top:
<Space
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="32dp"/>
Then inflate this and add it to your ListView:
val header = inflater.inflate(R.layout.list_header, listView, false)
listView.addHeaderView(header)
By default, a divider will still appear between this header and your first list item. If you don't want that, you can disable it:
listView.setHeaderDividersEnabled(false)
Dynamic padding in getView()
In your adapter's getView() method, you can programmatically set the padding based on the item's position. First, define two dimen resources like this:
<dimen name="extra_padding">32dp</dimen>
<dimen name="normal_padding">16dp</dimen>
Then, in getView(), add code like this:
val paddingTop = when (position) {
0 -> resources.getDimensionPixelSize(R.dimen.extra_padding)
else -> resources.getDimensionPixelSize(R.dimen.normal_padding)
}
view.setPadding(view.paddingLeft, paddingTop, view.paddingRight, view.paddingBottom)

Android custom text style

If I want display TextView as below :
-------------display-------------
The "display" field is a dynamic data, and the View is TextView, include text and lines on both sides, is any suggestion for the xml build? Or maybe can do it programmatically?
You can use string.xml to store the dash line as text and use String.format method to put dynamic text in your textview
Put the below line in string.xml:
<string name="dash_line">-----%1$s-----</string>
Now you can use the above string to set dashed line with dynamic data in your `textview as follows:
String formattedstring = String.format(getResources().getString(R.string.dash_line), yourStringToShow);
yourTextView.setText(formattedstring );
Ya you can do like below code.
In this code, both side line will adjust according to width,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"
android:background="#D0D0D0" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:singleLine="true"
android:text="Your text goes here" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"
android:background="#D0D0D0" />
</LinearLayout>
If you want to show some drawables, Create a Shape drawable as line. i.e. Name below file as dashed_line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:dashWidth="3dp" android:dashGap="1dp" android:color="#color/black" android:width="2dp"/>
</shape>
and use it as below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/dashed_line">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/white"
android:text="#string/hello_blank_fragment"/>
</LinearLayout>
Or If you simply want to put as a string, '------' on both side then just add it dynamically while setting text i.e.
String display="display";
textview.setText("---------"+display+"--------");

Background drawable doesn't match its size with its parent layout

I am having troubles applying background color onto a list item layout. I am doing a sample app to learn android. My problem is that it shows the colored background, but it is not the size of the list item layout. Its a bit small. Its not working for some reasons, every one here suggested to set a background drawable, set it like this:
view.setBackgroundResource(R.drawable.ic_green);
I am doing this inside CursorAdapter.bindView(View view, Context context, Cursor cursor). Because of course I am dealing with list. Each item has a corresponding color data that I need to reflect on each list item. I need to do this programmatically. Since I must allow my self to change the background color as I please.
It looks like this:
My drawable resources like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFCC" />
</shape>
And this is how my list item layout looks like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="5sp"
android:background="#drawable/note_style"
android:padding="5sp"
android:paddingLeft="10sp"
android:paddingRight="10sp" >
<TextView
android:id="#+id/textview_note_short"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:gravity="start"
android:maxLength="18"
android:maxLines="1"
android:text="#string/text_sample_short_note"
android:textSize="18sp" />
<TextView
android:id="#+id/textview_note_remind_every"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textview_note_short"
android:layout_alignStart="#id/textview_note_short"
android:layout_below="#id/textview_note_short"
android:text="#string/text_sample_remind_every"
android:textSize="12sp" />
<TextView
android:id="#+id/textview_note_date_created"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="#id/textview_note_short"
android:text="#string/text_sample_date"
android:textSize="12sp" />
<ImageView
android:id="#+id/imageview_note_notification_alarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/textview_note_remind_every"
android:layout_alignRight="#id/textview_note_date_created"
android:layout_alignEnd="#id/textview_note_date_created"
android:adjustViewBounds="true"
android:contentDescription="#string/content_description_past_due"
android:maxHeight="20dp"
android:maxWidth="20dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_action_alarm" />
<ImageView
android:id="#+id/imageview_note_notification_due"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/imageview_note_notification_alarm"
android:layout_toLeftOf="#id/imageview_note_notification_alarm"
android:layout_toStartOf="#id/imageview_note_notification_alarm"
android:adjustViewBounds="true"
android:contentDescription="#string/content_description_past_due"
android:maxHeight="20dp"
android:maxWidth="20dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_action_due" />
</RelativeLayout>
It does change the background color at whim but, as you can see on the picture, the green background doesn't hit the edges of layout which should match the width of the screen and height-wrap its content.
Any ideas? Thank you!

Android AutoCompleteTextView suggestions overlapping the TextView

I am making an application using Google maps. I am using AutoCompleteTextView to get suggestions of places. The output is as follows :
As you can see from the image, the border of the suggestion box is overlapping the TextView. I am using custom item layout with a Textview inside a Linear Layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:orientation="horizontal" >
<TextView
android:id="#+id/resultValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:textColor="#474040"
android:textSize="15sp" />
</LinearLayout>
Is there a way to bring the suggestion window down and to remove or change the colour of the grayish border of the suggestion window ?
Was doing the same thing as in the accepted answer, but was still having the overlap issue. To fix that make this call:
autocompleteView.setDropDownVerticalOffset(0);
Try like this, android:popupBackground="#EFEEEC"// change according your border you want for the suggestion box
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/text_area"
android:inputType="text|textNoSuggestions|textMultiLine"
android:paddingLeft="10dp"
android:popupBackground="#EFEEEC"
android:textColor="#333333"
android:textColorHint="#9c9c9c"
android:textSize="18sp"
android:completionThreshold="1" />
and auto_textview.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:textColor="#333333"
android:layout_margin="15dp"
android:textSize="16sp" >
</TextView>
text_area9.png
Final output will look like
Hope this help you.
Use a custom drawable like
AutoCompleteTextView txtNames= (AutoCompleteTextView)findViewById(R.id.editBox);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.drawable.customlist, restList);
txtRestNames.setAdapter(adapter);
and add customlist as
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cust_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:textSize="12dp"
android:singleLine="false"/>
Change the parameters/color to suit your needs. Try it....hope it helps

Text on weighted buttons don't take same space as button

I've built a button bar and want it spread like that the outer buttons taking 40% of the space and the middle button takes 20% of the space. As you can see in the screenshot this works but the text gets cropped within the button.
I found out that the text stays in the bounds of 1 weight (=20%) although the button "knows" it has more space - otherwise it wouldn't fill the background drawable properly.
Here's the setup of the button bar:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/headerbar_top"
android:layout_width="match_parent"
android:layout_height="#dimen/headerbar_divided_height"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="5"
>
<Button
android:id="#id/headerbar_btn_left"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="2"
android:gravity="center_horizontal|bottom"
android:paddingBottom="#dimen/headerbar_paddingBottom"
android:textSize="#dimen/headerbar_textSize"
android:textColor="#color/font"
android:text="Spots"
android:maxLines="1" android:lines="1"
android:ellipsize="end"
android:background="#drawable/headerbar_button_left_states"
android:onClick="onHeaderbarBtnClick"
android:layout_marginRight="#dimen/headerbar_divided_margin"
/>
<Button
android:id="#id/headerbar_btn_center"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal|bottom"
android:paddingBottom="#dimen/headerbar_paddingBottom"
android:textSize="#dimen/headerbar_textSize"
android:textColor="#color/font"
android:maxLines="1" android:lines="1"
android:ellipsize="end"
android:background="#drawable/headerbar_button_center_states"
android:onClick="onHeaderbarBtnClick"
android:layout_marginRight="#dimen/headerbar_divided_margin"
android:layout_marginLeft="#dimen/headerbar_divided_margin"
/>
<Button
android:id="#id/headerbar_btn_right"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="2"
android:gravity="center_horizontal|bottom"
android:paddingBottom="#dimen/headerbar_paddingBottom"
android:textSize="#dimen/headerbar_textSize"
android:textColor="#color/font"
android:text="Tours"
android:maxLines="1" android:lines="1"
android:ellipsize="end"
android:background="#drawable/headerbar_button_right_states"
android:onClick="onHeaderbarBtnClick"
android:layout_marginLeft="#dimen/headerbar_divided_margin"
/>
</LinearLayout>
Does anyone have an idea why the button text behaves so strangely?
EDIT:
Here's the XML-drawable for the left button:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/headerbar_button_left_on" android:state_selected="true"/>
<item android:drawable="#drawable/headerbar_button_left_off"/>
</selector>
And here are the drawables themselves:
Remove android:ellipsize="end" from the button's declaration.
Also: Your 9-patch-drawables seem to be missing the content area. The bottom and right corners are used for the 9-patch contents. Here's a good simple tutorial on 9-patches.

Categories

Resources