Is it possible to customise Android PopupMenu? - android

Is it possible to customise PopupMenu layout ? I just need with rounded corners, is it possible to customise with the stock one ?

For custom layouts don't use PopupMenu, alternate option is a PopupWindow
PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(clickbtn, -40, 18); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);
public PopupWindow popupDisplay() {
final PopupWindow popupWindow = new PopupWindow(this);
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.my_popupwindow_layout, null);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}
And create this my_popupwindow_layout.xml file in the res/layout folder, which is used to inflate the custom layout for the popupWindow.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>

Related

Inflate image on top of a popupview object

Currently for inflating images (zoom in animation) we use this library.
while on top of regular activities it looks fine (Simple layout), on top of a popupview the image is "behind" the popup.
Tried to change the elevation parameters, but no luck there either.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:elevation="2dp"
tools:context="com.MapActivity">
<com.vatsal.imagezoomer.ImageZoomButton
android:id="#+id/bt_picture"
android:layout_width="20dp"
android:layout_height="17dp"
android:layout_alignTop="#id/tx_description"
android:layout_marginTop="1dp"
android:layout_alignLeft="#+id/bt_date_time"
android:layout_marginLeft="-25dp"
android:elevation="10dp"
android:background="#drawable/ic_picture" />
</RelativeLayout>
And this is how the popupview and windows are inflated:
popupView = getLayoutInflater().inflate(R.layout.job, null);
popupWindow = new PopupWindow(popupView,
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
Should we try and use a different approach? Mabye ditch the library and inflate it differently?
Thanks
We've followed mmdreza's advice, here's a snippet of the implementation.
As the documantation states, the alertdialog will be on top of the popupview object.
myHolder.jobImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(v.getContext());
LayoutInflater inflater =
(LayoutInflater)v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mView = inflater.inflate(R.layout.inflated_picture_dialog, null);
Picasso.get().load(jobView.getJobImageURL()).fit().into(((ImageButton)mView.findViewById(R.id.inflated_picture)));
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
}
});

why dynamically set text to TextView not visible in popup window while hard coded is

I am trying to generate popup window containing dynamically generated text. log output clearly shows that the desired text is actually set to textView but I can't understand why it is not showing on the app.
MainActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
linearLayout = (LinearLayout) findViewById(R.id.main_activity_parent_layout);
int id = item.getItemId();
if(id == R.id.info_button){
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
//Updating a textview in different layout xml from the main activity
LinearLayout vi = (LinearLayout)layoutInflater.inflate(R.layout.activity_info, null);
TextView textView = vi.findViewById(R.id.info);
textView.setText(R.string.main_activity_info);
Log.i("MainActivity",String.valueOf(textView.getText()));
ViewGroup container = (ViewGroup)layoutInflater.inflate(R.layout.activity_info,null);
popupWindow = new PopupWindow(container, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//Note showAtLocation method should always in end for proper rendering of poup Window
popupWindow.showAtLocation(linearLayout,Gravity.TOP,0,100);
}
return super.onOptionsItemSelected(item);
}
activity_info.xml
<?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_height="match_parent">
<TextView
android:id="#+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:padding="8dp"
android:text="hello"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#android:color/black"/>
</LinearLayout>
popup window show hello which is hardcoded text.
You are inflating R.layout.activity_info two times and losing the TextView you are changing in the process. Make the following change to keep the first inflation and your modification:
popupWindow = new PopupWindow(vi, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);

How to add subview of some view in xml layout

