Trying to open a Pop up window when button is clicked - java

I'm trying to get a pop-up window (modal window) open when I click a button. I have no idea how it works in Java, I've seen a lot of tutorials and can't figure it out. Here is my button.
<?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:background="#color/white"
>
<LinearLayout
android:layout_margin="50dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
//This is the button that should open a modal window
android:id="#+id/btnfruit"
android:background="#mipmap/iconfruit"
android:layout_width="100dp"
android:layout_height="100dp" />
<Button
android:layout_marginTop="100dp"
android:background="#mipmap/vegetable"
android:layout_width="100dp"
android:layout_height="100dp" />
<Button
android:layout_marginTop="200dp"
android:background="#mipmap/protein"
android:layout_width="100dp"
android:layout_height="100dp" />
<Button
android:layout_marginTop="300dp"
android:background="#mipmap/dairy"
android:layout_width="100dp"
android:layout_height="100dp" />
<Button
android:layout_marginTop="400dp"
android:background="#mipmap/grain"
android:layout_width="100dp"
android:layout_height="100dp" />
<ImageView
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"/>
</RelativeLayout>
this is the modal window.
<?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="400dp"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="5dp"
>
<TextView
android:layout_width="30dp"
android:layout_height="30dp"
android:text="X"
android:textStyle="bold"
android:layout_gravity="end"
android:gravity="center"/>
<Button
android:id="#+id/btnaddfruit"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Add Item"/>
</LinearLayout>
I guess my question is, what goes on MainActivity.java or can I code it straight to the button? Thank you in advance for your time and answers.

