How to Change background of popup layout? - java

I have created a popup and I have set one background of popup.
My xml file is as below. I want to change background of popup window.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/pp1" >
<WebView
android:id="#+id/webview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:layout_marginTop="30dp" />
My showPopup method is as below. So how can I change my popup background? and also I load a webview in popup window.
private void showPopup(final Activity context, Point p) {
int popupWidth = 720;
int popupHeight = 380;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
if (i == 1) {
WebView wv = (WebView) layout.findViewById(R.id.webview1);
wv.loadUrl("file:///android_asset/aboutus.html");
}
/*
* Resources res = getResources(); Drawable drawable =
* res.getDrawable(R.drawable.newImage); LinearLayout linearLayout =
* (LinearLayout)findViewById(R.id.GameLayout);
* linearLayout.setBackgroundDrawable(drawable);
*/
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 130;
int OFFSET_Y = 100;
// Clear the default translucent background
// popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
+ OFFSET_Y);
// Getting a reference to Close button, and close the popup when
// clicked.
}

Try this,
Change background from xml file:
in layout tag in your xml file write
android:background="#drawable/image">
and add the image in your drawable folder, make sure the image is of .png format.
Change background at runtime:
btn.setBackground(this.getResources().getDrawable(R.drawable.yellow_button));
Or
Instead this, you can write your activity name with this
btn.setBackground(MainActivity.this.getResources().getDrawable(R.drawable.yellow_button));

Related

Popupwindow show at location, changing the gravity gives false position

I made a popup window appear when you click on text view, i followed a guide how to create popups and i work fine, just the location of the popup is not what i want, it seems that the popup make its left top corner to show at the point location, but i want the bottom left corner to be at the specified point like this,after searching i found that gravity is responsible for this see developer.android , but when I change the gravity to Gravity.BOTTOM|Gravity.LEFT the popup shows way above the text
The popup XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="#+id/popup"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:padding="7dp"
android:background="#d1a2a2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView37"
android:gravity="center"
android:textAlignment="gravity"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView41" />
</LinearLayout>
Here is the code I have
vfy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//Open popup window
if (p != null)
showPopup(MainActivity.this, p);
}
});
These are the methods
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
TextView button = (TextView) findViewById(R.id.textView2);
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
button.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
//............
private void showPopup(final Activity context, Point p) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 30;
int OFFSET_Y = 30;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
// Getting a reference to Close button, and close the popup when clicked.
TextView close = (TextView) layout.findViewById(R.id.textView37);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
}
You can use showAsDropDown() or showAtLocation() method.
Refer these links Change gravity of PopupWindow and
How to show PopupWindow at special location?

LayoutParams using AddRules

