EditText width not increasing at runtime - android

This is my view
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:elevation="4dp"
android:background="#android:color/white">
<EditText
android:layout_width="220dp"
android:id="#+id/defaultfragmentquicktask"
android:layout_height="match_parent"
android:hint="Quick Task"
android:layout_marginLeft="5dp"
android:layout_gravity="bottom"
android:background="#android:color/white"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/voicerecognition"
android:id="#+id/catalogactivityvoicerecognition"
android:background="#android:color/white"
android:layout_gravity="bottom"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/defaultfragmentcamera"
android:src="#mipmap/camera"
android:background="#android:color/white"
android:layout_gravity="bottom"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/defaultfragmentdrawingbrush"
android:background="#android:color/white"
android:src="#mipmap/drawingbrush"
android:layout_gravity="bottom"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/defaultfragmentsavebutton"
android:background="#android:color/white"
android:visibility="gone"
android:src="#mipmap/defaulfragmentsave"
android:layout_gravity="right"
/>
</LinearLayout>
When edittext gets focus i am setting camera,voice recognition and drawingbrush image's visibilty to gone and save image button's visibilty to visible.I wanted to move save button to the extreme right but it is not moving and i know it can be done using RelativeLayout but i dont want to do that,so i am incresing edittext width when it gets focus.
i used the following to increase its width but nothing works
edittext.getLayoutParams().width=32;
edittext.setWidth(32); and edittext.setEms(50);
Please help me to move savebutton to extreme right using LinearLayout, i already tried gravity="right",it doesnt work or let me know how to increase edittext width at runtime?
View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
voiceRecognitionButton.setVisibility(View.GONE);
camerButton.setVisibility(View.GONE);
drawingbrush.setVisibility(View.GONE);
save.setVisibility(View.VISIBLE);
quickTaskEditText.setWidth(330);
} else {
voiceRecognitionButton.setVisibility(View.VISIBLE);
camerButton.setVisibility(View.VISIBLE);
drawingbrush.setVisibility(View.VISIBLE);
save.setVisibility(View.GONE);
quickTaskEditText.setWidth(220);
}
}
};
inOnCreateView of my fragment
quickTaskEditText = (EditText) view.findViewById(R.id.defaultfragmentquicktask);
quickTaskEditText.setOnFocusChangeListener(onFocusChangeListener);

You can set the width by using the following code:
final LayoutParams lparams = new LayoutParams(your width, your height); // Width , height edittext.setLayoutParams(lparams); return edittext;
If you want your edit text to move to extreme right you can add a rule
```
alignParentRight = true;
```
Hope this helps

You have to apply params back to your View.
LayoutParams param = yourView.getLayoutParams(); // Get existing params
param.width = 330;
yourView.setLayoutParams(param); // Apply updated params

In if (hasFocus) i am setting
voiceRecognitionButton.setVisibility(View.INVISIBLE);
camerButton.setVisibility(View.INVISIBLE);
instaed of GONE so that save image button appears at the extreme right

Related

Listener not working on linearlayout which contain Edittext

