Customize Button of Android DatePicker - android

My current method to customize my UI is using the usual android DatePicker then use DatePicketDialog.getDatePicker() to get the inside component out, and customize it.
Now the result is in the image at https://dl.dropboxusercontent.com/u/3286004/Screen%20Shot%202557-08-29%20at%202.52.21%20AM.png
The Question is ... I want to customize the black line above the DONE button to another color.
Could you suggest how I can get that line component out, so I can change it.
Thank you in advance :D

This is absolutely possible, actually you could do whatever you want with it. Really, one of options is to use style and theme which however would not work in 4.x. The more, lets say, proper or easy way is to use views itself like following:
// we need this listener since only here all views are really drawn and accessible
yourDialog.setOnShowListener(new DialogInterface.OnShowListener() {
private boolean areButtonsFixed;
#Override
public void onShow(DialogInterface dialog) {
if (areButtonsFixed)
return;
// both buttons - you could search for only positive button or whatever button your dialog has
final Button btnPositive = getButton(DatePickerDialog.BUTTON_POSITIVE);
final Button btnNegative = getButton(DatePickerDialog.BUTTON_NEGATIVE);
final Button btnNeutral = getButton(DatePickerDialog.BUTTON_NEUTRAL);
// buttons layout parameters, change it into material style (gravity right)
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();
lp.weight = 0; // was 1 to fill 50% horizontally
// positive button, set your own label
btnPositive.setText(R.string.dialog_ok_label);
// set text color and size
btnPositive.setTextColor(ResHelper.getColor(R.color.blue_bright));
btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResHelper.getDimensPx(R.dimen.text_size_14));
btnPositive.setLayoutParams(lp);
// divider above buttons
((LinearLayout) btnPositive.getParent().getParent()).setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
areButtonsFixed = true;
}
This (prelast line) will remove divider above buttons at all. If you wish to customize it instead do it like following:
((LinearLayout) btnPositive.getParent().getParent()).setDividerDrawable(R.drawable.yer_drawable);

One way would be to use another theme. This theme is Holo i think, so you can't change colors.
I think you can create your dialog with a custom layout.
If you used a custom layout, you can change colors.
Or, you should use another theme, or create your own theme.
EDIT
Yep, at run-time too.
Many things using on your layout are locked, like colors, especially on widgets (searchView for example)

In default dialog it is impossible, this line has system color. You should convert this dialog to activity, then you can change color there.

Related

How to change existing TextView style in action

I have some intent with set of TextViews and a Button. If user clicks the button and there is some error I want to change look of one TextView. E.g. change the border to red and make font bold. I wrote a style for it but I have not found method setStyle on the TextView. After some self study I realized that Android does not support setting the style programmatically. There are some workarounds, when you create the intent source. But my intent already exists, it seems odd to recreate it.
Could you tell me the proper way?
use the workaround and create the TextView again
forget the styles and use java methods to decorate existing TextView
something else
Changing the style of the textview directly does not work as you know. But you can create a second textview with other styles in your layout, which you can show up if needed.
Just add this xml attribute android:visibility="gone" to the second textview, so this second textview is not displayed at first, but available.
When you now want to change the style of your textview, you simple need to swap the two textviews by hidding the first one and showing the second one
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.VISIBLE);
I used these two answers to make it work:
https://stackoverflow.com/a/5488652/1639556
https://stackoverflow.com/a/14195090/1639556
and the code is:
ViewManager parent = (ViewManager) unknown.getParent();
parent.removeView(unknown);
TextView newUnknown = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
newUnknown.setId(unknown.getId());
parent.addView(newUnknown, unknown.getLayoutParams());
unknown = newUnknown;
You can try using setTextAppearance() on the textview. The link is: setTextAppearance
Your style will need TextAppearance.SomeThing.SomeOtherThing as the parent.
Use R.style.YourStyleName as the integer argument.

Setting backgroundcolor in theme gives graphical buggy dialog

In my activity theme in the themes.xml I have set a background color in order to move away from the default (transparent/white?) background color to my own.
<item name="android:background">#color/red</item>
Unfortunately, when the I am showing my loading dialog the color shines halfway through that dialog now. Was this to be expected?
I have tried to use different themes, also defined by own dialog theme subclassing from Holo Light setting the background color explicitly to white, but the problem persists, only the currently still white areas are changed in this case.
What can I do? The only alternative is currently to use the Tradiotional Dialog Theme.
Try to set android:windowBackground instead. android:background attribute is applied to all nested views. Here is the discussion: What's the difference between windowBackground and background for activities style?
It looks like there's some padding or margins to the left and right of the title. If you're using the built-in ProgressDialog I'd suggest creating your own Dialog instead, that way you can change anything you want about it. Just create your own xml layout and create the dialog like this:
protected static Dialog getProgressDialog(Activity activity) {
Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View progressDialogView = inflater.inflate(R.layout.custom_progress_dialog, null);
dialog.setContentView(progressDialogView);
dialog.setCancelable(false);
return dialog;
}

How to add style to a NumberPicker in Android

