Adding a Button to a Pre-existing ToolBar from the Theme - java

So far, it seems that I need to provide a custom Toolbar in order to add any Button. From my meager understanding, I can only get "3 dots" to show in the right-side of the Toolbar given by the Theme. On top of that, the "3 dots" button appears to only allow a menu of items.
The following code causes a crash:
toolbar = findViewById( R.id.notes_toolbar );
setSupportActionBar( toolbar );
And thus, I get:
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
Avoiding a re-write of several of several class files, is there a simple way of addressing this?
Thank you kindly.
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>
</style>
</resources>
Course_NoteTaking_Activity.java :
public class Course_NoteTaking_Activity extends AppCompatActivity {
private Button saveButton;
private EditText notes_EditText;
private Toolbar toolbar;
#Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_course_notetaking );
toolbar = findViewById( R.id.notes_toolbar );
//TODO CAUSES A CRASH FROM PRE-EXISTING TOOLBAR.
//setSupportActionBar( toolbar );
//getSupportActionBar().setDisplayHomeAsUpEnabled( false );
//getSupportActionBar().setTitle( "Notes for the Course" );
Intent intent = getIntent();
String testString = intent.getStringExtra( "notes" );
saveButton = findViewById( R.id.notes_saveButtonXML );
saveButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick( View view ) {
Intent returnIntent = new Intent();
String transferString = "Just testing... 1, 2, and a 3!!!";
returnIntent.putExtra( "return_Notes", transferString );
}
});
notes_EditText = findViewById( R.id.notes_EditTextXML );
notes_EditText.setText( testString );
}
}
activity_course_notetaking.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="wrap_content"
android:orientation="vertical"
>
<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/notes_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
>
<Button
android:layout_width="wrap_content"
android:layout_height="8dp"
android:text="Test Button"
android:layout_gravity="right"
/>
</android.support.v7.widget.Toolbar>
<LinearLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
android:showDividers="none"
tools:context="com.weslange.Term_Scheduling.Course_ChangingDetails_Activity"
>
<Button
android:id="#+id/notes_saveButtonXML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Return to the Course's Details (Not Saved Yet)"
android:layout_marginVertical="2dp"
/>
<EditText
android:id="#+id/notes_EditTextXML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName|textCapWords"
android:imeOptions="actionDone"
android:layout_marginVertical="2dp"
/>
</LinearLayout>
</RelativeLayout>

it seems that I need to provide a custom Toolbar in order to add any Button
You do not need a custom Toolbar to add action bar items that show up as buttons in the action bar. You are welcome to use a Toolbar, of course, but that is not necessary.
From my meager understanding, I can only get "3 dots" to show in the right-side of the Toolbar given by the Theme
Or, you can inflate a menu resource, and add action bar items that either show up in the overflow ("3 dots") or as buttons (if there is room). This is the classic way of adding things to the action bar, and you can do it with Toolbar as well.
is there a simple way of addressing this?
In terms of the crash, don't use a theme that has an action bar. For example, you could replace parent="Theme.AppCompat.Light.DarkActionBar" with parent="Theme.AppCompat.Light.NoActionBar".

Related

Cart badge with bottom navigation view android

