What I Tried To Do
I tried to set my ImageButton's layout_gravity via Java code, the way I want the ImageButton to be is presented like the way within the Orange frame in the image below:
The Blue frame is a vertical LinearLayout act as a "base" layout and this is the layout I tried to add child layouts to.
The Orange and Red are both horizontal LinearLayout.
The Orange layout is the way I want to put the ImageButton and the TextView, this one is set up via XML
The Red layout is the result I tried to mimic the Orange layout via Java code.
The Related Code
Here's the XML code that set up the Orange layout, this is the effect I want to achieve via Java code:
<!-- Begin the Orange Layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/info_left_padding"
android:layout_marginRight="#dimen/info_right_padding" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:minHeight="#dimen/detail_min_line_item_height"
android:text="TextView" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:adjustViewBounds="true"
android:maxHeight="#dimen/abs__action_bar_default_height"
android:scaleType="fitCenter"
android:src="#drawable/navigation_cancel" />
</FrameLayout>
</LinearLayout>
Here's the Java code that set up the Red layout
int textHeight = (int)getResources().getDimension(R.dimen.detail_min_line_item_height);
int imgHeight = (int)getResources().getDimension(R.dimen.abs__action_bar_default_height);
TextView mTextView = new TextView(this);
ImageButton mDeleteButton = new ImageButton(this);
// Set Delete Button Padding
// mDeleteButton.setPadding(buttonPadding, buttonPadding, buttonPadding, buttonPadding);
// Set Imagebutton Scale type as fitCentre
mDeleteButton.setScaleType(ScaleType.FIT_CENTER);
// Set AdjustViewBounds
mDeleteButton.setAdjustViewBounds(true);
// Set max height of the image
mDeleteButton.setMaxHeight(imgHeight);
// Set the text appearance to be "large"
mTextView.setTextAppearance(this, android.R.style.TextAppearance_Large);
mTextView.setText(text);
// Set the minimum height of this textview
mTextView.setMinHeight(textHeight);
// Set the content of the textview to be centred
mTextView.setGravity(Gravity.CENTER_VERTICAL);
// Set the ImageButton's background image
mDeleteButton.setImageResource(R.drawable.navigation_cancel);
LinearLayout.LayoutParams hParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout hLayout = new LinearLayout(this);
// Set Margins
hParams.leftMargin = (int) getResources().getDimension(R.dimen.info_left_padding);
hParams.rightMargin = (int) getResources().getDimension(R.dimen.info_right_padding);
hParams.bottomMargin = (int) getResources().getDimension(R.dimen.text_layout_margin);
hLayout.setLayoutParams(hParams);
// Set orientation to horizontal
hLayout.setOrientation(LinearLayout.HORIZONTAL);
// The settings below is actually setting up some of the button's parameters
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.gravity = Gravity.RIGHT;
mDeleteButton.setLayoutParams(buttonParams);
hLayout.addView(mTextView);
hLayout.addView(mDeleteButton);
layout_blue.addView(hLayout);
What I've Tried So Far
According to some SO post like this: Java method for android:layout_gravity I initially tried to first put my ImageButton into a FrameLayout then set the params of this FrameLayout, like this:
FrameLayout buttonFrame = new FrameLayout(this);
FrameLayout.LayoutParams buttonParams = new FrameLayout.LayoutParams(android.widget.FrameLayout.LayoutParams.WRAP_CONTENT,
android.widget.FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.RIGHT | Gravity.CENTER_VERTICAL);
buttonFrame.setLayoutParams(buttonParams);
buttonFrame.addView(mDeleteButton);
But I had the same result as the image presented above. Later I also tried to change the LayoutParams width to MATCH_PARENTonly to find theImageButton` was stretched horizontally (Yes it's stretched)
Then I tried the method posted in these two SO posts:
How to set layout_gravity programmatically?
How to set a button's parameters programatically
Their method is to set up a LinearLayout.Params first, then apply this params to the button (The Code I posted in The Related Code section applies this method). In short it is:
// The settings below is actually setting up some of the button's parameters
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.gravity = Gravity.RIGHT;
mDeleteButton.setLayoutParams(buttonParams);
However, the result was still the same as the image presented above.
Question
Since I need to programmatically add more child views to the Blue layout later, I wonder if there's a way to set up each child layout like the Orange one in the image?
At Last
I found a solution which is quite similar to #Permita 's answer.
Here's my solution:
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
textParams.weight = 1.0f;
mTextView.setLayoutParams(textParams);
Add the below code, it will assign all the available space to the texView, shifting the button to right side of the layout to make it appear like the orangeLayout.
mTextView.setLayoutParams(LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f));
Try layout_width = "0dp" layout_weight="1" for the TextView. This tells TextView to occupy the whole available width, so that FrameLayout with ImageView will align to the right border.
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:minHeight="#dimen/detail_min_line_item_height"
android:text="TextView" />
Instead of using the standard LinearLayout, why not try to use the more complex RelativeLayout? With it, you can adjust the locations of each individual view relative to others. You can set the ImageButton in the orange layout to android:layout_alignParentRight="true", which will set the button attached to the right side of the parent layout, the RelativeLayout in my case, but the LinearLayout in yours. Here is the link to the API Guides for Relative Layouts on the Android developers website.
Related
I defined a LinearLayout as root element in activity_main.xml.
CASE 1: From onCreate() I tried to add Button in this Vertical LinearLayout, what confused me is, as per Google's API, I tried to call setWidth(20) on button before adding it in ViewGroup, but Button occupied width 'match_parent' rather than 20dp.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical"
android:id="#+id/first_layout">
</LinearLayout>
//Inside onCreate() of activity..
LinearLayout firstLayout = (LinearLayout) findViewById(R.id.first_layout);
Button button = new Button(this);
button.setText(R.string.click_on_me);
button.setWidth(20);
firstLayout.addView(button);
CASE 2:on setting layout_width of LinearLayout to 'wrap_content', and calling setWidth(20), It now considered given explicit width value i.e 20dp.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical"
android:id="#+id/first_layout">
</LinearLayout>
//Inside onCreate() method
LinearLayout firstLayout = (LinearLayout) findViewById(R.id.first_layout);
Button button = new Button(this);
button.setText(R.string.click_on_me);
button.setWidth(20);//In this case, its working
firstLayout.addView(button);
CASE 3: Finally, removing my custom call to setWidth(20), Button acquired width as 'wrap_content'.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical"
android:id="#+id/first_layout">
</LinearLayout>
//Inside onCreate() method.
LinearLayout firstLayout = (LinearLayout) findViewById(R.id.first_layout);
Button button = new Button(this);
button.setText(R.string.click_on_me);
firstLayout.addView(button);
Ques : So it is clear by case-2, that using LayoutParams is not necessary, if I wish to explicitly use setWidth() method.Then In Case 4: i.e LinearLayout's width set as 'match_parent' and button.setWidth(20) is also called.
But why Button is still not taking explicitly given width value, again output is exactly same as CASE 1.
Thanks in advance.
You need to define an appropriate LayoutParams for your button view. Then add it to your firstLayout.
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.height = XX;
params.width = XX;
button.setLayoutParams(params);
You have to understand why using Layoutparams is necessary when you dynamically create Button or any component.
Suppose if your using LayoutParams to Button to give width. Then When we set the layout params of the Button, we are telling the parent layout (that is LinearLayout in your case) rendering the Button, to set the specified height and width for the view. Therefore, it works fine while rendering.
In case 1 as you told it is not affecting setwidth of 20 that is because the Button has a minimum width of 64dip by default. Set it to 0 before setting the width.
btn.setMinimumWidth(0);
This link here could give you some help.
I have a linearlayout as a container for two relativelayouts. Both relativelayouts appear on the screen but they are side by side. I want them to be top and bottom. It looks as if the linearlayout initialization defaults to Horizontal. I have tried using setorientation to Vertical but the screen blanks out.
The following code is an example of what I am trying to do:
LinearLayout layoutContainer = new LinearLayout(this);
layoutContainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
//layoutContainer.setOrientation(LinearLayout.VERTICAL);
// Arguments here: width, height, weight
LinearLayout.LayoutParams childLp = new LinearLayout.LayoutParams(0,
LayoutParams.FILL_PARENT, 1);
layoutTop = new RelativeLayout(this);
layoutContainer.addView(layoutTop, childLp);
layoutBot = new RelativeLayout(this);
layoutContainer.addView(layoutBot, childLp);
layoutTop.setBackgroundColor(GREEN);
layoutBot.setBackgroundColor(Color.BLUE);
setContentView(layoutContainer);
It looks as if the linearlayout initialization defaults to Horizontal.
That is exactly right.
I have tried using setorientation to Vertical but the screen blanks out.
You do need to set orientation to vertical to get this effect. As for it "blanks out", I see several things wrong such as setting the width to "0". There is no width so it won't show anything. I think you would want something like LinearLayout.WRAP_CONTENT. Also, you are using LinearLayout params for your RelativeLayout which may or may not make a difference in this case.
If there isn't a necessary reason to create the layout in Java it is much easier to do this in the xml.
I think you are complicating your layouts by trying to programmatically manipulate them. Set the orientation to Vertical and do the following:
<LinearLayout
------
------
android:orientation="vertical">
Your first Relative layout
<RelativeLayout
android:id="#+id/rel1"
android:layout_alignParentTop="true"
----------------------
---------------------
/>
Your next Relative layout
<RelativeLayout
android:id="#+id/rel2"
android:layout_below="#id/rel1"
----------------------
---------------------
/>
I am using linear layout with 2 textviews, 4 buttons, 1 seekbar,1 image view. If I am place those textviews,buttons, etc. in a linear layout the alignment is fine in android phone. While I am running the same code in android tablet, alignment is not proper. Why this alignment is not proper in tablet.? I have created the textviews,buttons etc by java code. Even I am specifying the two text views horizontally by settings the left margin of the second text view by devicewidth/2 having the difference in android phone and tablet. I need to align like the below.
TextView1 TextView2
Button1 Button2 Button3 Button4 SeekBar ImageView
Here is my code.
LinearLayout.LayoutParams textViewParams1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textViewLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView TextView1=new TextView(this);
TextView1.setText("Text1");
textViewParams1.gravity=Gravity.CENTER;
textViewParams1.setMargins(60, 20, 40, 10);
textViewLayout.addView(chooseColorTextView, textViewParams1);
LinearLayout.LayoutParams textViewParams2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView TextView2=new TextView(this);
TextView2.setText("Text2");
int width=getWindowManager().getDefaultDisplay().getWidth();
textViewParams2.gravity=Gravity.CENTER;
textViewParams2.setMargins((width/2), 20, 40, 10);
textViewLayout.addView(strokeWidthTextView, textViewParams2);
parentlinearLayout.addView(textViewLayout, textViewLayoutParams);
In the next linear layout, I have added the 4 buttons,seekbar and image view. But facing problems in alignment.
I advise to create complex layout that must be rendered on different screen sizes in XML rather than programmatically, so you can have two different main.xml in res/layout and in res/layout-large and the system would pick up the correct one depending on screen size.
Use layout_weight and weightSum in XML:
<LinearLayout android:weightSum="1" android:layout_width="match_parent" android:layout_height="wrap_content">
<!-- First column -->
<LinearLayout android:layout_weight=".5" android:layout_width="0dp" android:layout_height="wrap_content"> ... </LinearLayout>
<!-- Second column -->
<LinearLayout android:layout_weight=".5" android:layout_width="0dp" android:layout_height="wrap_content"> ... </LinearLayout>
</LinearLayout>
This will produce a dynamically resizing 2 column layout. If you want the divide shorter or longer, change .5 to .3 and .7 for a 30/70% split etc.
Please read more about wrap_content and other android controls. Also read about dpi of tablets. Due to resolution appearance changes.
Why don't you use a TableLayout ? its the one you need to manage cells alignement:
All that you need is in the span attribute to make cells use multiple columns.
TableLayout myLayout = new TableLayout(myContext);
TableRow row1 = new TableRow(myContext);
TableRow.LayoutParams layouparams1 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
layouparams1.span = 4;
row1.setLayoutParams = layouparams1 ;
TableRow.LayoutParams layouparams2 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
layouparams2.span = 2;
TableRow row2 = new TableRow(myContext);
row2.setLayoutParams = layouparams2 ;
//then create and add your childs to each row :
...
row1.addView(text1);
row1.addView(text1);
row1.addView(button1);
row1.addView(button2);
row1.addView(button3);
row1.addView(button4);
row1.addView(seekbar);
row1.addView(imageView);
myLayout.addView(row1);
myLayout.addView(row2);
also consider adding layout_weight on children to manage the space they left to each other.
I am trying to add TextView into a view which extends LinearLayout. When I set its width of layoutparams to fill_parent or wrap_content, the text inside TextView will display vertically such like the following.
e.g.
T
E
X
T
However, when I set the width to some fixed number, it will display normally or as I expected.
e.g. TEXT
My question is why this happens and how to solve it, i.e. how I should set programmatically so that TextView can write text horizontally without setting a fixed width to it such as setting fill_parent or wrap_content?
Here is setting of XML code of the parent ilGallery which extends LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.illib.ilgallery.view.ILGallery
android:id="#+id/galleryLayout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
The following is the code how i initialize the children inside it:
ilViewPager = new ILViewPager(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
ilViewPager.setLayoutParams(params);
ilgallery = this;
//initialize default header and footer view
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
headerView = new TextView(context);
headerView.setLayoutParams(params2);
((TextView)headerView).setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
((TextView)headerView).setText("hello");
footerView = new TextView(context);
footerView.setLayoutParams(params2);
((TextView)footerView).setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
((TextView)footerView).setText("hello2");
There appears to be only a single item inside your LinearLayout. Please set the orientation of the LinearLayout to "horizontal", like this:
android:orientation="horizontal"
Also, whenever possible instead of creating new LayoutParams objects from scratch, you should get the LayoutParams from the current layout and modify that, like this:
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)ilViewPager.getLayoutParams();
lp.<change-any-values>;
ilViewPager.setLayoutParams(lp);
I went through several links yet i am not able to find if it is possible to set other attributes of relative layout, like android:gravity,android:orientation through java code.
I want to change the positions of the some of the views through java code and not through xml.
e.g, i want to set these attributes through java code for a particular view. android:gravity="center_vertical" android:orientation="vertical"
how can i do so? please help.
as Daan said you can use Layout params for relative layout in android
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.alignWithParent = true;
params.bottomMargin = 5;
params.height = 50;
params.leftMargin = 50;
But as you said you want to set following attributes for particular view, you cannot assign these attributes to RelativeLayout as these are belongs to LinearLayout and other views
android:gravity="center_vertical" android:orientation="vertical"
For setting gravity for any view please follow the code
tv = (TextView)findViewById(R.id.tv01);
tv.setGravity(Gravity.CENTER_VERTICAL);
You cannot set Orientation of any view.. instead you can set orientation to LinearLayout
like this
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Hope this will help you
Within a relative layout you have to use other layout params:
Layout params within relative layout