This is my Starting Activity. It is working fine programmatically.
The only problem which I'm facing is that the OverFlow-Menu comes is dark theme when called by the Hardware Menu key.
I've provided all the essential lines of code in this sample.
public class StartingActivity extends AppCompatActivity implements LoginActivity_Tab.loginActivityTab_interface {
private static final String LOGIN_URL = "http://rollhack.96.lt/login.php";
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting);
includeWidgets();
}
private void includeWidgets() {
///// Tab Layout /////
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
///// ToolBar /////
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("Note Pad");
}
///// This method is called for Exiting the App /////
public void exitApp() {
final AlertDialog.Builder exit = new AlertDialog.Builder(StartingActivity.this);
exit.setTitle("Exit Application")
.setMessage("Are you sure you want to Exit the application?")
.setCancelable(true)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
StartingActivity.this.finishAffinity();
}
})
.setNegativeButton(android.R.string.no, null);
exit.create().show();
}
///// This method is called to assign the Key Events /////
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
exitApp();
return true;
}
return super.onKeyUp(keyCode, event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_starting_activity, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
return Boolean.parseBoolean(null);
case (R.id.menu_about):
return Boolean.parseBoolean(null);
case (R.id.menu_exit):
exitApp();
return true;
}
return super.onOptionsItemSelected(item);
}
This is how it looks when it is called by the overflow menu icon of the ActionBar
This is how my App OverFlow-Menu looks when Hardware Menu key is pressed
Overflow menu by Icon
You can change the color of the overflow menu background by adding a new style in style.xml.
<style name="OverflowMenu"
parent="#android:style/Theme.Holo">
<item name="android:popupMenuStyle">#style/MyOverflowMenu</item>
<item name="android:itemTextAppearance">#style/TextAppearance</item>
</style>
<style name="MyOverflowMenu"
parent="#android:style/Widget.Holo.ListPopupWindow">
<item name="android:popupBackground">#color/your_color</item>
</style>
<style name="TextAppearance">
<item name="android:textColor">#color/your_color</item>
</style>
Overflow Menu by Hardware Key
To change the color of Overflow menu called by Hardware key, add another style.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="actionBarPopupTheme">#style/OverflowMenuHardware</item>
</style>
<style name="OverflowMenuHardware" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textColorSecondary">#color/your_color</item>
<item name="android:colorBackground">#color/your_color</item>
</style>
Implement either one of these to change the color of Overflow menu of either the one called by icon(former code) or by hardware key(latter code).
Related
I have a frame where I put different fragments in. Some has actionbar displayed, some does not. But the one given below does not display action bar as any other.
public class AFragment extends Fragment {
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_a, container, false);
ButterKnife.bind(this, view);
setHasOptionsMenu(true);
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_a, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_a) {
//getFragmentManager().popBackStackImmediate();
}
return super.onOptionsItemSelected(item);
}
}
This fragment is called from previous fragment like
AFragment aFragment = new AFragment ();
getFragmentManager().beginTransaction()
.replace(R.id.fragment, aFragment , "fragment_a")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(null)
.commit();
By the way, I do not show action bar in the previous fragment.
In case it is needed, 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>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppActionBar" parent="AppTheme">
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
For fragments you have to make sure that the setHasOptionsMenu() is set in the onCreate() method and now the onCreateView() method.
Then be sure to set the toolbar(if added in the fragment layout) as the toolbar for the activity.
public class AFragment extends Fragment {
View view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_a, container, false);
ButterKnife.bind(this, view);
//moved to onCreate() method
//setHasOptionsMenu(true);
//set toolbar as the default for the activity class if AppCompat
((AppCompatActivity) getActivity()).setSupportActionBar(/*your toolbar goes here*/);
//set toolbar is activity class is not AppCompat
getActivity().setActionBar(/*your toolbar goes here*/);
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_a, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_a) {
//getFragmentManager().popBackStackImmediate();
}
return super.onOptionsItemSelected(item);
}
}
I am a beginner Android developer and I have been struggling the issue that I can't see the toolbar in any activity that inherits Base Activity. According to other resource, to use the same toolbar in the different activities. I have to implement it in Base Activity and inherit it where I need to use it. Could anyone help me figure out the problem?
styles.xml
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>
quiz.menu.xml inside of menu folder
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/contact"
android:icon="#drawable/ic_contacts_black_24dp"
android:title="Contact"
app:showAsAction="ifRoom" />
<item android:id="#+id/language"
android:title="Language"
app:showAsAction="never" />
<item android:id="#+id/speech"
android:title="Speech"
app:showAsAction="never">
<munu>
<item android:id="#+id/subitem1"
android:title="Sub Item 1"/>
<item android:id="#+id/subitem2"
android:title="Sub Item 2"/>
</munu>
</item>
</menu>
BaseActivity
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.quiz_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.contact:
Toast.makeText(this, "Contact is selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.language:
Toast.makeText(this, "Language is selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.speech:
Toast.makeText(this, "Speech is selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.subitem1:
Toast.makeText(this, "Language is selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.subitem2:
Toast.makeText(this, "Speech is selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
WelcomeActivity (inherits base Activity)
public class WelcomeActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
final Button databaseButton = findViewById(R.id.database);
databaseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
Intent databaseIntent = new Intent(WelcomeActivity.this, Questionnaire.class);
startActivity(databaseIntent);
}
});
}
Questionnaire
public class Questionnaire extends BaseActivity {
public Spinner languageSpinner;
public int languageId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questionnaire);
...
}
}
The answer is actually simple. You firstly set contentview in OnCreate() method of the BaseActivity class, then you change the view to another xml file in OnCreate() methods of the child classes.
What I suggest is that you do not implement OnCreate() method in BaseActivity class but implement SetContentView() method in BaseActivity.
In short, delete onCreate() method from BaseActivity() and add setContentView() method in the below.
#Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
P.S - Your quiz_menu.xml file does not look like a menu file. :)
P.P.S - Let me know if it works, or if you have troubles. :)
I am creating an android application where in one activity I was displaying tabs with the help of actionbar, So I have to change actionbar background color. But whenever I am trying to change the background color it will display either white background or black background. So how can I modify the action bar to change its background color?
Below is my activity code:-
public class SavedRecord extends FragmentActivity implements ActionBar.TabListener{
private ActionBar actionBar;
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
String[] tabs = {"Audio","Video"};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.saved_record);
//initialization
actionBar = getActionBar();
viewPager = (ViewPager) findViewById(R.id.view_pager);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setIcon(null);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setTitle("Saved Record");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ff7043"));
actionBar.setStackedBackgroundDrawable(colorDrawable);
for (String tab_name:tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
#Override
public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
}
}
And the screenshot of the activity:-
I assume that you are using the "regular" ActionBar.
Try to use the SupportActionBar by importing
import android.support.v7.app.ActionBar;
and calling
actionBar = getSupportActionBar();
I think this should fix your problem.
Try to change using style of your App
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">/*your custom color*/</item>
</style>
</resources>
I want transparent navigation bar and status bar for my dialog activity and i have this codes in my style:
<style name="AppTheme.Dialog" parent="Theme.AppCompat.Dialog">
<item name="colorAccent">#color/gray</item>
<item name="windowNoTitle">true</item>
</style>
and this in java:
public class FilterActivity extends AppCompatActivity {
FloatingActionButton fab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.filter_activity);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
fab = (FloatingActionButton) findViewById(R.id.fab_filter_close);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
my dialog activity is full screen and has transparent background but navigation bar and status bar is black. what can i do?
I've been searching for half an hour and none of the solutions for other people are working for me.
Here is what is shows in the preview to the right:
Here is the java code in my activity (I removed the other stuff):
public class PlayGame extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_game);
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_playgame);
myToolbar.setTitle("ChessClock");
myToolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(myToolbar);
}
#Override
public void onBackPressed() { } //Back Button Disabled
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbaroverflow, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_pause:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
All the instructions said to make an XML in my 'menu' directory but I didn't have one so I made it (called 'menu' inside of 'res'). The XML 'actionbaroverflow' has this code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_pause"
android:orderInCategory="100"
android:title="Pause"
app:showAsAction="always"/>
And the item is simply not showing up when I run the app. I had an icon but someone said it could have been too big, so I just made it text and it's still not showing up.
Tou need to add to your R.menu.actionbaroverflow
android:icon="#drawable/your_icon"
Will be something like this:
<item
android:id="#+id/action_pause"
android:icon="#drawable/your_icon"
android:orderInCategory="100"
android:title="Pause"
app:showAsAction="always"/>
This works for me fine:
<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:activity=".ProductsNewOrder">
<item android:id="#+id/action_pause"
android:title="Pause"
android:visible="true"
app:showAsAction="always"
android:icon="#drawable/ic_pause"/>
</menu>
and in the activity:
setSupportActionBar(myToolbar);
if (myToolbar!= null) {
myToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_pause: {
//do something
break;
}
}
return true;
}
});
}
and where i inflate the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbaroverflow, menu);
return true;
}
Hope it helps!!!