I know there are related questions but please try my code 1st
I think my code is correct but I don't know why I can't get want I want
I'm trying to Replace the image into a RelativeLayout with the same LayoutParams dimension and placement(programmatically) then inside that RelativeLayout put a WebView in the center of it.
It was almost done but I'm having trouble for the placement of Webview.
see the images below
activity_main.java
#Override
protected void onCreate(Bundle savedInstanceState) {
.........
// Image
ImageView image = (ImageView)findViewById(R.id.image);
// Container of the image
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl_imageView);
loader(rl , image);
}
public void loader(RelativeLayout rl,View view){
// Creating RelativeLayout
RelativeLayout rlnew = new RelativeLayout(this);
// Getting the Layout Parameters of the image such as (position and dimension)
RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) view.getLayoutParams();
// Coloring the background of the new Relative Layout into blue
rlnew.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
// passing the parameters to the new relative layout from the image
rlnew.setLayoutParams(lp1);
// image turns into invisible
view.setVisibility(View.INVISIBLE);
// Creating a webView
WebView webView = new WebView(this);
// load the loading.gif
webView.loadDataWithBaseURL("file:///android_asset/", "<center><img src='loading.gif' style=''/></center>", "text/html", "utf-8", null);
// setting up dimension(height and weight) for the webview
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100, 60);
// adding position(Center) for the webview
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// setting up Layout parameters for the webiview
webView.setLayoutParams(lp);
// adding the webview to the new Relative Layout
rlnew.addView(webView);
// adding the new relative layout into the container
rl.addView(rlnew);
}
activity_main xml
<RelativeLayout
android:layout_width="250dp"
android:layout_height="100dp"
android:background="#ec0c0c"
android:id="#+id/rl_imageView"
android:layout_marginTop="47dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<!--Desired Output-->
<!--<RelativeLayout-->
<!--android:layout_width="70dp"-->
<!--android:layout_height="70dp"-->
<!--android:background="#010e6e"-->
<!--android:layout_alignParentTop="true"-->
<!--android:layout_alignParentEnd="true">-->
<!--<WebView-->
<!--android:layout_width="60dp"-->
<!--android:layout_height="30dp"-->
<!--android:id="#+id/imageView"-->
<!--android:layout_centerVertical="true"-->
<!--android:layout_centerHorizontal="true" />-->
<!--</RelativeLayout>-->
<!--Default Image-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sample1"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
</ImageView>
</RelativeLayout>
Output Images
I'm making a method that will ask for two parameters a RelativeLayout(that contains the second parameter of the method) and View so it can be anything like(Image, TextView, RelativeLayout , etc.,).
This code can replace a specific View like a relativeLayout or textView or Image
and replace it with a relativeLayout with the same size and position of the given View(mine was the image)(It was already Done if you move that image anywhere in the given container the code will replace the image into a relativeLayout no matter where it is as long inside of the given container).
The only problem is the WebView position. . why is webView there?. .I want it in the center of the new RelativeLayout
Can someone tell where did I go wrong
If you have another way just submit answer
Your code is correct, but your xml is not. Try to change your xml below and test again:
<ImageView
android:layout_width="your dimen in dp < rl_imageView. width"
android:layout_height="your dimen in dp < rl_imageView. height"
android:src="#drawable/sample1"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
</ImageView>
P/S: I suggest create this Imageview programmatically also.

Programatically add buttons that have no visible margins

I have a class that programmatically adds buttons to a layout, but I need the buttons that are added to be flush with the bottom of the layout and with eachother - no margins visible.
As you can see in the attached screenshot - there is a margin both below the buttons, between the buttons and to the left and right of the buttons. I need there to be no margins - the buttons should fill the entire lower part of the layout.
Here is my xml for the container layout:
<?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:id="#+id/someclass_rootlayout"
android:orientation="vertical"
android:background="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="somepackage.someclass"
>
<LinearLayout
android:id="#+id/someclass_buttonlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal"
/>
</LinearLayout>
And here is my code for adding the buttons programatically in a class that receives as input an array (buttonNames) containing the String names of the buttons to be added. For testing purposes I'm always passing an array of two elements and positioning them to the left and to the right of the layout.
private Window window;
private WindowManager.LayoutParams windowParams;
private int resourceIdentifier = 0;
...
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.someclass_fragment, container, false);
ButterKnife.inject(this, view);
window = getDialog().getWindow();
windowParams = window.getAttributes();
window.requestFeature(Window.FEATURE_NO_TITLE);
someclass_buttonlayout.setWeightSum(buttonNames.size());
for (String string : buttonNames) {
addNewButton(string);
resourceIdentifier++;
}
return view;
}
private void addNewButton(String name) {
Button newbutton = new Button(context);
newbutton.setText(name);
newbutton.setId(resourceIdentifier);
newbutton.setTextColor(getResources().getColor(R.color.black));
newbutton.setMinHeight(0);
newbutton.setMinWidth(0);
newbutton.setShadowLayer(0,0,0,0);
newbutton.setBottom(0);
newbutton.setPadding(0,0,0,0);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (resourceIdentifier==0) {
layoutParams.gravity = Gravity.LEFT;
} else {
layoutParams.gravity = Gravity.RIGHT;
}
layoutParams.weight = 1;
newbutton.setLayoutParams(layoutParams);
newbutton.setOnClickListener(this);
newbutton.setTag(name);
someclass_buttonlayout.addView(newbutton);
}
Does anyone have any ideas why despite everything these buttons still have a margin around them, and if there's any way of removing that margin?
You should add background color to the button or buttons while creating the buttons dynamically like this:
newbutton.setBackgroundColor(Color.parseColor("#848482")); //#848482 is dark gray
Set Background color or drawable to your button. That will remove the padding as well as Margins.

