Android measuring an imageview - android

I need to measure the width of an ImageView, but when i measure it after setting
android:adjustViewBounds on the ImageView the measuredWidth is incorrect...
What am i doing wrong and how should i fix this?
My layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/lines2"
android:orientation="vertical"
android:weightSum="917">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="143">
<RelativeLayout
android:id="#+id/rectangleLayout"
android:background="#drawable/customborder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/circle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/circle"
android:src="#drawable/circle"
/>
</RelativeLayout>
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.Inflate (Resource.Layout.pager1Fragment, container, false);
rectangleLayout = view.FindViewById<ViewGroup> (Resource.Id.rectangleLayout);
circle = view.FindViewById<ImageView> (Resource.Id.circle);
circle.Measure ( Android.Views.View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified),
Android.Views.View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified));
Initialize ();
return view;
}
public void Initialize ()
{
var rectanglePadding = (int)((double)circle.MeasuredWidth / 2d);
//measuredWidth is wrong when adjustviewbounds is set
Console.WriteLine ("padding " + rectanglePadding);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent);
p.LeftMargin = rectanglePadding;
rectangleLayout.LayoutParameters = p;
}

Ok getting View asap when my fragment is created.
ImageView Example = (ImageView) parentview.findViewById(R.id.Example);
Example.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout() {
Example.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = Example.getWidth() <--- this gives you width.
}
});
Just convert your code to that and your set.

Related

Error when creating dynamic views

Im am trying to create a dynamic object with imageviews and textviews
First I created a XML file to see what parameters I need to have to produce what I need.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout_new_event"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="#color/eventColor"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linearLayoutEventText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/LinearLayoutEventIndicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView_event_from"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5sp"
android:layout_weight=".5"
android:background="#color/backgroundColor3"
android:scaleType="centerCrop"
app:srcCompat="#drawable/arrow_up_white" />
<ImageView
android:id="#+id/imageView_event_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="#color/backgroundColor2"
android:scaleType="centerCrop"
app:srcCompat="#drawable/arrow_down_white" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Below is the java code I am using to create the above when pressing a floating button. I know that maybe there is unnecessary hierarchy in the layout, but this is part of a larger but same layout. So before I settle this I cannot proceed.
public class tabTueActivity extends Fragment {
LinearLayout layoutNewEvent, linearLayoutEventText, linearLayoutEventVoyage, linearLayoutEventIndicator, eventLayout;
ImageView imageViewEventFrom, imageViewEventTo, imageViewSearch;
TextView textViewEventFrom, textViewEventTo;
FloatingActionButton createEvent;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_tue_frag, container, false);
return rootView;
}
#Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
eventLayout = (LinearLayout) rootView.findViewById(R.id.event_layout);
createEvent = (FloatingActionButton) rootView.findViewById(R.id.add_event_button);
layoutNewEvent = (LinearLayout) rootView.findViewById(R.id.layout_new_event);
linearLayoutEventText = (LinearLayout) rootView.findViewById(R.id.linearLayoutEventText);
linearLayoutEventIndicator = (LinearLayout) rootView.findViewById(R.id.LinearLayoutEventIndicator);
imageViewEventFrom = (ImageView) rootView.findViewById(R.id.imageView_event_from);
imageViewEventTo = (ImageView) rootView.findViewById(R.id.imageView_event_to);
linearLayoutEventVoyage = (LinearLayout) rootView.findViewById(R.id.LinearLayoutEventVoyage);
textViewEventFrom = (TextView) rootView.findViewById(R.id.textView_event_from);
textViewEventTo = (TextView) rootView.findViewById(R.id.textView_event_to);
imageViewSearch = (ImageView) rootView.findViewById(R.id.imageView_search);
final LinearLayout.LayoutParams layoutNewEventParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutNewEventParams.setMargins(40, 40, 40, 40);
final LinearLayout.LayoutParams linearLayoutEventTextParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final LinearLayout.LayoutParams linearLayoutEventIndicatorParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
final LinearLayout.LayoutParams imageViewEventFromParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
imageViewEventFromParams.setMargins(0,0,0,10);
imageViewEventFromParams.weight = (float) 0.5;
final LinearLayout.LayoutParams imageViewEventToParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
imageViewEventToParams.weight = (float) 0.5;
createEvent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("EVENT BUTTON", "Create event button pressed.....");
layoutNewEvent.getLayoutParams().height = 140;
layoutNewEvent.setBackgroundResource(R.color.eventColor);
layoutNewEvent.setOrientation(LinearLayout.HORIZONTAL);
linearLayoutEventText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
linearLayoutEventText.setOrientation(LinearLayout.HORIZONTAL);
linearLayoutEventIndicator.setOrientation(LinearLayout.VERTICAL);
imageViewEventFrom.setBackgroundResource(R.color.backgroundColor3);
imageViewEventFrom.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageViewEventFrom.setImageResource(R.drawable.arrow_up_white);
imageViewEventTo.setBackgroundResource(R.color.backgroundColor2);
imageViewEventTo.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageViewEventTo.setImageResource(R.drawable.arrow_down_white);
linearLayoutEventIndicator.addView(imageViewEventTo, imageViewEventToParams);
linearLayoutEventIndicator.addView(imageViewEventFrom, imageViewEventFromParams);
linearLayoutEventText.addView(linearLayoutEventIndicator, linearLayoutEventTextParams);
layoutNewEvent.addView(linearLayoutEventText, layoutNewEventParams);
}
});
}
}
and the below is the error I'm having
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.widget.LinearLayout.getLayoutParams()' on a null object reference
The problem is probably in this line:
layoutNewEvent.getLayoutParams().height = 140;
Your reference to the the view layoutNewEvent is null and that is the cause for the NullPointerException when you are trying to invoke getLayoutParams().
To resolve this I suggest you double check (either by debugging or just by looking at the code) that you are actually have the view with id R.id.layout_new_event in your xml. I am not sure that the xml you are inflating tab_tue_frag.xml is actually the one you put in the question.

