I am making an Android Application, and in some of the activities, I'm making the application show a PopupMenu when some items are clicked.
Problem is that the PopupMenu does not show all the options at once, instead, I'll have to scroll down the PopupMenu to see the rest of the menu options.
This problem happens in two activities, but in a third activity, it's showing as I want, although I'm using the same code to create the PopupMenu in all of those three activities!
these two photos clarify the problem I'm having: enter image description here enter image description here
and this is a photo of the activity that shows the menu as expected: enter image description here
this is the code of one of the two activities that has the problem:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupMenu;
import com.example.android.grad_qurantutor.data.DatabaseHelper;
import java.util.ArrayList;
public class FavoritesActivity extends AppCompatActivity {
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.general_list);
dbHelper = new DatabaseHelper(this);
final ArrayList<Ayah> arr = dbHelper.getFavorites();
if(arr.size() > 0) {
final AyahAdapter itemsAdapter = new AyahAdapter(this, arr);
final ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
final Ayah a = arr.get(i);
PopupMenu popup = new PopupMenu(FavoritesActivity.this, listView);
popup.getMenuInflater().inflate(R.menu.menu_favorites_options, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
// Respond to a click on the "Add to Favorites" menu option
case R.id.unfavorite:
dbHelper.unfavorite(a.getId());
ArrayList<Ayah> arr = dbHelper.getFavorites();
itemsAdapter.clear();
itemsAdapter.addAll(arr);
itemsAdapter.notifyDataSetChanged();
return true;
case R.id.go_to_surah:
Surah surah = dbHelper.getSurah(a.getSurahId());
Intent myIntent = new Intent(FavoritesActivity.this, AyahsActivity.class);
myIntent.putExtra("surahObj", surah);
myIntent.putExtra("ayahNum", a.getNumber());
startActivity(myIntent);
return true;
case R.id.share_ayah:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
String shareBody = a.getText();
shareIntent.putExtra(Intent.EXTRA_TEXT , shareBody);
startActivity(Intent.createChooser(shareIntent , "Share Using"));
return true;
}
return true;
}
});
popup.show();
}
});
}
}
}
AND this is the menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/unfavorite"
android:title="Unfavorite" />
<item
android:id="@+id/go_to_surah"
android:title="Go to the Surah" />
<item
android:id="@+id/share_ayah"
android:title="Share" />
</menu>
AND this is the xml layout for the activity:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF8E1"
android:drawSelectorOnTop="true"
android:orientation="vertical" />
and here are the codes for the activity that is showing the menu properly as expected:
the activity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupMenu;
import com.example.android.grad_qurantutor.data.DatabaseHelper;
import java.util.ArrayList;
public class SearchActivity extends AppCompatActivity {
private DatabaseHelper dbHelper;
class Listener implements AdapterView.OnItemClickListener{
private ArrayList<Ayah> arr;
private ListView listView;
public Listener(ArrayList<Ayah> arr, ListView listView){
this.arr = arr;
this.listView = listView;
}
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
final Ayah a = arr.get(i);
PopupMenu popup = new PopupMenu(SearchActivity.this, listView);
popup.getMenuInflater().inflate(R.menu.menu_searched_ayahs_options, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
// Respond to a click on the "Add to Favorites" menu option
case R.id.add_favorite2:
dbHelper.addFavoriteAyah(a.getId());
return true;
case R.id.go_to_surah2:
Surah surah = dbHelper.getSurah(a.getSurahId());
Intent myIntent = new Intent(SearchActivity.this, AyahsActivity.class);
myIntent.putExtra("surahObj", surah);
myIntent.putExtra("ayahNum", a.getNumber());
startActivity(myIntent);
return true;
case R.id.share_ayah:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
String shareBody = a.getText();
shareIntent.putExtra(Intent.EXTRA_TEXT , shareBody);
startActivity(Intent.createChooser(shareIntent , "Share Using"));
return true;
}
return true;
}
});
popup.show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
}
public void search(View view){
dbHelper = new DatabaseHelper(this);
EditText editText = (EditText) findViewById(R.id.search_edit_text);
ArrayList<Ayah> arr = dbHelper.search(editText.getText().toString().trim());
if(arr.size() > 0) {
AyahAdapter itemsAdapter = new AyahAdapter(this, arr);
ListView listView = (ListView) findViewById(R.id.search_list);
listView.setAdapter(itemsAdapter);
listView.setOnItemClickListener(new Listener(arr, listView));
}
}
}
AND this is the menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/add_favorite2"
android:title="Add to Favorites" />
<item
android:id="@+id/go_to_surah2"
android:title="Go to the Surah" />
<item
android:id="@+id/share_ayah"
android:title="Share" />
</menu>
AND this the the xml layout for the activity:
<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:background="#FFF8E1"
android:orientation="vertical"
tools:context=".SearchActivity">
<EditText
android:id="@+id/search_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="12dp"
android:hint="search" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="24dp"
android:onClick="search"
android:text="Search" />
<ListView
android:id="@+id/search_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFF8E1"
android:drawSelectorOnTop="true"
android:orientation="vertical" />
</LinearLayout>