I'm building a shop android app which I need to set these items in bottom navigation view (Home, cart, favorites, more)
and I need to set badge on cart to view how many items in cart
I build bottom nav bar successfully but I can't add cart badge on icon.
this is my code in activity which I set menu for nav view.
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products);
Objects.requireNonNull(getSupportActionBar()).hide();
context = this;
tools = Tools.getInstance(context);
prefs = Prefs.getInstance(context);
tools.DisplayLang(prefs.GetLang(),this);
nav = findViewById(R.id.bottom_nav_view);
nav.inflateMenu(R.menu.menu);
nav.setSelectedItemId(R.id.home);
getSupportFragmentManager().beginTransaction().replace(R.id.frag_container,new Home())
.commit();
nav.setOnItemSelectedListener(btm_nav);
}
NavigationBarView.OnItemSelectedListener btm_nav =
new NavigationBarView.OnItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.home:
fragment = new Home();
getSupportFragmentManager().beginTransaction().replace(R.id.frag_container,fragment).commit();
break;
case R.id.fav:
fragment = new Favourite();
getSupportFragmentManager().beginTransaction().replace(R.id.frag_container,fragment).commit();
break;
case R.id.cart:
fragment = new Cart();
getSupportFragmentManager().beginTransaction().replace(R.id.frag_container,fragment).commit();
break;
case R.id.more:
PopupMenu menu = new PopupMenu(Products.this,
findViewById(R.id.more));
menu.getMenuInflater().inflate(R.menu.pop_up,menu.getMenu());
tools.setForceShowIcon(menu);
menu.show();
break;
}
return true;
}
};
#menu/menu
<?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">
<item android:id="#+id/home"
android:title="#string/home"
android:icon="#drawable/home"/>
<item android:id="#+id/cart"
android:title="#string/basket"
android:icon="#drawable/cart"
app:actionLayout="#layout/cart_layout"/>
<item android:id="#+id/fav"
android:title="#string/fav"
android:icon="#drawable/fav"/>
<item android:id="#+id/more"
android:title="#string/more"
android:icon="#drawable/more"/>
</menu>
#layout/cart_layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:src="#drawable/cart"
app:layout_constraintEnd_toStartOf="#+id/txt_count"
app:layout_constraintHorizontal_bias="0.458"
app:layout_constraintStart_toStartOf="#+id/txt_count"
app:layout_constraintTop_toTopOf="#+id/txt_count" />
<TextView
android:id="#+id/txt_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="668dp"
android:background="#drawable/cart_badge"
android:gravity="center"
android:padding="3dp"
android:text="0"
android:textColor="#color/white"
android:textSize="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.598"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I don't have any errors but my problem is that badge doesn't appear on cart icon. How can I do that ?
If you are using the Material Components library, then there is no need to create an extra layout for your badge, unless you want a custom badge. For adding badge to a menu item, first initialize a BadgeDrawable with your menu item's id. For example:
BadgeDrawable badge = nav.getOrCreateBadge(R.id.cart);
Here, nav is your bottom navigation view and R.id.cart is your menu item's id. After initialization, set the badge to be visible by badge.setVisible(true); and finally set the number on the badge by badge.setNumber(1);. You can even set the badge's background color by badge.setBackgroundColor(getResources().getColor(R.color.red)); and badge's text color by badge.setTextColor(getResources().getColor(R.color.white));. And finally when you want to remove the number just call badge.clearNumber();.

onNavigationItemSelected not being called for Bottom Navigation Bar

