Remove padding created from ImageView after scaling down LayoutParams - android

In my relative layout, I want two images to display on extreme right bottom and left bottom of the screen. I can see padding on the edges of the screen after scaling the width and height of the imageview to one third. I want padding to be removed and images should come to extreme right bottom and left bottom
Is there anything
Here is activity xml and java code
<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" >
<ImageView
android:id="#+id/basket2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:adjustViewBounds="true"
android:src="#drawable/basketclose2" />
<ImageView
android:id="#+id/basket1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true"
android:src="#drawable/basketclose2" />
Java Code:
leftBasket.getLayoutParams().width = width/3;
leftBasket.getLayoutParams().height = height/3;
rightBasket.getLayoutParams().width = width/3;
rightBasket.getLayoutParams().height = height/3;

So remove padding from your layout
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"

remove :
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
from relative layout.

you have mentioned padding in layout that is the issue just remove it

remove the paddings in the RelativeLayout.

write like this:
<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:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/basket2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:adjustViewBounds="true"
android:src="#drawable/basketclose2" />
<ImageView
android:id="#+id/basket1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true"
android:src="#drawable/basketclose2" />
</RelativeLayout>

Related

place an image between other elements so that it scales to fit all screens while keeping aspect ration intact

I want to do that in my xml. Currently I'm doing this:
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.MainActivity">
<ImageView
android:id="#+id/main_drawable_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/text" />
<LinearLayout
android:id="#+id/main_drawable_logo"
android:layout_width="match_parent"
android:layout_below="#id/main_drawable_text"
android:layout_height="wrap_content">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:src="#drawable/logo" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_below="#id/main_drawable_logo"
android:layout_height="wrap_content">
<TextView
android:id="#+id/credits"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:text="#string/credits" />
</LinearLayout>
</RelativeLayout>
In my testing device I only get part of the #drawable/logo, the rest falls out of the screen as does the credit's text. I can of course adjust the weight until I have the desired outcome, but how can I make sure I will get the expected result on all devices?
What I want is main_drawable_text always matching the width of the screen (from margin to margin), credits always being on the bottom of the screen and drawable_logo been scaled to fit the free space between them, yet with the original aspect ratio.
If I well understand UI you want to design you don't need LinearLayout but only existing RelativeLayout and inside it you can place your element according to each others positions (with android:layout_above, android:layout_below and so on). To keep original ratio of your drawable use android:scaleType="fitCenter".
Here is a layout which display (my interpretation of) what you want:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<ImageView
android:id="#+id/main_drawable_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/text"/>
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="#id/credits"
android:layout_below="#id/main_drawable_text"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/logo"/>
<TextView
android:id="#+id/credits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/credits"/>
</RelativeLayout>

RelativeLayout or TableLayout Android 3 buttons

