I have the following SearchView implementation in the xml, the text entry has been cut from the top as shown in the below image. No matter what textSize I am putting, nothing changes.
<SearchView
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"/>
The textsize of search view text is set using AutoCompleteTextView style.
You have to override above style to change the text size.
1) Add below in your main app theme
<item name="android:autoCompleteTextViewStyle">#style/AppAutoTextCompleteStyle</item>
2) Define new AutoTextCompleteStyle as below. Provide your desired textsize in the style.
<style name="AppAutoTextCompleteStyle" parent="Base.V7.Widget.AppCompat.AutoCompleteTextView">
<item name="android:textSize">16dp</item>
</style>
try adding android:layout_margin="10dp" and android:padding="10dp".
UPDATE: Best way to implement android search interface : https://developer.android.com/guide/topics/search/search-dialog.html
Related
I have a LinearLayout with that has multiple TextViews and want to set up a default global color for that layout only without having to add a textColor field inside each TextView. Also, if it's possible, would it also be possible to override the color value by adding it inside the TextView? i.e. If I set blue as a default color and black for a single TextView, would the blue change to black?
To set the default global TextView colors, first you can create your own theme in AndroidManifest.xml file for the following items:
textColorPrimary - for Large texts
textColorSecondary - for Medium texts
textColorTertiary - for Small texts
textColorHint - for Hint texts
For example, in AndroidManifest.xml:
<style name="TextViewTheme" parent="android:Widget.TextView">
<!-- Set the default global color for TextViews to Holo Blue Dark -->
<item name="android:textColorPrimary">#android:color/holo_blue_dark</item>
<item name="android:textColorSecondary">#android:color/holo_blue_dark</item>
<item name="android:textColorTertiary">#android:color/holo_blue_dark</item>
<item name="android:textColorHint">#android:color/holo_blue_dark</item>
</style>
Next, set the theme style on your LinearLayout. You can also override the default for a single TextView to black color, like the following which set the first TextView Hint text color to black in activity_main.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="#style/TextViewTheme">
<TextView
android:id="#+id/phone_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/phone_tv"
android:textColor="#android:color/black"
android:textColorHint="#android:color/black" />
<TextView
android:id="#+id/email_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/email_tv" />
</LinearLayout>
Hope this helps!
You can override default text colors for the entire application by setting textColorPrimary and textColorSecondary in your parent in styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="textColorPrimary">#color/black</item>
<item name="textColorSecondary">#color/grey</item>
</style>
If your TextViews are indeed very many to the extent that calling setTextColor() on each of them would be a herculean task, why not use a view that supports an adapter (i.e ListView, RecyclerView etc). Your TextViews would show up the exact same way as you intend them to appear with the LinearLayout.
While using an adapter, you can set up a model TextView layout and set a global textColor for all the TextViews. You can override this global textcolor in your adapter by using simple if and else statements.
I hope this helps.. Merry coding!
This code will work even if you add or remove TextViews from your layout. Just put it in your activity's onCreate();
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTextColor(Color.BLACK);
}
}
change the color to what you like.
If you want after this code you can change the color for any specific TextView.
Pretty late answer but this does the trick.
Inside style.xml lately changed to themes.xml
<style name="ErrorStyle">
<item name="android:textColor">#FF0000</item>
</style>
Then inside your xml layout
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/ErrorStyle">
.. All of your TextView
</LinearLayout>
Using this approach we can use other xml variation such as night mode, etc... and we still have te possibility to override the internal TextViews.
fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(PrivateDealActivity.this, R.color.colorWhite)));
This is not working. How to change background color of floating action button
Using the material components FloatingActionButton and a material theme by default it uses the color with the attribute colorSecondary.
To change this color you have different options:
You can use the app:backgroundTint attribute in xml:
<com.google.android.material.floatingactionbutton.FloatingActionButton
...
app:backgroundTint=".." />
You can use a custom style using the <item name="backgroundTint"> attribute
<!--<item name="floatingActionButtonStyle">#style/Widget.MaterialComponents.FloatingActionButton</item> -->
<style name="MyFloatingActionButton" parent="#style/Widget.MaterialComponents.FloatingActionButton">
<item name="backgroundTint">....</item>
</style>
starting from version 1.1.0 of material components you can also use the new materialThemeOverlay attribute to override the default color colorSecondary only for a component:
<style name="MyFloatingActionButton" parent="#style/Widget.MaterialComponents.FloatingActionButton">
<item name="materialThemeOverlay">#style/MyFabOverlay</item>
</style>
<style name="MyFabOverlay">
<item name="colorSecondary">#color/custom2</item>
</style>
You can use this code to change the background color:
FloatingActionButton button = findViewById(R.id.your_button);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && button instanceof AppCompatButton) {
((AppCompatButton) button).setSupportBackgroundTintList(ColorStateList.valueOf(*your color in integer*));
} else {
ViewCompat.setBackgroundTintList(button, ColorStateList.valueOf(*your color in integer*));
}
Please try this.
app:backgroundTint="#android:color/darker_gray"
Simply use this attribute:
android:backgroundTint="#color/yourColor"
Your code is completely correct. I also wrote in a similar way for changing background color of floating action button programatically. Just check your xml code once.
My XML Code :
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
app:borderWidth="0dp"
android:id="#+id/fab"
android:layout_margin="16dp"
app:elevation="8dp"/>
Remember here, keeping borderWidth="0dp" is important.
Now, in Java Code :
fab = view.findViewById(R.id.fab);
fab.setBackgroundTintList(ColorStateList.valueOf(required_color));
Here you can replace required_color using Color.parseColor() or ContextCompat.getColor().
A simple solution to your problem would be to change your default colorAccent.
As stated in Android Developers-FloatingActionButton :
The background color of this view defaults to the your theme's colorAccent. If you wish to change this at runtime then you can do so via setBackgroundTintList(ColorStateList).
If it is not possible to change your color accent, regarding the requirements of your project, but you need to implement this programmatically you can post your code and I'll be more than happy to update my answer.
You could use
app:backgroundTint="#color/colorAccent"
like below
<android.support.design.widget.FloatingActionButton
android:id="#+id/add_student_house"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#color/colorAccent"
android:src="#drawable/ic_house_add"/>
just note it is app:backgroundTint not android:backgroundTint
Also you could do it by adding style:
<style name="AppTheme.FloatingActionButton" >
<item name="colorAccent">#color/colorAccent</item>
</style>
And give this style as theme in floating action button like below:
android:theme="#style/AppTheme.FloatingActionButton"
<android.support.design.widget.FloatingActionButton
android:id="#+id/add_student_house"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.FloatingActionButton"
android:src="#drawable/ic_house_add"/>
I can able to set theme to button using android:theme attribute in xml. Is there any equivalent programmatical code?
<Button
android:id="#+id/btnPayment"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Add Payment"
android:theme="#style/ButtonPeter" // theme..
/>
style.xml
<style name="ButtonPeter" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/fbutton_color_peter_river</item>
<item name="android:textColor">#android:color/white</item>
</style>
Can I able to set theme dynamically using something like this btnPayment.setTheme(R.style.ButtonPeter) ?
I search a lot but all posts are related to set style or create dynamic button and apply style to it but I dont want to do that .I want to set theme to button
How to achieve this?
I think you are confused with style and theme. From this document, it says: theme for the whole activity while style is for view.
A style is a collection of properties that specify the look and format for a View or window.
A theme is a style applied to an entire Activity or application, rather than an individual View.
For this code:
android:theme="#style/ButtonPeter" // theme is used but only valid style attributes for button will be applied
Hope this help.
As I mention here, using TextViewCompat.setTextAppearance should work :-)
So I am trying to change my app color to blue and some of most the views I have are not willing to cooperate with me.
Here is the image:
Here I want to change the color of the green parts on the spinner, edittext and checkbox views (which are green) to black or blue.
I've looked all over Stack Overflow and I can't find the solution!
Thank you very much, If possible I would like to have a XML solution but I wouldn't mind a programmatic solution!
Add these to your base theme in styles.xml
<item name="colorControlNormal">#color/YOUR_COLOR</item>
<item name="colorControlActivated">#color/YOUR_COLOR</item>
<item name="colorControlHighlight">#color/YOUR_COLOR</item>
Note: The above change will affect the EditTexts and other views probably throughout the application.
If not, and if you are using the AppCompat v22 support library, you can specify the theme in the EditText like: android:theme="#style/Theme.App.Base.
This will ensure the style won't also affect other views in your layouts that you don't want to change
Also if you want to change the above solution, just add another Theme specific to EditTexts and Spinners and apply it to all Spinners if you want
<style name="MyWidgetTheme">
<item name="colorControlNormal">#color/YOUR_COLOR</item>
<item name="colorControlActivated">#color/YOUR_COLOR</item>
<item name="colorControlHighlight">#color/YOUR_COLOR</item>
</style>
and in your EditText, Spinner or any other View, just assign this theme:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Demo"
android:lines="1"
android:theme="#style/MyWidgetTheme"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/MyWidgetTheme"></Spinner>
Take a look to this resource generator.
Choose your color, the widgets you want to generate and voila! You copy them to your project and reference them in your xmls.
I am currently using an external library in my Android project imported via gradle.
This library show a notification bar with a ProgressBar circle.
This is the code I found in it's sources :
<ProgressBar
android:id="#+id/progress_bar"
android:layout_height="match_parent"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
style="#style/SuperActivityToast_Progress_ProgressBar"/>
The style associated is this one :
<style name="SuperActivityToast_Progress_ProgressBar" parent="android:Widget.Holo.ProgressBar">
<item name="android:layout_width">32dp</item>
<item name="android:layout_marginLeft">8dp</item>
</style>
If I understand correclty, the color of the circle shown is derived from the default one ( green on my phone ).
I need to change it!
Now, I can't modify the source code, and the library itself doesn't offer me the possibility to set the style programmatically.
There is a way to change the default style at app level or better override this specific style?
Thanks
Davide
If you are using the AppCompat theme, it uses the accentColor to tint the circle.
If you want to tint it to a different color than the Theme, then you should consider using ThemeOverylay. E.g. If you want to make the circle tint red you could do the following:
in your styles.xml
<style name="RedAccent" parent="ThemeOverlay.AppCompat.Light">
<item name="colorAccent">#F00</item>
</style>
in your ProgressBar, set the theme to be RedAccent.
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="32dp"
android:layout_height="32dp"
android:theme="#style/RedAccent"/>
And your circle will now be in red color!
After several attempts I found a solution :
ProgressBar progBar = (ProgressBar) context.getActivity().findViewById(R.id.progress_bar);
if (progBar != null) {
progBar.setVisibility(View.VISIBLE);
progBar.setIndeterminate(true);
progBar.getIndeterminateDrawable().setColorFilter(0xFFFFFFFF, android.graphics.PorterDuff.Mode.MULTIPLY);
}
Simply, i'll get a reference of the progress bar object created by the library and i change it's attributes. ( in my activity i must do that in a "OnStart" method otherwise it is null )
The most important part is the "setColorFilter" that do the magic.
For future references, this change has worked for me is:
Changing the colorControlActivated within AppTheme in your values/styles.xml file:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Main theme colors -->
....
<!-- Color for circle in progress bar -->
<item name="colorControlActivated">#DC0808</item>
</style>
With this approach, you do not need to perform any action on your <ProgressBar/> tag within your xml file.
Just add color in ProgressBar like below :
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:indeterminateTint="#color/colorPrimary" // add color here
android:layout_centerVertical="true"/>