Searhview theming issues

  • Replies:22
Lars
  • Forum posts: 13

Apr 23, 2012, 3:27:04 PM via Website

When creating a searchview in an android
4.03 when using the theme holo light with darkactionbar
The style of the drawables of the searchview
Doesn't match.
If have already reported it as a bug.
http://code.google.com/p/android/issues/detail?id=29288

Is there a way to set the style of the searchview
When using the holo light darkactionbar theme
To the style when using the theme holo dark??
Which has the right drawables.

Reply
Eric McBride
  • Forum posts: 1,790

Apr 25, 2012, 4:07:55 PM via Website

Hey Jeremiah...are you around to help Lars on this?

Reply
Lars
  • Forum posts: 13

Apr 25, 2012, 7:40:25 PM via App

i would verry much appriciate that!! :)

Reply
Jeremiah
  • Forum posts: 775

Apr 25, 2012, 9:40:43 PM via Website

Are you applying the theme to your whole activity? Have you tried adding the style to your SearchView itself. In your xml you would add,
1<SearchView
2 style="@style/android:Theme.Holo.Light"
3 ...
4 />

Also make sure the emulator or phone your testing it on is at least android 3.0 or higher.

Reply
Lars
  • Forum posts: 13

Apr 25, 2012, 9:54:10 PM via Website

well on my activity i have a normal holo.light.darkactionbar theme. but i can't add it to my xml because i don't inflate the searchview from an xml
i hard code it

1MenuItem item = menu.add("Search");
2item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
3SearchView sv = new SearchView(getAcitivty());
4sv.setOnQueryTextListener(this);
5item.setActionView(sv);

