Determining button's random topMargin - android

I am trying to place a button in a random location in a relative layout.
As suggested in similar questions I set the leftMargin and topMargin of the button.
I think the calculation should be:
Button b = (Button) findViewById(R.id.randBtn);
LayoutParams params = (LayoutParams) b.getLayoutParams();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
params.leftMargin =
new Random().nextInt(metrics.widthPixels - activity_h_margin * 2 - b.getWidth());
params.topMargin =
new Random().nextInt(metrics.heightPixels - activity_v_margin * 2 - b.getHeight());
b.setLayoutParams(params);
For some reason this code does not work well in the vertical axis - sometimes the button gets cut at the bottom of the screen.
This is my layout xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<Button
android:id="#+id/randBtn"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/randBtn_text" />
</RelativeLayout>

Get rid of the padding attributes of the relative layout.

Related

How can i make my video view to half the screen of my device?

I am using a video view and a relative layout. The app will only run in portrait mode. Currently, I am giving exact dps to get the desired size. What I would like to have is to get the video view with half the size of my layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_smile"
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="MyActivity">
<VideoView
android:id="#+id/videoViewSmile"
android:layout_width="match_parent"
android:layout_height="300dp" />
</RelativeLayout>
First thing, you can get both height and width using an android.Util class called DisplayMetrics :
DisplayMetrics displayMetrics = new DisplayMetrics();
And then insert the current display info into the declared DisplayMetrics :
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
Now you can get the height and width of your screen by using :
displayMetrics.widthPixels // Width
displayMetrics.heightPixels // Height
It is measured in pixels, and you can change the height by :
videoView.getLayoutParams().height = displayMetrics.heightPixels / 2;
you can get the height and width by using
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
mScreenWidth = displayMetrics.widthPixels;
mScreenHeight = displayMetrics.heightPixels;
Easiest way is to put in an invisible view and use layout_weights but that is not the correct way in my opinion. I would use a ViewTreeObserver
final RelativeLayout layout = (RelativeLayout)findViewById(R.id.your_layout);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
VideoView videoView = layout.findViewById(R.id.videoViewSmile);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(layout.getWidth(), layout.getHeight() / 2)
videoView.setLayoutParams(layoutParams);
videoView.invalidate();
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Not tested, but i hope you see the general idea
Try using weight feature of linearlayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_smile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="MyActivity">
<VideoView
android:id="#+id/videoViewSmile"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" />
<RelativeLayout
android:id="#+id/remaining_fifty_percent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
</RelativeLayout>
</LinearLayout>

Inflated images go out of screen

