android facebook UserSettingsFragment not working - android

I am pretty new to android and facebook plugin.trying to follow the below steps as mentioned in facebook
developer site.
https://developers.facebook.com/docs/android/scrumptious/authenticate/
Go to Step 3.
I followed it without missing any steps.
However, I do not see a menu at the bottom when i am logged in.Any help?
Below is the snippet of important codes and xml.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// only add the menu when the selection fragment is showing
if (fragments[SELECTION].isVisible()) {
if (menu.size() == 0) {
Log.d("onPrepareOptionsMenu","Yes");
settings = menu.add(R.string.settings);
}
return true;
} else {
menu.clear();
settings = null;
}
return false;
}
Following the main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.punit.xxx.SelectionFragment"
android:id="#+id/selectionFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment android:name="com.punit.xxx.SplashFragment"
android:id="#+id/splashFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment android:name="com.facebook.widget.UserSettingsFragment"
android:id="#+id/userSettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I tried to put some log and found that OnPrepareotionsMenu is never called.

Remove the onPrepareOptionsMenu(Menu menu) from your code.
Add *item
android:id="#+id/action_logout"
MyNews:showAsAction="never"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
android:orderInCategory="10"
android:title="#string/logout"/*
Note: replace * with < or >.
Add #Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_logout:
showSettingsFragment();
return true;
}
return false;
}
public void showSettingsFragment(){
showFragment(SETTINGS, true);
}
It worked for me.

Related

Not recognize one menu item in Android

I don't understand why in this case is not recongnizing my menu item.
I have another activities with similar code and it's working fine, but in this case i'm having NPE.
Layouts
General (activity_completas)
<?xml version="1.0" encoding="utf-8"?>
<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=".activities.CompletasActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_breviario_general" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_breviario_general
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<include
android:id="#+id/include"
layout="#layout/tv_zoomable" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
app:layout_constraintBottom_toTopOf="#+id/include"
app:layout_constraintEnd_toStartOf="#+id/include"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.BreviarioActivity">
<item
android:id="#+id/item_voz"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:icon="#drawable/ic_play"
android:visible="false"
android:title="#string/leer"
app:showAsAction="ifRoom" />
<item
android:id="#+id/item_calendario"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:icon="#drawable/ic_calendar_toolbar"
android:title="#string/all_calendar"
app:showAsAction="ifRoom" />
</menu>
Java code
Relevant parts only for this case:
public class CompletasActivity extends AppCompatActivity {
//...
private Menu menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_completas);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
showData();
}
private void showData() {
sbReader = new StringBuilder();
//I fill sbReader correctly
if (sbReader.length()>0) {
menu.findItem(R.id.item_voz).setVisible(true); //******NPE here*******
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (tts != null) {
tts.cerrar();
}
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.item_voz:
String html = String.valueOf(Utils.fromHtml(sbReader.toString()));
String[] textParts = html.split(SEPARADOR);
tts = new TTS(getApplicationContext(), textParts);
return true;
case R.id.item_calendario:
Intent i = new Intent(this, CalendarioActivity.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
super.onBackPressed();
if (tts != null) {
tts.cerrar();
}
}
}
Error
java.lang.NullPointerException: Attempt to invoke interface method
'android.view.MenuItem android.view.Menu.findItem(int)' on a null
object reference
I do the same thing in another activities and i don't have NEP. What's happening here?
You are getting null point exception because your menu isn't instantiated. As i can see, your are calling showData before creating the menu. Call showData in the onCreateMenu. This should solve your problem.

NavigationView actionaBar onOptionsItemSelected R.id.home not triggered

I want to implement the up button in an android application with only one activity that changes its content with different fragment.
I used the default navigation drawer activity provided by android studio where i added a frameLayout to the content_main.
In the fragment where i want the up botton to be shown i added this line of code in the onCreateView method:
ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
ad this line in the onCreate method:
setHasOptionsMenu(true);
and i added the method to catch the click of it:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Log.w("second fragment","clicked back");
return true;
}
return super.onOptionsItemSelected(item);
}
in the activiy i set onCreateOptionsMenu like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
but the click of it isn't triggered.
I tried to add a setting button and it is triggered.
I already read a lot of question about this but i can't figure out how to resolve it
setHasOptionsMenu(true) should be called in method onCreate() to let the FragmentManager know that your fragment needs to receive options menu callbacks.
I believe you use this constructor for ActionBarDrawerToggle which takes a Toolbar as a parameter. If you are using this constructor, onOptionsItemSelected will not be called if you click on the indicator. There are questions/answers regarding this problem, for example:
AppCompat v7 Toolbar onOptionsItemSelected not called
But for me, none of them worked perfectly so I used the Toolbar-specific constructor and here it's a workaround for my case. I put a transparent view on top of the toolbar which is "visible" only when I show the back button and I handle the click myself (calling onBackPressed() in my case).
I know it's a kind of hack, but it needed and worked for me.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/main_background">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/layoutMainContent"
android:layout_below="#id/appBar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
<!-- The important view -->
<View
android:id="#+id/viewFakeBack"
android:layout_width="56dp"
android:layout_height="56dp"
android:clickable="true"
android:visibility="gone"/>
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/navigation_drawer"/>
</android.support.v4.widget.DrawerLayout>
Try by using switch(...) case statement.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//this will make Hamburger button clickable.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
//this will make the HomeAsUpIndicator button clickable.
switch (item.getItemId()) {
case android.R.id.home:
Log.w("second fragment","clicked back");
break;
}
return super.onOptionsItemSelected(item);
}
Hope this will help you.

onOptionsItemSelected listener not working

I'm developing an android app and I have an issue with the options menu. The onOptionsItemSelected() method is not called when the menu item is pressed, I'm using an actionLayout so, as many questions posted in this site, I've set my own listener. Nothing seems to work and I can't see how my solution differs from others.
Here it's my code:
On MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d("Listener", "Created");
getMenuInflater().inflate(R.menu.main, menu);
for (int i = 0; i < menu.size(); i++) {
final MenuItem item = menu.getItem(i);
if (item.getItemId() == R.id.action_cart) {
View itemChooser = item.getActionView();
if (itemChooser != null) {
itemChooser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Listener", "Clicked1");
onOptionsItemSelected(item);
}
});
}
}
}
return super.onCreateOptionsMenu(menu);
}
#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.
switch (item.getItemId()) {
case R.id.action_cart:
Log.d("Listener", "Clicked");
CartFragment newFragment = new CartFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
On menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="cart"
android:id="#+id/action_cart"
app:showAsAction="always"
app:actionLayout="#layout/badge_layout"/>
</menu>
On layout/badge_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:addStatesFromChildren="true">
<com.joanzapata.iconify.widget.IconButton
android:layout_width="44dp"
android:layout_height="44dp"
android:textSize="24sp"
android:textColor="#ffffff"
android:background="#mipmap/ic_cart"
android:id="#+id/badge_icon_button"/>
<TextView
android:id="#+id/badge_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/badge_icon_button"
android:layout_alignLeft="#id/badge_icon_button"
android:layout_alignStart="#id/badge_icon_button"
android:text="10"
android:paddingEnd="12dp"
android:paddingRight="0dp"
android:paddingLeft="0dp"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="13sp"
android:textStyle="bold"
android:background="#drawable/badge_circle"/>
</FrameLayout>
As for the logs, "Listener: Created" is the only one printed.
I've already tried the solutions of several related questions like this one and this one. The actionlayout represents a badge inside a shopping cart and I know it has something to do with that since before the actionlayout it worked.
Any help is appreciated. Thanks.
remove the onOptionsItemSelected method and change the oncreateOptionsMenu to
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.action_cart);
if (item != null) {
item.getActionView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Listener", "Clicked");
}
});
}
return true;
}
and the layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true">
<com.joanzapata.iconify.widget.IconButton
android:layout_width="44dp"
android:layout_height="44dp"
android:clickable="false"
android:textSize="24sp"
android:textColor="#ffffff"
android:background="#mipmap/ic_cart"
android:id="#+id/badge_icon_button"/>
<TextView
android:id="#+id/badge_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/badge_icon_button"
android:layout_alignLeft="#id/badge_icon_button"
android:layout_alignStart="#id/badge_icon_button"
android:text="10"
android:paddingEnd="12dp"
android:paddingRight="0dp"
android:paddingLeft="0dp"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="13sp"
android:textStyle="bold"
android:background="#android:color/holo_red_dark"/>
</RelativeLayout>
What really solved the problem was android:clickable="false" on com.joanzapata.iconify.widget.IconButton

How to check if a fragment is visible

I have a layout that holds 3 fragments.
<?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:orientation="horizontal" >
<fragment android:name="com.carefreegroup.rr3.carefreeoncall.CarerAwayCarerListFragment"
android:id="#+id/carerlist"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/carerawaydatetimefragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/carerawayreasonlistfragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
.
The first fragment on the left will load automatically when this layout loads. I have a Fragment called dateTime Fragment that loads in the middle fragment based on a listview choice in the 1st fragment. The dateTime fragment has an optionsmenu in which i want to hide one of the options based on whether the far right fragment(reasonList Fragment) is showing.
My question is how can i check if the reasonList fragment on the far right is showing from the onPrepareOptionsMenu of the middle DateTime fragment.
I have tried the following but it returns true as the fragment container in the layout is there.
#Override
public void onPrepareOptionsMenu(Menu menu) {
View reasonListView = getActivity().findViewById(R.id.carerawayreasonlistfragment_container);
if(reasonListView != null && reasonListView.getVisibility() == View.VISIBLE){
menu.getItem(5).setVisible(false);
}else{
menu.getItem(5).setVisible(true);
}
super.onPrepareOptionsMenu(menu);
}
.
I can't use findFragmentByTag as there is only the reasonList container in the view at that time.
thanks
you can override setUserVisibleHint in the fragment
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
}
http://developer.android.com/reference/android/support/v4/app/Fragment.html#setUserVisibleHint%28boolean%29
[solved]
#Override
public void onPrepareOptionsMenu(Menu menu) {
CarerAwayReasonUpdatefragment reasonListView = (CarerAwayReasonUpdatefragment) getFragmentManager().findFragmentById(R.id.carerawayreasonlistfragment_container);
if(reasonListView == null ) {
menu.getItem(5).setVisible(true);
} else {
menu.getItem(5).setVisible(false);
}
super.onPrepareOptionsMenu(menu);
}

How to add a switch to android action bar?

I would like to add a button switch similar to jellybean native look. (Blue/gray switch at the top of the view)
Documentation shows how to create a menu there or add icons, but it does not say, how to add a custom elements. eg. a switch.
http://developer.android.com/guide/topics/ui/actionbar.html
Create a layout for the switch switch_layout.xml. Custom layouts for menu should always be RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Switch
android:id="#+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
Then, in your mainmenu.xml add the item as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="#layout/switch_layout"
/>
</menu>
And in your activity, inflate the mainmenu.xml as you always do
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
Finally figured out my problem: for those that's using the new AppCompat, you should be using android.support.v7.widget.SwitchCompat instead of Switch on the switch layout...otherwise, it won't show on the ActionBar (assumed you're using AppCompat ActionBar as well), well, the actionLayout attribute doesn't work, it has to be set in the code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/switchView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v7.widget.SwitchCompat
android:id="#+id/switchForActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
Then set the layout in the code:
#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);
MenuItem item = menu.findItem(R.id.on_off_switch);
item.setActionView(R.layout.on_off_switch);
return true;
}
If the widget is not appearing in the action-bar it is probably because you are using appCompat for your action-bar. To solve this switch "android:" to "app:" in front of "showAsAction" and "actionLayout" in your menu.xml
Add item to xml, with app: in place of android:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="#layout/switch_layout"
/>
</menu>
Make layout that you are using for your "app:actionLayout"
switch_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Switch
android:id="#+id/switchAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Inflate the menu in your ActionBarActivity as you would normally
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
This should make the switch appear in your action-bar, if it was not appearing.
For those who want to add,
Checked change Listener to the same Switch
Kotlin
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
val item = menu!!.findItem(R.id.my_switch_item)
item.setActionView(R.layout.switch_layout)
val mySwitch = item.actionView.findViewById(R.id.switch_id)
mySwitch.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{
override fun onCheckedChanged(p0: CompoundButton?, isChecked: Boolean) {
// do what you want with isChecked
}
})
return true
}
Java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.my_switch_item);
item.setActionView(R.layout.switch_layout);
Switch mySwitch = item.getActionView().findViewById(R.id.switch_id);
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something based on isChecked
}
});
return true;
}
P.S. You can change reference to Switch or SwitchCompat
The solution given by Ezequiel is awesome and works. Here goes another approach:
Define your custom layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" >
<Switch
android:id="#+id/actionbar_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
Inflate it programatically:
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbar_top);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
...
Switch button = (Switch) findViewById(R.id.actionbar_switch);

Categories

Resources