edittext android add style programmatically - android

I have style:
<style name="editTextInput">
<item name="android:textColor">#000000</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:digits">0123456780.</item>
<item name="android:inputType">numberDecimal</item>
</style>
I want add this style to my EditText. like this:
TableRow tableRowEdit = new TableRow(context);
EditText editText = new EditText(context, null, R.style.editTextInput);
tableRowEdit.addView(editText);
In layout XML this style work fine:
<TableRow style="#style/tableRowInput" >
<TextView
style="#style/tvTitleStyle"
android:text="#string/diameterSmall" />
<TextView style="#style/separatorInput" />
<EditText
android:id="#+id/diameterSmallValue"
style="#style/editTextInput" />
</TableRow>
but in programmatically dont'work.

try below code
XmlPullParser parser = getApplicationContext().getResources().getXml(R.style.editTextInput);
AttributeSet attributes = Xml.asAttributeSet(parser);
EditText editText = new EditText(context,attributes );
tableRowEdit.addView(editText);

Related

how to change the button text color on AlertDialog globally using style?

on android O when i do this :
<style name="MyTheme" parent="#android:style/Theme.Material.Light.NoActionBar">
<item name="android:textColor">#ff0000</item>
</style>
The text button color of my alertdialog change, but this not work under lollipop. Worse on lollipop it's change the color of the title of the alertdialog instead.
How from kitkat to android O I can globally change the font color of the button of all my alertdialog ?
With the MaterialComponents theme and the MaterialAlertDialogBuilder you can define globally the style using the materialAlertDialogTheme attribute in your app theme.
Something like:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="materialAlertDialogTheme">#style/My_MaterialAlertDialog</item>
</style>
Then you can define a custom style:
<style name="My_MaterialAlertDialog" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<!-- Style for positive button -->
<item name="buttonBarPositiveButtonStyle">#style/PositiveButtonStyle</item>
<!-- Style for negative button -->
<item name="buttonBarNegativeButtonStyle">#style/NegativeButtonStyle</item>
<!-- Style for neutral button -->
<item name="buttonBarNeutralButtonStyle">#style/NeutralButtonStyle</item>
</style>
with the button style defined by:
<style name="PositiveButtonStyle" parent="#style/Widget.MaterialComponents.Button">
<item name="android:textColor">#FFFFFF</item>
<item name="backgroundTint">#color/primaryDarkColor</item>
</style>
<style name="NegativeButtonStyle" parent="#style/Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#color/primaryDarkColor</item>
</style>
<style name="NueutralButtonStyle" parent="#style/Widget.MaterialComponents.Button.TextButton.Dialog">
....
</style>
With the version 1.1.0 of the library you can also simply override the default color using the materialThemeOverlay in the custom style:
<style name="My_MaterialAlertDialog" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="materialThemeOverlay">#style/DialogButtonOverlay</item>
</style>
<style name="DialogButtonOverlay">
<item name="colorPrimary">#color/...</item>
</style>
You need to write a theme for AlertDialog and set it to AppTheme. It will change you alet dialog theme globally.
<style name="AppAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">#style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">#style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#color/colorPrimaryText</item>
<item name="android:backgroundTint">#android:color/transparent</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#color/colorPrimaryText</item>
<item name="android:backgroundTint">#android:color/transparent</item>
</style>
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
<item name="alertDialogTheme">#style/AppAlertDialog</item>
</style>
Create a Java Class for your custom alert dialog
public class Dialog {
private static final int resId = R.layout.dialog_dialog;
private AlertDialog alertDialog;
/**
* Custom Dialog
*
* #param context
* #param titleText
* #param message
* #param positiveText
* #param negativeText
* #param type
* #param dialogListener
*/
public Dialog(final Context context,
String titleText,
String message,
String positiveText,
String negativeText,
Type type,
final DialogListener dialogListener) {
TextView labelTitle, labelMessage, buttonPositive, buttonNegative;
View view = LayoutInflater.from(context).inflate(resId, null, false);
labelTitle = view.findViewById(R.id.labelTitle);
labelMessage = view.findViewById(R.id.labelMessage);
buttonPositive = view.findViewById(R.id.buttonPositive);
buttonNegative = view.findViewById(R.id.buttonNegative);
if (Utils.isNotEmpty(titleText)) labelTitle.setText(titleText); //(HMI2Utils.isNotEmpty is a null check
if (Utils.isNotEmpty(message)) labelMessage.setText(message);
if (Utils.isNotEmpty(positiveText)) buttonPositive.setText(positiveText);
if (Utils.isNotEmpty(negativeText)) buttonNegative.setText(negativeText);
switch (type) {
case DANGEROUS:
labelTitle.setTextColor(ContextCompat.getColor(context, R.color.white));
}
buttonNegative.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
dialogListener.onNegativeButtonClick();
}
});
buttonPositive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
dialogListener.onPositiveButtonClick();
}
});
Utils.hideSoftKeyboard((Activity) context);
ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.MyDialogTheme);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setView(view);
alertDialog = alertDialogBuilder.create();
Window window = alertDialog.getWindow();
if (window != null) {
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(ContextCompat.getColor(context, R.color.app_theme_color));
}
alertDialog.show();
}
and for xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rlRoot"
android:layout_width="736.15px"
android:layout_height="532.66px"
tools:ignore="Overdraw">
<View
android:id="#+id/focus_eater_dummy"
android:layout_width="1px"
android:layout_height="1px"
android:focusable="true" />
<ImageView
android:layout_width="736.15px"
android:layout_height="532.66px"
android:scaleType="fitXY"
android:src="your drawable" />
<RelativeLayout
android:id="#+id/container"
android:layout_width="736.15px"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="67.83px"
android:layout_marginRight="67.83px"
android:layout_marginTop="57.855px"
android:orientation="vertical">
<TextView
android:id="#+id/labelTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_horizontal"
android:maxLines="1"
android:text="#string/delete_message_string"
android:textColor="#color/white"
android:textSize="24sp" />
<TextView
android:id="#+id/labelMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18px"
android:lineSpacingExtra="18px"
android:gravity="center_horizontal"
android:lineSpacingMultiplier="1"
android:text="#string/delete_message_string"
android:textColor="#color/white"
android:textSize="24sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/containerButtons"
android:layout_width="736.15px"
android:layout_height="532.66px"
android:weightSum="2"
android:gravity="bottom"
android:layout_gravity="bottom"
android:orientation="horizontal">
<TextView
android:id="#+id/buttonPositive"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="#dimen/margin_button_vertical_dai"
android:background="#drawable/popup_button_selector"
android:gravity="center"
android:textColor="#color/white"
android:textSize="32sp"
tools:text="OK" />
<TextView
android:id="#+id/buttonNegative"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#drawable/popup_button_selector"
android:gravity="center"
android:text="Cancel"
android:textColor="#color/white"
android:textSize="32sp" />
</LinearLayout>
</FrameLayout>
make your custom changes and call the dialog like
new Dialog(context,
"Title 1",
"Message 2",
"OK",
"Cancel",
Dialog.Type.DANGEROUS,
new Dialog.DialogListener() {
#Override
public void onPositiveButtonClick() {
//implement Click here
}
#Override
public void onNegativeButtonClick() {
//implement Click here
}
}
);
dialog.dismiss();