I have an EditText contained in a LinearLayout. For My project I have set an OnclickListener on the LinearLayout. When I launch application nothing happened on click to layout. Maybe It due to the EditText but need that OnCliclistener to be work on LenearLayout.
MyCode:
XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/lnlabelnameuniteholder"
android:id="#+id/choix_decategorie"
android:layout_marginTop="10dp" >
<EditText
android:text="#string/choix_de_categorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textSize="18sp"
android:textColor="#000" />
</LinearLayout>
CODE
maCategorie = (LinearLayout) findViewById(R.id.choix_decategorie);
maCategorie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
MyDB db = new MyDB(MyActivityCategorie.this);
ArrayList<Category> categoryList = db.getCategory();
ChoiceCategoryDialog categoryDialog = new ChoiceCategoryDialog(MyActivityCategorie.this, R.string.add_category, mCategoryTextView.getText().toString(), categoryList, MyActivityCategorie.this, true);
categoryDialog.show();
}
});
Thanks
In your code, you are referring to R.id.choix_decategorie (which is not shown in your code, is this a LinearLayout you want to set onClickListener on?
For a LinearLayout that has child elements (like yours does) - may need to prevent the child elements from receiving focus - you can set android:descendantFocusability="blocksDescendants" like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/choix_decategorie"
android:id="#+id/lnpriceholderlabel"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="10dp" >
For a good measure you may also set android:clickable="false" on each of the child elements. So your layout would look something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/choix_decategorie"
android:id="#+id/lnpriceholderlabel"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="10dp" >
<EditText
android:text="#string/choix_de_categorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:clickable="false"
android:textSize="18sp"
android:textColor="#000" />
</LinearLayout>
Give it a try and let us know if this works.
Try this example:
LinearLayout yourLayout = (LinearLayout) findViewById (R.id.yourID);
yourLayout.setOnClickListener(new View.OnClickListener() { // remember to use "View."
#Override
public void onClick(View v) {
Toast.makeText(this, "hello", Toast.LENGTH_LONG).show();
}
});
set LinearLayout attribute android:clickable="true"
if you have button or textview in layout set android:clickable="false" for all of them
You must have to set the LinearLayout clickable="true"
<LineartLayout....
android:clickable="true"...>
This will work
And you need to put some margins in EditText, because EditText comes upside on LinearLayout and space doesn't remain for LinearLayout to be clickable.
put
<EditText
android:layout_margin="5dp".../>
your click listener will work but your edit text hide the linear layout , if you remove the edit text it will work or you can set height and width not equal to linear layout.
As a workaround you could do something like this (kotlin code):
editText.inputType = InputType.TYPE_NULL
editText.setOnClickListener { linearLayout.performClick() }
In that case the editText won't pop up the keyboard and the linearLayout will receive the a click event.

Expand and collapse CardView

What is the proper way to expand a CardView?
Use an expandable list view with cardview
or even
You can use wrap content as height of cardview and use
textview inside it below title, so on click make the textview visible
and vice-versa.
but isn't it bad design ?
nope it isn't if you give some transition or animation when it's expanded
or collapsed
If you want to see some default transition then just write android:animateLayoutChanges="true" in parent layout.
If you are using CardViews inside a ListView or RecyclerView see my answer for recommended way of doing it:
RecyclerView expand/collapse items
If you are just using CardView then do this in your on onClickListener of cardview:
TransitionManager.beginDelayedTransition(cardview);
detailsView.setVisibility(View.VISIBLE);
By default keep the visibility of your detailsView to be GONE in your xml.
I used a cardview and an expand section item_description in the cardview. For the expand part I created a TextView below the header section (LinearLayout/item_description_layout) and when the user clicks on the header layout an expand/collapse method is called. Here is the code in the cardview:
<LinearLayout
android:id="#+id/item_description_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="48dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:orientation="horizontal">
<TextView
android:id="#+id/item_description_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.9"
android:gravity="center_vertical"
android:text="#string/description"/>
<ImageView
android:id="#+id/item_description_img"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:layout_gravity="center_vertical|end"
app:srcCompat="#drawable/ic_expand_more_black_24dp"/>
</LinearLayout>
<TextView
android:id="#+id/item_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="16dp"
android:gravity="center_vertical"
android:visibility="gone"
tools:text="description goes here"/>
Here is the method that is called. I also added a ObjectAnimator to handle the expand/collapse animation of the block. This is a simple animation that uses the length of the description text.
void collapseExpandTextView() {
if (mItemDescription.getVisibility() == View.GONE) {
// it's collapsed - expand it
mItemDescription.setVisibility(View.VISIBLE);
mDescriptionImg.setImageResource(R.drawable.ic_expand_less_black_24dp);
} else {
// it's expanded - collapse it
mItemDescription.setVisibility(View.GONE);
mDescriptionImg.setImageResource(R.drawable.ic_expand_more_black_24dp);
}
ObjectAnimator animation = ObjectAnimator.ofInt(mItemDescription, "maxLines", mItemDescription.getMaxLines());
animation.setDuration(200).start();
}
just a line of code before setting visibility GONE/ VISIBLE can do:
TransitionManager.beginDelayedTransition([the rootView containing the cardView], new AutoTransition());
no need to use the animateLayoutChanges=true in XML (this way also simple, but collapse behaviour is bad)
I originally tried doing this by just adding an extra View to the bottom of my CardView and setting its visibility to gone (gone vs invisible):
tools:visibility="gone"
Then, I set the CardView height to Wrap Content and added an icon with an onClickListener that changed the extra View's visibility to visible.
The issue with this was that even when the visibility was set to gone, my CardView was still behaving like the extra View was there.
To get around this, I explicitly set the visibility of the extra View to gone in my onBindViewHolder method:
holder.defPieces.visibility = View.GONE
After this, the expandable functionality worked how I wanted it to. I wonder if there's some odd order of operations that goes on when inflating a View to display in a RecyclerView.
mView.Click +=(sender, e) =>{
LinearLayout temp = mView.FindViewById<LinearLayout>(Resource.Id.LinerCart);
if (vs == false) {
temp.Visibility = ViewStates.Gone;
vs = true;
} else {
temp.Visibility = ViewStates.Visible;
vs = false;
}
};
I got the solution ( single cardview expandable listview )
check this link
http://www.devexchanges.info/2016/08/expandingcollapsing-recyclerview-row_18.html
if you add down arrow icon
you just use my code
create xml
<RelativeLayout
android:id="#+id/layout_expand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/item_description_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:clickable="true"
android:onClick="toggle_contents"
android:padding="10dp"
android:text="Guest Conditions"
android:textColor="#color/hint_txt_color"
android:fontFamily="sans-serif-medium"
android:textStyle="normal"
android:paddingBottom="15dp"
android:textSize="16dp"/>
<ImageView
android:layout_alignParentRight="true"
android:paddingTop="4dp"
android:paddingRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_keyboard_arrow_down"/>
<!--content to hide/show -->
<TextView
android:id="#+id/item_description"
android:layout_below="#+id/item_description_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:padding="10dp"
android:text="#string/about_txt2"
android:textColor="#color/hint_txt_color"
android:fontFamily="sans-serif-medium"
android:textStyle="normal"
android:paddingBottom="15dp"
android:visibility="gone"
android:textSize="12dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
///////////////////////////////////////////////
Mainactivity.java
RelativeLayout layout_expand = (RelativeLayoutfindViewById(R.id.layout_expand);
item_description = (TextView) findViewById(R.id.item_description);
TextView item_description_title;
item_description_title = (TextView) findViewById(R.id.item_description_title);
item_description.setVisibility(View.GONE);
///////////////////////////////////////////////////////////////////
animationUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
animationDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
layout_expand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(item_description.isShown()){
item_description.setVisibility(View.GONE);
item_description.startAnimation(animationUp);
}
else{
item_description.setVisibility(View.VISIBLE);
item_description.startAnimation(animationDown);
}
}
});
item_description_title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(item_description.isShown()){
item_description.setVisibility(View.GONE);
item_description.startAnimation(animationUp);
}
else{
item_description.setVisibility(View.VISIBLE);
item_description.startAnimation(animationDown);
}
}
});

