Make Searchview width to full width - android-studio

I want the length of searchview to occupy full width of action bar when clicked but instead it shows the others items as well. This is xml file of the view
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:title="Settings"
app:showAsAction="never"/>
<item
android:id="#+id/about"
android:title="About"
app:showAsAction="never"
/>
<item
android:id="#+id/edit"
android:icon="#drawable/ic_baseline_sort_24"
app:showAsAction="always"
android:title="Edit"/>
<item android:id="#+id/search"
android:title="search_title"
android:icon="#drawable/ic_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
</menu>
In the java file I am trying the following
androidx.appcompat.widget.SearchView searchView = (androidx.appcompat.widget.SearchView)
item.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
It is working as regular search bar with other items shown as well along the bar.

Related

How to dynamically update bitmap with rounded corners on Widget root layout?

I need to dynamically update a picture with rounded corners in the root layout of the Widget. There is only a simple setImageViewBitmap API for viewing related documents, but the exact size of the widget is required to draw the specified radius; this problem is easy to implement on Android 12 (refer to https://medium.com/androiddevelopers/updating-your-widget-for-android-12-92e7de87424c), but I see that some apps are also implemented on devices lower than Android 12, there should be other ways to achieve this problem
As I know is:
Before Android 12, I can't get real size of widget root view
Many APIs of RemoteViews are only supported in API 31 and above,including setClipToOutline, etc.
The View supported by RemoteViews is very limited and does not support custom View
my widget layout file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fl_root_view"
android:layout_width="match_parent"
android:layout_height="155dp">
// widget background
<ImageView
android:id="#+id/iv_root_view_bg"
android:src="#drawable/big_most_played_widget_layout_bg"
android:layout_width="match_parent"
android:layout_height="match_parent" />
// widget content
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal">
...
</LinearLayout>
</FrameLayout>
one of the background files:
//#drawable/big_most_played_widget_layout_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="24dp"/>
<solid android:color="#182327"/>
</shape>
And i have tried:
Update the layout background directly, the rounded shape will be lost
Generate a bitmap with the approximate width and height of the widget and use PorterDuff.Mode.SRC_IN to draw rounded corners, which has great uncertainty on various devices (the bitmap cannot completely fit the widget size)
override RemoteViews.apply(), but it not work
override fun apply(context: Context?, parent: ViewGroup?): View {
val result = super.apply(context, parent)
val colors = IntArray(2) { (0xff000000).toInt() or Random.nextInt(0x00ffffff) }
val bgDrawable = GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors).apply {
cornerRadius = 42f
}
result.setBackgroundDrawable(bgDrawable)
return result
}
I want to know:
In addition, is there a way to get the real width and height of the widget on devices lower than Android 12?
Other better implementation methods?

Removing XML Elements Based On Attribute Value Using Python Element Tree