the reason i do this is because when i inflate it from an xml i do get the right theme. but i get some issues
i got a sample project with that code in here http://code.google.com/p/searchview-test/
the problems i are described there (but that's irrelevant to this question).

I use the android 4.03 emulator

Reply
Jeremiah
  • Forum posts: 775

Apr 25, 2012, 10:25:07 PM via Website

Lars
well on my activity i have a normal holo.light.darkactionbar theme. but i can't add it to my xml because i don't inflate the searchview from an xml
i hard code it

1MenuItem item = menu.add("Search");
2item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
3SearchView sv = new SearchView(getAcitivty());
4sv.setOnQueryTextListener(this);
5item.setActionView(sv);

the reason i do this is because when i inflate it from an xml i do get the right theme. but i get some issues
i got a sample project with that code in here http://code.google.com/p/searchview-test/
the problems i are described there (but that's irrelevant to this question).

I use the android 4.03 emulator

You can set the style on the SearchView when you construct it. You can't change it once it's been constructed, but you can set it when you create it. First you need to get the attributes of your .xml file that contains your style.

XmlPullParser parser = resources.getXml(myResouce);
AttributeSet attributes = Xml.asAttributeSet(parser);

See:
http://developer.android.com/reference/android/util/AttributeSet.html

Then use the attributes when you construct the searchView:

SearchView(Context context, AttributeSet attrs)

Your code to construct would look something like SearchView sv = new Search View (getAcitivity(), attributes);

Reply
Lars
  • Forum posts: 13

Apr 25, 2012, 10:38:31 PM via Website

I think that is it!! but i have a silly question do i put the searchview style in the styles.xml or do i make a
separate xml and just say

1<SearchView
2style="@style/android:Theme.Holo.Light"
3/>

and also do you use for resources.. context.getResources()??

Reply
Jeremiah
  • Forum posts: 775

Apr 26, 2012, 12:33:02 AM via Website

I don't think you put it in styles.xml. You should use the resource/xml file for your activity, in which you set the holo theme for your activity.

XmlPullParser parser = resources.getXml(R.layout.main); //if you use main.xml for your activities xml

To get your resources use android.content.Context.getResources (). http://developer.android.com/reference/android/content/Context.html#getResources()

— modified on Apr 26, 2012, 12:36:16 AM

Reply
Lars
  • Forum posts: 13

Apr 26, 2012, 5:38:41 PM via Website

I have tried it but i doesn't work for me yet..
i have done this

this is my main.xml which i call in my activity
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:baselineAligned="false"
6 android:orientation="horizontal" >
7
8 <FrameLayout android:id="@+id/ListContent1"
9 android:layout_width="0dip"
10 android:layout_height="match_parent"
11 android:layout_weight="1" >
12
13 </FrameLayout>
14
15 <SearchView
16 android:layout_width="match_parent" // I HAD TO ADD THIS BECAUSE ELSE I GOT A ERROR
17 android:layout_height="match_parent" // THIS TO
18 style="@style/android:Theme.Holo">
19 </SearchView>
20
21</LinearLayout>

add i have done this i my code

1MenuItem item = menu.add("Search");
2 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
3 SearchView sv = new SearchView(getActivity(), attributes);
4 sv.setOnQueryTextListener(this);
5 item.setActionView(sv);

but it only adds a extra searchview in the middle of my activity
and doesn't change the old one

Reply
Jeremiah
  • Forum posts: 775

Apr 26, 2012, 6:34:02 PM via Website

It would add an extra searchview in your activity because your creating one in the .xml and one in code. How does the one your creating in the xml look? Does it have the holo theme? Try this:

1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3android:layout_width="match_parent"
4android:layout_height="match_parent"
5android:baselineAligned="false"
6android:orientation="horizontal"
7style="@style/android:Theme.Holo" >
8
9<FrameLayout android:id="@+id/ListContent1"
10android:layout_width="0dip"
11android:layout_height="match_parent"
12android:layout_weight="1" >
13
14 </FrameLayout>
15
16</LinearLayout>

1LinearLayout layout = (LinearLayout)findViewById(R.id.main);
2XmlPullParser parser = resources.getXml(R.id.main);
3AttributeSet attributes = Xml.asAttributeSet(parser);
4SearchView sv = new SearchView(getActivity(), attributes);
5sv.setOnQueryTextListener(this);
6sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
7layout.addView (sv);
8layout.requestLayout();

Reply
Lars
  • Forum posts: 13

Apr 26, 2012, 8:57:35 PM via Website

i have tried it but it doesn't work for me

I did got the right theme when i tried what you said earlier

Reply
Jeremiah
  • Forum posts: 775

Apr 27, 2012, 12:41:44 AM via Website

What gave you the right theme? I'm not sure which thing I said earlier your talking about?

Reply
Lars
  • Forum posts: 13

Apr 27, 2012, 1:04:13 AM via Website

The code where there is a searchview created trough xml which has the right theme but stands in the middle of tje page

Reply
Jeremiah
  • Forum posts: 775

Apr 28, 2012, 2:11:24 AM via Website

I would use the .xml code then, you just need to figure out how to put it where you want it. Could you post a screenshot of where it puts it now, and tell me where you would like it to be?

Reply
Lars
  • Forum posts: 13

Apr 28, 2012, 11:09:15 AM via Website

i don't think that i have to move the searchview that was created in the middle of the screen because i don't need that 1,
because its not connect to my list so there's no use for it, i think i have to work with how to add a style attribute to my other searchview witch is connect
to my list and works.

Reply
Jeremiah
  • Forum posts: 775

Apr 28, 2012, 9:47:54 PM via Website

Did you try my example code:

1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3android:layout_width="match_parent"
4android:layout_height="match_parent"
5android:baselineAligned="false"
6android:orientation="horizontal"
7style="@style/android:Theme.Holo" >
8
9<FrameLayout android:id="@+id/ListContent1"
10android:layout_width="0dip"
11android:layout_height="match_parent"
12android:layout_weight="1" >
13
14 </FrameLayout>
15
16</LinearLayout>

1LinearLayout layout = (LinearLayout)findViewById(R.id.main);
2XmlPullParser parser = resources.getXml(R.id.main);
3AttributeSet attributes = Xml.asAttributeSet(parser);
4SearchView sv = new SearchView(getActivity(), attributes);
5sv.setOnQueryTextListener(this);
6sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
7layout.addView (sv);
8layout.requestLayout();

Did you notice line 7 of the .xml file?

Reply
Lars
  • Forum posts: 13

Apr 30, 2012, 3:28:30 PM via Website

I have tried you're code but the line

LinearLayout layout = (LinearLayout)findViewById(R.id.main);

can't work because main is a layout and is not defined as an id and changing it to

LinearLayout layout = (LinearLayout)findViewById(R.layout.main);

also doesn't work because i it uses findViewById.

Reply
Jeremiah
  • Forum posts: 775

Apr 30, 2012, 7:40:07 PM via Website

Add an id to the layout like this:

1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3android:id="@+id/main"
4android:layout_width="match_parent"
5android:layout_height="match_parent"
6android:baselineAligned="false"
7android:orientation="horizontal"
8style="@style/android:Theme.Holo" >
9
10<FrameLayout android:id="@+id/ListContent1"
11android:layout_width="0dip"
12android:layout_height="match_parent"
13android:layout_weight="1" >
14
15</FrameLayout>
16
17</LinearLayout>

Then use LinearLayout layout = (LinearLayout)findViewById(R.id.main); In your code.

Reply
Lars
  • Forum posts: 13

Apr 30, 2012, 8:25:01 PM via Website

i have done that but that leads me to another problem.
i get this error:

Cannot make a static reference to the non-static method findViewById(int) from the type Activity

i guess this is because i am working in an Fragment
and not in an activity

Reply
Lars
  • Forum posts: 13

May 3, 2012, 5:08:24 PM via Website

i don't think i want to change the way my fragments and activitys are created because i have had trouble with that in the past and it finally works
i think i have to try a different approach
i can inflate an searchview from an xml like this

inflater.inflate(R.layout.menu_items_klas, menu);
SearchView sv = (SearchView) menu.findItem(R.id.menu_search_klas).getActionView();
sv.setOnQueryTextListener(this);

these have the right themes because i can set the icons from an xml
but the problems with this is that the filters look kinda like active
because when i open the app it works fine but when i switch a tab
the listfragment is left blank for that tab
but then when i open the searchview and press backspace so i delete the text (but there is none in)
the list appears so the filters seems to be active than

also when i filter something in the seachview if i let the text in there
and i select an other tab the searched text is taken in the new searchview

hope you can help me!

Reply
Jeremiah
  • Forum posts: 775

May 4, 2012, 5:19:58 AM via Website

Have to admit I'm not sure what to do here, I'm not very good with ui, and no experience with fragments. I would try posting your question to XDA developers forum, http://www.xda-developers.com/
Another good forum is http://www.anddev.org but last time I checked it the forums were totally bombed with spam, hope they can get them fixed.

Reply
Lars
  • Forum posts: 13

May 5, 2012, 11:16:42 AM via Website

Alright i will try it there.
Thanks for every thing Jeremiah!!!!

Reply