I want to do something simple, 3 buttons in 1 column, responsive, like on screen.
Should I create it on RelativeLayout like in my code, or TableLayout would be better idea?
How to link RelativeLayout to the top and to the bottom, to be sure that last button never go out of the bottom of screen?
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main"
tools:context=".MainActivity"
android:background="#color/green"
>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="NAGRAJ"
android:id="#+id/nagraj"
android:src="#drawable/button_sos"
android:scaleType="fitStart"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:background="#null"
android:layout_marginBottom="25px"
android:tag="nagraj"
/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="112"
android:id="#+id/zadzwon"
android:layout_below="#+id/nagraj"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/button_112"
android:scaleType="fitStart"
android:background="#null"
android:adjustViewBounds="true"
android:layout_marginBottom="25px"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ZDJECIE"
android:id="#+id/foto"
android:layout_below="#+id/zadzwon"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/button_foto"
android:scaleType="fitStart"
android:background="#null"
android:adjustViewBounds="true"
/>
</RelativeLayout>
Use both RelativeLayout and LinearLayout like this
RelativeLayout
LinearLayout (orientation:vertical && weightsum = 3)
ImageView (weightsum = 1)
ImageView (weightsum = 1)
ImageView (weightsum = 1)
LinearLayout
Button (align to the bot of the above LinearLayout and android:layout_alignParentBottom="true")
RelativeLayout
Have you tried using LinearLayout vertically and adding layout_weight parameters? This will always keep buttons in the same spacing, regardless the screen size. I think this might be helpful: https://stackoverflow.com/a/4517358/4890826
Edit:
New Google Percent Support Library might be also helpful - especially in responsive design - http://www.androidauthority.com/using-the-android-percent-support-library-630715/
You should not use either RelativeLayout or TableLayout for this. You should use a LinearLayout with layout_weight.
The layout_weight parameter basically means that if 2 (or more) layouts have the same value, they get the same amount of screen space. In the code below, all 3 buttons have the same layout_weight (1 in this example), so they each get exactly 1/3 of the space available, without going outside the screen.
This code below should give you what you need.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main"
tools:context=".MainActivity"
android:background="#color/green"
android:orientation="vertical">
<ImageButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="NAGRAJ"
android:id="#+id/nagraj"
android:src="#drawable/button_sos"
android:background="#null"
android:layout_marginBottom="14dp"
android:tag="nagraj"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="112"
android:id="#+id/zadzwon"
android:src="#drawable/button_112"
android:background="#null"
android:layout_marginBottom="14dp"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="ZDJECIE"
android:id="#+id/foto"
android:src="#drawable/button_foto"
android:background="#null"/>
</LinearLayout>

Android Studios - Adjusting VideoView Size in Layout Editor

I've only ever seen questions asking how to full screen the VideoView but what I'm trying to do is have a video playing as only a part of the activity, with some additional elements on the page. How would I go about doing that? It doesn't seem that I have control of the size of the VideoView, I can only shift it around in the designer.
Edit: Additional Info
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.MyActivity">
<Button
Default button Stuff
/>
<TextView
Default textview stuff
/>
<VideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/videoView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android: />
</RelativeLayout>
As far as ideal proportions go, maybe something like 500 x 400 dp? I can play with numbers after I understand the process.
If I understand you correctly, this is something that you'd want:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.MyActivity">
<LinearLayout
android:id="#+id/componentLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="Button"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<TextView
android:text="This is a placeholder text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<VideoView
android:layout_marginTop="20dp"
android:layout_below="#+id/componentLayout"
android:layout_width="300dp"
android:layout_height="400dp"
android:id="#+id/videoView"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You can define the android:layout_width and android:layout_height attributes as you wish. However, they can not exceed the height or width of the parent layout.
Result:
Width: 300dp, Height: 400dp

Android relativeLayout: How to make button take bottom port of screen?

I was wondering can I create a button that would take the bottom portion of the screen as seen here:
I have tried to play with the alignment and margins, but to no avail:
Another question that I had was why my movie poster is not aligning to the left of the top text properly? As can be seen, it's a bit to the right. I had attempted to drag it into place.
Here is my XML:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.abushawish.randommovie.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="Black Hawk Down"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView3"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:src="#drawable/bhd" />
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="New Movie Suggestion"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_marginLeft="-50dp"
android:layout_marginRight="-50dip"
android:layout_marginBottom= "-20dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_centerVertical="true"
android:text="Description"
android:layout_marginLeft="1dp" />
Thank you!
Try to remove these.
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
They add padding to the RelativeLayout.
You should start by removing the padding and margins in order to have a simple layout.
When it is ok, you can put back the padding & margins one by one to see which ones are ok or not.
Remove the padding from the buttons parent layout, then your button will align with the edges of the screen.
<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:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.abushawish.randommovie.MainActivity$PlaceholderFragment" >
If you still want to have padding for the other objects add them in a separate layout with padding.
Edit
To put the picture in the middle add
android:layout_centerHorizontal="true"
Try android:gravity="bottom" and in the relative layout add android:layout_alignParentBottom="true"
This will give u the intended look.. and yes, don't give weight to children of RelativeLayout
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="New Movie Suggestion" />