edittext does not grow downwards, text goes out of the visible area

Problem :- In case i have more than 1 line in my edittext, the previous line goes out of the visible area. I was expecting it to grow downwards so that all lines are visible.
Xml for my compound control which has this edittext:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/composite_control_layout"
android:background="#null"
tools:context="com.gp.app.professionalpa.layout.ListItemLayout" >
<Button android:id="#+id/compositeControlAddItem"
android:layout_height="30dip"
android:layout_width="30dip"
android:text="#string/plus"/>
<ImageView
android:id="#+id/compositeControlImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:contentDescription="#string/note_image"
android:scaleType="fitStart"
android:adjustViewBounds="true"
android:layout_toRightOf="#id/compositeControlAddItem"
android:layout_alignBottom="#id/compositeControlAddItem"
android:layout_alignParentLeft="true"
android:visibility="invisible"/>
<!-- <ImageButton
android:id="#+id/compositeControlBulletButton"
android:layout_width="35dip"
android:layout_height="#dimen/composite_control_height"
android:layout_marginRight="2dip"
android:contentDescription="#string/set_importance"
android:src="#drawable/ic_pin_black_bullet_point" /> -->
<EditText
android:id="#+id/compositeControlTextBox"
android:layout_width="250dip"
android:layout_height="#dimen/composite_control_height"
android:background="#null"
android:hint="#string/add_note"
android:layout_marginLeft="2dip"
android:layout_toRightOf="#+id/compositeControlAddItem"
android:layout_alignBottom="#+id/compositeControlAddItem"
android:inputType="textAutoCorrect|textMultiLine|textAutoComplete|textCapSentences"
android:maxLines="7"
android:maxLength="200"/>
<Button android:id="#+id/compositeControlDeleteItem"
android:layout_height="30dip"
android:layout_width="30dip"
android:layout_toRightOf="#+id/compositeControlTextBox"
android:layout_alignBottom="#+id/compositeControlTextBox"
android:text="#string/minus"/>
</RelativeLayout>
In my activity i am creating this compound contrl using the following code:-
private void addNewListItem()
{
ListViewItemLayout currentAddedListItem = new ListViewItemLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.alignWithParent = true;
if(lastAddedListItem != null)
{
layoutParams.addRule(RelativeLayout.BELOW, lastAddedListItem.getId());
}
currentAddedListItem.setLayoutParams(layoutParams);
activityLayout.addView(currentAddedListItem, listItems.size());
listItems.add(currentAddedListItem);
Button button = currentAddedListItem.getAddButton();
lastAddedListItem = currentAddedListItem;
}
Image showing that the previous lines becomes invisible in case edittext enters new line:-
Try editing the layout height of your edit text ... prob looks with your composite height used
strong textjust use android:paddingEnd = "your "

