Add Layout Params to view programmatically - Java - java

I am inflating a view in my activty like this
View myView = getLayoutInflater().inflate(R.layout.my_view, my_activity_layout);
And now I want to center myView in the regular layout
I tried this
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)myView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
myView.setLayoutParams(layoutParams);
But got this error
android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
How could I center this view?
Thanks
EDIT
Here is my xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp" android:layout_height="250dp"
android:id="#+id/discountView" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Discount:"
android:id="#+id/discountTitleText"
android:layout_marginTop="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:textColor="#ffffff" />

Replace FrameLayout.LayoutParams instead if RelativeLayout.LayoutParams.
OR
You have to change FrameLayout to RelativeLayout in my_view.xml file

What is error here? To understand it you need to understand how view/viewgroup hierarchy is created for that refer : http://android-developers.blogspot.in/2009/03/android-layout-tricks-3-optimize-by.html
So if you go back to your view actually you have FrameLayout-->RelativeLayout-->Textview
Solution
Instead of use
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)myView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
myView.setLayoutParams(layoutParams);
use
FrameLayout.LayoutParams layoutParams =
(FrameLayout.LayoutParams)myView.getLayoutParams();
layoutParams.addRule(//your code);
myView.setLayoutParams(layoutParams);
or alternatively get RelativeLayout from your view
RelativeLayout rootLayout = myView.findViewById(R.id.discountView);
//And using RelativeLayout params as you have done
rootLayout.setLayoutParams(layoutParams)

Related

Scroll view is not working properly when adding through java code

RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new RelativeLayout(this);
setContentView(layout);
ScrollView sc = new ScrollView(this);
sc.setBackgroundColor(Color.YELLOW);
sc.setLayoutParams(layoutParams(300, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.ALIGN_PARENT_RIGHT));
RelativeLayout rl = new RelativeLayout(this);
rl.setBackgroundColor(Color.CYAN);
rl.setLayoutParams(layoutParams(300, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.ALIGN_PARENT_RIGHT));
int k=0;
for(int i=0;i<15;i++){
ImageView iv1 = new ImageView(this);
iv1.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
iv1.setLayoutParams(layoutParams(200, 200,0));
iv1.setTranslationY(k);
k+=200;
rl.addView(iv1);
}
sc.addView(rl);
layout.addView(sc);
}
public RelativeLayout.LayoutParams layoutParams(int width,int height,int rule1){
RelativeLayout.LayoutParams lpb = new RelativeLayout.LayoutParams(width,height);
if(rule1 != 0){
lpb.addRule(rule1);
}
return lpb;
}
Output of code
when i'm using RelativeLayout without ScrollView then it is showing all images but when i'm using ScrollView then it is not showing all images.
Please check where is the problem in the code.
it should be in right alignment inside ScrollView.
Desired output
XML Code of desired output ,which i'm trying to convert in java code.
<ScrollView
android:layout_width="300px"
android:layout_height="match_parent"
android:layout_alignParentRight="true" >
<RelativeLayout
android:layout_width="300px"
android:layout_height="fill_parent"
android:background="#android:color/holo_purple" >
<ImageView
android:id="#+id/iv1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/ic_action_home" />
<ImageView
android:id="#+id/iv2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#+id/iv1"
android:src="#drawable/ic_action_home" />
<ImageView
android:id="#+id/iv3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#+id/iv2"
android:src="#drawable/ic_action_home" />
<ImageView
android:id="#+id/iv4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#+id/iv3"
android:src="#drawable/ic_action_home" />
</RelativeLayout>
</ScrollView>
A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects.
So when you are using Relative Layout it is displaying images but when your are using scroll view which doesnot contain all the images under one layout it doesnot work, so try putting all images under LinearLayout (recommend) or other layouts.

How to add relative Layout below some element dynamically

Please help me to add a RelativeLayout below a RadioButton dynamically (I have to create RelativeLayout and RadioButton in class).
Something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioButton3"
android:text="RadioButton" />
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_below="#+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</LinearLayout>
To View I can write:
myLayout.addView(myView, layoutParams);
But how about ViewGroup?
I am not sure whether I understand u. Please check it and give me feedback. Thank you!
LinearLayout mLayout = (LinearLayout) findViewById(R.id.myLayout);
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
RadioButton mRadioButton = new RadioButton(this);
mRadioButton.setLayoutParams(p);
mLayout.addView(mRadioButton);
p = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
RelativeLayout mRelativeLayout = new RelativeLayout(this);
mRelativeLayout.setLayoutParams(p);
mLayout.addView(mRelativeLayout);

Cannot center view in RelativeLayout

I am trying to center a view within a parent RelativeLayout:
private void addLoadingAnimation() {
RelativeLayout mainView = (RelativeLayout) findViewById(R.id.medical_supply_tile_activity);
mainView.removeAllViews();
GifView gifView = new GifView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
gifView.setId(R.id.loading_gif);
mainView.setBackgroundColor(getResources().getColor(android.R.color.background_light));
mainView.addView(gifView,layoutParams);
}
I have also tried adding layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); before the CENTER_IN_PARENT line. Nothing seems to be working. It is always aligned to the bottom right.
Here is my parent view:
<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="#android:color/background_light"
tools:context=".MedecineTileActivity"
android:id="#id/medical_supply_tile_activity"
android:layout_gravity="left">
</RelativeLayout>
The gravity=left is because when the loading animation is removed, I insert fragments and want them to go to the left.
I have achieved this with TextViews and ImageViews by aligning the child View's right to the parent's right and also aligning the child View's left to the parent's left.
<TextView
style="#style/NewTextM2"
android:id="#+id/soft_dialog_info_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/<Parent>"
android:layout_alignLeft="#+id/<Parent>"
android:layout_alignRight="#+id/<Parent>"
android:gravity="center"
android:tag="item_text"
android:text="#string/text"
android:maxLines="1" />
This will align the view to the bottom center of the parent.

Change ListView position dynamically

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);

Center a WebView Programmatically

I am creating my controls programmatically, based on JSON received from a server. One of the controls I need to create is a WebView that is horizontally centred on the screen. This is simple in xml layouts as shown below using the layout_gravity option. But how do you do this in code, the WebView unlike TextView does not have a setGravity(Gravity.HORIZONTAL_GRAVITY_MASK) method.
<?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" >
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
I would use a RelativeLayout, then you can use LayoutParams when adding the view:
RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
relativeLayout.addView(yourWebView, layoutParams);
i use LinearLayout to show webview and setGravity.
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
linearLayout.setGravity(Gravity.CENTER);
WebView view = new WebView(this);
linearLayout.addView(view);
setContentView(linearLayout);
it help you.

Categories

Resources