I'm trying to enable users to change layout of my apps.
This is my XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menuTheme"
android:orderInCategory="1"
android:showAsAction="never"
android:title="Theme">
<menu>
<item
android:id="#+id/themeBlack"
android:title="Black" />
<item
android:id="#+id/themeWhite"
android:title="White" />
</menu>
</item>
<item
android:id="#+id/menuAbout"
android:orderInCategory="1"
android:showAsAction="never"
android:title="About" />
Here are the screenshots from my emulator:
This is my menu.
After clicking the item Theme under the menu, this submenu will appear.
Lets say i wish to add a white layout, where and how should i add codes?I have a XML file named themewhite.XML in the res/layout folder. I tried to add a switch in the item theme but it doesn't seemed to be correct.
This is my code:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.menuTheme:
{
switch(item.getItemId())
{
case R.id.themeWhite;
{
setContentView(R.layout.themewhite);
}
}
break;
}
case R.id.menuAbout:
{
break;
}
}
return super.onOptionsItemSelected(item);
}
I guess you don't want to set new layout, you want change just visualisation of existing.
If so you should manilpuate with themes of your activity. For first you need to create themes (white and so on) and decalre views attributes with using values from style. And then in your listener after click one of items, you should change your theme by calling
getContext().setTheme(resid)
Here a good article about themes http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html
Related
I integrated cast to my app, and the default cast button color is black, but i'd prefer it white.
So I have 2 questions :
From what style does it taken from default ?
How can i change the style or color of the icon ?
Im using cast sdk v3.
I read the other related posts but they're all relevant to sdk v2.
this is my menu item layout as in google sample :
<item
android:id="#+id/media_route_menu_item"
android:title="#string/media_route_menu_title"
app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
app:showAsAction="always"/>
this is the menu code to create the button :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_podcast, menu);
mMediaRouteItem =
CastButtonFactory.setUpMediaRouteButton(getApplicationContext(),
menu,
R.id.media_route_menu_item);
return true;
}
Solution was simple as changing the activity theme to extend Theme.AppCompat instead of Theme.AppCompat.Light.DarkActionBar
Try to check the Customize App guide for Castv3 in Android. You can customize the colors, the overlay style, controller style, and more.
Customize Theme
The IntroductoryOverlay class supports various style attributes that your app can override in a custom theme. This example shows how to override the text appearance of both the button and title over the overlay widget:
<style name="CustomCastIntroOverlay" parent="CastIntroOverlay">
<item name="castButtonTextAppearance">#style/TextAppearance.CustomCastIntroOverlay.Button</item>
<item name="castTitleTextAppearance">#style/TextAppearance.CustomCastIntroOverlay.Title</item>
</style>
<style name="TextAppearance.CustomCastIntroOverlay.Button" parent="android:style/TextAppearance">
<item name="android:textColor">#FFFFFF</item>
</style>
<style name="TextAppearance.CustomCastIntroOverlay.Title" parent="android:style/TextAppearance.Large">
<item name="android:textColor">#FFFFFF</item>
</style>
I am trying to check a default item in a menu group. I am not changing anything in the menu using code.
The menu is as follows:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Map type">
<menu>
<group
android:checkableBehavior="single"
>
<item
android:id="#+id/nav_map_type_normal"
android:title="Normal"
android:checked="true"
/>
<item
android:id="#+id/nav_map_type_satellite"
android:title="Satellite"
/>
<item
android:id="#+id/nav_map_type_terrain"
android:title="Terrain"
/>
<item
android:id="#+id/nav_map_type_hybrid"
android:title="Hybrid"
/>
</group>
</menu>
</item>
<item
android:id="#+id/nav_toggle_traffic"
android:title="Toggle traffic"
/>
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
/>
</menu>
When the menu is first displayed, the first item (Normal) is highlighted to indicate it is checked. This is what I want to happen. I then want the highlighting to move through the group as each item is pressed.
The problem is:
If I press Satellite, BOTH Normal and Satellite are highlighted
If I then press Hybrid, Normal remains highlighted, Hybrid is
highlighted and Satellite reverts to its unchecked state.
Only after actually pressing Normal does its checked state behave as it should.
I have tried removing the android:checked="true" and using performIdentifierAction in code, but this did not change the checked state.
Since you are making NORMAL as default TRUE, you need explicitly SET it to false as and when Satellite, Hybrid or Terrain is selected.
I believe currently, you would be setting NORMAL as android:checked=false on the onclickmenu selection code of nav_map_type_normal. i.e you are basically
un-checking it as by default it is selected as TRUE. Please do the same when other options are selected.
Sorry, I forgot to mention that the menu is used in a NavigationView and as it it turns out that changes everything.
navigationView.setCheckedItem(R.id.nav_map_type_normal);
performs exactly the way I want. I found it in a comment here Navigation drawer: How do I set the selected item at startup?
I tried to create a menu with search and refresh button, but since refresh button will only be available at several activities, I want to set the visibility to false, and then add this:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
MenuItem item = menu.findItem(R.id.action_refresh);
item.setVisible(true);
return true;
}
to the activity java
Using the below code, the menu is displayed properly, with refresh on the left and search on the right
<item
android:id="#+id/action_search"
android:title="Search"
android:menuCategory="system"
android:orderInCategory="2"
android:showAsAction="always"
android:icon="#drawable/ic_action_search"/>
<item
android:id="#+id/action_refresh"
android:title="Refresh"
android:menuCategory="system"
android:orderInCategory="1"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:visible="false"/>
But by reversing the item order,
<item
android:id="#+id/action_refresh"
android:title="Refresh"
android:menuCategory="system"
android:orderInCategory="1"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:visible="false"/>
<item
android:id="#+id/action_search"
android:title="Search"
android:menuCategory="system"
android:orderInCategory="2"
android:showAsAction="always"
android:icon="#drawable/ic_action_search"/>
the refresh button/item will not appear.
While I have found the workaround and could proceed, this still puzzles me very much, and I haven't got any good explanation.
Why would that happen?
Are you using Eclipse?
I have seen a bug in Eclipse where simply changing the order of elements in an XML resource file would not recompile the file causing all kinds of weird things to happen.
I suggest you do a "Project > Clean" to make sure the resource identifiers get recreated from scratch.
I have a two level PreferenceScreen:
<PreferenceScreen>
general settings
<PreferenceScreen android:key="adv_settings">
more advanced settings
</PreferenceScreen>
</PreferenceScreen>
My problem is that the second screen doesn't show the back/up button on the action bar automatically. How do I make the up button appear on adv_settings?
You can add the arrow by writing a custom ActionBar style to be used with your application theme.
res/values-v11/styles.xml: (or add these to your existing styles.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:displayOptions">showHome|homeAsUp|showTitle</item>
</style>
</resources>
Then apply this theme in your AndroidManifest.xml:
<application android:theme="#style/MyTheme">
Note: The obvious way to add this arrow should be to call:
getActionBar().setDisplayHomeAsUpEnabled(true);
once the second screen has loaded, but I think there's an Android bug where getActionBar() always returns the first-tier ActionBar object, as opposed to the one that is currently visible, so setting the arrow dynamically fails.
This may be more work, but you could create two PreferenceAtivity files each with their own PreferenceFragment. Each PreferenceFragment will have its own PreferenceScreen XML (the first level screen, and the second. level screen). From the firsts level screen you launch the second PreferenceActivity with an Intent within the tag. In the second PreferenceActivity you can set the home icon by doing this:
ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
and then also had a handler for the home button:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == android.R.id.home) {
finish();
}
return false;
}
Assets:
FirstPreferenceActivty
FirstPreferenceFragment
pref_first.xml (layout with PreferenceScreen and Prefernce nodes)
SecondPreferenceActivty
SecondPreferenceFragment
pref_second.xml (layout with PreferenceScreen and Prefernce nodes)
If the button does not come automatically, you could add it manually like done on these links:
Android: How can make custom PreferenceScreen?
http://pastebin.com/334Eip35
There is a example code in second answer of that SO question and the snippet in it is taken probably from the another pastebin location.
I have a side navigation menu that I want to be able to utilize the dpad with. I am coding in java. I have xml document for states of the buttons and can't get the state to show when my dpad is on the item, but not selected (press ok). I can navigate down to the next, state stays same, then press okay and the new screen associated with this tab displays. I want to show the user they are hovered on the next item. How?
Not sure if its different on Google TV, but android:state_focused="true" should be what you want in your state list.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/buttonpressed"
android:state_pressed="true" />
<item android:drawable="#drawable/buttonfocused"
android:state_focused="true" />
<item android:drawable="#drawable/button"
/>
</selector>