I tried solutions found in other posts but none of them seems to work in my code.
Here is a piece of code:
EDIT: updated, removed one RelativeLayout, still not centered
GridView gridView = new GridView(this);
HallAdapter hallAdapter = new HallAdapter(this, listOfSeats);
gridView.setNumColumns(columnCount);
gridView.setAdapter(hallAdapter);
ZoomView zoomView = new ZoomView(this);
zoomView.addView(gridView);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.ABOVE, R.id.infoTextView);
RelativeLayout relativeLayoutMain = (RelativeLayout)findViewById(R.id.relativeLayout);
relativeLayoutMain.addView(zoomView, params);
I think that i have to add some LayoutParameters, but I don't know where.
Part of XML file of activity:
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/relativeLayout">
This is how its look now:
EDIT:
I wonder is it look like this? Or am I wrong?
Use layout params when adding the view
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
relativeLayoutMain.addView(relativeLayoutForZoom, params);
After second look at your question I have another answer.
Do you need relativeLayoutForZoom at all?
GridView gridView = new GridView(this);
HallAdapter hallAdapter = new HallAdapter(this, listOfSeats);
gridView.setNumColumns(columnCount);
gridView.setAdapter(hallAdapter);
ZoomView zoomView = new ZoomView(this);
zoomView.addView(gridView);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.ABOVE, R.id.infoTextView);
RelativeLayout relativeLayoutMain = (RelativeLayout)findViewById(R.id.relativeLayout);
relativeLayoutMain.addView(zoomView, params);
Modify your code to this
GridView gridView = new GridView(this);
HallAdapter hallAdapter = new HallAdapter(this, listOfSeats);
gridView.setNumColumns(columnCount);
gridView.setAdapter(hallAdapter);
ZoomView zoomView = new ZoomView(this);
zoomView.addView(gridView);
RelativeLayout relativeLayoutForZoom = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ABOVE, R.id.infoTextView);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
relativeLayoutForZoom.addView(zoomView);
RelativeLayout relativeLayoutMain = (RelativeLayout)findViewById(R.id.relativeLayout);
relativeLayoutMain.setGravity(Gravity.CENTER);
relativeLayoutMain.addView(relativeLayoutForZoom,params);
Set layout parameters of zoomView to params and add the rule to params as params.addRule(RelativeLayout.CENTER_HORIZONTAL);
Regards,
Try Adding
parms.addRule(RelativeLayout.CENTER_IN_PARENT);
along with params.addRule(RelativeLayout.ABOVE, R.id.infoTextView);
<GridView
android:id="#+id/gridView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:numColumns="3" >
</GridView>
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)positiveButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
gridview.setLayoutParams(layoutParams);
![ android:layout_centerHorizontal="true"
android:layout_centerVertical="true"]1
Related
I want to add a ViewStub to my layout. This is how the ViewStub should look like, as xml:
<ViewStub
android:id="#+id/viewstub_id"
android:layout="#layout/use_this_layout"
android:inflatedId="#+id/inflated_stub"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I have come up with this code, but the layout is not showing.
I'm reusing the id, because I'm deleting and adding the stub again. I guess the code not working has something to do with the constraints or the layout width/height, but I'm not sure. What am I doing wrong?
ViewStub vs = new ViewStub(this);
vs.setId(R.id.viewstub_id);
vs.setLayoutResource(R.layout.use_this_layout);
vs.setInflatedId(R.id.inflated_stub);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(0, 0);
vs.setLayoutParams(layoutParams);
ConstraintLayout cl = findViewById(R.id.viewStubWrapper);
ConstraintSet cs = new ConstraintSet();
cs.clone(cl);
cs.connect(ConstraintSet.PARENT_ID, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
cs.applyTo(cl);
cl.addView(vs); // the ConstraintLayout is the ViewStub's parent
vs.inflate();
I found something that seems to work:
ViewStub vs = new ViewStub(this);
vs.setId(R.id.viewstub_id);
vs.setLayoutResource(R.layout.use_this_layout);
vs.setInflatedId(R.id.inflated_stub);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
vs.setLayoutParams(lp);
ConstraintLayout cl = findViewById(R.id.viewStubWrapper);
cl.addView(vs);
vs.inflate();
Seems like I didn't need the constraints after all, I just needed to use LayoutParams.MATCH_PARENT for width and height instead of 0.
I'm having troubles trying to create a LinearLayout with an ImageView inside programatically (and add this LinearLayout to another LinearLayout). The XML code that I'm tryng to recreate is this:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout_morty_combinationsV"
android:gravity="center">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:background="#color/black"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/morty"
android:id="#+id/imageView1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:background="#color/black"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/morty"
android:id="#+id/imageView2"/>
</LinearLayout>
</LinearLayout>
This is my java code
LinearLayout ll_combinations = (LinearLayout) findViewById(R.id.linearLayout_morty_combinationsV);
Iterator<MortyObj> iterator = mortys.iterator();
while (iterator.hasNext()) {
final MortyObj mortyC = iterator.next();
LinearLayout ll_new = new LinearLayout(this);
ll_new.setOrientation(LinearLayout.VERTICAL);
ll_new.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.weight = 1f;
ll_new.setLayoutParams(params);
ll_new.setBackgroundColor(Color.WHITE);
ImageView morty_imageview = new ImageView(this);
try {
InputStream ims = getAssets().open("morty_images/" + mortyC.getId() + ".png");
Drawable d = Drawable.createFromStream(ims, null);
morty_imageview.setImageDrawable(d);
} catch (IOException e) {
e.printStackTrace();
}
FrameLayout.LayoutParams imgParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
morty_imageview.setLayoutParams(imgParams);
ll_new.addView(morty_imageview);
ll_combinations.addView(ll_new);
}
This is how it should look:
XML
And this is how It looks:
JAVA
I've been stuck in this for the last 2 hours.
You can create a layout with something like
View view = (View) findViewById(R.layout.current_layout); //the layout you set in `setContentView()`
LinearLayout picLL = new LinearLayout(CurrentActivity.this);
picLL.layout(0, 0, 100, 0);
picLL.setLayoutParams(new LayoutParams(1000, 60));
picLL.setOrientation(LinearLayout.HORIZONTAL);
((ViewGroup) view).addView(picLL);
What parameters you pass in layout() are obviously going to depend on what you want. Then you can create separate Views to add to the Layout you just created. But I highly advise reading through the docs to understand what all can be done here.
Edit
ImageView myImage = new ImageView(this);
picLL.addView(myImage);
//set attributes for myImage;
Just found the problem. If i load an image from res/drawables I get the view I want, if I load it from assets it doesn't work
This question already has answers here:
Adding child views to Relative Layout
(2 answers)
Closed 7 years ago.
I have a background image:
android:background="#drawable/bg">
and a TextView:
final TextView theTimer = new TextView(this);
If I use setContentView I can only place one of them on the screen.
How can I fit them both in one activity?
Do something like this -
my_layout.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bg">
</LinearLayout>
Activity code
setContentView(R.layout.my_layout);
final TextView theTimer = new TextView(this);
LinearLayout ll = (LinearLayout)findViewByID(R.id.ll);
ll.addView(theTimer);
Create
RelativeLayout create ImageView
RelativeLayout layout = new RelativeLayout(this);
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.your_drawable);
TextView tv1 = new TextView(this);
tv1.setText("B");
RelativeLayout.LayoutParams layoutParams =new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
tv1.setLayoutParams(layoutParams);
layout.addView(img);
layout.addView(tv1);
setContentView(layout);
I have a hierarchy that is like this:
LinearLayout(horizontal)
ImageView
LinearLayout(vertical)
TextView
TextView
TextView
TextView
I want to be able to add the hierarchy above through iteration as long as there is data that could be obtained from the database(using Parse)
I have tried putting up the ImageView and LinearLayout under the parent LinearLayout but it doesn't seem to work. Here is my code in MainActivity.Java:
LinearLayout LL_Outer = (LinearLayout) findViewById(R.id.new_linearLayoutOuter);
LL_Outer.setOrientation(LinearLayout.VERTICAL); // set orientation
LL_Outer.setBackgroundColor(color.white); // set background
// set Layout_Width and Layout_Height
LinearLayout.LayoutParams layoutForOuter = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
LL_Outer.setLayoutParams(layoutForOuter);
LinearLayout LL_Inner = (LinearLayout) findViewById(R.id.new_linearLayoutInner);
LL_Inner.setOrientation(LinearLayout.HORIZONTAL);
LL_Inner.setBackgroundColor(color.white);
LinearLayout.LayoutParams layoutForInner = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LL_Inner.setLayoutParams(layoutForInner);
//LL_Outer.addView(LL_Inner);
ImageView IV = (ImageView) findViewById(R.id.new_imageViewPP);
//IV.getLayoutParams().height = 55;
//IV.getLayoutParams().width = 55;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(14, 12, 0, 0);
params.height = 55;
params.weight = 55;
IV.setBackgroundColor(color.black);
IV.setLayoutParams(params);
LL_Inner.addView(IV);
LL_Outer.addView(LL_Inner);
I don't know where I went wrong as my code did not prompt any error. Please help.
EDIT: I have edited the Orientations accordingly and when I run the app, it stops working. And prompts an error in LogCat saying "The specified child already has a parent. You must call removeView() on the child's parent first.
Here's my XML:
<LinearLayout
android:id="#+id/new_linearLayoutOuter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal" >
<ImageView
android:id="#+id/new_imageViewPP"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginLeft="14dp"
android:layout_marginTop="12dp"
android:background="#color/black"
android:contentDescription="#string/pp" />
<LinearLayout
android:id="#+id/new_linearLayoutInner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical" >
<TextView
android:id="#+id/new_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/movie_title"
android:paddingTop="10dp"
android:paddingLeft="7dp"
android:textSize="15sp" /> <!-- Title of the movie -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/review_by"
android:paddingTop="3dp"
android:paddingLeft="7dp"
android:textSize="12sp" /> <!-- By -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/movie_stars"
android:paddingTop="3dp"
android:paddingLeft="7dp"
android:textSize="12sp" /> <!-- Rating and date -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sample_string"
android:maxLines="5"
android:scrollbars="vertical"
android:paddingTop="10dp"
android:paddingLeft="7dp"
android:textSize="12sp"
android:layout_gravity="center_vertical|right" /> <!-- Review content -->
</LinearLayout>
</LinearLayout>
You want that hierarchy programmatically.
- LinearLayout(horizontal)
- ImageView
- LinearLayout(vertical)
- TextView
- TextView
- TextView
- TextView
Ok lets start with Parent LinearLayout
LinearLayout parent = new LinearLayout(context);
parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
parent.setOrientation(LinearLayout.HORIZONTAL);
//children of parent linearlayout
ImageView iv = new ImageView(context);
LinearLayout layout2 = new LinearLayout(context);
layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);
parent.addView(iv);
parent.addView(layout2);
//children of layout2 LinearLayout
TextView tv1 = new TextView(context);
TextView tv2 = new TextView(context);
TextView tv3 = new TextView(context);
TextView tv4 = new TextView(context);
layout2.addView(tv1);
layout2.addView(tv2);
layout2.addView(tv3);
layout2.addView(tv4);
And you are done :)
The problem is because the views are already added to the layout in the XML file. Then you findViewById (find them) and try to add them to the layout again. That is why the app crashes complaining that, view already has a parent and you can't add it again.
In the XML File LinearLayout already has child view. So there is not need to add them in code.
i suggest you to remove the xml file and just use full code on the java side. you can add the views programatically from the java side. this one from xtreemdeveloper but i change few line for the parent layout.
// remove this params set it up below
parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// change the code above into
.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
addContentView(parent,layoutParams);
// end of my change
it will look like this in full code =
LinearLayout parent = new LinearLayout(context);
// remove this params set it up below
parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// change the code above into
.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
addContentView(parent,layoutParams);
// end of my change
parent.setOrientation(LinearLayout.HORIZONTAL);
//children of parent linearlayout
ImageView iv = new ImageView(context);
LinearLayout layout2 = new LinearLayout(context);
layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);
parent.addView(iv);
parent.addView(layout2);
//children of layout2 LinearLayout
TextView tv1 = new TextView(context);
TextView tv2 = new TextView(context);
TextView tv3 = new TextView(context);
TextView tv4 = new TextView(context);
layout2.addView(tv1);
layout2.addView(tv2);
layout2.addView(tv3);
layout2.addView(tv4);
I'm doing this Application where I dynamically change a ListView Size like in the following code:
RelativeLayout.LayoutParams mParam = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
sucs.setLayoutParams(myListView);
But when I do this, the ListView appears on top of the view. Is there any way where I can place it below the textview where it is supposed to be?
Thanks in advance!
EDITED:
HereĀ“s my xml:
<TextView
android:id="#+id/sucursales_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/direccion_empresa"
android:background="#drawable/barra"
android:text="#string/sucursales"
android:visibility="gone" />
<ListView
android:id="#+id/sucursalesEmpresaList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/sucursales_empresa"
android:cacheColorHint="#00000000"
android:visibility="gone" />
You have to add rule for listview LayoutParams like this..
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.BELOW, R.id.textview);
listview.setLayoutParams(params1);
Try this
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, view.getId());
view.setLayoutParams(params);
ViewGroup parentView = (ViewGroup) findViewById(R.id.viewgroup);
parentView.addView(mylsitview);