Soft keyboard hides half of EditText

I have a ListView and the last list item contains EditText:
<?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:orientation="vertical" >
<RelativeLayout
android:id="#+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/messageEditText"
android:layout_below="#+id/messageEditText"
android:layout_marginRight="14dp"
android:src="#drawable/test" />
<EditText
android:id="#+id/messageEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/sendImageButton"
android:ems="10"
android:gravity="top"
android:hint="#string/messageEditText"
android:inputType="textMultiLine"
android:minHeight="55dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" />
<ImageButton
android:id="#+id/sendImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/messageEditText"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/messageEditText"
android:adjustViewBounds="true"
android:maxHeight="55dp"
android:maxWidth="55dp"
android:padding="12dp"
android:scaleType="centerInside"
android:src="#drawable/sendmessage" />
</RelativeLayout>
</RelativeLayout>
Half of the EditText is hidden. I also can't scroll ListView. Any solution?
Solution was to set an android:softInputMode attribute to adjustResize in Manifest and put layout(not list item layout) inside a ScrollView.
I Had a similar issue, then i used android:windowSoftInputMode="adjustResize|stateHidden" as mentioned here.. It works very well for me..
From my early days as a Android Developer I struggled with the virtual keyboard. I am amazed that Android is still not giving an elegant and clear solution for this.
So here is the Workaround that will put this mess behind you. it will work without the ScrollView workaround or giving away your full screen flag.
Add this awesome Library to your gradle file:
compile'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:1.0.1'
Make sure your activity has the following keyboard settings:
android:windowSoftInputMode="adjustResize"
wrap your EditText with a vertical LinearLayout and add a View with Visibility of Gone:
<com.ylimitapp.ylimitadmin.views.NormalFontEditText
android:id="#+id/input_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapSentences|textMultiLine"
android:imeOptions="actionSend"
android:paddingStart="20dp"
android:paddingEnd="40dp"
android:paddingTop="10dp"
android:maxHeight="120dp"
android:adjustViewBounds= "true"
android:scrollHorizontally="false"
android:textColorHint="#7b7b7b"
android:hint="#string/type_your_message"
android:background="#drawable/msg_inputfield_bg"
android:textColor="#color/black_text_color"
android:textSize="15.33sp"
/>
<View
android:id="#+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="20dp"
android:visibility="gone"
/>
</LinearLayout>
calculate the screen size so you can calculate the height of the view that pushes the EditText:
private void storeScreenHeightForKeyboardHeightCalculations() {
Rect r = new Rect();
View rootview = getActivity().getWindow().getDecorView();
rootview.getWindowVisibleDisplayFrame(r);
mOriginalScreenHeight = r.height();
Rect rectangle = new Rect();
Window window = getActivity().getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop =
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;
if (titleBarHeight == 0) {
mOriginalScreenHeight -= (24 * Utils.getDensity(getContext()));
}
}
Add a Listener for keyboard open and close event and then set the height of the View below the EditText on runtime so we will set the height properly on any device and custom keyboards. then simply make it Visible when the Keyboard is Open:
private void addkeyBoardlistener() {
KeyboardVisibilityEvent.setEventListener(
getActivity(),
new KeyboardVisibilityEventListener() {
#Override
public void onVisibilityChanged(boolean isOpen) {
if (isOpen) {
Rect r = new Rect();
View rootview = getActivity().getWindow().getDecorView(); // this = activity
rootview.getWindowVisibleDisplayFrame(r);
int keyboardHeight = (mOriginalScreenHeight - r.height());
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) keyboard_view.getLayoutParams();
params.height = (int) ((keyboardHeight + 5 * Utils.getDensity(getContext())));
keyboard_view.setLayoutParams(params);
keyboard_view.setVisibility(View.VISIBLE);
} else {
keyboard_view.setVisibility(View.GONE);
}
}
});
}
This is the result:
Disable the keyboard on window startup
this .getWindow().setSoftInputMode(WindowManager.LayoutParams. SOFT_INPUT_STATE_ALWAYS_HIDDEN );
Set an android:softInputMode attribute for your Activity element in your Manifest.
See the Manifest documentation for a full list of valid values and their effect. The ones of particular interest to you will likely be adjustPan and adjustResize.
Add this in the manifest like so:
<activity
android:name="your_activity_name"
android:windowSoftInputMode="adjustResize"
android:theme="#style/Theme.AppCompat.DayNight.NoActionBar" />
Then add paddingBottom="20dp" in your layout and editText.