I'm trying to implement a bottom nav bar that changes fragments when the nav items are clicked. However, when I click on the nav bar items, the fragments don't change. Using log.d, I noticed onNavigationItemSelected is not being called. How do I fix this?
To note, the startFeedFragment, startScheduleFragment, & startSettingsFragmentare implemented the same way and they work for the buttons in the toolbar. I also referenced this tutorial and this question for help.
MainActivity.java
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setUpRecycler();
startFeedFragment();
BottomNavigationView bottomNavBar = (BottomNavigationView) findViewById(R.id.navigation);
bottomNavBar.bringToFront();
bottomNavBar.setOnNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Log.d("NAV BAR", "onNavigationItemSelected hit");
switch (item.getItemId()) {
case R.id.feedMenu:
startFeedFragment();
Log.d("NAV BAR", "feed");
break;
case R.id.myScheduleMenu:
startScheduleFragment();
Log.d("NAV BAR", "schedule");
break;
case R.id.settingsMenu:
startSettingsFragment();
Log.d("NAV BAR", "settings lol");
break;
default:
Log.d("NAV BAR", "false");
return false;
}
return true;
}
/**
* Switches out the fragment for {#link FeedFragment} and sets an appropriate title. startScheduleFragmens & startSettingsFragment are implemented the same way
*/
private void startFeedFragment()
{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainer, new FeedFragment());
transaction.commit();
getSupportActionBar().setTitle(R.string.title_fragment_feed);
}
}
Snippet from xml file
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="#android:color/white"
app:itemIconTint="#color/colorPrimaryDark"
app:itemTextColor="#color/colorPrimaryDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_nav"
android:elevation="8dp"/>
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
Solved! It was because I had set the buttons in the menu to android:enabled="false". That meant they couldn't be pressed, which is why onNavigationItemSelected wasn't being called. I set all buttons to android:enabled="true" to fix this.
Correct code for bottom_nav.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/bottom_nav_my_schedule"
android:title="#string/menu_my_schedule"
android:icon="#drawable/ic_event_available_white_24dp"
android:enabled="true"/> <!--make sure enabled is set to true!-->
<item android:id="#+id/bottom_nav_feed"
android:title="#string/menu_feed"
android:icon="#drawable/ic_view_list_white_24dp"
android:enabled="true" /> <!--make sure enabled is set to true!-->
<item android:id="#+id/bottom_nav_settings"
android:title="#string/menu_settings"
android:icon="#drawable/ic_settings_white_24dp"
android:enabled="true"/> <!--make sure enabled is set to true!-->
</menu>
As for me the problem was when my activity main layout was LinearLayout, so I've solved it by changing it to RelativeLayout, I have absolutely no thoughts why it's working, because RelativeLayout layouts reply only for elements location, not its view or something else, but it's working.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#drawable/selector"
android:background="#color/white"
app:menu="#menu/menu_bottom"
app:itemIconTint="#drawable/menu_trainings"
app:labelVisibilityMode="unlabeled"
/>
</RelativeLayout>

How to customize tab bar with icons?

Can someone explain to me what I should add exactly to this code from this website:
III. To change tab icon use TabLayout.Tab#setIcon method. You can get TabLayout.Tab object via TabLayout#getTabAt method, which accept tab index as parameter.
...
//after initialization TabLayout and ViewPager
TabLayout.Tab tabCall = tabLayout.getTabAt(ITEM_CALL);
tabCall.setIcon(R.drawable.selector_call);
//repeat this code for all your tabs
...
Because I get tabLayout and ITEM_CALL in red!
You need to first create the selector xml files, and place them in your drawable folder, and those selector files need to reference valid drawables for the selected and non-selected states.
Then, just set the tab icons in onCreate() of the Activity.
Here is what your Activity should look like in order to show three tabs, with each tab showing only an icon:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new TabPagerAdapter(getSupportFragmentManager()));
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
TabLayout.Tab tabCall = tabLayout.getTabAt(0);
tabCall.setIcon(R.drawable.selector_call);
TabLayout.Tab tabHeart = tabLayout.getTabAt(1);
tabHeart.setIcon(R.drawable.selector_heart);
TabLayout.Tab tabContacts = tabLayout.getTabAt(2);
tabContacts.setIcon(R.drawable.selector_contacts);
}
class TabPagerAdapter extends FragmentPagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 3;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new CallFragment();
case 1:
return new HeartFragment();
case 2:
return new ContactsFragment();
}
return null;
}
}
}
Each tab needs its own selector xml file, here is an example of what the selector for the third tab should look like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/contacts_selected" />
<item
android:state_selected="false"
android:drawable="#drawable/contacts_unselected" />
</selector>
declare this as a constant in your MainActivity class
private static final Striing ITEM_CALL = 0;
do this declaration on your onCreate() :
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
then create selector_call.xml in the drawable folder with this code :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/ic_call_selected" />
<item
android:state_selected="false"
android:drawable="#drawable/ic_call_unselected" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<!-- coordinate layout add more than one widget in systematic way and
inhance others property-->
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Tab_Bar">
<!--Add space for toolbar and tab bars-->
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<!-- "?attr/actionBarSize" means same size as
original Toolbar size
"scroll|enterAlways" here "scroll" means when
we scroll viewPager up it is scrolled up and "enterAlways" means when
we scroll up it reappears -->
<androidx.appcompat.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/ThemeOverlay.AppCompat.Light" />
<!--tabGravity="fill", if you want the tabs to be occupied the
fullwidth of the screen
If you want to keep your tabs horizontally centered, assign
tabGravity="center"
app:tabMode=> Defines the mode of the tab layout.
In our case the value should be “fixed” as we have limited number of
tabs but if you have many number of tabs where there is insufficient
space on the screen to fit all of them the you can change the
app:tabMode to "scrollable".-->
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabIndicatorColor="#F4F7FA"
app:tabGravity="fill">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:icon="#drawable/one_img"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two"
android:icon="#drawable/two_img"/>
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Android custom dialog button styles not applied

