remove black border from inflated layout - android

I am using PopupWindow, which is working fine, but the problem is after inflating the layout view that is shown with black border.
final PopupWindow popupWindow = new PopupWindow(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.pop_up_window_menu_layout, null);
popupWindow.setContentView(view);
pop_up_window_menu_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/solid_white"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:text="SAVE"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/black"
android:textSize="#dimen/txt_size_sliding_list" />
I've tried to remove border by using popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)), but problem is still same.
Can anyone tell me how to remove unwanted black border?

I don't see any border around my popup windows, but I do set them up slightly different than you. Hopefully this points you in the right direction.
popup_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#f5f5f5"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btn_close"
android:text="Close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/main_view"
tools:context=".MainActivity">
<Button
android:id="#+id/btn_popup"
android:text="Popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt_text"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
View popupView;
Button btnPopup;
Button btnClose;
PopupWindow popupWindow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
popupView = getLayoutInflater().inflate(R.layout.popup_view, null);
btnClose = (Button) popupView.findViewById(R.id.btn_close);
btnClose.setOnClickListener(this);
popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
btnPopup = (Button) findViewById(R.id.btn_popup);
btnPopup.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_popup) {
popupWindow.showAtLocation(findViewById(R.id.main_view), Gravity.NO_GRAVITY, 100, 200);
//popupWindow.showAsDropDown(btnPopup, 10, 10);
} else if (v.getId() == R.id.btn_close) {
popupWindow.dismiss();
}
}
}

If you getting a black border this means that the default background is set.
this is how you remove it :
popup.setBackgroundDrawable(null);
Try this

Just try this and let me know..
Be sure to create your Pop-up window referencing your custom theme:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DialogTheme" parent="android:Theme.Dialog">
<!-- Fill the screen -->
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<!-- No backgrounds and No titles , and no Window Floating -->
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<!-- Set background what you want-->
<item name="android:background">#ff0000</item>
</style>
</resources>

This is because you are sending context inside popup window constructor. -
final PopupWindow popupWindow = new PopupWindow(context);
Change this by popup window view -
View popupView = getLayoutInflater().inflate(R.layout.popup_view, null);
PopupWindow popupWindow = new Pop

Related

Custom Alertdialog width is bigger than the shown width in layout xml

