Add layout with parameters programmatically Android - android

Hello I have a main 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"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="55dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/toggleButton1"
android:background="#android:drawable/btn_default"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop"
android:src="#drawable/plus_grey" />
<LinearLayout
android:id="#+id/lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="0dp"
android:background="#A0FFFFFF"
android:orientation="horizontal"
android:visibility="invisible" >
<TextView
android:id="#+id/locinfo"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textColor="#color/cherniy"
android:textSize="20sp"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
And there is a imageButton
button = (ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bIcon == false) {
button.setImageResource(R.drawable.plus_yellow);
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
t = (TextView)findViewById(R.id.locinfo);
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);
bIcon = true;
}
else {
button.setImageResource(R.drawable.plus_grey);
m.remove();
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
t.setVisibility(View.INVISIBLE);
linLayout.setVisibility(View.INVISIBLE);
bIcon = false;
}
}
});
I want to programmatically add
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT);
where w
display = getWindowManager().getDefaultDisplay();
#SuppressWarnings("deprecation")
w = display.getWidth();
But when I do like this
button.setImageResource(R.drawable.plus_yellow);
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT);
linLayout.setLayoutParams(lp);
t = (TextView)findViewById(R.id.locinfo);
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);
bIcon = true;
My application crashes. Can you please tell how to programmatically create with my parameters (tied with the width and height of the screen of the device)? I will take any suggestions. Thank you.

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
(w*2)/3,
LayoutParams.WRAP_CONTENT
);
linLayout.setLayoutParams(lp);
This will throw error because the LinearLayout is the child of Relative layout, so you have to set RelativeLayoutParams for that.
Instead use this
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
(w*2)/3,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
linLayout.setLayoutParams(lp);
or
Instead of creating new LayoutParams you can reuse the existing LayoutParams of the view as shown below.
RelativeLayout.LayoutParams lp = linLayout.getLayoutParams();
lp.width = (w*2)/3;
lp.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
linLayout.setLayoutParams(lp);

Instead of making a new LayoutParams, you should modify the existing one.
Change your code to:
button.setImageResource(R.drawable.plus_yellow);
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
LinearLayout.LayoutParams lp = linLayout.getLayoutParams();
final int left_margin = whatever;
final int top_margin = 0;
final int right_margin = whatevermore;
final int bottom_margin = 0;
lp.setMargins(left_margin, top_margin, right_margin, bottom_margin);
lp.width = (w*2)/3;
linLayout.setLayoutParams(lp);
t = (TextView)findViewById(R.id.locinfo);
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);
bIcon = true;

Related

How to stop view from going out of screen for randomized positions?

I have a screen ButtonPress that shows a button on random positions, it is working for most of the iterations but in some, it is going out of the screen and the button is not visible. I tried to follow this example but that does not seem to help as the button is still invisible for some iterations in the ButtonPress activity. Here's how I am generating the random positions:
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_press);
button = findViewById(R.id.imageView5);
button.setClickable(true);
button.setOnClickListener(this);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
params.leftMargin = button.getWidth()
+ new Random().nextInt(metrics.widthPixels - 2*button.getWidth());
params.topMargin = button.getHeight()
+ new Random().nextInt(metrics.heightPixels - 3*button.getHeight());
button.setLayoutParams(params);
}
And here's the layout file for the button:
<LinearLayout 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:orientation="vertical"
android:background="#03B0F5"
>
<TextView
android:id="#+id/button_press"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:gravity="center"
android:background="#FFFFFF"
android:text="#string/button_press"
android:textSize="50sp"
android:textColor="#03B0F5"
android:layout_marginStart="0dp"
/>
<ImageView
android:id="#+id/imageView5"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="#drawable/ic_button"
tools:ignore="VectorDrawableCompat" />
</LinearLayout>
I removed all the padding and margins from this resource file but it's still not working.
Try with the below code it will help you.
new Handler(this.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
int width = (getResources().getDisplayMetrics().widthPixels)/2;
int height = (getResources().getDisplayMetrics().heightPixels);
Random random = new Random();
int randomXPos = random.nextInt(width-(button.getWidth())/3);
int randomYPos = random.nextInt(height-(button.getHeight())/3);
button.animate().x(randomXPos).y(randomYPos).start();
button.setLayoutParams(params);
}
},100);

