I've been trying to get an Action Button working on my Android app, and been failing miserably. I've read so many questions and answers and nothing seems to work. The video link always just appears in the menu which I do not want
Here is the menu Xml.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:puertogaleradivesites="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/youtube"
android:icon="#drawable/ic_play_arrow_black_24dp"
android:title="YouTube"
puertogaleradivesites:showAsAction="ifRoom"/>
<item android:id="#+id/action_settings"
puertogaleradivesites:showAsAction="never"
android:title="Settings"/>
</menu>
And here is the main Java Fragment
public class DivesiteFragment extends Fragment {
private Divesite mDivesite;
private TextView mDepthField;
private TextView mSiteDescription;
private TextView mDistance;
private ProportionalImageView mImageView;
public static final String EXTRA_SITE_ID = "com.blueribbondivers.extraSiteID";
private Context mContext;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
UUID crimeId = (UUID)getActivity().getIntent()
.getSerializableExtra(EXTRA_SITE_ID);
mDivesite = DiveSites.get(getActivity()).getDiveSite(crimeId);
mContext = getActivity().getApplicationContext();
//getActivity().getActionBar().setTitle(Html.fromHtml("<font color=\"#1c3565\">" + mDivesite.getName() + "</font>"));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_divesite, parent, false);
Resources resources = mContext.getResources();
//setHasOptionsMenu(true);
mDepthField = (TextView)v.findViewById(R.id.divesite_display_depth);
mDepthField.setText(mDivesite.getMaxDepth());
mSiteDescription = (TextView)v.findViewById(R.id.divesite_display_description);
mSiteDescription.setText(mDivesite.getSiteDescription());
mSiteDescription.setMovementMethod(new ScrollingMovementMethod());
mDistance = (TextView)v.findViewById(R.id.divesite_display_distance);
mDistance.setText(mDivesite.getTravelTime());
mImageView = (ProportionalImageView)v.findViewById(R.id.divesite_display_imageView);
String imageName = mDivesite.getPhoto();
final int resourceID = resources.getIdentifier(imageName,"drawable",mContext.getPackageName());
mImageView.setImageResource(resourceID);
setHasOptionsMenu(true);
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.divesitemenu, menu);
super.onCreateOptionsMenu(menu,inflater);
}
}
I have the full project on Github also https://github.com/Jonnyblue/PuertoGaleraDiveSites
Any help would be greatly appreciated.
For displaying menus like Youtbe, gmail:
You should use yourapp:showAsAction="always"
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".HomeActivity">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search"
yourapp:showAsAction="always"
android:title="#string/search"/>
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_refresh"
yourapp:showAsAction="always"
android:title="#string/refresh"/>
</menu>
Hope it will help you.
Your activity extends FragmentActivity. If you'd like a backward compatible action bar that works the same on all API levels (things changed significantly in API 21), you should instead extend AppCompatActivity
If you want to show an ActioBar item a as a button but not as an overflow menu, You need to define an android:orderInCategory for it. So that it doesn't comes inside the overflow 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/action_search"
android:icon="#drawable/ic_magnify"
android:orderInCategory="100"
app:showAsAction="ifRoom|collapseActionView"
android:title="#string/noticiation_count"/>
</menu>
Also as you are using fragments its safe to use this line inside the onCreate() method,
setHasOptionsMenu(true);
Tested and working perfectly with Fragments.
Related
I want to transition the color of my background when a button is clicked but my app seems to crash once I press the button. The button runs the TransitionDrawable code. I have set my background to the drawable folder containing the transition.Could anyone help me? Thank you very much!
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void colourChangeButton(View view) {
final TransitionDrawable transition = (TransitionDrawable) view.getBackground();
transition.startTransition(1000);
}
}
Here are my drawable files that define the colours:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#2575fc"></color>
</item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#ff0844"></color>
</item>
</selector>
Here is my transition drawable file :
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/blue_background"></item>
<item android:drawable="#drawable/pink_background"></item>
</transition>
You are almost there!
You need to set the transition drawable as the background of your view.
Let's say your activity's layout xml is called activity_main.xml and your
transition drawable file is called transition.xml.
In the root layout of your activity_main.xml:
<LinearLayout
...
id="#+id/main_layout"
background="#drawable/transition">
<Button
id="#+id/start_transition_button"
... />
...
In MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
TransitionDrawable transition = (TransitionDrawable) layout.getBackground();
Button button = (Button) findViewById(R.id.start_transition_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
transition.startTransition(500);
}
}
}
}
Here is a good post about it:
https://proandroiddev.com/transitiondrawable-small-gems-of-the-android-framework-4dcdd3c83319
I created a Toolbar and a "3-dot menu" that goes on the Toolbar. In my HomeActivity, the menu displays. However, in other activities, the menu doesn't (but the Toolbar does). I'm not sure what's happening here.
Here is the toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/Theme.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
Here is the settings_menu.xml that should show the 3-dot 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/item2"
android:title="Item 2"
app:showAsAction="never" />
<item android:id="#+id/item3"
android:title="Item 3"
app:showAsAction="never">
<menu>
<item android:id="#+id/subitem1"
android:title="Sub Item 1"/>
<item android:id="#+id/subitem2"
android:title="Sub Item 2"/>
</menu>
</item>
</menu>
Here is the onCreate() method of HomeActivity, where the menu DOES appear.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// deactivate the fullscreen mode used by the splash screen
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.activity_home);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tvClients = findViewById(R.id.tvClients);
tvClientList = findViewById(R.id.tvClientList);
tvClientName = findViewById(R.id.tvClientName);
buttonSettings = findViewById(R.id.buttonSettings);
buttonLogout = findViewById(R.id.buttonLogout);
buttonCreateNewClient = findViewById(R.id.buttonCreateNewClient);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
And here is the onCreate() method of another activity, accessed through a button click in the HomeActivity, where the menu button does NOT appear.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_new_client);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Create New Client");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
etClientFirstName = findViewById(R.id.etClientFirstName);
etClientLastName = findViewById(R.id.etClientLastName);
etClientAge = findViewById(R.id.etClientAge);
buttonCreateClient = findViewById(R.id.buttonCreateClient);
buttonCreateClient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createClient();
}
});
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
}
you have to override the onCreateOptionsMenu in every activity you need menu to be shown within
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings_menu, menu);
// return true so that the menu pop up is opened
return true;
}
check this thread for full details
Tl;dr: I am using ListView's setSelector(...) to change the background color of a selected item, and while the color does change, it picks a default color and not the one I want.
I want to create a ListView that allows just one item to be selected at a time. (Almost) everything works fine, but the selected item is always colored in a default color, no matter what I tried. Here is my code:
ListView:
<android.support.constraint.ConstraintLayout
android:id="#+id/account_picker_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rounded_corner_button" >
<ListView
android:id="#+id/account_picker_list"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/account_picker_list_corner"
android:backgroundTint="#color/white"
android:choiceMode="singleChoice"
android:listSelector="#drawable/selection_effect"
app:layout_constraintBottom_toTopOf="#+id/account_picker_divider2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ListView>
[...]
</android.support.constraint.ConstraintLayout>
selection_effect.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/highlightColor" android:state_activated="false"/>
<item android:drawable="#color/green" android:state_pressed="true"/>
<item android:drawable="#color/red" android:state_activated="true"/>
<item android:drawable="#color/colorAccent" android:state_focused="true"/>
<item android:drawable="#color/lightred" android:state_enabled="true"/>
<item android:drawable="#color/lightgreen" android:state_checked="true"/>
<item android:drawable="#color/colorAccentLight" android:state_active="true"/>
<item android:drawable="#color/buttonColor" android:state_selected="true"/>
<item android:drawable="#android:color/black" android:state_hovered="true"/>
<item android:drawable="#color/red" android:state_window_focused="true"/>
<item android:drawable="#color/red" android:state_single="true"/>
</selector>
relevant code in the activity that manages the ListView:
private void createAccountViewAdapter() {
// Create the adapter to convert the array to views
ArrayList<Account> al = new ArrayList<>();
if (accountViewAdapter == null) {
accountViewAdapter = new AccountPickerViewAdapter(this, al, this);
}
ListView listView = findViewById(R.id.account_picker_list);
listView.setSelector(R.drawable.selection_effect);
listView.setAdapter(accountViewAdapter);
updateAccountViewAdapter();
}
public void updateAccountViewAdapter() {
TextView noAccounts = findViewById(R.id.account_picker_no_account);
ArrayList<Account> al = new ArrayList<>();
if (!AccountHandler.getAllAccounts().isEmpty()) {
al = AccountHandler.getAllAccounts();
noAccounts.setText("");
} else {
noAccounts.setText(getResources().getString(R.string.no_accounts));
}
accountViewAdapter.refreshItems(al);
}
The custom ListView adapter only implements getView(...) and does not change the backround color in any way.
As you can see from the statements in the selection_effect.xml, I already tried about every combination of possible states android let me use, needless to say, the actual color displayed in the app it not among the ones I defined above. What am I missing?
Edit: Turns out the app was using the default colors of the AppTheme I set for the activity in question. After changing the default colors in the styles.xml, the selection color was now defaulting to the new color. However, I still don't understand why I can't set a custom color for the selected item.
<activity
android:name=".AccountPicker"
android:theme="#style/AppTheme.Popup">
</activity>
<style name="AppTheme.Popup" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="colorPrimary">#color/colorAccentLight</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Edit2: Here is the AccountPickerViewAdapter class:
public class AccountPickerViewAdapter extends ArrayAdapter<Account> {
ArrayList<Account> accounts;
Activity intent;
public AccountPickerViewAdapter(Context context, ArrayList<Account> accounts, Activity intent) {
super(context, 0, accounts);
this.intent = intent;
this.accounts = accounts;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.account_picker_item, parent, false);
}
final Account myAccount = getItem(position);
TextView name = convertView.findViewById(R.id.account_picker_item_name);
TextView balance = convertView.findViewById(R.id.account_picker_item_balance);
TextView description = convertView.findViewById(R.id.account_picker_item_description);
Button confirm = convertView.findViewById(R.id.account_picker_item_confirm);
name.setText(myAccount.getName());
balance.setText(AccountHelper.stringifyBigDec(myAccount.getBalance(), false, true, myAccount.getId()));
description.setText(myAccount.getDescription());
/*
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AccountHandler.setActiveAccount(myAccount.getId());
intent.finish();
}
});
confirm.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
return true;
}
});
*/
return convertView;
}
public void refreshItems(ArrayList<Account> items) {
this.accounts.clear();
this.accounts.addAll(items);
notifyDataSetChanged();
}
}
Add an extra attribute named such as isSelected to Account class.
class Account {
... other attributes
private boolean isSelected;
}
In getView of AccountPickerViewAdapter:
final Account myAccount = getItem(position);
if (myAccount.isSelected()) {
// set selected background
} else {
// set default background
}
When onItemClick of the list view set isSelected as true of the clicked account item.
Or, for another approach see this question
The main XML
Need to access the Item click via id (nav_camera)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Camera" />
</group>
This s Xml file
Try to access via following code.
public class Camera extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
Menu nav_camera =(Menu)findViewById(R.menu.nav_camera);
}
}
need to access Item(nav_camera) via id;
Try this:
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.menu_landing_page, menu);
super.onCreateOptionsMenu(menu, menuInflater);
MenuItem pinMenuItem = menu.findItem(R.id.menuItemID);
...
...
...
}
or please add more description on what do you want to do so I can provide more specific answer
I have 3 activities in my project and I would like to remove/hide the searchView in the actionbar on only one activity.
I tried searchView.setVisibility(View.GONE); but there is still the clickable icon.
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_search"
android:title="#string/menu_search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/item_clear_memory_cache"
android:title="#string/menu_item_clear_memory_cache"/>
<item
android:id="#+id/item_clear_disc_cache"
android:title="#string/menu_item_clear_disc_cache"/>
</menu>
menu.findItem(R.id.menu_search).setVisible(false); should be the way.
You can hide the searchview and the searchicon by doing this:
searchItem.setVisible(false);
searchView.setVisibility(View.GONE);
Then you can bring it back by:
searchItem.setVisible(true);
searchView.setVisibility(View.VISIBLE);
Well, after some researches here you can find my complete example. Don't forget to set visibility and visible back in activity or fragment where you need to show your search view back.
I was looking a way to hide search view in preferences.
public class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this means that we're telling the Activity that is hosting the Fragment that we want to display menus
setHasOptionsMenu(true);
}
/**
* ********************* SEARCH ********************
*/
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) item.getActionView();
item.setVisible(false);
searchView.setVisibility(View.GONE);
}
}
And menu_main.xml
<item android:id="#+id/action_search"
android:title="#string/action_search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
/>