I am trying to create custom alertdialog. Here is my alertdialog custom layout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:orientation="horizontal">
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Processing" />
</LinearLayout>
</RelativeLayout>
and the code from activity where i call custom alertdialog
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.layoutalertprogress, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setView(dialogView);
alertDialog = alertDialogBuilder.create();
if(!alertDialog.isShowing())
{
alertDialog.show();
}
problem is even in layout file linearlayout width is wrapped to its content like this
the real width of alertdialog looks like this in runtime
so the real width of alertdialog is wider (wider than the layout shown above).
It seems that linearlayout width is not wrapped to its contents so there is big unused space from its edges to its child elements. how to make this linearlayout width to wrap its contents so the unused space from its edges can be removed? Thanks
Try this
set background color to child LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:background="#android:color/white"
android:orientation="horizontal">
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Processing" />
</LinearLayout>
</RelativeLayout>
and then add this line in your java code
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
after this
AlertDialog alertDialog = alertDialogBuilder.create();
Use simply just setLayout method with your desired width and height. This will make your dialog width and height as specified.
Kotlin version,
private fun showDialog() {
val dialog = AlertDialog.Builder(this)
val layout = LayoutInflater.from(this).inflate(R.layout.custom_dialog_layout, null, false)
dialog.setView(layout)
val alert = dialog.create()
alert.show()
alert.window?.setBackgroundDrawableResource(android.R.color.transparent)
alert.window?.setLayout(600, 600)
}
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.layoutalertprogress, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setView(dialogView);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
if(!alertDialog.isShowing())
{
alertDialog.show();
}
I faced the same problem and the solution which worked for me is:
Create a theme with the followings:
<style name="yourThemeName" parent="Theme.AppCompat.Dialog">
...
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
And then in code:
inflatedView= layoutInflater.inflate(R.layout.yourLayoutId, null)
dialog = AlertDialog.Builder(this, R.style.yourThemeName)
.setView(inflatedView)
dialog.show()
change your relative layout as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:orientation="horizontal">
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Processing" />
</LinearLayout>
</LinearLayout>
Create a theme for alertdialog in style.xml as below
<style name="AlertDialogCustom" parent="AlertDialog.AppCompat">
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">10sp</item>
<item name="android:layout_width">wrap_content</item>
</style>
Create below method to call your dialog.
public void showDemoDialog() {
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.demodg, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogCustom);
alertDialogBuilder.setView(dialogView);
AlertDialog alertDialog = alertDialogBuilder.create();
if (!alertDialog.isShowing()) {
alertDialog.show();
alertDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
This is the result when run on device.

Include layout inside popup menu items [duplicate]

This question already has answers here:
How to make an overflow menu like in Chrome?
(2 answers)
Closed 4 years ago.
I try to add a popup menu in my service, for some features. I want to add some horizontal icons. I tried to add them in a layout and after include that layout in a menu item. But nothing.
<?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/menu_id"
android:title="TEST"
app:showAsAction="always"
app:actionLayout="#layout/menu_navigation"/>
<item android:id="#+id/downloads"
android:title="#string/downloads"
android:icon="#drawable/ic_download"
app:showAsAction="always|withText"/>
<item android:id="#+id/settings"
android:title="#string/settings"
android:icon="#drawable/ic_settings"
app:showAsAction="always|withText" />
</menu>
try to use Popup Window use custom layout, this way you will be able to add icons
LayoutInflater layoutInflater = (LayoutInflater)
MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.pop_window_layout,null);
Button but1 = (Button)customView.findViewById(R.id.popwinitem1);
Button but2 = (Button)customView.findViewById(R.id.popwinitem2);
Button but3 = (Button)customView.findViewById(R.id.popwinitem3);
//instantiate popup window
final PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//display the popup window below whatever widget you want
popupWindow.showAsDropDown(text1);
//popupWindow.showAtLocation(holder.text1, Gravity.RIGHT, 0, 0);
//close the popup window on button click
but1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// yourfunction();
popupWindow.dismiss();
}
});
but2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// yourfunction();
popupWindow.dismiss();
}
});
but3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
pop_window_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/colorWhite"
android:padding="10dp">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/popwinitem1"
android:padding="5dp"
android:textSize="15sp"
android:text="#string/save"
android:textColor="#color/colorBlack"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/popwinitem2"
android:text="#string/delete"
android:textSize="15sp"
android:padding="5dp"
android:textColor="#color/colorBlack"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/popwinitem3"
android:text="#string/cancel"
android:padding="5dp"
android:textSize="15sp"
android:textColor="#color/colorBlack"/>

Android Custom Popup menu with switch

How can I customize the menuItems in a popup menu? I need a switch for the first menuitem. Here is what I got so far:
menu.xml
<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="android.oli.com.fitnessapp.activities.LiveSelectActivity">
<item
android:icon="#drawable/ic_access_alarm_white_24dp"
android:orderInCategory="100"
app:showAsAction="always"
android:visible="true"
android:title="#string/action_settings"
android:onClick="showPopup"/>
</menu>
menu_popup.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/one"
android:title="One"/>
<item
android:id="#+id/setTime"
android:title="Two"
android:onClick="showTimePickerDialog"/>
</menu>
Activity snippet
public void showPopup(MenuItem menuItem){
View view = findViewById(R.id.action_alarm);
PopupMenu popup = new PopupMenu(this, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_popup, popup.getMenu());
popup.show();
}
You can use popupwindow since it allows to use custom layouts.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="5dp">
<Switch
android:id="#+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="Play with the Switch" />
<TextView
android:id="#+id/switchStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/mySwitch"
android:layout_marginTop="22dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
and in ur activity implement this method:
/* you should refer to a view to stick your popup wherever u want.
** e.g. Button button = (Button) findviewbyId(R.id.btn);
** if(popupWindow != null)
** showPopup(button);
**/
public void showPopup(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.popup_filter_layout, null);
popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
//TODO do sth here on dismiss
}
});
popupWindow.showAsDropDown(v);
}

How to center an image in an Android Alert Dialog Title?