I am trying to add a custom style to an Android NumberPicker. What I am trying to do is have the text displayed by the picker as white since by default (or according to the default theme on the device) I have it in black. The problem is that my app background is a dark color so I want the text to be white or something clear.
so I have something like this in my style.xml file :
<style name="myPicker">
<item name="android:textColor">#FFFFFF</item>
</style>
the textcolor attribute doesn't even exist on the NumberPicker widget but I just tried to add it so that my text color changes. It does not work of course. The only thing that can be customized on this widget seems to be the background
Now the question : how can we change the text color of the picker ?
if somebody has the same issue or has solved it then please let us know.
I have found an "ugly" way to do it. Just get a reference to the child views of the number picker widget. There are three (3) child views : the upper arrow, the textview (containing the text or value to be displayed) and the lower arrow.
Let's say we have a Number picker called np
np.setValue(10);
np.setMinValue(5);
np.setMaxValue(50);
// retrieve the textview reference
TextView npTextView = (TextView) np.giftAmount.getChildAt(1); // since indexing begins at 0
npTextView.setTextColor(getResources().getColor(
R.color.my_custom_color)
I know that's bad but it works ...

Overlapping views crossing boundaries (Fake actionbar)

I am trying to create a custom title bar strictly for Android 2.1 that somewhat emulates the ActionBar found in Android 3.0 and up.
So far I've had fairly decent luck, but I cannot find an easy way to make the drop-down lists that can appear below the icon / buttons in the ActionBar.
I would like these drop-down lists to be able to cross the boundary between the title layout and the content layout without being clipped / cut off. I would also like the drop-down lists to be positioned relatively below the icons / buttons in the ActionBar. I've seen advice to use FrameLayouts and RelativeLayouts already but these did not seem to solve my particular problem (unless I'm looking at it wrong). I keep thinking it must be possible since I believe the newer versions of Android have support for exactly this type of behavior.
Here is a screenshot of what I am trying to accomplish with some notes.
http://img217.imageshack.us/img217/3279/67343249.png
Please help. Thanks!!!
I think best way to do that is to create a linear layout and add your drop-down list item into that linear layout. Then add linear layout on button click and display it just below to the button by using relative layout layoutparams.
You need to hold your content with a FrameLayout. A FrameLayout can hold more than one child, and it will allow them to overlap on each other.
A possible layout xml could look like:
<FrameLayout>
<LinearLayout android:margin_top="30dip">...</LinearLayout>
<CustomActionBar />
</FrameLayout>
The 30dip is just the height of your ActionBar, you might want to define it in values. And the LienarLayout you can replace with anything which is just for holding the other content in your application.
PS. I am not sure if FrameLayout renders first child first or last. In case you found the linearlayout stuff overlaps the customactionbar, swap their position.
=== Edit ===
To make the code much cleaner, one can do in this way:
<CustomActionBar>
<LinearLayout/>
</CustomActionBar>
Which replaced the custom action bar to be the "root" element of any xml layout that utilized the action bar. When doing the custom action bar, you need to extend it from FrameLayout, and assign the child's margin accordingly. Which the idea is much the same as the one I presented above.
After a few days of research I found the best way to implement this is to use a custom dialog box with a custom theme that contains a listView. When the user clicks on the button the dialog should be displayed directly below the button. This provides a good user experience and also means you don't have to completely change your basic layout structure to accomodate this functionality. (This is basically the same approach that ActionBar Sherlocke uses for their backwards compatibility menus)
Here is a code snippet of what I'm describing (this is written in C# using Mono for Android but it should be easy to translate to Java):
Dialog dlgHome = new Dialog(this, Resource.Style.ActionBarMenu);
dlgHome.SetCanceledOnTouchOutside(true);
dlgHome.SetContentView(Resource.Layout.ActionBarMenu);
ListView lsvHome = (ListView)dlgHome.FindViewById(Resource.Id.lsvHome);
lsvHome.Adapter = new ArrayAdapter<string>(this, Resource.Layout.ActionBarMenuItem, new string[] { "Add Activity", "Edit Log", "Program Details" });
Rect r = new Rect();
btnHome.GetLocalVisibleRect(r);
int x = r.Left + (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 5, Resources.DisplayMetrics);
int y = r.Bottom;
WindowManagerLayoutParams param = dlgHome.Window.Attributes;
param.Gravity = (int)(GravityFlags.Top | GravityFlags.Left);
param.X = x;
param.Y = y;
dlgHome.Window.Attributes = param;
Also, here is the style xml (goes in style.xml)
<style name="ActionBarMenu" parent="android:Theme.Dialog">
<item name="android:windowBackground">#color/actionbar_menu_bg</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>

Get an EditText's 'default' color value from theme

I have an Activity that contains an EditText on 3.1. Based on user input, I change the color of the text in the EditText (red for an error), and then reset it to black when the text is OK.
One issue relates to changing the overall theme of the Activity. For instance, changing it to the regular dark theme from the light theme results in the black text being shown against a black background - so I need to go in and change the code, instead resetting the text to white when the data is OK.
Instead of having to change this code if I make a theme change to the Activity, I was wondering if there was a way to pull the default EditText text color for a given theme programmatically, then I can just switch the text back to the default color instead of hard-coding in the white, black, etc.
According to the Theme's docs get the colour directly using obtainStyledAttributes.
TypedArray themeArray = context.getTheme().obtainStyledAttributes(new int[] {android.R.attr.editTextColor});
try {
int index = 0;
int defaultColourValue = 0;
int editTextColour = themeArray.getColor(index, defaultColourValue);
}
finally
{
// Calling recycle() is important. Especially if you use alot of TypedArrays
// http://stackoverflow.com/a/13805641/8524
themeArray.recycle();
}
Use R.attr.
setTextColor(android.R.attr.editTextColor)
EditText.getCurrentTextColor() and EditText.getTextColors() will also provide the default colour if you retrieve them before changing the colour. Additionally this approach can be used pre 3.0 which is not possible when using android.R.attr.editTextColor.

Categories

Resources