Positioning Custom ProgressDialog Android

I have made a transparent custom progress bar.I want to move the progress bar to a certain location on the screen programatically.But every time the custom progress bar get drawn at the middle of the screen.
public class CustomProgressBar extends Dialog {
ProgressBar mProgressBar;
TextView mTextMessage;
public CustomProgressBar(Context context, int id) {
super(context, R.style.ProgressDialogTheme);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_progree_dialog,
null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);//the progress bar does not get aligned to left top with the defined margin space.
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);//
params.setMargins(50, 50, 0, 0);//
// layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, id);
this.addContentView(view, params);
}
}
And this is how I am using it inside my activity
Dialog mProgressBar = new CustomProgressBar(
MyActivity.this,
otherView.getId());
My progress bar layout file custom_progree_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ProgressBar
android:id="#+id/id_server_connection_progress_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="#drawable/somedrwablefile" />
<TextView
android:id="#+id/id_server_connection_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/padding_30px"
android:layout_marginTop="#dimen/padding_4px"
android:layout_toRightOf="#id/progress_dialog"
android:text="#string/connectingMessage"
android:textColor="#color/white"
android:textSize="#dimen/padding_40px" />
</RelativeLayout>
Please guide me and let me know where I am going wrong.I have spent two days trying to figure out the problem.
So I solved this problem by making changes to my custom_progress_dialog and aligning the progress bar and the text to the location I want.Also I am not using the custom class any more.I am just inflating the layout at runtime and adding it as a child view inside my activity.
if (mView == null)
{
mView = getLayoutInflater().inflate(R.layout.server_connection_progree_dialog,null);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
MyActivity.this.addContentView(mView, layoutParams);
}
else
if (!mView.isShown())
{
mView.setVisibility(View.VISIBLE);
}

Android Custom Action bar not filling parent, title not center aligned

I have been experimenting the following issue: I have created a custom action bar:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/com_facebook_blue" >
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_centerInParent="true"
android:textSize="30dp"
android:textColor="#ffffff"
android:text="#string/app_title"/>
</RelativeLayout>
Which is inflated by a GraphicsBuilder class in the following way:
public static void setupActionBar(String title, Activity a) {
final ViewGroup actionBarLayout = (ViewGroup) a.getLayoutInflater()
.inflate(R.layout.action_bar_layout, null);
final ActionBar actionBar = a.getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
final int actionBarColor = a.getResources().getColor(R.color.main_red);
actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));
TextView tv = (TextView) actionBarLayout.findViewById(R.id.action_bar_title);
Typeface font = Typeface.createFromAsset(a.getAssets(), "fonts/Molle-Regular.ttf");
tv.setText(title);
tv.setTypeface(font);
}
Which leads to the following results (I have kept the title bar background blue to highlight the issue):
Basically it looks like that the custom action bar does not fill the parent width, therefore any try to align the title at the center is useless. How can I fix this?
I know this is a long time gone.
Solved it by changing the layout params once it's been set as custom view.
My logic was that when you inflate it the first time, it assumes the size of the original container.
When you call:
actionBar.setCustomView(titlebar);
It's already got its dimensions preset.
My solution was:
View titlebar = inflater.inflate(R.layout.titlebar, null);
// Do stuff to Title Bar //
ActionBar actionBar = getActionBar();
actionBar.setCustomView(titlebar);
View v = actionBar.getCustomView();
LayoutParams lp = v.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
v.setLayoutParams(lp);
Alternatively, you can first set the action bar layout without the inflater:
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.id.titlebar);
View v = actionBar.getCustomView();
// Do something to the view E.g Set button listeners blah //
// Rest of code

Categories