In whatever class your Button object is in, I would assume MainActivity, create a PopupWindow object (you will have to import the class. Inside of the onClick() method of your button, initialize it and set the location
PopupWindow myWindow = new PopupWindow(View content, int height, int width, true);
myWindow.showAtLocation(View content, Gravity.CENTER, 0, 0);
Obviously you need to fill in the unfilled parameters with your own values.
In order to get the View object needed, you will have to create a new View for the pop up window using a LayoutInflater.

Related

ImageView covering other contents in android

The image is covering the textView and buttons(I cant even see them at all so I'm guessing they are behind the image). Image is attached. I want the textview and buttons to appear below the imageview. Below is my xml file.
<?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"
>
<ImageView
android:id="#+id/imageView9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:src="#drawable/bw" />
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
</LinearLayout>
You can change the main layout to RelativeLayout, set the ImageView at the top and wrap the views below the ImageView, inside a separate layout.
By setting android:layout_above="#+id/linear" for the ImageView, you will be sure that the ImageView will never cover the other views.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linear"
android:layout_alignParentTop="true"
android:scaleType="fitStart"
android:src="#drawable/bw" />
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:gravity="center_horizontal">
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
</RelativeLayout>
Try changing the parent layout to a RelativeView, or a ConstraintView. If you don't want to redo your positioning of your layout, you can wrap everything except the Image in a LinearLayout.
<RelativeView>
<ImageView>
<LinearLayout>
//...All your stuff
</LinearLayout>
<RelativeView>
This isn't one way of course, try to experiment. Feel free to ask if you still encounter problems.

how to make a fixed portion of scroll activity to display on every screen sizes

How can I make a fixed portion of a scroll activity display on every screen size?
I have a scrollable activity which contains a pageviewer, and below that, 4 buttons. On an average screen it just displays only these things, the rest comes below the screen, which means there are few more views below the button. If we use a wider screen, then these views also appear in main view before scrolling. I need to make the view range the same for all screen sizes, meaning for wider screen also, I need to just view the pageviewer and buttons only, the rest should be seen only when we scroll.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/idd_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="500dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Button1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Button1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Button1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Button1" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="other view" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="other view" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="other view" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="other view" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="other view" />
</LinearLayout>
</ScrollView>
</LinearLayout>
set ViewPager height programatically so it can match mobile height

overlap cardview over image

I am new to android programming and I am trying to slide my view over image.This is my code(Not sure if this is the right way)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ron"
android:scaleType="fitCenter"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/cv">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_photo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_name"
android:layout_toRightOf="#+id/person_photo"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_age"
android:layout_toRightOf="#+id/person_photo"
android:layout_below="#+id/person_name"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
I want to scroll my cardview over image keeping image fixed.something like eclipsing the image.
and after scrolling completely the image should get hidden.(I dont want it to flow under toolbar)
Try to place your CardView into ScrollView and add CardView's marginTop which equals to your Image's height.

List view - auto modify height

I'm learning Android and now I have this tutorial: http://www.informit.com/articles/article.aspx?p=1928230
I modify (I want scroll all page) this and i have:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#+id/petList"
android:layout_width="wrap_content"
android:layout_height="700dp" //here!!!!
android:divider="#000"
/>
<Button
android:id="#+id/ButtonAddNews"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add new Pet" >
</Button>
</LinearLayout>
</ScrollView>
Single item:
<LinearLayout
android:id="#+id/relative"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="5dp"
orientation="horizontal">
<TextView
android:id="#+id/TextView01"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#f00"
android:layout_alignParentLeft="true"
android:text="two" ></TextView>
<TextView
android:id="#+id/TextView02"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#f00"
android:text="three"
android:layout_alignParentRight="true"></TextView>
</LinearLayout>
This working ok, but i must modify android:layout_height.
For example if i have 3 item in list then 700dp it is too more.
If I use fill_parent, match_parent or wrap_content then this show me only one item (I can scroll it).
What I have to type in the layout_height to automatically expanding, with the number of items?
First of all: REMOVE THAT SCROLLVIEW!! Never place a ListView inside a ScrollView! Google for "android listview inside scrollview" to get more information about that.
Now, usually you would implement that layout somehow like that:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#+id/petList"
android:layout_width="wrap_content"
android:layout_height="0dp" // set layout_height=0dp
android:layout_weight="1" // add layout_weight=1
android:divider="#000"
/>
<Button
android:id="#+id/ButtonAddNews"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add new Pet" >
</Button>
</LinearLayout>
This way the list will use the whole available height, except what the button is using. If you want the button to scroll with the list, use a list footer.

How can i place two buttons side by side with an image at the bottom of the button?

How can i place two buttons side by side with an image at the bottom of the button?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/a_button"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Button-a" />
<Button
android:id="#+id/b_button"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Button-b" />
</LinearLayout>
Instead of using button u can use linear-layout(Clickable) and use its id instead of Button id.something like this...... Using this you can customize your Button as per your requirements.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
//Button-A
<LinearLayout
android:id="#+id/a_button"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.50"
android:orientation="vertical"
android:clickable="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="Text-A"
android:paddingTop="10dip"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/image_A"
android:paddingTop="13dip" />
</LinearLayout>
//For Horizontal partition line
<TextView
android:layout_width="2dp"
android:layout_height="fill_parent"
android:background="#303030"
android:paddingTop="20dip" />
//Button-B
<LinearLayout
android:id="#+id/b_button"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.50"
android:orientation="vertical"
android:clickable="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text-B"
android:gravity="center_horizontal|center_vertical"
android:paddingTop="10dip" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/image_B"
android:paddingTop="13dip"
/>
</LinearLayout>
</LinearLayout>
You can place images at the bottom of buttons with the Drawable_Bottom property.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#android:attr/buttonBarStyle"
android:orientation="horizontal" >
<Button
android:id="#+id/a_button"
style="#android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="#android:drawable/btn_star"
android:text="Button-a" />
<Button
android:id="#+id/b_button"
style="#android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="#android:drawable/btn_star"
android:text="Button-b" />
</LinearLayout>
Just add below line to both your buttons and specify the drawable you want to use.
android:drawableBottom="#drawable/image"
Hope this helps.
EDIT:
Above method works if you want to use your drawables as it is i.e. without modifying its dimensions. If you wish you modify you drawable dimentions as well then you can do that through simple Java code written below:
((Button)findViewById(R.id.button)).setCompoundDrawables(null, null, null, getDrawable(getApplicationContext(), R.drawable.image, 25));
.....
.....
Drawable getDrawable(Context context, int drawable, float form_factor) {
Drawable imgDrawable = context.getResources().getDrawable(drawable);
imgDrawable.setBounds(0, 0, (int)form_factor, (int)form_factor);
return imgDrawable;
}

Categories