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
Related
I have this:
my problem is that i have a Linearlayout wrapper with a scrollview and a Linearlayout inside.. i want every item to be 1/3 of the scrollviews height no matter how many items i put into it..
When i add items to the Linearlayout it is linearlayouts, if i add 3 'items' it is almost fine.. but if i add 8 items, they are all quite small and scrollable. This is what i want, but i want them to have the height of 1/3 of the scrollview/linearlayout.
i want it to align so its as big as the buttons in the 2nd layout i have.
i just cant seems to make it right, hope its just a simple solution i have refused to see.
my code so far:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:layout_weight="75">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2"
android:layout_gravity="center_horizontal"
android:fillViewport="true"
android:scrollbarSize="5dp"
android:fadeScrollbars="false"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:scrollbarThumbVertical="#drawable/custom_scroll_style">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/coats"
android:orientation="vertical"
android:weightSum="75">
</LinearLayout>
</ScrollView>
</LinearLayout>
This is my way of adding 'children' to the layout wrapped by the scrollview
LinearLayout w0 = new LinearLayout(this);
LinearLayout w1 = new LinearLayout(this);
ImageView w2 = new ImageView(this);
TextView w3 = new TextView(this);
TextView w4 = new TextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 25);
params.setMargins(pixelToDp(0),pixelToDp(5),pixelToDp(0),pixelToDp(5));
w0.setLayoutParams(params);
w0.setOrientation(LinearLayout.HORIZONTAL);
w0.setClickable(true);
w0.setBackgroundColor(Color.argb(100, 100, 100, 100));
w0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < LLCoats.getChildCount(); i++) {
View vv = LLCoats.getChildAt(i);
vv.setSelected(false);
vv.setBackgroundColor(Color.argb(100, 100, 100, 100));
}
LinearLayout LL = (LinearLayout)LLCoats.getChildAt(i);
TextView TV = (TextView)LL.getChildAt(1);
TV.requestFocus();
if(TV.getError() != null)
{
Snackbar snackbar = Snackbar
.make(findViewById(android.R.id.content), ""+TV.getError(), Snackbar.LENGTH_LONG);
snackbar.show();
}
v.setSelected(true);
v.setBackgroundColor(Color.argb(255,255,255,255));
}
});
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 2);
w1.setLayoutParams(params2);
w1.setOrientation(LinearLayout.VERTICAL);
w1.setClickable(false);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
w2.setLayoutParams(params3);
w2.setScaleType(ImageView.ScaleType.FIT_CENTER);
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
w3.setLayoutParams(params4);
w3.setTextSize(25f);
w3.setGravity(Gravity.CENTER);
w3.setClickable(false);
LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
w4.setLayoutParams(params5);
w4.setTextSize(45f);
w4.setGravity(Gravity.CENTER);
w4.setClickable(false);
w1.addView(w2);
w1.addView(w3);
w0.addView(w1);
w0.addView(w4);
LLCoats.addView(w0);
UPDATE!:
Now its getting really weird, as i have found a way to calculate the height and add it with:
DisplayMetrics display = this.getResources().getDisplayMetrics();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, pixelToDp((int)(display.heightPixels* .8 * 0.25)));
This works really well on my PI64 and a ASUS tablet, but my phone makes the height 100% the same as the scrollview, how is happening??
I made it works like this, getting the screen size *.8 because the screen is used as a popup and then 0.25 as items are 1/4 of the screen and in the end -10 for my margins
DisplayMetrics display = this.getResources().getDisplayMetrics();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int)(display.heightPixels* .8 * 0.25 - pixelToDp(10)));
params.setMargins(pixelToDp(0),pixelToDp(5),pixelToDp(0),pixelToDp(5));
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
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 want to implement an imageview and a textview side by side. I achieved this by using XML. However i wanted to achieve this programmatically but had no luck so far. My XML and Java code are below. Please help me to execute programmatically.
I'm executing the Java code in a fragment.
XML CODE:
<RelativeLayout
android:id="#+id/rl1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<com.loopj.android.image.SmartImageView
android:id="#+id/my_image1"
android:layout_width="160dip"
android:layout_height="100dip"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/my_image1"
android:layout_alignTop="#+id/my_image1"
android:layout_toRightOf="#+id/my_image1"
android:gravity="center_vertical"
/>
</RelativeLayout>
JAVA CODE:
RelativeLayout rl1 = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams rlParams;
rlParams = new RelativeLayout.LayoutParams(
newLayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rlParams.addRule(LinearLayout.VERTICAL);
rl1.setLayoutParams(rlParams);
SmartImageView siv1 = new SmartImageView(getActivity());
siv1.setId(rand.nextInt(50000) + 1);
siv1.setLayoutParams(new LayoutParams(width,height));
siv1.setAdjustViewBounds(true);
siv1.setScaleType(ScaleType.FIT_XY);
siv1.setImageUrl(Uri);
TextView tv1 = new TextView(getActivity());
RelativeLayout.LayoutParams relativeLayoutParams;
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(relativeLayoutParams);
relativeLayoutParams.setMargins(10, 0, 0, 0);
tv1.setGravity(Gravity.CENTER_VERTICAL);
tv1.setText("Sample Text");
rl1.addView(siv1);
rl1.addView(tv1);
ll.addView(rl1);
By executing the above code, i'm getting the image but the text is inside the image. But, i want to get the image on the left and the text on the right.
Thanks in advance
Add below code to your Activity:
rlParams.addRule(RelativeLayout.ALIGN_TOP, siv1);
rlParams.addRule(RelativeLayout.ALIGN_BOTTOM, siv1);
rlParams.addRule(RelativeLayout.RIGHT_OF, siv1);
tv1.setLayoutParams(rlParams);
and then do:
rl1.addView(siv1);
rl1.addView(tv1);
Hope this helps.
In your case it is better to use a horizontal LinearLayout:
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(param);
TextView textview = new TextView(getActivity());
...
SmartImageView siv1 = new SmartImageView(getActivity());
...
ll.addView(textview);
ll.addView(siv1);
To align the views in RelativeLayout is much difficult. I will suggest you to use the TableRows or LinearLayouts. Both these give much easier way to align views side by side. Here is a source in which one TextView is aligned-left with 4 ImageViews aligned on its right. You can get idea from this
TextView taking/consuming too much space above and below the text
You can set the margin of your text like this
relativeLayoutParams.setMargins(200, 0, 0, 0);
but this is not the best practice so do one thing take linear layout set orientation to horizontal add both imageview and textview to it and then add this linearlayout to your relative layout.
Hope this helps.
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);