What should I do to place 4th and 5th image in the next line, here how it looks:
4th and 5th image go out of screen how to fix that?
MainActivity:
LayoutInflater l = getLayoutInflater();
LinearLayout ll = (LinearLayout) findViewById(R.id.main);
Integer odpowiedzi[] = {R.drawable.kwiaty1, R.drawable.kwiaty2, R.drawable.kwiaty3, R.drawable.kwiaty4, R.drawable.kwiaty5};
for (Integer odp : odpowiedzi) {
View v = l.inflate(R.layout.activ2, null);
ImageView b = (ImageView) v.findViewById(R.id.imageView6);
b.setImageResource(odp);
ll.addView(v);
activ2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView6"/>
</LinearLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main">
</LinearLayout>
Make linear layout main layout vertically align
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main">
</LinearLayout>
Now handle adding layout dynamically like this
get screen width
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
if screen width is more than 500dp add another horizontal aligned linear layout in main layout and add imageViews in it.
else if screen width is less than 500dp add first horizontal aligned linear layout in main layout and add imageViews in it. And then add another horizontal aligned linear layout in main layout and add remain imageViews in it
// Create new LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// Add textviews
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
textView1.setText("programmatically created TextView1");
textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView1.setPadding(20, 20, 20, 20); // in pixels (left, top, right, bottom)
linearLayout.addView(textView1);
TextView textView2 = new TextView(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.RIGHT;
layoutParams.setMargins(10, 10, 10, 10); // (left, top, right, bottom)
textView2.setLayoutParams(layoutParams);
textView2.setText("programmatically created TextView2");
textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
textView2.setBackgroundColor(0xffffdbdb); // hex color 0xAARRGGBB
linearLayout.addView(textView2);
// Set context view
mainLinearLayout.addView(linearLayout);
Try change the width of the LinearLayout on activ2 to 0px and weight 1.
In the ImageView add android:adjustViewBounds="true" to fit parent.
In activ1 change the width to match_parent with some padding.
That way the images sizes will be even distributed and should match their parent.
Try with:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imageView6"
android:adjustviewbounds="true"
android:layout_weight="1"/>
</LinearLayout>
And your activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main"
android:weightsum="NUMBER_OF_OBJECTS" >
</LinearLayout>
Hope it helps!

Android ScrollView - ensure that it is always larger than the screen size?

I have a scrollview with multiple different views inside of it. What I want is to have all the elements of the scrollview EXCEPT the last element fill the whole screen. The user can then scroll down to reveal the last item in the scrollview. I do not want that last item to be visible in any other case.
My XML looks like this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3.45"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<View 1 ... android:weight="2.25" />
<View 2 ... android:weight="0.8" />
<View 3 ... android:weight="0.40" /> //last item
</LinearLayout>
</ScrollView>
What this currently does is completely fill the whole screen, but does not have the last item push off.
according to the last post(now deleted),You should place the last item outside LinearLayout group or in a different LinearLayout.
It will be smth like
<ScrollView
android:layout_width="match_parent"<!-- added-->
android:layout_height="match_parent"><!-- added-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:oriention="vertical"><!-- added-->
<LinearLayout
android:id="#+id/visible_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"><!-- changed-->
...{items}
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
{last item}
</LinearLayout>
</LinearLayout>
<ScrollView>
UPDATE: now you need to extend height of linearLayout to fill screen:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
final Point point = new Point();
try {
display.getSize(point);
} catch (java.lang.NoSuchMethodError ignore) { // Older device
point.x = display.getWidth();
point.y = display.getHeight();
}
LinearLayout layout = (LinearLayout) findViewById(R.id.visible_items);
LayoutParams lp = layout.getLayoutParams();
lp.height = point.y;
layout.setLayoutParams(lp);
NOTE: put above code in onWindowFocusChanged() methos of activity

ScrollView not scrolling in android

i am trying to add images to my relative layout with an for loop but it isn't working.
I tried to remove the for loop and making one image longer and the scroll view seems to work the but when i try to add more than one image to the relative layou it adds it but i cant scroll through them.
Meaning that i can see that there are 5 images but i can only see the top half of it and cant scroll to see the bottom half.
Here is my xml that i use for the activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none" >
<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:id="#+id/MainRelativeLayout"
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.tutecentral.tvtimeschedule.app.MainActivity"
android:background="#87CABE"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</RelativeLayout>
</ScrollView>
</LinearLayout>
And here is the activity that i use to add the images
int amountOfChannels = 9;
int paddingTopOfImage = 0;
for(int i = 1; i <= amountOfChannels; i++) {
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.channel_1);
//setting image position
imageView.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
imageView.getLayoutParams().height = 400;
imageView.getLayoutParams().width = 1000;
imageView.setPadding(10, 10, 10, 10);
imageView.setBackgroundColor(Color.WHITE);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setY(paddingTopOfImage);
//adding view to layout
RelativeLayout mainRelativeLayout = (RelativeLayout) findViewById(R.id.MainRelativeLayout);
mainRelativeLayout.addView(imageView);
paddingTopOfImage = paddingTopOfImage + 450;
}
I don't think you have to put ScrollView under LinearLayout, because ScrollView by default is LinearLayout. Try Removing the LinearLayout.

ListView height android

I have a FrameLayout and a Listview inside the FrameLayout.
I populate the Listview with another Layout.
The problem is if I set the layout_height to wrap_content my getview is getting called multiple time for a single record and if I set the layout_height to fill_parent or wrap_content my getview is being called 3 times for a single record. If I set the layout_height to some dp value it is working fine. I mean I dont have any issues with performance and my getview is getting called only once for a record.
So I get the screen height and width programmatically using
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
But if I set the width and height of my Listview with the value measured using the above code the last record of my Listview is partially not visible.
I guess this should be because the obtained screen height includes the title bar height, status bar height and notification bar height.
So if that is the prob can someone help me with calculating the available screen height? That is screen height-titlebar height-statusbar height-notification bar height?
else What is the problem? why my last record in my ListView is not fully visible?
XML File
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:background="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView android:id="#+id/android:list"
android:background="#ffffff"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:clickable="true"
android:fastScrollEnabled="true"
android:smoothScrollbar="true"
/>
</FrameLayout>
Then I set the layout_height and layout_width of the list view in my java code as
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
FrameLayout.LayoutParams parameters = new FrameLayout.LayoutParams((int)(width),(int)(height));
ListView listview=(ListView) getListView();
listview.setLayoutParams(parameters );
Hi all thanks for your help
I changed my code as below and now its working :)
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height=0;
int width = display.getWidth();
switch (metrics.densityDpi)
{
case DisplayMetrics.DENSITY_HIGH:
height = display.getHeight() - (48*2);
break;
case DisplayMetrics.DENSITY_MEDIUM:
height = display.getHeight() - (32*2);
break;
case DisplayMetrics.DENSITY_LOW:
height = display.getHeight() - (24*2);
break;
default:
height = display.getHeight() - (48*2);
}
FrameLayout.LayoutParams parameters = new FrameLayout.LayoutParams((int)(width),(int)(height));
ListView listview=(ListView) getListView();
listview.setLayoutParams(parameters );
in java:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int s_width,s_height;
s_width = metrics.widthPixels;
s_height = metrics.heightPixels/3;
TableRow.LayoutParams parameters = new TableRow.LayoutParams(s_width,s_height);
LinearLayout b_list_table=(LinearLayout) findViewById(R.id.b_list_table);
b_list_table.setLayoutParams(parameters);
in xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="180dip"
android:id="#+id/b_list_table"
>
<ListView android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView android:id="#+id/listf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</TableRow>
</LinearLayout>

Categories

Resources