So I'm programmatically creating new buttons and adding them to a LinearLayout, however I want to initialize these buttons with a predefined style. I've spent some time searching for a solution and trying out answers, but I still can't seem to get it to work.
When I add a new button to the layout, it should look like the buttons (near the top) in this picture.
I've tried creating an xml file in res/values/ and initializing a button with new Button(context, null, R.style.ChoiceButton), but it doesn't work resulting in this happening.
I've also tried the workaround of creating a new layout xml for the button and using (Button)getLayoutInflater().inflate(R.layout.choice_buttton_layout, null), but that also didn't work, resulting in this (two buttons to show lack of margin).
res/values/choice_button.xml
<resources>
<style name="ChoiceButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:layout_marginBottom">7dp</item>
<item name="android:minWidth">250dp</item>
<item name="android:background">#ff27ae60</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:enabled">true</item>
</style>
</resources>
Snippet from Main.java
public void btnAdd_click(View view) {
Button newBtn = new Button(getApplicationContext(), null, R.style.ChoiceButton);
newBtn.setText("new button");
newBtn.setId(Util.generateViewId());
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutTop);
layout.addView(newBtn);
}
activity_main.xml
A bit long to paste in here.
Is there just something I'm missing? Is this even possible?
Ok #kin3tik, I found an old application I made with some custom button.. see what it looks like :
there is my xml for one button :
<Button
android:id="#+id/num1"
android:layout_width="110dp"
android:layout_height="100dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="#string/num1"
android:textSize="20sp" />
I created file .xml in my drawable folder custombutton.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/bleuperso"/> // you can put #XXXXXX for the color you want
<corners android:radius="4dp"/>
And I just put the style in java :
Bfrancois.setBackgroundResource(R.drawable.custombutton);
With this you should be able to find yourself ;)
Related
I want to make grey my seekbar when I disable it.
I know I can programmatically make this changes by changing color but I'd like to know if it is possible to do this directly in my xml activity layout.
In fact in my code I'd like to just write this:
mySeekBarNuance.setEnabled(true);
And automatically my seekbar becomes grey.
Thanks
Do it like this
In your styles.xml
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> // or the themw you are using
...
...
<item name="android:seekBarStyle">#style/MySeekBarStyle</item>
</style>
Add also a new style
<style name="MySeekBarStyle" parent="android:Widget.SeekBar">
<item name="android:colorControlNormal">#color/gray</item> // put here the color for disable state
</style>
controlColorNormal is used for disabled state and colorAccent for enabled state
You can use a selector as background for the Seekbar
A selector set what you want if condition are met (here : android:state_enabled="true")
In your activity/fragment layout you put the seekbar with a background that is a selector :
<SeekBar
android:id="#+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector_seekbar"/>
You make the selector in your drawable folder :
selector_seekbar.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#android:color/darker_gray" />
<item android:state_enabled="true"
android:drawable="#android:color/holo_green_dark" />
</selector>
If enabled, the seekbar will be holo green, otherwise it will be gray
You can do the same thing if the seekbar is focused, pressed etc thaht way.
And you can not only set the color but an entire drawable instead
I'm doing some UI designing for class on an open source project. I'm trying to make the appbar and tabs of this tablayout transparent but no matter what I do they remain white. If it were transparent the background would be able to be seen but i get this white instead.
I have tried-
changing the xml values in the mainfragments xml to transparent
changing the colors.xml values to transparent (all of them because i wanted
to be sure)
changing and even removing the "light theme" and "dark theme" values in
styles.xml and removing the app theme line in the android manifest
android:theme="#style/AppTheme.FullScreen.Light"
Am I missing something else that i can try? I would be very thankful for any suggestions.
in style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
in color.xml
value #color/transparent is the color value #00000000
<activity android:name=".YourActivity" android:theme="#style/Theme.Transparent">
</activity>
set alpha value in layout XML or dynamically in view to make view transparent.
view.setAlpha(40);
oR add from xml - value between 0 to 1
android:alpha="0.4"
In android Studio res->values->colors.xml
Step 1 : For selecting color
Step 2 : Changing transparency(you can move left to right for adjusting transparency )
Step 3 : Give this color as views background
<RelativeLayout
android:id="#+id/top_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/color_name">
This may help you.
How do I change the color/s in my styles.xml which looks like this
<?xml version="1.0" encoding="utf-8"?>
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#FF9800</item>
<item name="colorButtonNormal">#FF9800</item>
<item name="android:colorPrimaryDark">#FF9800</item>
<item name="android:navigationBarColor">#FF9800</item>
</style>
when I toggle my ToggleButton in my activity_main.xml, which looks like this
<ToggleButton
android:id="#+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Light Theme"
android:textOff="Dark Theme"
android:onClick="onToggleClicked"
android:layout_above="#+id/button"
android:layout_centerHorizontal="true" />
so far I have done this to make that behavior happen in my MainActivity.java
public void onToggleClicked(View view) {
// check if toggle active
boolean on = ((ToggleButton) view).isChecked();
if (on) {
} else {
}
}
The reason is that I want to make a dark and a light theme for my app:)
You cannot dynamically change the appTheme. But there is a workaround.
Check out this link.
Android - Change app Theme on onClick
Themes are, unfortuantly, immutable, meaning that you can't change them programatically during runtime.
Nor can you simply set a style. You can, however, change the style by inflating a new view. You may find this thread useful.
This question already has answers here:
Android: disabling highlight on listView click
(15 answers)
Closed 6 years ago.
I want to disable the highlight that appears when the user selects a row (listSelector) from code. I don't want to disable the onClick and enabled settings (I still want to listen to clicks, just want to remove the highlight).
Specify android:listSelector="#android:color/transparent" in your ListView XML.
Just create a drawable that has a transparent color in it, something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#android:color/transparent"/>
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="true" android:drawable="#drawable/list_focused_holo" />
</selector>
And then set by code or by XML:
listView.setSelector(R.drawable.my_transparent_selector);
The javadoc for this method says:
Set a Drawable that should be used to highlight the currently selected item.
and the XML attribute is:
android:listSelector
You can play with all the states, remember that you also have the focus state.
I have done this way:
By adding two properties of ListView.
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent"
Your ListView should looks like below:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent">
</ListView>
Done
try listview.setSelector(new ColorDrawable(Color.TRANSPARENT));
The highlight effect is a style on the listSelector. You can override the listSelector style.
This is a example with a listview : Android: disabling highlight on listView click
I'm attempting to override the default ToggleButton appearance. Here's the XML that defines the ToggleButton:
<ToggleButton android:id="#+id/FollowAndCenterButton"
android:layout_width="30px"
android:layout_height="30px"
android:textOn="" android:textOff="" android:layout_alignParentLeft="true"
android:layout_marginLeft="5px"
android:layout_marginTop="5px" android:background="#drawable/locate_me"/>
Now, we have two 30 x 30 icons we want to use for the clicked/non-clicked states. Right now we have code that programmatically changes the background icon depending on the state:
centeredOnLocation.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (centeredOnLocation.isChecked()) {
centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me_on));
} else {
centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me));
}
}
});
Obviously I'm looking for a better way to do this. I've tried to make a selector for the background image, which would automatically switch between the states:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/locate_me" /> <!-- default -->
<item android:state_checked="true"
android:drawable="#drawable/locate_me_on" /> <!-- pressed -->
<item android:state_checked="false"
android:drawable="#drawable/locate_me" /> <!-- unchecked -->
But this does not work; reading the ToggleButton API (http://developer.android.com/reference/android/widget/ToggleButton.html), it appears that the only inherited xml attributes are
XML Attributes
Attribute Name Related Method Description
android:disabledAlpha The alpha to apply to the indicator when disabled.
android:textOff The text for the button when it is not checked.
android:textOn The text for the button when it is checked.
There does not seem to be the android:state_checked attribute, despite the class having the method isChecked() and setChecked().
So, is there a way to do what I want in XML, or am I stuck with my messy workaround?
Your code is fine. However, the toggle button will display the first item in your selector that it matches, so the default should come last. Arrange the items in the following manner to ensure they will all be utilized:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" /> //currently pressed turning the toggle on
<item android:state_pressed="true" /> //currently pressed turning the toggle off
<item android:state_checked="true" /> //not pressed default checked state
<item /> //default non-pressed non-checked
</selector>