params.setMargins does not work programmatically

I set layout_marginStart and layout_marginTop in xml and it is ok, but when I try to set programmatically margins it does not work. I try to find how to resolve, but can not find resolution.
What I do not correctly?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:orientation="vertical"
android:weightSum="3"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_resourses">
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/MyGridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|center"
android:background="#color/borderHigh"
android:columnCount="2"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/firstLinear1"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginStart="#dimen/resourceBorder"
android:layout_marginTop="#dimen/resourceBorder"
android:background="#drawable/resource_panel"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/firstLinear2"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginStart="#dimen/resourceBorder"
android:layout_marginTop="#dimen/resourceBorder"
android:background="#drawable/resource_panel"
android:orientation="vertical" />
</GridLayout>
</ScrollView>
XML above works
I try to do this by code, and it must work but i can not resolve this problem
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
int dp9 = dpToPx(9);
GridLayout MyGridLayout = findViewById(R.id.MyGridLayout);
final int N = 10; // total number of textviews to add
for (int i = 0; i < N; i++) {
LinearLayout testlayout = new LinearLayout(this);
//LinearLayout.LayoutParams tst = new LinearLayout.LayoutParams(0,0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.height = (width/2)-dp9;
params.width = (width/2)-dp9;
params.setMargins(6, 6, 0, 0);
testlayout.setLayoutParams(params);
testlayout.setOrientation(LinearLayout.VERTICAL);
testlayout.setBackgroundResource(R.drawable.resource_panel);
TextView title = new TextView(this);
title.setText("TextView "+i);
title.setTextColor(getResources().getColor(R.color.colorPrimary2));
title.setAllCaps(true);
title.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(title, 10, 24, 1, TypedValue.COMPLEX_UNIT_SP);
testlayout.addView(title);
TextView text = new TextView(this);
text.setText("Text "+i);
testlayout.addView(text);
TextView date = new TextView(this);
date.setText("date "+i);
testlayout.addView(date);
}

BackgroundColor in LinearLayout of the width of the screen

I created a gallery with horizontalscrollview. In the imageview LinearLayout I've put a color background. I want the LinearLayout spans the width of the screen with or without images. How I can do?
<LinearLayout
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dp"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:id="#+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/estandar"
android:padding="2dp"
/>
</HorizontalScrollView>
</LinearLayout>
main.xml
LinearLayout linear2 = (LinearLayout) findViewById(R.id.linear2);
LayoutParams params = linear2 .getLayoutParams();
params.height = 100;
params.width = 300;
but my code does not work.
EDIT:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int anchoPantalla = metrics.widthPixels;
if (listaImagenes.length > 0) {
for (String nombreImagen : listaImagenes) {
InputStream is = getAssets().open(directorioImagenes + "/" + nombreImagen);
final Bitmap bitmap = BitmapFactory.decodeStream(is);
final ImageView imageView = new ImageView(getApplicationContext());
alto = bitmap.getHeight();
ancho = bitmap.getWidth();
final float calculo = ancho / (alto / ALTO_IMAGEN);
imageView.setLayoutParams(new ViewGroup.LayoutParams((int)calculo, ALTO_IMAGEN));
imageView.setImageBitmap(bitmap);
imageView.setPadding(2, 2, 2, 2);
imageView.setBackgroundColor(colorResources);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
...
}
});
linear2.addView(imageView);
}
} else {
// PAINT LINE WIDTHSCREEN x 120dp
}
LinearLayout galeria2 = (LinearLayout) findViewById(R.id.linear2);
LayoutParams params = galeria2.getLayoutParams();
params.height = 100;
params.width = 300;
galeria2.setLayoutParams(params);
You have to dynamically add width to each element of HorizontalScrollView. I have modified your code have a look. Its working fine.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dp"
android:orientation="vertical">
<HorizontalScrollView
android:id="#+id/hsv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:id="#+id/linear2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#FF0040"
android:padding="2dp"
/>
</HorizontalScrollView>
Now in code add element like (see the highlited line)
LinearLayout galeria2 = (LinearLayout) findViewById(R.id.linear2);
ImageView iv1 = new ImageView(this);
iv1.setImageResource(R.drawable.ic_launcher);
**iv1.setLayoutParams(new LinearLayout.LayoutParams(
720, LayoutParams.MATCH_PARENT));**
ImageView iv2 = new ImageView(this);
**iv2.setLayoutParams(new LinearLayout.LayoutParams(
720, LayoutParams.MATCH_PARENT));**
galeria2.addView(iv1);
galeria2.addView(iv2);

