I want to add a check box to Navigation Drawer, and I want one of them to be default checked
I'm using this: app:actionViewClass="android.widget.Switch" as suggested in this answer.
But I couldn't figure out how to make one of them default checked. If I use this property android:checked="true", then the option is checked instead of checkbox (See image).
Does anyone know how to make it default checked (if possible, I want to do it in XML only)?
Here is my activity_drawer.xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<group
android:checkableBehavior="single">
<item
android:id="#+id/checkboxX-axis"
android:title="Show x-axis"
android:icon="#drawable/ic_x_axis_black_24dp"
app:actionViewClass="android.widget.CheckBox"
android:checked="true"
/>
<item
android:id="#+id/checkboxY-axis"
android:title="Show y-axis"
android:icon="#drawable/ic_y_axis_black_24dp"
app:actionViewClass="android.widget.CheckBox"
/>
<item
android:id="#+id/checkboxZ-axis"
android:title="Show z-axis"
android:icon="#drawable/ic_z_axis_black_24dp"
app:actionViewClass="android.widget.CheckBox"
/>
</group>
</menu>
You can do this:
MenuItem item = navigation.getMenu().findItem(R.id.checkboxX-axis);
CompoundButton compoundButton = (CompoundButton) item.getActionView();
compoundButton.setChecked(true);
replace navigation with the NavigationView's name.
Related
I want to add a background image to an item in a navigation drawer.
What I currently have:
Desired output:
On each column there would be a background image. I'm going to remove the text and just fill items with images. Is this possible? If yes, do I need different resolutions for background images?
activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_ITEM1"
android:title="#string/ITEM1" />
<item
android:id="#+id/nav_ITEM2"
android:title="#string/ITEM2" />
<item
android:id="#+id/nav_ITEM3"
android:title="#string/ITEM3" />
</group>
</menu>
I have this activity_main_drawer.xml file containing my drawer to my main activity:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/cityAdd"
android:icon="#drawable/ic_menu_camera"
android:title="Add City" />
<item
android:id="#+id/cityList"
android:title="Cities">
<menu>
<item android:title="I want to add these items"></item>
</menu>
</item>
</group>
</menu>
I am trying to reference my menu through Java so I could programmatically add items to my drawer while my app is running. This is the method(I think that's what it's called I'm still a noob) :
private void addMenuItemNavMenuDrawer(String added) {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
menu.add(added);
}
The problem is the items are not being added to the menu making this code useless.
If any other parts of my code are needed I will be happy to share. Any help is appreciated!
I found that it was due to me not understanding static and non-static variables that I created earlier in my code.
I added share button to my ActionBar, but icon of that button seems not ok.
Default button looks like that:
https://monosnap.com/image/yiBpHo6XcMWKmXD1zwcy2w2gRkFBPN.png
But in my case it looks just like "SHARE" textin actionBar:
https://monosnap.com/image/d8cXbr5XIFtwItoaXAKkTjVBPr5eEa.png
How to change it to default?
Here is my menu xml file for activity:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.XXXXXXX.XXXXXXXXXXX.app.SomeActivity" >
<item
android:id="#+id/menu_item_share"
app:showAsAction="always"
android:title="Share"
android:actionProviderClass= "android.widget.ShareActionProvider" />
</menu>
add this
android:icon="#android:drawable/ic_menu_share"
<item
android:id="#+id/menu_item_share"
android:orderInCategory="100"
android:title="Share"
android:icon="#drawable/ic_launcher"
app:showAsAction="always"/>
On an Android View, I want to have a some clickable text that works similar to a webpage hyperlink.
It looks just like normal text (no button border), but when touched, I want the text color to change and I may also want its background to turn a reverse color.
Is it better to use a Button with a transparent background or a TextView. When should I choose one over the other?
Thanks much
you can use Button and make selector for background and text both.
textselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#0000ff"/> <!-- pressed -->
<item android:state_focused="true" android:color="#android:color/white"/> <!-- focused -->
<item android:color="#android:color/white"/> <!-- default -->
</selector>
buttonselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#android:color/white"/> <!-- pressed -->
<item android:state_focused="true" android:color="#0000ff"/> <!-- focused -->
<item android:color="#0000ff"/> <!-- default -->
</selector>
In your layout xml
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonselector"
android:text="some text"
android:textColor="#drawable/textselector" />
hotveryspicy gave a great answer and really pointed me in the right direction. Thank you, but I had a few issues with the response.
What I want is a button that has Black text and a Gray background in its normal state and a Red background with White text in its selected state. Like this:
Using hotveryspicy's suggestions I had the following issues:
I get an error when trying to assign a drawable to textColor. Funny but I see other references to doing this sort of thing. Maybe the XML validation is stricter now?
I also got an error if I didn't define a android:drawable in the buttonselector.xml. It seems you can set the color of a drawable in a selector eiter, you need to actually create a drawable.
Anyhow this is what I did:
I created a Color State Resource that looks like this:
File: res/colors/link_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ffffff"/>
<item android:color="#000000"/>
</selector>
Then the following 3 drawables:
My button background in its normal state using a Shape Drawable:
File: res/drawable/link_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#cccccc"/>
</shape>
My button background in its selected state, also a Shape Drawable:
File: res/drawable/link_button_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff0000"/>
</shape>
Then I created a Drawable Selector that chooses between the 2 shapes.
File: res/drawable/link_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/link_button_selected"
android:state_pressed="true" />
<item
android:drawable="#drawable/link_button" />
</selector>
Then in my layout my button looks like this:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Button"
android:textColor="#color/link_button"
android:background="#drawable/link_button_selector"
/>
Thanks for the help everyone!
Add String like
<string name="link">'Google'</string>
In your layout
<TextView
android:id="#+id/link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:linksClickable="true"
android:text="#string/link" />
You can linkify your text using the linkify class.
Have a look at this and this with few examples
For Google's sake:
Rather than make a button look like a link why not make a TextView clickable? This was effectively what I wanted and worked for my use. I wanted text to be clickable to spawn an intent.
TextView.setOnClickListener(new View.OnClickListener()
I want to create a customized submenu where i can add an "about page" to my application. So my submenu would just be a text view with a bunch of info. Im currently defining my menu in XML. Any help or links would be great, thanks!!!
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/settings_menu"
android:title="Settings" />
<item android:id="#+id/about_menu"
android:title="About">
<menu>
<item android:id="#+id/about_submenu"
android:title="Tip Message" />
</menu>
</item>
<item android:id="#+id/quit_menu"
android:title="Quit" />
</menu>
Also just out of curiosity, how would i define a more complex custom submenu, like one that takes user input from a keyboard.
I think it's easier to make something look like a menu. Try Activity with #android:drawable/menu_submenu_background as a background for example.