The line below with the comment "no effect" has ... no effect, i.e. the width is not set to 200. The ListView fills the entire width of the screen. What am I doing wrong?
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.list_alphabet_layout);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT );
relativeLayout.setLayoutParams(layoutParams);
ListView listView = new ListView(this);
// the following line as no effect
listView.setLayoutParams(new ViewGroup.LayoutParams(200, ViewGroup.LayoutParams.MATCH_PARENT));
listView.setFastScrollEnabled(true);
// side index
LinearLayout sideIndexLinearLayout = new LinearLayout(this);
sideIndexLinearLayout.setId(R.id.sideIndex);
LinearLayout.LayoutParams sideIndexLinearLayoutParams = new LinearLayout.LayoutParams(100, // width
LinearLayout.LayoutParams.MATCH_PARENT); // height
sideIndexLinearLayout.setLayoutParams(sideIndexLinearLayoutParams);
sideIndexLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
sideIndexLinearLayout.setOrientation(LinearLayout.VERTICAL);
// add now so that sideIndexLinearLayout is a child of the RelativeLayout.
// This will enable us to get the RelativeLayout.LayoutParams to addRule()
relativeLayout.addView(listView);
relativeLayout.addView(sideIndexLinearLayout);
RelativeLayout.LayoutParams listViewParams = (RelativeLayout.LayoutParams)listView.getLayoutParams();
listViewParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
listView.setLayoutParams(listViewParams); //causes layout update
RelativeLayout.LayoutParams sideIndexParams = (RelativeLayout.LayoutParams)sideIndexLinearLayout.getLayoutParams();
sideIndexParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
sideIndexLinearLayout.setLayoutParams(sideIndexParams); //causes layout update
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_alphabet_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</RelativeLayout>
Here
listView.setLayoutParams(new ViewGroup.LayoutParams(200, ViewGroup.LayoutParams.MATCH_PARENT));
listView.setFastScrollEnabled(true);
Try to set
listView.setLayoutParams(new RelativeLayout.LayoutParams(200, RelativeLayout.LayoutParams.MATCH_PARENT));
listView.setFastScrollEnabled(true);
instead
Layout parameters depends on the container element. In this case your ListView is inside a RelativeLayout so you need RelativeLayout.LayoutParams
Related
This is the XML that work fine, when I run in Android Studio, how to make this programmatically?
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:context="com.test.MainActivity">
<TextView
android:text="Texto 1"/>
</GridLayout>
This is the code, that I try to make without success...
//TODO create Grid params
GridLayout.LayoutParams gridParams = new GridLayout.LayoutParams();
gridParams.height = GridLayout.LayoutParams.WRAP_CONTENT;
gridParams.width = GridLayout.LayoutParams.WRAP_CONTENT;
//TODO set gravity to CENTER
gridParams.setGravity(Gravity.CENTER);
//TODO create GridLayout
GridLayout gridLayout = new GridLayout(context);
//TODO set Grid Params
gridLayout.setLayoutParams(gridParams);
//TODO create TextView
TextView textView = new TextView(context);
textView.setText("Text 1");
//TODO add to Grid
gridLayout.addView(textView);
setContentView(gridLayout);
Android Studio version 2.3.3
Tested in Tablet running Android 4.2.2
EDIT Working
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
RelativeLayout parentRelativeLayout = new RelativeLayout(context);
parentRelativeLayout.setLayoutParams(layoutParams);
parentRelativeLayout.setGravity(Gravity.CENTER);
parentRelativeLayout.addView(gridLayout);
Your grid layout needs to be inside another layout like RelativeLayout that has a width and height of match parent.
Its not that it isn't working, just think of it like this, the gridLayout is only big enough to contain its own containers, which isn't the size of the screen.
Try this.
GridLayout.LayoutParams gridParams = new GridLayout.LayoutParams();
gridParams.height = GridLayout.LayoutParams.WRAP_CONTENT;
gridParams.width = GridLayout.LayoutParams.MATCH_PARENT;
params.gravity = Gravity.CENTER;
params.weight=1.0f;
//TODO create Grid params
I am creating LinearLayout in RelativeLayout programmatically. What I am trying to do is to assign the weigtsum in LinearLayout and want to set the layout_above attribute as well. The problem is weightSum is available in LinearLayout.LayoutParams and layout_above is available in RelativeLayout.LayoutParams. Currently,, I am doing
LinearLayout ll_menu_code = new LinearLayout(this);
ll_menu_code.setId(1001);
LinearLayout.LayoutParams parmas_ll_menu_code = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 2f);
parmas_ll_menu_code.setMargins(0, 20, 0, 0);
ll_menu_code.setLayoutParams(parmas_ll_menu_code);
Which sets the weightSum. How to set the layout_above? What should the way to set the both attributes?
I want to create the following xml layout programmatically
<LinearLayout
android:layout_below="#id/ll_menu_code"
android:id="#+id/ll_quantity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:weightSum="5">
If you want to add LinearLayout into RelativeLayout programmatically, then you must create instance of RelativeLayout.LayoutParams rather than LinearLayout.LayoutParams since LinearLayout is a child to Relativelayout
So your code must be
LinearLayout ll_menu_code = new LinearLayout(this);
ll_menu_code.setId(1001);
ll_menu_code.setWeightSum(2f);
RelativeLayout.LayoutParams parmas_ll_menu_code = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
parmas_ll_menu_code.setMargins(0, 20, 0, 0);
parmas_ll_menu_code.addRule(RelativeLayout.BELOW, 1001);
ll_menu_code.setLayoutParams(parmas_ll_menu_code);
I Hope it helps..
I am trying to create a dialog window in Android where I am dynamically creating the layout and adding the views in to it. But I want to add my view to a specific position in layout.
Here is my code snippet
final Dialog dialog = new Dialog(this);
FrameLayout fl=new FrameLayout(this);
TextView et = new TextView(this);
et.setText("asdas");
fl.addView(et,100,1200);
ColorDrawable cd=new ColorDrawable(android.graphics.Color.TRANSPARENT);
dialog.getWindow().setBackgroundDrawable(cd);
dialog.setContentView(fl);
I used this to place a image button at a specified location in my relative layout. hope this helps:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin = (int)(marginLeft);
layoutParams.topMargin = (int)(marginTop);
imageButton.setLayoutParams(layoutParams);
EDIT:
You can do this for other layouts that support positioning of child views. This method works even for lower API versions. Atleast it worked on API 10. :)
yes you can do this ...something like this
AdView ad = new AdView(this, AdSize.SMART_BANNER, getString(R.string.admob_id));
LinearLayout control_container =(LinearLayout)findViewById(R.id.ad_container);
control_container.addView(ad);
AdRequest adRequest = new AdRequest();
ad.loadAd(adRequest);
and in your xml file...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E9E5E6" >
<LinearLayout
android:id="#+id/ad_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
suppose if you want to add TextView at (200,200) position
try this...
FrameLayout frameLayout = new FrameLayout(this);
TextView textView = new TextView(this);
textView.setTop(200);
textView.setLeft(200);
frameLayout.addView(textView, textViewWidth, textViewHeight);
I think this should work.
Dialog dialog = new Dialog(this);
FrameLayout frm_layout = new FrameLayout(this);
android.widget.FrameLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.leftMargin = 100;
params.topMargin = 1200;
TextView et = new TextView(this);
et.setText("asdas");
frm_layout.addView(et, params);
dialog.setContentView(frm_layout, new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.FILL_PARENT));
I have a RelativeLayout defined in main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</RelativeLayout>
And add a TextView and EditText programatically in onCreate:
public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.main);
addContentView(customGlView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
addContentView(myTextView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addContentView(myEditText, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
They both show up, but are overlapping in the top left corner. I've spend hours to figure out how to either position them just below each other, or one in the left corner of the screen and the other in the right corner. If I add them through the main.xml neither of them will show up. Can anyone give me a hint to the right direction?
For a RelativeLayout, you need to specify the relative positions of your elements, using the LayoutParams:
myTextView.setId(1);
myEditText.setId(2);
addContentView(myTextView, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.BELOW, myTextView.getId());
addContentView(myEditText, params);
Add a rule to your LayoutParams which will set one textview below the other..
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
tv1.setId(1);
tv1.setText("myTextView");
params1.addRule(RelativeLayout.BELOW, tv1.getId());
TextView tv2 = new TextView(this);
I couldn't solve this with RelativeLayout, but using LinearLayout does exactly what you want to do.
addView(myEditText);
Or you can use this method to add them to any position you want. For example, you can first add myEditText and then add myTextView above myEditText:
addView(myEditText);
addView(myTextView, 0);
I create a button from code and places it on bottom of rootView
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rootView.addView(getSaveButton(), buttonParams);
In xml layout I have a list that height = wrap_content. I want align list bottom with button top when button is created.
<ExpandableListView
android:id="#+id/episodes_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:layout_below="#id/show_status_buttons_layout"
android:groupIndicator="#drawable/indicator"
/>
How I can add android:layout_above="#id/button_save" layout parameter to ListView?
Thanks.
RelativeLayout.LayoutParams listParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
listParams.addRule(RelativeLayout.ABOVE, getSaveButton().getId());
episodesList.setLayoutParams(listParams);
This should work
buttonParams.addRule(RelativeLayout.ABOVE, R.id.button_save);