I am a beginner programming android. I am searching for info how to create transparent sub view on current window layout.
I have created simple layout this is source:
<?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="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple text" />
</LinearLayout>
Now i want to create a View when navigation button is pressed. That View i want to add on top of this layout with transparent about 40%. It should look something like this:
Also it should be easy to add Buttons, Drop box or else and i could easy remove this View.
Maybe someone did this and could me share ideas, how do do this ?
Thanks.
check out this and do programatically
view.getBackground().setAlpha(100); // to make background transparent
you can use PopupWindow to show on top of parent view as following
View mainview ;
PopupWindow popupwindow;
public void onCreate(Bundle savedInstanceState){
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mainview = inflater.inflate(R.layout.main, null, false);
setContentView(mainview);
// load sub menu from xml layout in popupwindow
View submenu_popup = inflater.inflate(R.layout.submenu_popup, null, false);
// make backgraound transparent of popup submenu
submenu_popup.getBackground().setAlpha(100);
popupwindow = new PopupWindow(submenu_popup ,300,300,false);
popupwindow.setOutsideTouchable(true);
popupwindow.setTouchable(true);
}
// call it on click of button or menu to show submenu
public void onClickButton(){
int x=0,y=0;
// show popupwindow on x, y position in main view (parent view) by using this
popupwindow.showAtLocation(mainview , Gravity.NO_GRAVITY, x, y);
}
May be you should learn how to use "android:theme" property

Inflater inflate but height is small ( looks like wrap_content and need fill_parent)

I have layout and I want to inflate that
<?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"
>
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</EditText>
</LinearLayout>
like
LinearLayout ll=(LinearLayout)findViewById(R.id.llContainer);
View view;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.question_free_text, null);
ll.addView(view);
where ll is
<LinearLayout
android:id="#+id/llContainer"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
</LinearLayout>
in other xml, but problem is when it inflate it shows but height is big (fill_parent, it looks like wrap_content, but there is no wrap_content in layout). Can anybody help me ?
As Yashwanth Kumar correctly mentioned in the comments, the second parameter of the inflate-method should be the root view in which the new view will be inserted:
LinearLayout ll = (LinearLayout) findViewById(R.id.llContainer);
View view;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.question_free_text, ll);
If a root view is provided in the inflate-call, the LayoutInflator calls the generateLayoutParams(ViewGroup.LayoutParams p) method of this root view to get some LayoutParams (basically containing information about how big a view can/should be) which will be passed to the new view.
Note that if you provide a root view, the inflated view will be automatically added to the root view via root.addView(View child, LayoutParams params).
You can also pass a third parameter to the inflate-method (boolean attachToRoot), if this value is false, the new view will not be added automatically to the root view, but the LayoutParams are still set with setLayoutParams(params). You can use this if you wand to add you view manually to the root view (e.g. at a specific position/index):
LinearLayout ll = (LinearLayout) findViewById(R.id.llContainer);
View view;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.question_free_text, ll, false); // the LayoutParams of view are set here
ll.addView(view, 2);

inflate popup menu items programmatically

I have a button in my activity, pressing upon which, the following method gets called -
private ArrayList<ListView> myListViewList;
private ArrayList<MyListAdapter> myAdapterList;
public void onPopupButtonClick(View button) {
Log.v(TAG, "onPopupButtonClick");
PopupMenu popup = new PopupMenu(this, button);
Menu menu = popup.getMenu();
// how do I display the myListViewList as items of in this popup menu
// i.e how do I inflate myListViewList into menu object?
}
I know how to do this programmatically for an activity - creating linearlayouts and listViews and finally using addContentView on the activity's context.
But finding it hard to do the same for the popup menu scenario as described above.
Check this code for Pop-up window
LayoutInflater inflater = (LayoutInflater) PopupMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.popup,
(ViewGroup)findViewById(R.id.popup_menu_root));
PopupWindow pw = new PopupWindow(layout, 300,100, true);
pw.dismiss();
pw.showAtLocation(layout, Gravity.BOTTOM, 0, 10);
And your New pop-up layout should be as follows :
Popup.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_menu_root"
android:background="#FFFFFF"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="20dip" >
<TextView android:id="#+id/popup_menu_button1"
android:text="Copy"
android:textColor="#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
This line decide the Height and width of your New View(Pop up window)
PopupWindow pw = new PopupWindow(layout, 300,100, true);
and pw.showAtLocation(layout, Gravity.BOTTOM, 0, 10);
tells where the Pop-up window should displays..

Categories

Resources