I've been working with this much of the day but haven't been able to find a solution. I have a fairly large XML file that I need to strip of some data. The 'fields' are annotated using attributes by Id (field name) and Num (unique number for the field name).
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Item Id="Type" Num="30">3</Item>
<Item Id="Version" Num="50">180</Item>
<Owner>
<Item Id="IdNumber" Num="20">00000000</Item>
<Item Id="race1" Num="160">01</Item>
<Item Id="race2" Num="161">88</Item>
<Item Id="race3" Num="162">88</Item>
<Dog>
<Item Id="Breed" Num="77">Mutt</Item>
<Item Id="Weight" Num="88">88</Item>
</Dog>
<Dog>
<Item Id="Breed" Num="77">Retriever</Item>
<Item Id="Weight" Num="88">77</Item>
</Dog>
</Owner>
<Owner>
<Item Id="IdNumber" Num="20">00033000</Item>
<Item Id="race1" Num="160">03</Item>
<Item Id="race2" Num="161">88</Item>
<Item Id="race3" Num="162">88</Item>
<Dog>
<Item Id="Breed" Num="77">Poodle</Item>
<Item Id="Weight" Num="88">21</Item>
</Dog>
</Owner>
</Data>
Here's the pretty simple python code that I assumed would do the trick, but it's stripping out the data as expected.
#!/usr/bin/env python3
import xml.etree.ElementTree as ET
# list of "Nums" to drop from each Owner and Dog
drops = ['160', '161', '162', '88']
# read in XML file
with open('dogowners.xml') as xmlin:
tree = ET.parse(xmlin)
root = tree.getroot()
for x in root:
for y in x:
# checking to make sure it's an Owner
if y.attrib:
# if the value for attribute Num is in the list of drops then remove it
if y.attrib["Num"] in drops:
x.remove(y)
# finally output new tree
tree.write('output.xml')
output.xml
The issue I'm running into is it's not removing all the drops/Nums listed. In the case with this small XML, it's only doing the first and third value on the Owner level, which is consistent with my large file because it appears to be only removing every other one.
<Data>
<Item Id="Type" Num="30">3</Item>
<Item Id="Version" Num="50">180</Item>
<Owner>
<Item Id="IdNumber" Num="20">00000000</Item>
<Item Id="race2" Num="161">88</Item>
<Dog>
<Item Id="Breed" Num="77">Mutt</Item>
<Item Id="Weight" Num="88">88</Item>
</Dog>
<Dog>
<Item Id="Breed" Num="77">Retriever</Item>
<Item Id="Weight" Num="88">77</Item>
</Dog>
</Owner>
<Owner>
<Item Id="IdNumber" Num="20">00033000</Item>
<Item Id="race2" Num="161">88</Item>
<Dog>
<Item Id="Breed" Num="77">Poodle</Item>
<Item Id="Weight" Num="88">21</Item>
</Dog>
</Owner>
</Data>
I feel like I might be missing something kind of obvious, but XML parsing is not my forte and I've been fighting with this for a couple of hours. Any help is greatly appreciated.
This seems like a good candidate for the identiy transform pattern. The following will copy the xml document, but will exclude the Item elements that match the empty template at the end of the xsl string.
owner-dog.py
#!/usr/bin/env python3
from lxml import etree
# list of "Nums" to drop from each Owner and Dog
drops = ('160', '161', '162', '88')
# we turn it into an xsl attribute pattern:
# #Num = '160' or #Num = '161' or #Num = '162' or #Num = '88'
attr_vals = list(map(lambda n: f'#Num = \'{n}\'', drops))
attr_expr = ' or '.join(attr_vals)
xsl = etree.XML('''<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent='yes'/>
<xsl:strip-space elements="*" />
<!-- copy all nodes ... -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<!-- ... except for any elements matching the following template -->
<xsl:template match="//Item[ {attr_vals} ]" />
</xsl:stylesheet>'''.format(attr_vals=attr_expr))
transform = etree.XSLT(xsl)
with open('owner-dog.xml') as xml:
print(transform(etree.parse(xml)))
Output
<?xml version="1.0"?>
<Data>
<Item Id="Type" Num="30">3</Item>
<Item Id="Version" Num="50">180</Item>
<Owner>
<Item Id="IdNumber" Num="20">00000000</Item>
<Dog>
<Item Id="Breed" Num="77">Mutt</Item>
</Dog>
<Dog>
<Item Id="Breed" Num="77">Retriever</Item>
</Dog>
</Owner>
<Owner>
<Item Id="IdNumber" Num="20">00033000</Item>
<Dog>
<Item Id="Breed" Num="77">Poodle</Item>
</Dog>
</Owner>
</Data>
Comparing the original xml with the ouput
diff <(xmllint --format owner-dog.xml) <(./owner-dog.py)
1c1
< <?xml version="1.0" encoding="UTF-8"?>
---
> <?xml version="1.0"?>
7,9d6
< <Item Id="race1" Num="160">01</Item>
< <Item Id="race2" Num="161">88</Item>
< <Item Id="race3" Num="162">88</Item>
12d8
< <Item Id="Weight" Num="88">88</Item>
16d11
< <Item Id="Weight" Num="88">77</Item>
21,23d15
< <Item Id="race1" Num="160">03</Item>
< <Item Id="race2" Num="161">88</Item>
< <Item Id="race3" Num="162">88</Item>
26d17
< <Item Id="Weight" Num="88">21</Item>
29a21
>

How to increase space between text and it's underline in TextView Android?

