After I created my own Style, my Actionbar doesnt appear.I'm using a Pageviewer with tabhost which is visible but my actionbar isn't.I think it is caused by my Styles.xml.I also tried to edit the theme with the Android theme Editor but it did not help.
This is my Styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Newme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarItemBackground">#drawable/selectable_background_newme</item>
<item name="popupMenuStyle">#style/PopupMenu.Newme</item>
<item name="dropDownListViewStyle">#style/DropDownListView.Newme</item>
<item name="actionBarTabStyle">#style/ActionBarTabStyle.Newme</item>
<item name="actionDropDownStyle">#style/DropDownNav.Newme</item>
<item name="actionBarStyle">#style/ActionBar.Solid.Newme</item>
<item name="actionModeBackground">#drawable/cab_background_top_newme</item>
<item name="actionModeSplitBackground">#drawable/cab_background_bottom_newme</item>
<item name="actionModeCloseButtonStyle">#style/ActionButton.CloseMode.Newme</item>
<item name="actionBarWidgetTheme">#style/Theme.Newme.Widget</item>
</style>
<style name="ActionBar.Solid.Newme" parent="#style/Widget.AppCompat.Light.ActionBar.Solid">
<item name="background">#color/theme_color</item>
<item name="backgroundStacked">#color/theme_color</item>
<item name="backgroundSplit">#color/theme_color</item>
<item name="progressBarStyle">#style/ProgressBar.Newme</item>
</style>
<style name="PopupMenu.Newme" parent="#style/Widget.AppCompat.PopupMenu">
<item name="android:popupBackground">#drawable/menu_dropdown_panel_newme</item>
</style>
<style name="DropDownListView.Newme" parent="#style/Widget.AppCompat.ListView.DropDown">
<item name="android:listSelector">#drawable/selectable_background_newme</item>
</style>
<style name="ActionBarTabStyle.Newme" parent="#style/Widget.AppCompat.ActionBar.TabView">
<item name="android:background">#drawable/tab_indicator_ab_newme</item>
</style>
<style name="DropDownNav.Newme" parent="#style/Widget.AppCompat.Spinner.DropDown.ActionBar">
<item name="android:background">#drawable/spinner_background_ab_newme</item>
<item name="android:popupBackground">#drawable/menu_dropdown_panel_newme</item>
<item name="android:dropDownSelector">#drawable/selectable_background_newme</item>
</style>
<style name="ProgressBar.Newme" parent="#style/Widget.AppCompat.ProgressBar.Horizontal">
<item name="android:progressDrawable">#drawable/progress_horizontal_newme</item>
</style>
<style name="ActionButton.CloseMode.Newme" parent="#style/Widget.AppCompat.ActionButton.CloseMode">
<item name="android:background">#drawable/btn_cab_done_newme</item>
</style>
<style name="Theme.Newme.Widget" parent="#style/Theme.AppCompat">
<item name="popupMenuStyle">#style/PopupMenu.Newme</item>
<item name="dropDownListViewStyle">#style/DropDownListView.Newme</item>
</style>
I hope some of you can help me.
EDIT:
Here is my Mainactivity:
package com.miguel.fitnessapp;
public class MainActivity extends FragmentActivity implements OnTabChangeListener, OnPageChangeListener {
private TabsPagerAdapter mAdapter;
private ViewPager mViewPager;
private TabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.theme));
}
initialiseTabHost();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
// Fragments and ViewPager Initialization
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(MainActivity.this);
}
private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec) {
tabSpec.setContent(new MyTabFactory(activity));
tabHost.addTab(tabSpec);
}
public void onTabChanged(String tag) {
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
// Manages the Page changes, synchronizing it with Tabs
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
int pos = this.mViewPager.getCurrentItem();
this.mTabHost.setCurrentTab(pos);
}
#Override
public void onPageSelected(int arg0) {
}
private void initialiseTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Training").setIndicator("Training"));
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Ernährung").setIndicator("Ernährung"));
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Körper").setIndicator("Körper"));
mTabHost.setOnTabChangedListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu_edit, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
int id = item.getItemId();
if (id == R.id.benutzer){
}
return super.onOptionsItemSelected(item);
}
}
Related
I have created an application which has a RecyclerView and each item has an option menu, which will show the popup to share. Everything is working fine but when I try to click on the option menu it crashes. I am getting a weird error. I google it and it seems like there is something wrong with my theme. I tried many solutions available on the internet but none of them working for me.
Here is my theme:
<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="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:textColorPrimary">#ffffff</item>
<item name="android:textColorSecondary">#color/about_us_link_text_color</item>
</style>
This is my Adapter class
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popUp = new PopupMenu(context,holder.buttonViewOption);
popUp.inflate(R.menu.option_menu);
popUp.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.share:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,news.get(position).getUrl());
intent.putExtra(Intent.EXTRA_SUBJECT,"check out this site");
context.startActivity(Intent.createChooser(intent,"share"));
break;
case R.id.save:
break;
}
return false;
}
});
popUp.show();
}
});
option_menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/share"
android:title="Share" />
<item
android:id="#+id/save"
android:title="Save" />
</menu>
The error I am getting is :
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValue{t=0x1d/d=0xffff4081 a=3 r=0x7f0c001a}
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:705)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6890)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7071)
at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:446)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:386)
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:385)
at android.view.LayoutInflater.inflate(LayoutInflater.java:502)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.support.v7.view.menu.MenuAdapter.getView(MenuAdapter.java:93)
at android.support.v7.view.menu.MenuPopup.measureIndividualMenuWidth(MenuPopup.java:160)
at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:153)
at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187)
at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290)
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175)
at android.support.v7.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:141)
at android.support.v7.widget.PopupMenu.show(PopupMenu.java:233)
at com.kotlin.whatshappening.activity.adapter.NewsAdapter$2.onClick(NewsAdapter.java:142)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Can someone tell me what I am doing wrong?
Instead of:
PopupMenu popUp = new PopupMenu(context, holder.buttonViewOption);
Perform:
PopupMenu popUp = new PopupMenu(view.getContext(), holder.buttonViewOption);
Try this
popUp.getMenuInflater().inflate(R.menu.option_menu, popUp.getMenu());
I hope this helps you.
First create a method in you adapter class like the following.
private void showPopup(View view, String url) {
PopupMenu popup = new PopupMenu(activityContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.option_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.share:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,url);
intent.putExtra(Intent.EXTRA_SUBJECT,"check out this site");
activityContext.startActivity(Intent.createChooser(intent,"share"));
break;
case R.id.save:
break;
}
return true;
}
});
popup.show();
}
And in your onBindViewHolder method call the above function,
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showPopup(view, news.get(holder.getAdapterPosition()).getUrl());
}
});
I'm working in my app and have a searchview inside toolbar but the hint has a small padding after the cursor.
How can I remove the padding?
Here is my searchview:
and I would like that:
here is my onCreateOptionsMenu:
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
MenuItem item=menu.findItem(R.id.procura);
Drawable drawable = menu.findItem(R.id.procura).getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);
}
searchView=(SearchView) MenuItemCompat.getActionView(item);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getResources().getString(R.string.app_hint));
try {
Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
mDrawable.setAccessible(true);
Drawable drw = (Drawable) mDrawable.get(searchView);
drw.setBounds(0, 0, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
My styles:
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="submitBackground">#color/colorPrimary</item>
<item name="queryBackground">#color/colorPrimary</item>
<item name="android:colorFocusedHighlight">#color/colorAccent</item>
</style>
My menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="#+id/procura"
android:title="#string/app_hint"
android:icon="#mipmap/ic_search_black_24dp"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
I found the solution for my problem.
Just add this in the styles:
<item name="searchHintIcon">#null</item>
The styles now is that:
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="submitBackground">#color/colorPrimary</item>
<item name="queryBackground">#color/colorPrimary</item>
<item name="searchHintIcon">#null</item>
<item name="android:colorFocusedHighlight">#color/colorAccent</item>
</style>
And onCreateOptionsMenu now is that:
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
MenuItem item=menu.findItem(R.id.procura);
searchView=(SearchView) MenuItemCompat.getActionView(item);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getResources().getString(R.string.app_hint));
return true;
I'm trying to use Material Design in my aplication, and to use the Floating Action Button, I'm using appcompat-v7 library. But, for using this, I must use the theme: "Theme.AppCompat.Light". And I just can't set the background color on the app for API 22. I'm not interesting to make this work on API's 20 or bellow
In Android Documentation, it's says that i just have to user colorPrimary, and colorPrimaryDark on android:Theme.Material
https://developer.android.com/intl/pt-br/training/material/theme.html
My style look like this:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:colorPrimary">#color/primary</item>
<item name="android:statusBarColor">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:textColorSecondary">#color/secondary_text</item>
<item name="android:colorAccent">#color/accent</item>
<item name="android:textColor">#color/textColor</item>
</style>
</resources>
My MainActivity:
package com.example.rafael.encomendasmaterial.activities;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.example.rafael.encomendasmaterial.R;
public class MainActivity extends ActionBarActivity {
private FloatingActionButton myFAB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFAB = (FloatingActionButton) findViewById(R.id.myFAB);
setTheme(R.style.AppTheme);
callCadastroEncoemnda();
}
public void callCadastroEncoemnda(){
myFAB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CadastroEncomenda.class);
MainActivity.this.startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
In my case, status bar was not reflecting because i set
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
Removing the lines solved it
Since you're using the support library, certain properties that didn't exist in API 7 should not be android namespaced since they are provided by the library.
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:textColorSecondary">#color/secondary_text</item>
<item name="colorAccent">#color/accent</item>
<item name="android:textColor">#color/textColor</item>
</style>
</resources>
I Forget to put "android:" before colorPrimary, colorPrimaryDark, and colorAccent. It's working now! Thanks.
<style name="AppTheme" parent="android:Theme.Material.Light">
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:textColorSecondary">#color/secondary_text</item>
<item name="android:colorAccent">#color/accent</item>
<item name="android:textColor">#color/textColor</item>
</style>
Im trying to do a menu with only one icon, but instead of that icon, i am getting the typican three dots, and inside my option. So, i only want one icon. That is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.bartek.gestionpea.MainActivity">
<item
android:id="#+id/ayuda"
android:icon="#android:drawable/ic_dialog_info"
android:title="Ayuda"
app:showAsAction="ifRoom"/>
</menu>
Here goes my class:
public class MainActivity extends Activity {
PeniaSQLiteHelper usdbh;
private SQLiteDatabase db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setTitle("GESTION");
Button btnCrearPena = (Button) findViewById(R.id.btncrearpena);
btnCrearPena.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, CrearNueva.class);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_principal, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.ayuda:
return true;
}
return super.onMenuItemSelected(featureId, item);
}
}
You are using some things for the native action bar (e.g., inheriting from Activity) and some things for the appcompat-v7 action bar backport (e.g., app:showAsAction). Choose one and stick with it:
If you wish to use the native action bar, use android:showAsAction
If you wish to use the appcompat-v7 action bar backport, inherit from AppCompatActivity (in the latest appcompat-v7) or ActionBarActivity (for prior versions of appcompat-v7)
Since you use the standard Activity, try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ayuda"
android:icon="#android:drawable/ic_dialog_info"
android:title="Ayuda"
android:showAsAction="always" />
</menu>
You can modify the app theme (in themes.xml):
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionOverflowButtonStyle">#style/MyActionButtonOverflow</item>
</style>
<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">#drawable/your_icon_here</item>
<item name="android:background">?android:attr/actionBarItemBackground</item>
<item name="android:contentDescription">"options"</item>
</style>
More info here
Note that when using appcompat themes, you sometimes need to set xml attibutes twice:
once with and once without the android namespace
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>
I have created a custom DialogFragment like it is described in the developer guide.
Now what I am trying to do sounds simple enough, but I cannot get it to work.
I have defined: android:background="#android:color/transparent" in my layout xml which I am loading like this (in my onCreateDialog):
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.pausedialog, null);
setStyle(STYLE_NO_FRAME, R.style.CustomDialog);
As you can see I also tried to set a custom style in the DialogFragment which is defined like this:
<style name="CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:alwaysDrawnWithCache">false</item>
<item name="android:windowContentOverlay">#null</item>
</style>
And I also tried getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
which leads to a null pointer exception.
I am using android.support.v4.app.DialogFragment. Can this be the cause?
Or am I doing something else wrong?
Try to set style and theme in onCreate method of your dialogFragment class implementation.
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int style=DialogFragment.STYLE_NO_TITLE;
int theme=android.R.style.Theme_Translucent;
setStyle(style, theme);
}
Or
if you are using Dialog class then you can also set Style and theme on dialog instance.
This worked for me. I created a new style:
<style name="CustomDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
And then set this style in my DialogFragment's onCreate() method like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);
}
This is the style I use to totally remove the Dialog`s background.-
<style name="Theme.Dialog" parent="#android:style/Theme.Translucent.NoTitleBar">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
As for the Dialog creation, you're creating a DialogBuilder but then you manually inflate a view, I guess that's the problem. Try this instead.-
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.customTheme));
AlertDialog dialog = builder.create();
dialog.show();
EDIT
Another approach is extending AlertDialog.-
public class CustomDialog extends AlertDialog {
public DialogParent(Context context) {
super(context, R.style.CustomDialog);
setContentView(R.layout.pausedialog);
// More initialization stuff
}
}
And then 'manually' instantiate it.-
AlertDialog dialog = new CustomDialog(getActivity());
dialog.show();