show Imageview at distance 30% from top of the screen android - android

I have a splash screen which displays the app logo in Imageview at center of the screen .
I need to show the imageview at a distance 30% from the top of the screen , so that the 70% of the screen at bottom looks empty
I have idea about padding , but don't know how to use %( percentage ) with this condition
<ImageView
android:id="#+id/img_splashlogo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="SplashImage"
android:layout_margin="20dip"
android:src="#drawable/app_logo_splash" />

here is the code that you can use putting the image in the linear layout and then use this code
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Button okButton=new Button(this);
okButton.setText("some text");
ll.addView(okButton, layoutParams);

try like this,
<?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"
android:background="#drawable/app_splash" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.30"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/img_splashlogo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="SplashImage"
android:layout_margin="20dip"
android:src="#drawable/app_logo_splash" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_width="0.70"
android:background="#android:color/transparent"/>
</LinearLayout>

Related

Weighted View in Layout

I try to add a PlotView (custom View) into a LinearLayout. My LinearLayout has a weightSum of 8. Inside my .xml I defined a Space (weight of 3) that has to appear above my PlotView and a Button (weight of 1) that should follow my plot underneath. So far, so good.
Now the PlotView is added programmatically, with a weight of 4. However, it will always consume almost the entire screen, and I am not sure what I am doing wrong here.
My Code:
main_activity.xml (snippet)
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true"
android:id="#+id/linlayout"
android:weightSum="8">
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calibrate"
android:id="#+id/calibration"
android:layout_weight="1"
android:layout_gravity="center_horizontal" />
</LinearLayout>
main_activity.java (snippet)
LinearLayout layout = (LinearLayout) findViewById(R.id.linlayout);
plotView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 4f));
layout.addView(plotView, 1);
Any idea what I am doing wrong?
The solution is to set the height, which is affected by the weight, to 0.
Code in .xml:
android:layout_height="0dp"
Code in .java:
plotView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 0, 4f));

Android weightsum not working as expected

I have one horizontal Linear Layout with Width = match_parent and weightsum=5.
If i insert 5 vertical Linear Layouts with each width=0 and weight=1 everything looks as expected, the layouts each get the same width.
If i add only 2 vertical with each width=0 and weight=1 they take more space than they should. I expected them to also take 1/5 of the space.
Maybe it is the correct behaviour that they take more space and I understood the concept of weight/weightsum wrong.
Thanks for any help!
edit:
I try to add some Code
LinearLayout linear=null;
LinearLayout.LayoutParams layoutParams= new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linear=new LinearLayout(getApplicationContext());
linear.setOrientation(LinearLayout.HORIZONTAL);
linear.setLayoutParams(layoutParams);
linear.setPadding(15, 0, 15, 10);
linear.setWeightSum(Float.valueOf(modulo));
//modulo 5 in my example
LinearLayout linear2=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
if(count%modulo!=modulo-1){
lp1.setMargins(0, 0, 15, 0);
} else {
lp1.setMargins(0, 0, 0, 0);
}
linear2.setLayoutParams(lp1);
linear2.setOrientation(LinearLayout.VERTICAL);
I add the layout linear 2 into linear in a loop
Why can you click run code :D
Try this
<?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="horizontal"
android:weightSum="5">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_bright"
android:layout_weight="1"/>
</LinearLayout>
If the logical solution does not work fill the rest with
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="3"/> <!-- or whatever the rest is -->
or
View space = new View(this); // NEVER CREATE VIEWS WITH APP CONTEXT!
LinearLayout.LayoutParams spaceParams = new LinearLayout.LayoutParams(0, 0, 3f);
linear.addView(space, spaceParams);
NOTE: Space widget didn't work in this case.
<?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="horizontal"
android:weightSum="5">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="Your bg"
android:layout_weight="2.5"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="Your bg"
android:layout_weight="2.5"/>
</LinearLayout>
divide weight equally.

Adding a View to RelativeLayout changes its size