I want to increase space between text and it's underline. I can change line height but I don't change height between text and it's underline.How can I do this ?
private static final String FONTH_PATH = "fonts/Brandon_bld.otf";
...
selectTextView = (TextView) findViewById(R.id.selectTextView);
selectTextView.setTypeface(font);
SpannableString content = new SpannableString(getResources().getString(R.string.select_your_prove_type));
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
selectTextView.setText(content);
...
and xml file
<TextView
android:id="#+id/selectTextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="536"
android:gravity="left"
android:lineSpacingMultiplier="0.8"
android:text="#string/select_your_prove_type"
android:textColor="#color/Blue"
android:textSize="#dimen/select_size" />
Just use the
paddingBottom
as follows
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/fragment_activity_edit_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:hint="Description"
android:textSize="34sp"
tools:text="Secret Death Ray Design"/>
</android.support.design.widget.TextInputLayout>
Okay here's a more complete answer:
In your styles.xml, add a style:
<style name="MyTour.HeadingText">
<item name="android:background">#drawable/tour_header_line_layerlist</item>
</style>
Now create a tour_header_line_layerlist.xml file with content:
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="20dp" android:drawable="#drawable/tour_header_line"/>
</layer-list>
The above will set a margin top of 20dp for the line (extract it out to dimen.xml for completeness)
Now create the corresponding tour_header_line.xml:
<shape android:shape="line"
xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#color/tour_subheading_color" />
Now reference the style in your control:
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/tour_1_heading_text"
android:id="#+id/heading_text" android:layout_gravity="center_horizontal"
style="#style/MyTour.HeadingText" //notice this
android:layout_marginTop="#dimen/margin_large"/>
Let me know if you have other questions!
Add these line in EditText
<EditText
...
android:includeFontPadding="true"
android:paddingBottom="20dp" />
You can add the custom underline with a drawable file (in my case I've created a shadow) and set this custom underline with
android:background="#drawable/shadow_file.xml"
After that with adding padding to your EditText you can add more space between the line and the text.
android:paddingBottom="#dimen/underline_space"
and define this dimension on your dimens.xml file.
That works for me, and I hope for you too :)

How to get the attribute value of an xml code using linq

<Shape ID="1" NameU="Start/End" Name="Start/End" Type="Shape" Master="2">
....</Shape>
<Shape ID="2" NameU="Start/End" Name="Start/End" Type="Shape" Master="5">
....</Shape>
I have to return the Master value for every ID value.
How can i achieve it by using LINQ to XMl.
You didn't really present how your XML document looks like, so I assumed it's as follow:
<Shapes>
<Shape ID="1" NameU="Start/End" Name="Start/End" Type="Shape" Master="2">
</Shape>
<Shape ID="2" NameU="Start/End" Name="Start/End" Type="Shape" Master="5">
</Shape>
</Shapes>
You can simply get Master attribute value for all different ID like that:
var xDoc = XDocument.Load("Input.xml");
var masters = xDoc.Root
.Elements("Shape")
.ToDictionary(
x => (int)x.Attribute("ID"),
x => (int)x.Attribute("Master")
);
masters will be Dictionary<int, int> where key is your ID and value is corresponding Master attribute value.

JavaFX & FXML: how do I set the default selected item in a ChoiceBox in FXML?

I have the following FXML:
<ChoiceBox>
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="2 minutes" />
<String fx:value="5 minutes" />
<String fx:value="15 minutes" />
</FXCollections>
</items>
</ChoiceBox>
But in the GUI it just shows a ChoiceBox with a default of nothing. I would like the first element in the list to be the default, and for a choice of "null" or nothing to be prohibited.
How do I accomplish this?
I added the value attribute to the ChoiceBox tag, and that worked.
<ChoiceBox value="2 minutes">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="2 minutes" />
<String fx:value="5 minutes" />
<String fx:value="15 minutes" />
</FXCollections>
</items>
</ChoiceBox>
First, you should import your needed value model, like Crowell answer, you should import like this in your fxml header:
<?import javafx.collections.*?>
Second, if you want's import your own model, import it first and then like this:
<?import com.zzg.mybatis.generator.model.*?>
....
<ChoiceBox layoutX="24.0" layoutY="14.0" prefWidth="150.0">
<items>
<FXCollections fx:factory="observableArrayList">
<DatabaseDTO name="MySQL" value="1"></DatabaseDTO>
<DatabaseDTO name="Oracle" value="2"></DatabaseDTO>
</FXCollections>
</items>
</ChoiceBox>
#Groostav: In case we programmatically "know" the value that should appear as selected (for example, we landed in an edit form), we can do the following:
1) Add a new item with index 0 (that is, the element we need to show as selected):
myChoiceBox.getItems().add(0, ItemObtainedProgrammatically);
2) Show the item as selected (since we already know it's at position 0):
myChoiceBox.getSelectionModel().select(0);
Probably this qualifies as a dirty hack, but it works. The con: You have the same item twice in your choicebox

Resources