Button text SPANNABLE not applied

I am using SpannableStringBuilder to apply to different style for a text in Button. But it is not applied.
SpannableStringBuilder spannableStringBuilder
= new SpannableStringBuilder(valueText);
spannableStringBuilder.setSpan(
new TextAppearanceSpan(mContext, R.style.ListItemButtonTitleTheme),
0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
spannableStringBuilder.setSpan(
new TextAppearanceSpan(mContext, R.style.ListItemButtonValueTheme),
6, valueText.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
holder.valueButton.setText(
spannableStringBuilder.toString(), Button.BufferType.SPANNABLE
);
list_item.xml
<Button
android:id="#+id/value_button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintDimensionRatio="1:0.7"
style="#style/ListItemButtonTheme"
app:layout_constraintTop_toBottomOf="#id/hr_line_2"
app:layout_constraintStart_toEndOf="#id/type_button"
app:layout_constraintEnd_toStartOf="#id/sell_button"
android:text="Google" />
styles.xml
<style name="ListItemButtonTitleTheme" parent="#android:style/Widget.TextView">
<item name="android:textColor">#color/list_item_content_text_color</item>
<item name="android:textSize">25sp</item>
<item name="font">#font/proxima_nova_semibold</item>
<item name="android:textAllCaps">false</item>
</style>
<style name="ListItemButtonValueTheme" parent="#android:style/Widget.TextView">
<item name="android:textColor">#android:color/black</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">20sp</item>
<item name="font">#font/proxima_nova_semibold</item>
<item name="android:textAllCaps">false</item>
</style>
Use AppCompactButton instead of Button
<android.support.v7.widget.AppCompatButton
android:id="#+id/value_button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintDimensionRatio="1:0.7"
style="#style/ListItemButtonTheme"
app:layout_constraintTop_toBottomOf="#id/hr_line_2"
app:layout_constraintStart_toEndOf="#id/type_button"
app:layout_constraintEnd_toStartOf="#id/sell_button"
android:text="Google"
/>
and change spannable to Spanned
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(valueText);
spannableStringBuilder.setSpan(new TextAppearanceSpan(mContext, R.style.ListItemButtonTitleTheme), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringBuilder.setSpan(new TextAppearanceSpan(mContext, R.style.ListItemButtonValueTheme), 6, valueText.length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.valueButton.setText(spannableStringBuilder.toString(), Button.BufferType.SPANNABLE);

Android: changing colour of floating label (hint) in TextInputLayout in error state

I want the floating label of TextInputLayouts to change colour (e.g. to red) when there is an error. I can change the colour of the error text, but it has no effect on the appearance of the floating label (unlike someone in some other thread claimed). I couldn't use selectors for the hint colour to solve this problem either, as there does not seem to be a state defined for errors. Does anyone have any idea how to do this without having to manually program the change on error events/create a new java class (with EditText as parent)?
Here are the stylings I defined:
<style name="EditTextFloatingLabel" parent="#android:style/TextAppearance">
<item name="android:textSize">#dimen/textsize_caption_small</item>
<item name="android:layout_marginBottom">8dp</item>
<item name="android:textColor">#color/input_text_color</item>
</style>
<style name="EditTextErrorText" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/error_color</item>
</style>
<style name="EditTextLayout">
<item name="android:textColorHint">#color/placeholder_color</item>
<item name="android:background">#drawable/input_field_background</item>
<item name="android:paddingBottom">#dimen/default_margin_bottom</item>
<item name="android:paddingStart">#dimen/default_margin_left</item>
<item name="android:paddingEnd">#dimen/default_margin_right</item>
</style>
<style name="EditTextTheme">
<item name="android:imeOptions">actionDone</item>
<item name="android:maxLines">1</item>
<item name="colorControlNormal">#color/primary_line_color</item>
<item name="colorControlActivated">#color/nordea_blue</item>
<item name="android:colorControlHighlight">#color/error_color</item>
<item name="android:textColorPrimary">#color/input_field_text</item>
<item name="android:textSize">#dimen/textsize_caption</item>
<item name="android:textColorHint">#color/placeholder_color</item>
</style>
<style name="EditText">
<item name="android:theme">#style/EditTextTheme</item>
<item name="android:textCursorDrawable">#drawable/cursor_blue</item>
<item name="android:paddingTop">#dimen/default_padding_top</item>
<item name="android:paddingStart">#dimen/payment_text_input_padding</item>
</style>
Usage:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_field_error_wrapper_light"
style="#style/EditTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/testlogin_text_input_end_padding"
android:layout_marginStart="#dimen/testlogin_text_input_start_padding"
android:paddingTop="#dimen/default_padding_top"
app:hintTextAppearance="#style/EditTextFloatingLabel"
app:errorTextAppearance="#style/EditTextErrorText"
app:errorEnabled="true">
<EditText
android:id="#+id/input_field_light_error"
style="#style/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#Input Field Disabled Light"
android:imeOptions="actionDone"
/>
</android.support.design.widget.TextInputLayout>
And this is what I see:
As I needed to perform some other actions as well when the layout was in error state, I decided to go with the solution of creating a custom error state and a custom TextInputLayout which can enter this state.
Code:
Defining the new error state in res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<!-- custom states -->
<declare-styleable name="ErrorState">
<attr name="state_error" format="boolean" />
</declare-styleable>
</resources>
ErrorStateTextInputLayout class:
public class ErrorStateTextInputLayout extends TextInputLayout {
private boolean hasError = false;
private static final int[] ERROR_STATE = new int[]{R.attr.state_error};
public ErrorStateTextInputLayout(Context context) {
super(context);
}
public ErrorStateTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ErrorStateTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected int[] onCreateDrawableState(int extraSpace) {
int[] state = super.onCreateDrawableState(extraSpace + 1);
if (hasError) {
mergeDrawableStates(state, ERROR_STATE);
}
return state;
}
#Override
public void setError(#Nullable CharSequence error) {
if (error == null) {
setHasError(false);
} else {
setHasError(true);
}
super.setError(error);
}
public void setHasError(boolean error) {
this.hasError = error;
refreshDrawableState();
}
}
Selector for setting the hint text colour:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:color="#color/disabled_text_color" android:state_enabled="false" />
<item android:color="#color/error_color" app:state_error="true" />
<item android:color="#color/input_text_color" android:state_focused="true" />
<item android:color="#color/placeholder_color" />
</selector>
This blog post helped me a lot: http://code.neenbedankt.com/example-of-custom-states-in-android-components/
Try this..solid_red floating color.....
<style name="TextLabelInput" parent="TextAppearance.AppCompat">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#color/solid_red</item>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/solid_red</item>
<item name="android:duration">200</item>
<item name="android:textColorHighlight">#color/solid_red</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#color/solid_red</item>
<item name="colorControlNormal">#color/solid_red</item>
<item name="colorControlActivated">#color/solid_red</item>
<item name="colorPrimary">#color/solid_red</item>
<item name="colorPrimaryDark">#color/solid_red</item>
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
app:errorTextAppearance="#style/TextLabelInput"
>
hint text color manage over your main theme
which are colorPrimary and colorAccent
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>

How to define the text color of all children in an Android Layout?

I am currently filling a TableLayout with data from external sources. After creating the TableRows and TextViews programmatically, their text color is set to white (seems to be the default).
I know that I can set the color by using myTextView.setTextColor(Color.BLACK);, but I am looking for a way to define the color in the XML layout file (I want to separate visuals from the logic). Does anyone know how to do this?
My layout (filled with example rows):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/mytable"
android:stretchColumns="0,1,2"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
style="#style/AppTheme" >
<TableRow>
<TextView android:text="field1" />
<TextView android:text="field2" />
<TextView android:text="field3" />
</TableRow>
</TableLayout>
My java code:
public void updateTable() {
TableLayout table = (TableLayout) findViewById(R.id.mytable);
table.removeAllViewsInLayout();
TableRow row = new TableRow(getApplicationContext());
TextView text;
// Add Heading
text = new TextView(getApplicationContext());
text.setText("Head1");
text.setTextColor(this.textColor);
row.addView(text);
text = new TextView(getApplicationContext());
text.setText("Head2");
text.setTextColor(this.textColor);
row.addView(text);
text = new TextView(getApplicationContext());
text.setText("Head3");
text.setTextColor(this.textColor);
row.addView(text);
table.addView(row);
for(Data d : this.getData()) {
TableRow r = new TableRow(getApplicationContext());
TextView t1 = new TextView(getApplicationContext());
TextView t2 = new TextView(getApplicationContext());
TextView t3 = new TextView(getApplicationContext());
t1.setTextColor(this.textColor);
t2.setTextColor(this.textColor);
t3.setTextColor(this.textColor);
t1.setText(d.getData1());
t2.setText(d.getData2());
t3.setText(d.getData3());
r.addView(t1);
r.addView(t2);
r.addView(t3);
table.addView(r);
}
}
Create a styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:textViewStyle">#style/MyTextViewStyle</item>
</style>
<style name="MyTextViewStyle" parent="android:Widget.TextView">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
Then just apply that theme to your application in AndroidManifest.xml:
<application […] android:theme="#style/MyTheme">…
1.Create one table_item.xml file of TextView
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:text="#string/hello_world"
android:textStyle="bold"
android:textColor="#ff00ff"/>
2.Inflate table_item.xml
LayoutInflater inflater = LayoutInflater.from(getContext());
TextView text = (TextView)inflater.inflate(R.layout.table_item, this);
3.Add into TableLayout
r.addView(text);
table.addView(r);
If you want to do dynamically then after initializing mytable in java and then proceed as
for (int i = 0; i < mytable.getChildCount(); i++) {
View view = mytable.getChildAt(i);
if(view instanceof EditText)//
((EditText)view).setTextColor(Color.BLACK);
}
Create one common style in style.xml in values folder
<style name="WelcomeTextViewTitleStyle">
<item name="android:textSize">#dimen/welcome_textview_title_textsize</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/white</item>
</style>
apply to in your textview
<TextView android:id="#+id/tv_title2"
style="#style/WelcomeTextViewTitleStyle"
android:text="#string/wantToKnow" />
try this code
<TextView android:text="assads"
android:textColor="#000000"/>
</TableRow>

How can I position my actionbar list navigation on the right side?

This image demonstrats what I need to do. I need the dropdown menu to always stay on the left next to the overflow menu icon
How can i do this?
My styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Variation on the Holo Light theme that styles the Action Bar -->
<style name="Theme.AndroidDevelopers" parent="Theme.Sherlock.Light">
<item name="android:actionBarItemBackground">#drawable/ad_selectable_background</item>
<item name="actionBarItemBackground">#drawable/ad_selectable_background</item>
<item name="android:popupMenuStyle">#style/MyPopupMenu</item>
<item name="popupMenuStyle">#style/MyPopupMenu</item>
<item name="android:dropDownListViewStyle">#style/MyDropDownListView</item>
<item name="dropDownListViewStyle">#style/MyDropDownListView</item>
<item name="android:actionBarTabStyle">#style/MyActionBarTabStyle</item>
<item name="actionBarTabStyle">#style/MyActionBarTabStyle</item>
<item name="android:actionDropDownStyle">#style/MyDropDownNav</item>
<item name="actionDropDownStyle">#style/MyDropDownNav</item>
<item name="android:listChoiceIndicatorMultiple">#drawable/ad_btn_check_holo_light</item>
<item name="android:listChoiceIndicatorSingle">#drawable/ad_btn_radio_holo_light</item>
<!-- <item name="android:actionOverflowButtonStyle">#style/MyOverflowButton</item> -->
</style>
<!-- style the overflow menu -->
<style name="MyPopupMenu" parent="Widget.Sherlock.Light.PopupMenu">
<item name="android:popupBackground">#drawable/ad_menu_dropdown_panel_holo_light</item>
</style>
<!-- style the items within the overflow menu -->
<style name="MyDropDownListView" parent="Widget.Sherlock.ListView.DropDown">
<item name="android:listSelector">#drawable/ad_selectable_background</item>
</style>
<!-- style for the tabs -->
<style name="MyActionBarTabStyle" parent="Widget.Sherlock.Light.ActionBar.TabBar">
<item name="android:background">#drawable/actionbar_tab_bg</item>
</style>
<!-- style the list navigation -->
<style name="MyDropDownNav" parent="Widget.Sherlock.Light.Spinner.DropDown.ActionBar">
<item name="android:background">#drawable/ad_spinner_background_holo_light</item>
<item name="android:popupBackground">#drawable/ad_menu_dropdown_panel_holo_light</item>
<item name="android:dropDownSelector">#drawable/ad_selectable_background</item>
</style>
<!--
the following can be used to style the overflow menu button
only do this if you have an *extremely* good reason to!!
-->
<!--
<style name="MyOverflowButton" parent="Widget.Sherlock.ActionButton.Overflow">
<item name="android:src">#drawable/ic_menu_view</item>
<item name="android:background">#drawable/action_button_background</item>
</style>
-->
<style name="customRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable" >#drawable/custom_ratingbar</item>
<item name="android:indeterminateDrawable">#drawable/money_off</item>
<item name="android:minHeight">10dip</item>
<item name="android:maxHeight">15dip</item>
<item name="android:scaleType">centerInside</item>
</style>
</resources>
As an example for implementing it as Action view, "Emanuel Moecklin" is the owner of following Code
ActionBar actionBar = getSupportActionBar();
final Context context = actionBar.getThemedContext();
final String[] entries = new String[] { "Entry 1", "Entry 2", "Entry 3" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
R.layout.sherlock_spinner_item, entries);
adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
// create ICS spinner
IcsSpinner spinner = new IcsSpinner(this, null,
R.attr.actionDropDownStyle);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(IcsAdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(context, "Item selected: " + entries[position],
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(IcsAdapterView<?> parent) {
}
});
// configure custom view
IcsLinearLayout listNavLayout = (IcsLinearLayout) getLayoutInflater()
.inflate(R.layout.abs__action_bar_tab_bar_view, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;
listNavLayout.addView(spinner, params);
listNavLayout.setGravity(Gravity.RIGHT); // <-- align the spinner to the
// right
// configure action bar
actionBar.setCustomView(listNavLayout, new ActionBar.LayoutParams(
Gravity.RIGHT));
actionBar.setDisplayShowCustomEnabled(true);
As far as I know, it is impossible to move Action bar features from the left to the right. You can, however, add a Spinner as an Action View and customize it so that it looks and behaves exactly like the Spinner from the Action bar list navigation.
Add UI components at the Right side of Actionbar using ToolBar:
Check this link for more details of ToolBar.
Screenshot attached.
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
style="#style/ToolBarStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="#dimen/abc_action_bar_default_height_material">
<RelativeLayout
android:id="#+id/rlToolBarMain"
android:layout_width="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical">
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Style for Application
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/myPrimaryColor</item>
<item name="colorPrimaryDark">#color/myPrimaryDarkColor</item>
<item name="colorAccent">#color/myAccentColor</item>
<item name="android:textColorPrimary">#color/myTextPrimaryColor</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="android:windowBackground">#color/myWindowBackground</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#android:color/white</item>
</style>
<style name="ToolBarStyle" parent="">
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
<item name="theme">#style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
</resources>
Include xml in activity_main.xml
<include
android:id="#+id/toolbar_actionbar"
layout="#layout/toolbar_default"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Your Activity must be extends AppCompatActivity
on onCreate of Activity
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
on onCreateView of Fragment
Toolbar mToolBar = (Toolbar) getActivity().findViewById(R.id.toolbar_actionbar);
RelativeLayout rlToolBarMain = (RelativeLayout)mToolBar.findViewById(R.id.rlToolBarMain);
Spinner mSpinner = new Spinner(getActivity());
String[] frags = new String[]{
"category1",
"category2",
"category3",
};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,frags);
mSpinner.setAdapter(arrayAdapter);
rlToolBarMain.addView(mSpinner);
Done
Just try this ,
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="#string/app_name">
<Spinner
android:id="#+id/Method"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:layout_weight="1"
android:gravity="right|center_vertical" />
</android.support.v7.widget.Toolbar>

Categories

Resources