RelativeLayout inside RelativeLayout is not aligned left completely

I have a relativelayout inside another relativelayout which has 4 buttons inside. I want to align the inner relativelayout to the left of the parent relativelayout but it doesn't aligned completely, there's an empty space between the left of the inner view with the left of the parent view, here's the xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="raw4.kaw.mp2.MainActivity"
android:background="#color/background_all" >
<RelativeLayout
android:id="#+id/main_btn_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/btn_main_news"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="#string/btn_news_text"
android:layout_marginTop="10dp"
android:layout_alignParentTop="true"
android:background="#color/background_btn"
android:textColor="#color/text_color_btn" />
<Button
android:id="#+id/btn_main_products"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="#string/btn_products_text"
android:layout_below="#id/btn_main_news"
android:layout_marginTop="10dp"
android:background="#color/background_btn"
android:textColor="#color/text_color_btn" />
<Button
android:id="#+id/btn_main_aboutus"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="#string/btn_aboutus_text"
android:layout_below="#id/btn_main_products"
android:layout_marginTop="10dp"
android:background="#color/background_btn"
android:textColor="#color/text_color_btn" />
<Button
android:id="#+id/btn_main_contactus"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="#string/btn_contactus_text"
android:layout_below="#id/btn_main_aboutus"
android:layout_marginTop="10dp"
android:background="#color/background_btn"
android:textColor="#color/text_color_btn" />
</RelativeLayout>
here's the code in MainActivity.java to reduce the inner layout width to 80% with the alignment rules added:
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int dpi = metrics.widthPixels;
int dpiPerc = (int)(dpi * 0.8);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
dpiPerc,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
RelativeLayout main_rl_container = (RelativeLayout)findViewById(R.id.main_btn_container);
main_rl_container.setLayoutParams(params);
but this is the result:
as you can see there's a gap at the left side of the inner layout. thanks for help.
copy paste this code it will solve your problem
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="raw4.kaw.mp2.MainActivity"
android:background="#color/background_all" >
<RelativeLayout
android:id="#+id/main_btn_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/btn_main_news"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="#string/btn_news_text"
android:layout_marginTop="10dp"
android:layout_alignParentTop="true"
android:background="#color/background_btn"
android:textColor="#color/text_color_btn" />
<Button
android:id="#+id/btn_main_products"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="#string/btn_products_text"
android:layout_below="#id/btn_main_news"
android:layout_marginTop="10dp"
android:background="#color/background_btn"
android:textColor="#color/text_color_btn" />
<Button
android:id="#+id/btn_main_aboutus"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="#string/btn_aboutus_text"
android:layout_below="#id/btn_main_products"
android:layout_marginTop="10dp"
android:background="#color/background_btn"
android:textColor="#color/text_color_btn" />
<Button
android:id="#+id/btn_main_contactus"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="#string/btn_contactus_text"
android:layout_below="#id/btn_main_aboutus"
android:layout_marginTop="10dp"
android:background="#color/background_btn"
android:textColor="#color/text_color_btn" />
</RelativeLayout>
your problem is your parent layout uses android:paddingLeft="#dimen/activity_horizontal_margin" that s why it is not completely to left remove this line and you will get your solution you can copy paste my above code too :)
There is a padding on the parent RelativeLayout.
Remove the following line:
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
It Because of the Dimensions.
You do not need to remove these Lines
But Go to res folder > Values > dimen.xml .
Here By default Specified Some Space allocation for the activity_horizontal_margin change it to 0dp.
if you delete that line That's Good but, You will face these problem in every XML file you going to Create. So I think this solution is better.
Seams to me because of the padding that are set in root RelativeLayout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="raw4.kaw.mp2.MainActivity"
android:background="#color/background_all" >

Categories

Resources