Get position of the view

I have a Relative layout with title centered and cancel button right aligned.
I want to check if the title overlaps with the cancel button and if so, i will have shift the title left based of how much it overlaps.
This is how my xml looks:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.app.mobile"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="#dimen/actionBarHeight"
android:layout_alignParentTop="true"
android:id="#+id/actionbar"
android:background="#F8F8F8">
<com.app.mobile.subview.CustomButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/cancel_btn"
android:text="Cancel"
app:typeface="fonts/HelveticaNeue"
app:customStyle="Regular"
android:textSize="#dimen/titleTextSize"
android:textColor="#378BFB"
android:background="#android:color/transparent"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:visibility="gone"/>
<com.app.mobile.subview.CustomButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/share_btn"
app:typeface="fonts/HelveticaNeue"
app:customStyle="Regular"
android:textColor="#378BFB"
android:visibility="gone"/>
<com.app.mobile.subview.CustomTextView
android:id="#+id/title"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:typeface="fonts/HelveticaNeue"
app:customStyle="Medium"
android:textSize="#dimen/titleTextSize"
android:textColor="#000"
android:text="Test Title"/>
</RelativeLayout>
And I'm trying to get the positions like below
float xPos = screenTitle.getX();
float titleEnd = xPos + screenTitle.getWidth();
xPos = cancelButton.getX();
if(titleEnd > xPos){
Log.e("Title","Title overlaps cancel button");
}
cancelButton.getX() is returning me 0.0 whereas title is returning correct value.
1.This is how the layout is with small title
http://i.stack.imgur.com/3TFdg.jpg
it depends on where in your Java code you're attempting to get the value of getX()
If Android has not already completed drawing the entire layout, cancelButton has not been drawn and the X is 0.0.
I've found that getting the value in onCreate() or onCreateView() is very easy with a post and runnable
cancelButton.post( new Runnable() {
#Override
public void run() {
float x = cancelButton.getX();
}
});
this ensures the button has been fully drawn before you attempt to use the value

Categories

Resources