Add view in Android

I created a DrawCircle View Class to draw a Circle. I like to add this DrawCircle View Class to a fragment.
The DrawCircle View class is as follow.
public class DrawCircle extends View {
private Paint paint;
public DrawCircle(Context context) {
super(context);
// create the Paint and set its color
paint = new Paint();
paint.setColor(Color.WHITE);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
canvas.drawCircle(200, 200, 100, paint);
}
}
Inside my fragment, I have a Layout already inflated.
public class GripForce extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_grip_force, container, false);
final Button buttonAcce = (Button) v.findViewById(R.id.gripbutton);
buttonAcce.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (vibrate == false) {
vibrate = true;
buttonAcce.setText("VIBRATE STOP");
// Vibrate for 300 milliseconds
timer = new Timer();
myTimerTask = new MyTimerTask();
timer.schedule(myTimerTask, 1, 80000);
mVibrator.vibrate(100000);
} else {
timer.cancel();
timer.purge();
mVibrator.cancel();
vibrate = false;
buttonAcce.setText("VIBRATE");
}
}
});
mVibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
return v;
}
}
My question is how to get this DrawCirle View on top of the existing View.
Thanks in advance.
EDIT:
I set the View in the xml.
<FrameLayout 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"
tools:context="com.forcetest.GripForce">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|top">
<com.abbott.forcetest.DrawCircle
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/circleView"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_gravity="center_horizontal|bottom">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="VIBRATE"
android:id="#+id/gripbutton"
android:background="#e71919"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="20dp" />
</LinearLayout>
</FrameLayout>
Then in the onCreateView, the program crashed at the line
v = inflater.inflate(R.layout.fragment_grip_force, container, false);
What is worng?
EDIT2:
I changed my xml to
<FrameLayout 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"
tools:context="com.abbott.forcetest.GripForce"
android:id="#+id/gripLayout">
<!--<LinearLayout-->
<!--android:orientation="vertical"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_gravity="center_horizontal|top">-->
<!--<com.abbott.forcetest.DrawCircle-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:id="#+id/circleView"-->
<!--android:layout_weight="1"/>-->
<!--</LinearLayout>-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_gravity="center_horizontal|bottom">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="VIBRATE"
android:id="#+id/gripbutton"
android:background="#e71919"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="20dp" />
</LinearLayout>
</FrameLayout>
Then in the program,
final FrameLayout l_out = (FrameLayout) v.findViewById(R.id.gripLayout);
DrawCircle circleVIew = new DrawCircle(this.getContext());
l_out.addView(circleVIew);
It works now.
Just add it like you would add any other view dynamically.
Just create it -
DrawCircle circle = new DrawCircle(context);
Then add it to your your parent layout -
yourParentLayout.addView(circle);
Set any properties you want while adding the circle view to place it accordingly.
See here how to add views dynamically.
You are trying to self-define view. And now assuming that you already make everything related to the view drawing fine( I haven't check your code related to view drawing), and now what you need to do to place that view is just simply code it in your layout file.
Like this:
<com.example.yourprojectname.DrawCircle
android:layout_width=xxxx
android:layout_height=xxx
/>
Like what you do to TextView or Button, code it in layout xml file and remember to findViewById in you java code. It will appear.
And of course you could do it programmingly, try addView method in your code. But write it in xml file is a little bit easier, also you can define the position you want that view to show up.

Adjusting DialogFragment Width and Height

I have problem with DialogFragmnt's Width and Height. Here is my class representing DialogFragmetn:
public class RecipeAddDialogFragment extends DialogFragment {
private ArrayList<RecipeDialogItem> recipeDialogItems;
private RecipeAddDialogAdapter recipeDialogAdapter;
private String recipeUniqueId;
private CoordinatorLayout coordinatorLayout;
private RecipeAddDialogFragment recipeDialogFragment;
#Override
public void onStart() {
super.onStart();
if (getDialog() == null) {
return;
}
int dialogWidth = 600;
int dialogHeight = 300;
getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
getDialog().setTitle(getString(R.string.recipe_dialog_title));
}
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme_DialogFragment);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_fragment, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
recipeDialogItems = new ArrayList<>();
RecyclerView dialogRecyclerView = (RecyclerView) view.findViewById(
R.id.dialog_recycler_view);
recipeDialogAdapter = new RecipeAddDialogAdapter(getContext(), recipeDialogItems,
R.layout.recipe_dialog_item);
recipeDialogAdapter.setRuidClRdf(recipeUniqueId, coordinatorLayout, recipeDialogFragment);
dialogRecyclerView.setHasFixedSize(true);
dialogRecyclerView.setAdapter(recipeDialogAdapter);
dialogRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
fillRecipeDialogArray();
}
private void fillRecipeDialogArray() {
String name = getString(R.string.add_to_favourites);
int icon = R.drawable.ic_heart_48dp;;
RecipeDialogItem dialogItem = new RecipeDialogItem();
dialogItem.setRowIcon(icon);
dialogItem.setRowOption(name);
recipeDialogItems.add(dialogItem);
recipeDialogAdapter.notifyDataSetChanged();
}
public void setReferences(String recipeUniqueId, CoordinatorLayout coordinatorLayout,
RecipeAddDialogFragment recipeDialogFragment) {
this.recipeUniqueId = recipeUniqueId;
this.coordinatorLayout = coordinatorLayout;
this.recipeDialogFragment = recipeDialogFragment;
}
}
Here is .xml which I infalte in this DialogFragment:
<?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:gravity="center|left"
android:padding="16dp"
android:orientation="horizontal"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">
<!-- Option Icon -->
<ImageView
android:id="#+id/recipe_dialog_option_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:tint="#color/primary" />
<!-- Text Option -->
<TextView
android:id="#+id/recipe_dialog_option_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#color/primary_text" />
</LinearLayout>
The problem is that when I set it's size to 600 x 300 it is displayed fine in my 1280x720 device, but for example when my friend displays it on 1920x1080 resolution dialog is wrapped and only title is shown. List is wrapped and is not entire shown. Any idea how can I automaticly set it's size to fit every display and show entire dialog which is wrapped to it's content?
Edit
I have figured out to adjust the width of the DialogFragment to it's content like this:
getDialog().getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
getDialog().setTitle(getString(R.string.recipe_dialog_title));
However height is not working properly :/
In DialogFragment.onResume():
int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);
getDialog().getWindow().setLayout(width, height);
In the layout for the dialog:
android:layout_width="match_parent"
android:layout_height="match_parent"
Can take whole screen with:
getDialog().getWindow().setLayout(
getResources().getDisplayMetrics().widthPixels,
getResources().getDisplayMetrics().heightPixels
);
Hope that helps
Found the idea here How to set DialogFragment's width and height?