I am trying to center the image that I am adding to my Alert Dialog in an Android app as follows:
android.app.AlertDialog.Builder appInfoDialog = new AlertDialog.Builder(this);
appInfoDialog.setIcon(R.drawable.image);
appInfoDialog.setTitle(" ");
appInfoDialog.setMessage("App Info...");
appInfoDialog.setCancelable(true);
AlertDialog dialog = appInfoDialog.create();
dialog.show();
But how can I center this image? Currently, it is left-aligned.
I cannot find a gravity or layout_align property for it!
You can not do this, but You can create Your own Dialog like this:
/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
</resources>
/layout/custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dip"
android:text="wpisz tekst by wyszukać piosenkę"
android:textColor="#5E636D" />
<EditText
android:id="#+id/value_mini"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40.0dip"
android:background="#drawable/input_text"
android:paddingLeft="10px" />
<Button
android:id="#+id/search_mini"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dip"
android:text="szukaj" />
</RelativeLayout>
in method:
Dialog dialog = new Dialog(Start.this, R.style.Dialog);
dialog.setContentView(R.layout.custom_dialog);
Button button = (Button) dialog.findViewById(R.id.search_mini);
final EditText et = (EditText) dialog.findViewById(R.id.value_mini);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
dialog.show();

how to remove rectangle frame of the custom dialog

i custom a dialog :
public class CustomizeDialog extends Dialog implements OnClickListener {
Button close;
TextView tv;
public CustomizeDialog(Context context,String Stringcontent) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_diolog_main);
tv=(TextView) findViewById(R.id.content);
tv.setText(Stringcontent);
close = (Button) findViewById(R.id.close);
close.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == close)
dismiss();
}
}
the xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="100dip"
android:orientation="vertical"
android:background="#drawable/custom_diolog_bg"
android:layout_width="250dip">
<TextView android:layout_height="wrap_content"
android:textColor="#000"
android:textStyle="bold"
android:textSize="18sp"
android:id="#+id/content"
android:layout_marginLeft="15dip"
android:layout_marginTop="5dip"
android:layout_alignParentTop="true"
android:layout_width="250dip"
android:text=" Custom Dialog "/>
<Button android:layout_width="70dip"
android:layout_marginLeft="80dip"
android:background="#drawable/custom_dialog_button_bg"
android:layout_alignParentBottom="true"
android:layout_height="40dip" android:text="关闭"
android:id="#+id/close"></Button>
</RelativeLayout>
my dialog is vrry well,but custom_diolog_bg is a rounded rectangle image,and when i show my dialog ,it show a system Frame behide my custom,so i used this.getwindow.setBackgroundDrawable(null),then the system Frame seems have remove but only the Four Corners not remove,we also see dark Four Corners,because i used the rounded rectangle image.so my question how to remove all the Frame so that my dialog seem Very well
the pic is http://i.stack.imgur.com/EG7oz.jpg ,so you can see there is dark frame in the last,how to remove it? thank you
Solution that worked for me
<style name="DialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
Dialog dialog = new Dialog(this, R.style.DialogTheme);
Use following lines before calling setContentView() :-
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
will work perfectly.
Dialog mydialog = new dialog (this,android.R.style.Theme_Translucent_NoTitleBar);
instead of calling
super(context);
call
super(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
Update : Use this xml layout instead
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="100dip"
android:orientation="vertical"
android:background="#drawable/custom_diolog_bg"
android:layout_width="250dip">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:layout_height="wrap_content"
android:textColor="#000"
android:textStyle="bold"
android:textSize="18sp"
android:id="#+id/content"
android:layout_marginLeft="15dip"
android:layout_marginTop="5dip"
android:layout_alignParentTop="true"
android:layout_width="250dip"
android:text=" Custom Dialog " />
<Button
android:layout_width="70dip"
android:layout_marginLeft="80dip"
android:background="#drawable/custom_dialog_button_bg"
android:layout_alignParentBottom="true"
android:layout_height="40dip"
android:text="关闭"
android:id="#+id/close"></Button>
</LinearLayout>
</RelativeLayout>
Try this it worked for me like a charm.
ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo);
final LayoutInflater inflater = (LayoutInflater) wrapper.getSystemService(LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);

Categories

Resources