switch videoview to fullscreen mode android

I want to display a videoview in fullscreen mode when the user press a full screen button
I have searched on the internet but all solutions I have found doesnt' fix the issue
here is my current code which doesnt' work too :
public void setAnchorView(final View view) {
super.setAnchorView(view);
Button fullScreen = new Button(VideoDetails.this);
fullScreen.setText("FullScreen");
Log.e("media controller","Set anchorView");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(view.getWidth(), 0, 5, 20);
params.gravity = Gravity.RIGHT;
addView(fullScreen, params);
fullScreen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoview.getLayoutParams();
params.leftMargin = 0;
videoview.setLayoutParams(params);
}
});
}
and here is the xml layout
<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:background="#818181" >
<RelativeLayout
android:id="#+id/video_details_header"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#000000" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:clickable="true"
android:onClick="returnback"
android:text="Back"
android:textColor="#ffffff" />
<TextView
android:id="#+id/video_details_text_header_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ffffff" />
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/video_details_header"
android:layout_centerInParent="true" >
<RelativeLayout
android:id="#+id/layout_page"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/video_details_video"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<ImageView
android:id="#+id/video_details_thumbnail"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:onClick="showvideo"
android:padding="11dp"
android:visibility="visible" />
<VideoView
android:id="#+id/video_details_videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:padding="11dp"
android:visibility="gone" />
</FrameLayout>
which contains the videoview
do you have any idea
Try requestLayout() call after setting the layout params
...
params.leftMargin = 0;
videoview.setLayoutParams(params);
videoview.requestLayout();
try to change the width of the view too:
WindowManager wm = (WindowManager)videoview.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int displayWidth = display.getWidth();
params.leftMargin = 0;
params.width = displayWidth;
videoview.setLayoutParams(params);
videoview.requestLayout();
Not sure because can't all code, but for me it seems like you don't need setting margin at all
you may just manipulate with view's width and gravity.
You might have forgotten this line of code for it to fit the full screen.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
params.width = metrics.widthPixels;
params.height = metrics.heightPixels;
params.leftMargin = 0;
videoView.setLayoutParams(params);

Set TextView width to 0dp doesn't work

How to set programmatically width of TextView to be 0dp ? I am trying to put in LinearLayout couple TextViews but all to be same width, and when I put in xml like
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:gravity="left"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="test"
android:layout_gravity="right"
android:gravity="right"
/>
</LinearLayout>
but when in code try like
TextView txtTemp = new TextView(this);
txtTemp.setText(captures.get(i));
LinearLayout.LayoutParams tempLL = (LinearLayout.LayoutParams) txtTemp
.getLayoutParams();
tempLL.width =0;
tempLL.height = LinearLayout.LayoutParams.WRAP_CONTENT;
tempLL.weight = 1;
if (i == 0) {
tempLL.gravity = Gravity.LEFT;
}
if (i == (captures.size() - 1)) {
tempLL.gravity = Gravity.RIGHT;
} else {
tempLL.gravity = Gravity.CENTER;
}
txtTemp.setLayoutParams(tempLL);
I got exception on tempLL.width null exception. How to solve this ?
Instead of using
LinearLayout.LayoutParams tempLL = (LinearLayout.LayoutParams) txtTemp
.getLayoutParams();
tempLL.width =0;
tempLL.height = LinearLayout.LayoutParams.WRAP_CONTENT;
tempLL.weight = 1;
try this
LinearLayout.LayoutParams tempLL = new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT,1.0);
txtTemp.setLayoutParams(tempLL);
You are creating a new textview and there is no layoutparams specified yet. So getLayoutParams should return null. So create a new layoutparam object and set that to the new textview.

Categories

Resources