Dynamically change View Stub layout_height attribute?

I am using view stub in my application. I am inflating the view stub to the layout and it is working fine. Now if you see the dummy_layout.xml there I have given the android:layout_height=20dp. What I need to do is to change that view stub height from 20 dp to Wrap_Content on a button click. How can I achieve this?
My ultimate goal is this. I have a button in it. when i click on it for first time I need to show the full layout of stub. and again when I click on it for the second time I need to animate the stub to slide down and show only a part of it.
dummy_layout.xml
<RelativeLayout
android:id="#+id/topLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_watch_pic" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/dark_grey"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
<ViewStub
android:id="#+id/viewStub1"
android:layout="#layout/stub_layout"
android:layout_width="wrap_content"
**android:layout_height="20dp"**
android:layout_alignParentBottom="true"/>
</RelativeLayout>
And here is my class:
public class DummyFragment extends Fragment {
private View mRootView;
private Context mContext;
private Button mButton;
private boolean isVisible = true;
private RelativeLayout mParentLayout;
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.dummy_layout,
container, false);
mContext = getActivity();
mButton = (Button) mTopLayout.findViewById(R.id.button1);
final ViewStub stub = (ViewStub) mRootView.findViewById(R.id.viewStub1);
**final View inflated = stub.inflate();**
inflated.setVisibility(View.VISIBLE);
final View viewstublayout = mRootView.findViewById(R.id.viewStub1);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(isVisible)
{
isVisible=false;
inflated.setVisibility(View.VISIBLE);
}
else
{
isVisible=true;
inflated.setVisibility(View.INVISIBLE);
}
}
});
return mRootView;
}
}
You can change the width/height of a view by editing its LayoutParams. Example:
View stub = findViewById(R.id.viewStub1);
ViewGroup.LayoutParams lp = stub.getLayoutParams();
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
stub.setLayoutParams(lp);
The better question is why you want to do this? Maybe there's a better way of achieving your goal.
Try this
final ViewStub stub = (ViewStub) mRootView.findViewById(R.id.viewStub1);
View view = stub .inflate();
inside button click
LayoutParams params = (LayoutParams) view.getLayoutParams();
params.width = LayoutParams.WRAP_CONTENT;
view.setLayoutParams(params);
Try this and let me know if works well.
LayoutParams lp = (LayoutParams) (YourView).getLayoutParams();
lp.height = newheight;
YourView.setLayoutParams(lp);