I wish to create a simple dialog for the user with 2 buttons as follows:
Dialog Layout (dialog_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical">
<Button
android:id="#+id/btn_select_choice_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Choice"
android:theme="#style/secondary_button_normal" />
<Button
android:id="#+id/btn_select_choice_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Choice"
android:theme="#style/secondary_button_normal" />
</LinearLayout>
secondary_button_normal:
<style name="secondary_button_normal" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/button_secondary_normal_background</item>
<item name="android:textColor">#color/button_secondary_normal_text</item>
<item name="android:textSize">#dimen/button_textSize</item>
<item name="android:padding">#dimen/button_padding</item>
</style>
Activity's onCreate:
final Dialog selection = new Dialog(this);
selection.setContentView(R.layout.dialog_layout);
Button selectFirstChoice = (Button)selection.findViewById(R.id.btn_select_choice_1);
Button selectSecondChoice = (Button)selection.findViewById(R.id.btn_select_choice_2);
selectFirstChoice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
selection.dismiss();
}
});
selectSecondChoice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
selection.dismiss();
}
});
selection.setTitle("Some Title");
selection.setCancelable(false);
selection.show();
The preview is alright:
Preview
It works well on Nougat, but when I run it on Lollipop (5.0 and 5.1.1), the buttons are without styling, although the same button styling worked on activity buttons on Lollipop:
App
I wonder what could be going wrong, I also tried moving the Dialog into a DialogFragment but I faced the same behavior.
Found the following solution:
In my Styles.xml, I added:
<style name="dialog_button" parent="Theme.AppCompat.Light.Dialog">
<item name="colorButtonNormal">#color/button_secondary_normal_background</item>
<item name="android:textColor">#color/button_secondary_normal_text</item>
<item name="android:textSize">#dimen/button_textSize</item>
<item name="android:padding">#dimen/button_padding</item>
</style>
And in my custom dialog layout, I used that style as a theme for my buttons:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_margin="8dp"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_select_student"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/student"
android:theme="#style/dialog_button" />
<Button
android:id="#+id/btn_select_tutor"
android:layout_width="match_parent"
android:text="#string/tutor"
android:layout_height="wrap_content"
android:theme="#style/dialog_button" />
</LinearLayout>

Button in dialog not behaving as a button when clicked

After create and testing my custom dialog I've noticed that my button does not show any visual sign of change (such as the ripple effect or highlighting) when clicked. Does anyone know what I'm doing wrong and how to resolve this issue?
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<Button
android:id="#+id/world"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:background="#color/green"
android:textColor="#color/white"
android:text="#string/world"
android:textAllCaps="false"
style="#android:style/TextAppearance.Medium"/>
</LinearLayout>
Java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_other_lines) {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_hello_world);
dialog.setTitle("Dialog");
Button world = (Button) dialog.findViewById(R.id.world);
world.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
return super.onOptionsItemSelected(item);
}
You override the default onClick effect by putting a background color.
android:background="#color/green"
You can do it by creating a custom background xml file on drawable, like this.
custom_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/Pressed_Color" />
<item android:state_activated="false" android:drawable="#color/green"/>
</selector>
then you call it on the button styling as like this:
android:background="#drawable/custom_background"
when you change your default appearance, you should handle different state if you want like this
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_sel" android:state_selected="true" />
<item android:drawable="#drawable/button_sel" android:state_pressed="true" />
<item android:drawable="#drawable/button_unsel" />
</selector>
save this as xml drawable and add as android:background

Categories