I have a RelativeLayout called "Dress" with LayoutParams of WRAP_CONTENT. All that is inside it is an ImageView, so it is the same size as the ImageView. When I add one more view to the RelativeLayout in OnCreate, like this:
photoSorter = new MultitouchImagesView(this.getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
((ViewGroup) findViewById(id.Dress)).addView(photoSorter, params);
The RelativeLayout called Dress changes size, to take up the whole RootView, basically the whole activity, minus the actionbar.
I take a screenshot of the Dress Layout. So when photoSorter is in the RootLayout (NOT the Dress Layout) like this:
photoSorter = new MultitouchImagesView(this.getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addContentView(photoSorter, params);
Layout Dress is correct size and I get this screenshot which is what I want:
But I need PhotoSorter in the Dress Layout so that I can include it in my screenshot and not have the black strips. When I add the photoSortr to the Dress Layout like this:
photoSorter = new MultitouchImagesView(this.getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
((ViewGroup) findViewById(id.Dress)).addView(photoSorter, params);
It makes the Dress Layout take up the whole screen so the screenshot of the Dress Layout looks like this:
Here is my code to take a screenshot:
public Bitmap takeScreenshot() {
View v = findViewById(R.id.Dress);
v.setDrawingCacheEnabled(true);
return v.getDrawingCache();
}
Here is my XML for the activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:longClickable="true"
android:onClick="deleteImage"
android:background="#android:color/transparent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/Dress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent" >
<ImageView
android:id="#+id/imageViewDress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:background="#00CED1"
android:padding="10dp"
android:scaleType="centerInside" />
</RelativeLayout>
<com.edmodo.cropper.CropImageView
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/CropImageView"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Also When I added com.edmodo.cropper.CropImageView to the DressLayout, it made the Dress Layout take up the whole activity as well. So adding anything to the Dress Layout is making it bigger.
How can I add photoSorter to the Dress Layout without making Dress Layout take up the whole activity size?
Thanks.
I managed by trying things out in XML. The key is to align Dress with imageViewDress, I could align the other sides to be more thorough, not just bottom.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:longClickable="true"
android:background="#android:color/transparent"
tools:context=".DressingRoomActivity" >
<RelativeLayout
android:id="#+id/DressBig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/imageViewDress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="centerInside" />
<RelativeLayout
android:id="#+id/Dress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_alignBottom="#+id/imageViewDress"
android:layout_centerInParent="true" >
</RelativeLayout>
</RelativeLayout>
<com.edmodo.cropper.CropImageView
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/CropImageView"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
This XML, then add photoSorter to Dress programmatically, then take a screenshot of DressBig.
The following should work
<RelativeLayout android:id="#+id/Dress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="...">
<ImageView
android:id="#+id/imageViewDress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
android:scaleType="fitXY" />
EDIT:
You can add the below rule to your params so it's laid out over the other content if that's what you want. This is my third attempt at trying to figure out what you're asking for so if it's not what you need, you'll have to figure it out yourself seeing as you are being lazy and won't do a sketch to try and explain clearly how you want it laid out
params.addRule(RelativeLayout.CENTER_IN_PARENT);

How can i set my image to the bottom of the screen in android using xml code

hi i have displayed a PNG image on screen in android by using XML code:
android:src="#drawable/grass"
but it is showing image at top of the screen ,now i want that image at bottom of the screen.
make sure your layout file is taking full of your screen by setting match_parent or fill_parent for both layout_height and layout_width.
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/grass"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/grass"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
EDIT
Programmatically
ImageView pic;
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
pic = (ImageView) findViewById(R.id.pic);
pic.setLayoutParams(param);

How to get 2 images attached to layout bottom

I want to attach 2 images
1st at left bottom corner and other on right bottom corner
but I want that images to be hardcore in bottom so that if I scroll the page the images should remain at the same place
Dynamically ,i am succesful to answer my own question so i m hereby putting the code for all the user and it works so have the code and enjoy
LinearLayout llMain=new LinearLayout(this);
llMain.setOrientation(LinearLayout.VERTICAL);
llMain.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
llMain.setBackgroundColor(Color.WHITE);
LinearLayout llLsView=new LinearLayout(this);
llLsView.setOrientation(LinearLayout.VERTICAL);
llLsView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1.0F));
//llLsView.setPadding(0,0,0,20);
LinearLayout llImage=new LinearLayout(this);
llImage.setOrientation(LinearLayout.HORIZONTAL);
llImage.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
llImage.setBackgroundColor(Color.BLACK);
LinearLayout llHome=new LinearLayout(this);
//llHome.setOrientation(LinearLayout.HORIZONTAL);
llHome.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT,0.5F));
llHome.setPadding(0,0,5,0);
llHome.setGravity(Gravity.RIGHT);
ImageView imgHome=new ImageView(this);
imgHome.setImageResource(R.drawable.home);
llHome.addView(imgHome);
LinearLayout llBack=new LinearLayout(this);
//llBack.setOrientation(LinearLayout.HORIZONTAL);
llBack.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT,0.5F));
llBack.setPadding(5,0,0,0);
llBack.setGravity(Gravity.LEFT);
ImageView imgBack=new ImageView(this);
imgBack.setImageResource(R.drawable.back);
llBack.addView(imgBack);
llImage.addView(llBack);
llImage.addView(llHome);
llMain.addView(llLsView);
llMain.addView(llImage);
setContentView(llMain);
Something like:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/nvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<ImageView
android:id="#+id/nvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
<RelativeLayout >
<ScrollView >
</scrollView>
<RelativeLayout android:layout_alighParentBottom=true abdroid:backgrond=#color/transparnt">
<Button >
</Button >
<Button android:layout_alighParentRight=true>
</Button >
<RelativeLayout >
</RelativeLayout >

Categories

Resources