view.getWidth() in Dialog not working

I want to create square buttons, but because I can't get the width by XML to assign it to button's height, I'm doing it by Java. So I do this:
View body = inflater.inflate(R.layout.picker_numeric, null);
Button up = (Button) body.findViewById(R.id.picker_numeric_up);
Log.d(TAG, "" + up.getWidth());
int height = up.getWidth();
up.setHeight(height);
........................
AlertDialog.Builder builder = new AlertDialog.Builder(activity)
.setTitle(title)
.setView(body)
........................
I used Log.d to confirm that getWidth() returns 0.
Anyone knows why this happens? I'm out of options.
EDIT:
After what MKJParekh and Simon said my code is this:
dialogContent.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/picker_numeric_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:baselineAligned="false"
android:padding="#dimen/padding_small"
android:weightSum="3"
tools:ignore="HardcodedText,UselessParent" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="#+id/picker_numeric_up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/padding_medium"
android:layout_marginRight="#dimen/padding_medium"
android:gravity="center"
android:text="+" />
<EditText
android:id="#+id/picker_numeric_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/padding_medium"
android:layout_marginRight="#dimen/padding_medium"
android:gravity="center"
android:inputType="number" />
<Button
android:id="#+id/picker_numeric_down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/padding_medium"
android:layout_marginRight="#dimen/padding_medium"
android:gravity="center"
android:text="-" />
</LinearLayout>
</LinearLayout>
Method in dialog's creator class:
public void equalDimens() {
up.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int height = up.getMeasuredWidth();
android.util.Log.d("dd", ""+height);
up.setLayoutParams(new LinearLayout.LayoutParams(height, height));
down.setLayoutParams(new LinearLayout.LayoutParams(height, height));
}
Then in Activity's Class:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createMyDialog();
LinearLayout mainLayout = (LinearLayout ) this.getLayoutInflater().inflate(R.layout.main, null);
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
myDialogClass.equalDimens();
}
});
setContentView(mainLayout);
}
Without calling myDialogClass.equalDimens() I obtain this:
And calling the method I obtain this:
You need to measure your view before you can get height and width of the view.
You can do it like this,here ll2 is your any view object
ll2.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int height = ll2.getMeasuredHeight();
int width = ll2.getMeasuredWidth();
And after getting the height and width you will need to set them as,
ll2.setLayoutParams(new LayoutParams(width, height));
You view is measured and inflated but not yet drawn in onCreate.
You can use (amongst other solutions):
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// get width here
}
If you can't use this, e.g. you have dialog boxes spawned from your activity, try a global layout listener.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// inflate your main layout here (use RelativeLayout or whatever your root ViewGroup type is
LinearLayout mainLayout = (LinearLayout ) this.getLayoutInflater().inflate(R.layout.main, null);
// set a global layout listener which will be called when the layout pass is completed and the view is drawn
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
// measure your views here
}
}
);
setContentView(mainLayout);
}

Categories

Resources