I keep on getting this error when I try to setErrorEnabled on my textInputLayout :
03-12 12:29:03.206 5706-5706/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp, PID: 5706
java.lang.RuntimeException: Failed to resolve attribute at index 24
at android.content.res.TypedArray.getColor(TypedArray.java:401)
at android.widget.TextView.<init>(TextView.java:696)
at android.widget.TextView.<init>(TextView.java:632)
at android.widget.TextView.<init>(TextView.java:628)
at android.widget.TextView.<init>(TextView.java:624)
at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:380)
at com.bekwaai.popupwindow.RGNamePopUp$1.onClick(RGNamePopUp.java:48)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
This is the scenario - I have a textInputLayout inside a popupwindow. This is the code inside the popupwidow.
public class NamePopUp extends PopupWindow {
android.support.v7.widget.AppCompatEditText name;
TextInputLayout nameInput;
public NamePopUp(final Context context) {
super(context);
View popupView = LayoutInflater.from(context).inflate(R.layout.popup_rg_confirm, null);
nameInput = (TextInputLayout) popupView.findViewById(R.id.name_input_layout);
}
}
This is my xml for the popupwindow layout:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:theme="#style/TextLabel"
app:backgroundTint="#color/white"
app:rippleColor="#color/white"
android:id="#+id/name_input_layout"
android:layout_marginBottom="8dp"
android:paddingRight="24dp"
android:paddingLeft="24dp">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/enter_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textCursorDrawable="#drawable/color_cursor"
android:hint="#string/groupname"/>
</android.support.design.widget.TextInputLayout>
This is the style I'm using:
<style name="TextLabel" parent="TextAppearance.AppCompat">
<!-- Hint color and label color in FALSE state -->
<item name="android:textSize">20sp</item>
<item name="android:textColorHint">#color/white</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#color/white</item>
<item name="colorControlNormal">#color/white</item>
<item name="colorControlActivated">#color/white</item>
</style>
The popupwindow is called inside a android.support.v4.app.Fragment and the fragment is inside a AppCompatActivity.
The error occurs here when the user clicks the ok button but the name edittext is blank. In other words, the user has not entered anything in the edittext and have clicked ok:
button_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (StringUtils.isNotEmpty(name.getText().toString())) {
nameInput.setErrorEnabled(false);
groupNameEvent.doEvent(name.getText().toString());
} else {
nameInput.setErrorEnabled(true); //ERROR OCCURS HERE!
nameInput.setError(context.getString(R.string.no_name));
}
}
});
How do I get this error to disappear?
Just change the base theme for TextLabel to Widget.Design.TextInputLayout
<style name="TextLabel" parent="Widget.Design.TextInputLayout">
For me a following works (https://stackoverflow.com/a/42779409/2914140):
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="textColorError">#color/design_textinput_error_color_light</item>
</style>
Related
I am working on an app in Android Studio, and after adding buttons to MainActivity to move to other activities, despite the fact that the layout shows up properly in the editor, I get the error which will be posted at the end. There are other activities in my project which I did not include, because the error refers to activity_main.xml, but I will include them if they are necessary to solve the problem.
Here is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/view_img_main_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/grocery_img_1" />
<Button
android:id="#+id/btn_inventory"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_below="#id/view_img_main_menu"
android:text="#string/btn_my_groceries_text"
android:theme="#style/BtnTheme" />
<Button
android:id="#+id/btn_shopping_list"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_below="#id/btn_inventory"
android:text="#string/btn_shopping_list_text"
android:theme="#style/BtnTheme" />
</RelativeLayout>
Here is MainActivity:
public class MainActivity extends AppCompatActivity {
private Button btnInventory;
private Button btnShopList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnInventory = findViewById(R.id.btn_inventory);
btnShopList = findViewById(R.id.btn_shopping_list);
btnInventory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent inventoryIntent = new Intent(MainActivity.this, Inventory.class);
startActivity(inventoryIntent);
}
});
btnShopList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent shopListIntent = new Intent(MainActivity.this, ShoppingList.class);
startActivity(shopListIntent);
}
});
}
Here is the error I get awhen I try to run the code:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.grocerystatusapplication/com.example.grocerystatusapplication.MainActivity}: android.view.InflateException: Binary XML file line #15 in com.example.grocerystatusapplication:layout/activity_main: Binary XML file line #15 in com.example.grocerystatusapplication:layout/activity_main: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3344)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3488)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
at android.os.Handler.dispatchMessage(Han
E/AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7506)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:956)
Here is styles.xml:
<resources>
<!-- 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="android:windowBackground">#color/colorLayoutBackground</item>
</style>
<style name="BtnTheme" parent="TextAppearance.AppCompat.Widget.Button.Colored">
<item name="colorButtonNormal">#color/colorViewBackground</item>
</style>
<style name="TextViewTheme" parent="">
<item name="android:background">#drawable/rounded_corners</item>
<item name="android:textColor">#android:color/white</item>
</style>
I have tried changing the attributes of the Button that the error refers to (line 15, btn_inventory), but I did not get anywhere. Did not come across this error before and don't really understand what is the problem. What can I change to make it work? After looking through posts about the same error, I did not get an answer to my question, there seemed to be a quite a few reasons this error came up.
In both Button tags change
android:theme="#style/BtnTheme"
to
style="#style/BtnTheme"
I am having an issue with the TextInputLayout error state. When I set the error on the TextInputLayout, then error message is red, along with the underline, but the hint does not (it stays the default colour - grey). I have played around with setting the errorTextAppearance, yet I cannot seem to get the hint text to change colour.
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="My Hint"
app:errorTextAppearance="#style/MyErrorTextAppearance">
<android.support.design.widget.TextInputEditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="10"
android:maxLines="1" />
styles.xml
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="MyErrorTextAppearance" parent="TextAppearance.Design.Error">
<item name="android:textColor">#color/colorError</item>
</style>
I set and clear the error as follows:
myTextInputLayout.setErrorEnabled(true);
myTextInputLayout.setError(errorMessage);
myTextInputLayout.setError(null);
myTextInputLayout.setErrorEnabled(false);
try following code:
fun setHintColor(#ColorInt color: Int)
{
try {
var stateList = ColorStateList(arrayOf(intArrayOf(0)), intArrayOf(color))
val defaultTextColorField = TextInputLayout::class.java.getDeclaredField("mDefaultTextColor")
defaultTextColorField?.let {field->
field.isAccessible = true
field.set(this, stateList)
}
val focusedTextColorField = TextInputLayout::class.java.getDeclaredField("mFocusedTextColor")
focusedTextColorField?.let{field->
field.isAccessible = true
field.set(this, stateList)
}
}catch(e: java.lang.Exception){
// handle exception
}
}
I am using TextInputLayout in which i want to change the color of hinttext when i enter text then the color of hint text should be blue other wise color should be gray and when edittext contains some value then the color of hintText should blue for more information please check my image.
Ultimate i want this after edit text containing some value.
This is my current result after using this code
<style name="labelcolor" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#868686</item>
<item name="android:textSize">16sp</item>
<item name="android:paddingTop">5sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#0ea3ff</item>
<item name="colorControlNormal">#0ea3ffr</item>
<item name="colorControlActivated">#0ea3ff</item>
</style>
please help me how can i solve this issues.
Change TextInputLayout Lebel Color Dynamicaly in on Focus change of edittext and pass color according work for me.
public static void textInoutLayoutColor(TextInputLayout textInputLayout, #ColorInt int color) {
try {
Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
fDefaultTextColor.setAccessible(true);
fDefaultTextColor.set(textInputLayout, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
Field fFocusedTextColor = TextInputLayout.class.getDeclaredField("mFocusedTextColor");
fFocusedTextColor.setAccessible(true);
fFocusedTextColor.set(textInputLayout, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
} catch (Exception e) {
e.printStackTrace();
}
}
and change the color like this.
input_layout= (TextInputLayout) findViewById(R.id.input_layout);
etuser_name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasfocus) {
if(hasfocus){
textInoutLayoutColor(input_layout,getResources().getColor(R.color.bullet_color));
}
else
{
if(etuser_name.getText().toString().trim().length()>0) {
textInoutLayoutColor(input_layout,getResources().getColor(R.color.bullet_color));;
}
else
{
textInoutLayoutColor(input_layout,getResources().getColor(R.color.hint_grey));
}
}
}
});
Create your as bellow and try
<style name="EditTextHint" parent="TextAppearance.AppCompat">
<item name="colorAccent">#android:color/white</item>
<item name="android:textColorHint">#color/BackgroundtWhiteColor</item>
<item name="colorControlNormal">#color/BackgroundtWhiteColor</item>
<item name="colorControlActivated">#color/your color</item>
<item name="colorControlHighlight">#color/BackgroundtWhiteColor</item>
</style>
Use it as bellow
<android.support.design.widget.TextInputLayout
android:theme="#style/EditTextHint"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
Firstly you need to set your TextInputLayout
android.support.design.widget.TextInputLayout textInputLayout = (TextInputLayout) view.findViewById(R.id.textInputLayout);
textInputLayout.setHintTextAppearance(R.style.Instyle);
and then in styles.xml file you can create your styles with the color for the hint color.
<style name="Instyle" parent="AppThemeLight">
<itemname="android:textColorPrimary">#android:color/darker_gray</item>
<item name="android:textColor">#android:color/darker_gray</item>
</style>
TextInputLayout takes colorAccent to hintTextColor when you start entering values in edittext.
So if you want it to be blue then set your colorAccent to blue colour in your theme. And when edittext has no value then set hintTextColor to grey. Do like this
<android.support.design.widget.TextInputLayout
android:id="#+id/userNameTIL"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#color/spinnerBg"
android:clipToPadding="false"
android:gravity="bottom"
android:paddingTop="4dp"
android:textColorHint="#color/grey"> /*Here it sets color to hintText when edittext has no value */
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:background="#null"
android:hint="Username"
android:inputType="text"
android:maxLength="30"
android:paddingEnd="3dp"
android:paddingStart="25dp"
android:paddingTop="3dp"
android:singleLine="true"
android:textColor="#color/black"
android:textColorHint="#color/black"
android:textSize="20sp"/>
</android.support.design.widget.TextInputLayout>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!--EditText hint color-->
<item name="android:textColorHint">#color/default_app_white</item> <!-- TextInputLayout text color-->
<item name="colorControlActivated">#color/default_app_green</item>
<!-- EditText line color when EditText on-focus-->
<item name="colorControlHighlight">#color/default_app_green</item>
<!-- EditText line color when EditText in un-focus-->
<item name="colorControlNormal">#color/default_app_white</item>
</style>
<!-- In your style.xml file -->
try this one but it affects your whole application
I am using the toolbar for my app it works fine in the lollipop version.But in the previous version the app gets crashed.I have tried changing the styles to
Theme.AppCompat.NoActionBar but it didn't works.
Styles:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppThemes" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/windowBackground</item>
</style>
XmlToolbar:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
Logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{nidhinkumar.webviewss/nidhinkumar.webviewss.ParseMainAct}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:816)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:305)
at android.app.Activity.setContentView(Activity.java:1966)
at nidhinkumar.webviewss.ParseMainAct.onCreate(ParseMainAct.java:36)
at android.app.Activity.performCreate(Activity.java:5286)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
ParseMainAct:
public class ParseMainAct extends Activity{
private static String TAG = ParseMainAct.class.getSimpleName();
private Toolbar mToolbar;
private ListView listView;
private List<Message> listMessages = new ArrayList<>();
private MessageAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parseact_main);
listView = (ListView) findViewById(R.id.list_view);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
adapter = new MessageAdapter(this);
listView.setAdapter(adapter);
Intent intent = getIntent();
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String message = intent.getStringExtra("message");
Message m = new Message(message, System.currentTimeMillis());
listMessages.add(0, m);
adapter.notifyDataSetChanged();
}
private class MessageAdapter extends BaseAdapter {
LayoutInflater inflater;
public MessageAdapter(Activity activity) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listMessages.size();
}
#Override
public Object getItem(int position) {
return listMessages.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.parselist_row, null);
}
TextView txtMessage = (TextView) view.findViewById(R.id.message);
TextView txtTimestamp = (TextView) view.findViewById(R.id.timestamp);
Message message = listMessages.get(position);
txtMessage.setText(message.getMessage());
CharSequence ago = DateUtils.getRelativeTimeSpanString(message.getTimestamp(), System.currentTimeMillis(),
0L, DateUtils.FORMAT_ABBREV_ALL);
txtTimestamp.setText(String.valueOf(ago));
return view;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
}
parse_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/my_tool_bar" />
</LinearLayout>
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
You should create your toolbar.xml inside you layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xxxxxx">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:orientation="vertical">
<include
android:id="#+id/main_menu_activity_toolbar"
layout="#layout/XmlToolbar" />
And then inflate it your activity:
mToolbar = (Toolbar) findViewById(R.id.main_menu_activity_toolbar);
EDIT
Use NoActionBar in your base application theme
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
May be somewhere in your app AppTheme or 'AppThemes' has been used. you see you have give parent theme as Theme.AppCompat.Light.DarkActionBar. that means your Activity having this theme has ActionBar.
You cannot use ToolBar As long as ActionBaris there.
just Change Theme.AppCompat.Light.DarkActionBar
to Theme.AppCompat.Light.NoActionBar. and you are good to go.
Hope this will help you.
Update
Set your ToolBaras Your ActionBar
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
You have to made two styles.xml file one for device below lollipop and other is styles.xml (v-21) for device lollipop and above version.
Sample of style.xml
<style name="MyAppThemes" parent="MyTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/windowBackground</item>
</style>
Sample of style.xml(v-21)
<style name="MyAppThemes" parent="MyAppThemes.Base">
<item name="android:windowActionBar">false</item>
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/windowBackground</item>
</style>
Now inside your AndroidMainfest.xml change application theme
android:theme="#style/MyAppThemes">
I hope it will solve your problem.
NullPointerException when long-press over field
compile 'com.android.support:appcompat-v7:19.1.+'
XML
<EditText
android:id="#+id/field"
style="#style/Field"
android:textSize="20sp"
android:hint="#string/hint"
android:nextFocusDown="#+id/other"/>
Style
<style name="Field" parent="Theme.AppCompat.Light">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#color/blue</item>
<item name="android:singleLine">true</item>
<item name="android:gravity">center_vertical</item>
<item name="android:inputType">textCapCharacters|textNoSuggestions</item>
<item name="android:imeOptions">actionNext</item>
<item name="android:popupBackground">#color/white</item>
</style>
LogCat
java.lang.NullPointerException
at android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:302)
at android.support.v7.app.ActionBarImplJB.getThemedContext(ActionBarImplJB.java:20)
at android.support.v7.app.ActionBarActivityDelegate.getActionBarThemedContext(ActionBarActivityDelegate.java:208)
at android.support.v7.app.ActionBarActivityDelegateICS.onActionModeStarted(ActionBarActivityDelegateICS.java:195)
at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onActionModeStarted(ActionBarActivityDelegateICS.java:359)
at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionMode(PhoneWindow.java:2437)
at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionModeForChild(PhoneWindow.java:2362)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:665)
at android.view.View.startActionMode(View.java:4554)
at android.widget.Editor.startSelectionActionMode(Editor.java:1551)
at android.widget.Editor.performLongClick(Editor.java:859)
at android.widget.TextView.performLongClick(TextView.java:8373)
at android.view.View$CheckForLongPress.run(View.java:18441)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
The ActionBar is null. That's because you're using an activity without a title (and thus also without an actionbar):
requestWindowFeature(Window.FEATURE_NO_TITLE);
There are two ways to fix it.
Simple:
Make YourActivity NOT extends ActionBarActivity but normal Activity.
More difficult:
Make transparent ActionBar with no title, no icon, no home button etc. How to do this? First, define ActionBar style and new theme that extends YourAppTheme:
<!-- Your App Theme-->
<style name="YourAppTheme.Light" parent="#style/Theme.AppCompat.Light">
<...>
</style>
<!-- Transparent ActionBar Style -->
<style name="YourAppTheme.Light.ActionBar.Transparent" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">#android:color/transparent</item>
<item name="background">#android:color/transparent</item>
</style>
<!-- Fullscreen Activity Theme -->
<style name="YourAppTheme.FullScreen.Light" parent="#style/YourAppTheme.Light">
<item name="android:actionBarStyle">#style/YourAppTheme.Light.ActionBar.Transparent</item>
<item name="actionBarStyle">#style/YourAppTheme.Light.ActionBar.Transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
Next, set this theme to your fullscreen activity:
<activity
android:name="your.package.name.YourFullscreenActivity"
android:label="#string/app_name"
android:theme="#style/YourAppTheme.FullScreen.Light" />
Finally, add following lines to YourFullscreenActivity#onCreate method:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
I hope it will help.
I think I have a better solution to this issue.
Remove requestWindowFeature(Window.FEATURE_NO_TITLE); from activity and <item name="android:windowNoTitle">true</item> from activity theme.
To your activity code add:
To onCreate() method
View actionBar = getActionBarView();
if (actionBar != null) {
actionBar.setVisibility(View.GONE);
}
To activity class:
public View getActionBarView() {
Window window = getWindow();
View v = window.getDecorView();
int resId;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
resId = getResources().getIdentifier(
"action_bar_container", "id", getPackageName());
} else {
resId = Resources.getSystem().getIdentifier(
"action_bar_container", "id", "android");
}
if (resId != 0) {
return v.findViewById(resId);
} else {
return null;
}
}
#Override
public void onSupportActionModeStarted(ActionMode mode) {
super.onSupportActionModeStarted(mode);
View actionBar = getActionBarView();
if (actionBar != null) {
actionBar.setVisibility(View.VISIBLE);
}
}
#Override
public void onSupportActionModeFinished(ActionMode mode) {
super.onSupportActionModeFinished(mode);
View actionBar = getActionBarView();
if (actionBar != null) {
actionBar.setVisibility(View.GONE);
}
}
This is remove action bar and at the same time allows you to manipulate text in edit text with action mode buttons.
Cheers
The second solution to the above answer works, but causes the top row that would have been covered by the actionbar to be unclickable. Set the height item to zero in the actionbar style to solve this issue:
<item name="android:height">0dp</item>
I think erakitin is a VERY good answer, and quiet simple if you take the "DO NOT extend ActionBarActivity, but in case anyone needs or want to avoid making that change, I'm leaving another answer which I think is also very simple and solves the problem PERFECTLY.
Try adding this to your activity's theme.
<item name="android:windowActionBar">false</item>
public class MainActivity extends ActionBarActivity
Change to:
public class MainActivity extends Activity