I am trying to 'dynamically' create a layout in a program, but I am having issues getting the layout to change its position and size without a delay.
Code:
package com.example.alexander.compassvsredneckinterpretertimetable;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.constraint.ConstraintLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
public class testActivity extends Activity
{
class testUI
{
LinearLayout linearLayout;
TextView start;
TextView end;
int startValue, endValue;
private ShapeDrawable makeBackgorundShape()
{
ShapeDrawable BackgroundShape = new ShapeDrawable();
BackgroundShape.setShape(ItemBackground.makeRoundRect());
BackgroundShape.getPaint().setColor(0xffa6b2fc);
return BackgroundShape;
}
testUI(Context context, final ConstraintLayout parent, int startValue_, int endValue_)
{
this.startValue = startValue_ * 100;
this.endValue = endValue_ * 100;
start = new TextView(context);
start.setText(". " + String.valueOf(endValue) + " .");
end = new TextView(context);
end.setText(". " + String.valueOf(startValue) + " .");
linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(start);
linearLayout.addView(end);
linearLayout.setBackground(makeBackgorundShape());
parent.addView(linearLayout);
final Handler handler = new Handler();
Runnable runnable = new Runnable()
{
#Override
public void run()
{
linearLayout.layout(parent.getLeft(), linearLayout.getTop() + startValue, parent.getRight(), linearLayout.getBottom() + endValue); //without being postDelayed, this doesn't work
}
};
handler.postDelayed(runnable, 1000);
//handler.post(runnable); //doesn't work
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ConstraintLayout constraintLayout = findViewById(R.id.constLayout);
new testUI(this, constraintLayout, 8, 10);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="#+id/constLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".testActivity">
</android.support.constraint.ConstraintLayout>
if handler.postDelayed(runnable, 1000); is changed to handler.post(runnable); or the handler and runnable are removed completely (just leaving the layout line), the layout doesn't change position or size at all.
I am not sure what the minimum delay is, but having the delay results in elements visibly flickering or changing position or size. Is there a way to get rid of the delay?
The canonical way of changing a View's dimensions in Android is to adjust the View's LayoutParams. You don't want to call layout() directly (in this situation); it's not designed to be used in that way.
Perhaps something like this (instead of your Runnable):
ConstraintLayout.LayoutParams params;
params = (ConstraintLayout.LayoutParams) linearLayout.getLayoutParams();
params.width = LayoutParams.MATCH_PARENT;
params.height = endValue - startValue;
params.topMargin = startValue;
linearLayout.setLayoutParams(params);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.connect( R.id.myLinearLayout, ConstraintSet.TOP, R.id.constLayout, ConstraintSet.TOP, 0 );
constraintSet.applyTo( parent );
In order for this to work, all of constLayout's children must have resource IDs. For that reason, you may find it easiest to just have them in the .xml file to begin with:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:id="#+id/constLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".testActivity">
<LinearLayout
android:id="#+id/myLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="#+id/startValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/endValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.support.constraint.ConstraintLayout>
Naturally, you would find them with findViewById(), rather than adding them dynamically, in this case.
Related
My goal is to dynamically create a grid of TextViews in a ConstraintLayout. I've seen many examples but there's an issue that I'm just not seeing. I've broken the task into two steps:
creating the View
positioning the View
I seem to have mastered the first but not the second. Usually I can modify a View's ConstraintSet if it is defined in XML and positioned at startup, but never when I've created it programmatically. When I create a View I'm careful to set an id, layout_width and layout_height. Method's I've tried for creating a new View:
creating it and setting parameters from a new LayoutParameters object
creating it and setting parameters from another View.
inflating it from an XML template.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/activity_main" >
<Button
android:id="#+id/makeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:text="Make View"
android:onClick="makeView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/placeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:text="Place View"
android:onClick="placeView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/makeView" />
<TextView
android:id="#+id/anchorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:layout_marginStart="20sp"
android:text="View 00"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.textviewonthefly;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
final String TAG = "DEBUGME";
final int START = ConstraintSet.START;
final int END = ConstraintSet.END;
final int TOP = ConstraintSet.TOP;
int counter = 0;
View anchorView;
TextView nextView;
ConstraintLayout mainLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (ConstraintLayout) findViewById(R.id.activity_main);
anchorView = (TextView) findViewById(R.id.anchorView);
}
public void makeView(View view) {
nextView = new TextView(this);
nextView.setLayoutParams(anchorView.getLayoutParams());
int nextViewId = nextView.generateViewId();
nextView.setId(nextViewId);
int ViewCount = ++counter;
String newName = String.format(Locale.US, "View %02d", ViewCount);
nextView.setText(newName);
this.mainLayout.addView(nextView);
}
public void placeView(View view) {
ConstraintSet set = new ConstraintSet();
set.clone(this.mainLayout);
set.clear(nextView.getId());
set.connect(nextView.getId(), TOP, anchorView.getId(), TOP);
set.connect(nextView.getId(), START, anchorView.getId(), END);
set.applyTo(this.mainLayout);
anchorView = nextView;
}
}
Solved.
clear(nextView.getId()); was too aggressive. I replaced that line with more specific directives:
clear(nextView.getId(), END);
clear(nextView.getId(), BOTTOM);
It's now working as intended, with a little revision my app creates a horizontal chain as I Add and Place:
[View 00][View 01][View 02]
I am learning android and doing a simple calculator. I am building the calculator inputs button programmatically by iterating a list of operator labels like such
final String[] inputLabels = {"9", "8", "7", "/", "6" ...}
It will be 4 rows and 4 columns like this
// please do not mind me about the design
9 8 7 /
6 5 4 x
1 2 3 -
0 AC = +
Those inputs will be under ConstraintLayout, because I want to learn using it. But before going into my logic of how constrainting those inputs from my 1D array, I want to test if simple code such as creating 2 buttons and constraint them works, which it does not and that is the main problem I want to solve here.
I already tried googling it and they all seems very simple, it is strange that I could not achieve such simple thing and I tried to google my problem but I found nothing(sorry If those problem exists and I didnt found it)
These below are what I tried to follows:
Android : How to programmatically set layout_constraintRight_toRightOf "parent"
ConstraintLayout: change constraints programmatically
Here is the main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="calculatedValue"
type="java.lang.String"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/mainLayout"
tools:context=".MainActivity">
<include
layout="#layout/calculate_value_display"
bind:calculatedValue="#{calculatedValue}" />
<fragment
android:name="com.example.calculator.CalculatorInputs"
android:id="#+id/calculatorInputs"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</layout>
The calculator_inputs.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".CalculatorInputs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:id="#+id/calculatorInputs"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.constraint.ConstraintLayout>
</FrameLayout>
The MainActivity.java is just what it is when the project created and here is the CalculatorInputs.java
package com.example.calculator;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public final class CalculatorInputs extends Fragment {
private final String[] inputLabels = {"7", "8", "9", "/", "4", "5", "6", "x", "1", "2", "3", "-", "0", "AC", "=", "+"};
private final ConstraintLayout.LayoutParams buttonConstraintLayout =
new ConstraintLayout.LayoutParams(220, 150);
private final float buttonTextSize = 20.f;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.calculator_inputs, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ConstraintLayout layout = (ConstraintLayout) view.findViewById(R.id.calulatorInputs);
ConstraintLayout.LayoutParams layoutParams;
final ArrayList<Button> buttons = new ArrayList<Button>(this.inputLabels.length);
while (buttons.size() < this.inputLabels.length) {
buttons.add(null);
}
int buttonIndex = 0;
//////////////////////////////////////////////////////////////////
/////////////////////////// BEGIN HERE ////////////////////////////
//////////////////////////////////////////////////////////////////
final ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(layout);
final Button button1 = this.createButton("X");
final Button button2 = this.createButton("Y");
layout.addView(button1);
constraintSet.connect(
button1.getId(),
ConstraintSet.RIGHT,
ConstraintSet.PARENT_ID,
ConstraintSet.RIGHT
);
constraintSet.connect(
button1.getId(),
ConstraintSet.TOP,
ConstraintSet.PARENT_ID,
ConstraintSet.TOP
);
constraintSet.applyTo(layout);
layout.addView(button2);
constraintSet.connect(
button2.getId(),
ConstraintSet.RIGHT,
ConstraintSet.PARENT_ID,
ConstraintSet.RIGHT
);
constraintSet.connect(
button2.getId(),
ConstraintSet.TOP,
ConstraintSet.PARENT_ID,
ConstraintSet.TOP
);
///////////////////////////////////////////////////////////////////
/////////////////////////// END HERE /////////////////////////////
//////////////////////////////////////////////////////////////////
}
Button createButton(final String buttonLabel) {
final Button button = new Button(this.getActivity());
button.setLayoutParams(this.buttonConstraintLayout);
button.setText(buttonLabel);
button.setTextSize(this.buttonTextSize);
button.setId(View.generateViewId());
return button;
}
}
So below are the expected output and the reality:
You should execute constraintSet.clone(layout) after layout.addView(button1).
BTW,you can use Layout Inspector to check your layout running on the device,it shows a View Tree and you can find where is your buttons gone.
Update:Change your button.setLayoutParams(this.buttonConstraintLayout); to button.setLayoutParams(new ConstraintLayout.LayoutParams(220, 150)); in createButton,do not share the View's LayoutParams object.
The problem here is that I shared the LayoutParams for every button creation, that is why it didnt work.
Inside the createButton method, it works if I changed to this
Button createButton(final String buttonLabel) {
final Button button = new Button(this.getActivity());
button.setLayoutParams(new ConstraintLayout.LayoutParams(220, 150);
button.setText(buttonLabel);
button.setTextSize(this.buttonTextSize);
button.setId(View.generateViewId());
return button;
}
I want to set textview height and width programmatically which support all screen sizes.
Here is my activity.xml in which I have two LinearLayout. In the second layout I put multiple textview dynamically more than 10 or 12.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="horizontal"
tools:context="com.example.sagargajera.setballs.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="6">
</LinearLayout>
<LinearLayout
android:id="#+id/layout_balls"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:orientation="horizontal"
android:background="#3F51B5"
android:layout_weight="1">
<!--Here I tried to put layout and inside this layout I want to put textview. -->
</LinearLayout>
</LinearLayout>
Here is my MainActivity.java in which i tried to create layout and textview according to my loop.
package com.example.sagargajera.setballs;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout=(LinearLayout)findViewById(R.id.layout_balls);
// DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
// int width = displayMetrics.widthPixels;
// int height = displayMetrics.heightPixels;
for(int i=1;i<=12;i++){
// LinearLayout.LayoutParams param1=new LinearLayout.LayoutParams(width, (height/7));
LinearLayout.LayoutParams param1=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
LinearLayout layout_1=new LinearLayout(this);
layout_1.setOrientation(LinearLayout.HORIZONTAL);
layout_1.setLayoutParams(param1);
layout_1.setGravity(Gravity.CENTER);
layout_1.setBackgroundColor(Color.parseColor("#f32f32"));
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20,20);
param1.weight=1;
TextView tv=new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setLayoutParams(params);
tv.setText("" + i);
tv.setBackgroundDrawable(getResources().getDrawable(R.drawable.cicle));
tv.setTextColor(Color.parseColor("#FFFFFF"));
layout_1.addView(tv);
layout.addView(layout_1);
}
}
}
Here is my screenshot:
Above code which create layout as well as textview. A Textview is set inside the dynamic layout and dynamic layout is set inside my xml specific layout namely layout_balls.
The problem is when I run this app in small device it give me perfect output but when I run this app in large devices or tablet output was unexpected.
You can get the device density like this:
float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20 * scaledDensity,20 * scaledDensity);
param1.weight=1;
There is some little bit change on below post.
float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20 * scaledDensity,20 * scaledDensity);
param1.weight=1;
The only change is we need to convert float into int so use below:
float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20 * (int)scaledDensity,20 * (int)scaledDensity);
param1.weight=1;//This is the correct answer.
I have a basic app working, but now I am focusing on formatting the app and running into difficulty laying things out as I want them.
I have a list of images that the user can switch from one to the next with using a next button. Both the images and the next button are added programmatically to the page (I clear out anything in the layout, and then add the ImageView and Button). Now, instead of laying them out one on top of the other, I am trying to lay them out next to each other, so the image will take up most of the space, and then the next button will be to its right.
Looking through the documentation I was leaning towards using a RelativeLayout to accomplish this. However, I ran into some questions while using RelativeLayouts programmatically.
Activity.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:orientation="vertical"
android:id="#+id/activity_training_package_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.xxx.PackageActivity"
tools:ignore="MergeRootFrame">
</RelativeLayout>
</ScrollView>
Attempt to programmatically add the button:
public void addNextButton(final int currentFile, final RelativeLayout layout) {
Button next = new Button(this);
next.setWidth(100);
int id = layout.getChildAt(0).getId(); // the image is the only thing there
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.RIGHT_OF, id);
next.setLayoutParams(lay);
next.setText("NEXT >>");
next.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
showNextFile(currentFile, layout);
}
//layout.setLayoutParams(lay);
layout.addView(next);
...
I am just wondering which LayoutParams I am supposed to be setting for this, the LayoutParams of the layout, or of the view? When I try to set it to the layout, I get a cast exception (it is expecting a FrameLayout.LayoutParams, not a RelativeLayout.LayoutParams for some reason).
Could someone please point me in the right direction to figure out how layouts are used? I cannot seem to find resources that explain which LayoutParams I should be setting.
TL;DR How do you use RelativeLayouts and LayoutParams programmatically?
The simplest solution is to use the HorizontalSrollView with a LinearLayout.
activity_test.xml
<HorizontalScrollView 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">
<LinearLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" />
</HorizontalScrollView>
TestActivity.java
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout container = (LinearLayout)findViewById(R.id.container);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
String next = getResources().getString(R.string.next);
for(int i=0;i<5;i++){
ImageView imgView = new ImageView(mActivity);
imgView.setLayoutParams(params);
imgView.setImageResource(R.drawable.photo);
container.addView(imgView);
Button button = new Button(mActivity);
button.setLayoutParams(params);
button.setText(next);
container.addView(button);
}
}
}
set Linear Layout weight Property in your XML file and assign weight to the imageview and button
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="1" />
MainActivity.java
package com.example.stackoverflowdemos;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout container = (LinearLayout)findViewById(R.id.container);
//Dynamically generate imageview and button
ImageView imgView = new ImageView(this);
imgView.setLayoutParams(new TableLayout.LayoutParams(0, LayoutParams.FILL_PARENT, .2f)); //set imageview height and width using weight
imgView.setImageResource(R.drawable.ic_launcher);
container.addView(imgView);
Button button = new Button(this);
button.setLayoutParams(new TableLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, .8f)); //set Button height and width using weight
button.setText("next");
container.addView(button);
}
}
I'm trying to get linearlayout's size.
I tested as following code.
But I just get value -1 (linearlayout's width).
How to get correct size?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
/>
</LinearLayout>
package com.example.LayoutSize;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mainLayout);
ViewGroup.LayoutParams layoutParams = linearLayout.getLayoutParams();
System.out.printf("linearLayout width : %d", layoutParams.width);
}
}
The reason you get -1 is because the width parameter is set to MatchParent which has a value -1.
To get the size of a layout, your should be using getWidth() or getMeasuredWidth() methods. However, these methods won't give you a meaningful answer until the view has been measured. Read about how android draws views here.
You can get the correct size, by overriding the onWindowFocusChanged() as mentioned in this thread.
Alternatively, (hacky solution), you could do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mainLayout);
linearLayout.post(new Runnable() {
#Override
public void run() {
System.out.printf("linearLayout width : %d", linearLayout.getMeasuredWidth());
}
});
}
public static final int MATCH_PARENT = -1;