different values in onProgressChanged() and onStopTrackingTouch in SeekBar - android

I'm dynamically modifying height of the button based on seekbar progress.
Now I want to retreive height of the button. So I used btn1.getHeight() in onProgressChanged() method of SeekBar.
But it gives incorrect/old set of values for button height.
Instead, if I used btn1.getHeight() in onStopTrackingTouch(), I'm getting correct values.
This simply means , when I try to retreive height of the button in onProgressChanged() , UI changes are visible on screen but yet not registered wchich results into getting incorrect/old values.
How to get correct values in onProgressChanged() ?
Is there other way round to do this? Any help appreciated.
Edit
#Luksprog : I made following changes :
<SeekBar
android:id="#+id/seekBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:maxHeight="9dp"
android:minHeight="9dp"
android:padding="10dp"
android:max="100"
android:progressDrawable="#drawable/seekbar_progress"
android:thumb="#drawable/shine_btn" />
<RelativeLayout
android:id="#+id/Graph01"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_below="#id/seekBar"
android:layout_marginTop="50dp"
android:gravity="bottom" >
<Button
android:id="#+id/btnGraph01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#99f" />
<com.example.calci.views.MyTextView
android:id="#+id/tvGraph01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/btnGraph01" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/Graph02"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_below="#id/seekBar"
android:layout_toRightOf="#id/Graph01"
android:layout_marginTop="50dp"
android:gravity="bottom" >
<Button
android:id="#+id/btnGraph02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#99f" />
<com.example.calci.views.MyTextView
android:id="#+id/tvGraph02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/btnGraph02" />
</RelativeLayout>
</RelativeLayout>
and in activity
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(20,
2 * progress * 1);
lp2.setMargins(55, 0, 0, 0);
btnGraph01.setLayoutParams(lp2);
RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(20,
RelativeLayout.LayoutParams.WRAP_CONTENT));
lp3.setMargins(55, 0, 0, 30);
tvGraph01.setLayoutParams(lp3);
tvGraph01.setTextSize(6);
tvGraph01.setGravity(Gravity.CENTER);
But TextView is getting displayed inside button... Why it is so?
PLEASE CORRECT ME IF I"M WRONG...

First I'm guessing this is related to your previous question. I wouldn't use weights, instead I would simply use a RelativeLayout with the Buttons attached to the bottom, this way the Buttons will increase in height upwards. Like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parent" >
<SeekBar
android:id="#+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:max="200"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#99cc00"
android:text="Button"
android:onClick="test" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/button1"
android:background="#0077cc"
android:text="Button" />
</RelativeLayout>
Second, I don't see why you can't get the desired height. With my code above by using button.getHeight() you'll get the previous height store(as the new height will not be stored until you get out of onProgressChanged). But this shouldn't stop you from getting the new height because you are actually calculating that value yourself with :
progress * 10
Code:
#Override
public void onProgressChanged(SeekBar seekBar, final int progress,
boolean fromUser) {
LayoutParams lp = findViewById(R.id.button1).getLayoutParams();
lp.width = 50;
lp.height = progress * 10;
findViewById(R.id.button1).setLayoutParams(lp);
Log.e("XZX", "Progress : " + progress + " |Old height : "
+ findViewById(R.id.button1).getHeight()
+ " |New height : " + (progress * 10));
}
Edit:
Your layout file is wrong:
<RelativeLayout
android:id="#+id/Graph01"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_below="#id/seekBar"
android:layout_marginTop="50dp" >
<Button
android:id="#+id/btnGraph01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#99f" />
<com.example.calci.views.MyTextView
android:id="#+id/tvGraph01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/btnGraph01" />
</RelativeLayout>
Now in code there is no need to modify the LayoutParams of tvGraph01, it will follow the Button as it grows/shrinks, also get the current LayoutParams of the Button and modify it instead of creating new ones again and again:
RelativeLayout.LayoutParams lp2 = (RelativeLayout.LayoutParams)btnGraph01.getLayoutParams();
lp2.width = 20;
lp2.height = 2 * progress * 1;
lp2.setMargins(55, 0, 0, 0); // <= do you really need the margin? what is its purpose?
btnGraph01.setLayoutParams(lp2);
tvGraph01.setTextSize(6);

Related

Why are programmatically set weights for Android linear layout columns not working correctly?

I am attempting to create a key for an above pie chart within this Linear layout. However, the weights aren't working properly, and the three columns are split up equally. Does anyone know what may be causing this? Thanks!
Layout XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tableLayout1"
android:orientation="vertical"
android:layout_centerHorizontal="true">
<TableRow android:gravity="center"
android:id="#+id/tableRow"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:weightSum="4">
<ImageButton
android:layout_width="0dp"
android:layout_height="100dp"
android:id="#+id/aiButton"
android:layout_gravity="center_horizontal"
android:src="#drawable/barchartwhite"
android:background="#CCAF00"
android:layout_weight="1"
android:scaleType="fitCenter"
/>
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
android:text="TP Activity"
android:id="#+id/textView2"
android:layout_gravity="center|center_vertical"
android:textSize="40sp"
android:background="#CCAF00"
android:layout_weight="3"
android:layout_alignParentTop="true"
android:textColor="#FFFFFF"
android:layout_centerHorizontal="true"
android:gravity="center_vertical" />
</TableRow>
<TableRow android:gravity="center"
android:id="#+id/tableRowSub"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Percentage of Activity per TP"
android:id="#+id/textViewSub"
android:layout_gravity="center"
android:textSize="16sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dip">
<com.shannonsystemsllc.ediconnect.PieChartView
xmlns:chart="http://schemas.android.com/apk/res-auto"
android:id="#+id/piechart"
android:paddingRight="12dp"
android:paddingLeft="12dp"
android:layout_width="300sp"
android:layout_height="300dp"/>
</TableRow>
</LinearLayout>
Code for creating key:
for (int i = 0; i < customers.size(); i++) {
int c[] = {Color.parseColor("#FF4B66"),Color.parseColor("#00A9AC"),Color.parseColor("#70A200"),Color.parseColor("#FAB448"),Color.parseColor("#BFBFBF"),Color.GREEN,custColor,Color.CYAN,Color.BLUE};
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
TableLayout.LayoutParams tl = new TableLayout.LayoutParams();
final float scale = getActivity().getResources().getDisplayMetrics().density;
int pixels = (int) (30 * scale + 0.5f); //set pixels to dp
TableRow row = new TableRow(activity);
row.setWeightSum(9);
//first row
tl.weight = 1;
lp.weight = 6;
lp.height = pixels;
row.setLayoutParams(lp);
TextView tv1a = new TextView(getActivity());
tv1a.setTextSize(22);
tv1a.setText(customers.get(i));
tv1a.setLayoutParams(lp);
tv1a.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
TextView tv1b = new TextView(getActivity());
tv1b.setTextSize(24);
Float perFloat = ((float)pieChartValues[i]/dataTotal*100);
remainderTotal = remainderTotal - perFloat.intValue();
String percentage;
if (perFloat.intValue() == 0)
{
percentage = ("1%");
}
else
{
percentage = (perFloat.intValue() + "%");
}
lp.weight = 2;
tv1b.setText(percentage);
tv1b.setLayoutParams(lp);
tv1b.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
lp.weight = 1;
ImageView tv1c = new ImageView(getActivity());
tv1c.setLayoutParams(lp);
tv1c.setBackgroundColor(c[i]);
row.setWeightSum(3);
row.addView(tv1b);
row.addView(tv1c);
row.addView(tv1a);
row.setId(i);
ll.addView(row);
}
Screenshot:
Set the weight of the contents inside each row as follows:
- For the percentage TextView, set the weight to 3.
- For the ImageView, set the weight to 8.
- For the name Textview, set the weight to 7.
This will result to the entire row to look slightly offset from the centre. Make sure all three views are set to match parent width.
Once you set weights for the views call:
yourLinearLayout.invalidate()
Try this out and see if it works.

Dynamically created ImageView not respecting RelativeLayout rules

I'm adding ImageViews dynamically to a RelativeLayout. I'm trying to align each image to the right of the last image. The initial image aligns correctly but when I set the next image to align to the right side of the last image, it displays at the top left of the screen, disregarding the alignment rules.
Hierarchy View in the Android Device Monitor is indicating that the IDs are correctly getting set and the ImageView is recognizing that it's supposed to align to the id of the last ImageView.
List<String> image_urls = rally.getTransportationImgs();
List<String> methods = rally.getTransportationStrs();
ArrayList<ImageView> IMGS = new ArrayList<ImageView>();
for(int i = 0; i < methods.size(); i++) {
String method = methods.get(i);
for(int j = 0; j < image_urls.size(); j++) {
String img_url = image_urls.get(j);
if(img_url.toLowerCase().contains(method.toLowerCase()) == true) {
ImageView methodImage = new ImageView(this);
methodImage.setId(j + 100);
IMGS.add(methodImage);
RelativeLayout detailsLayout = (RelativeLayout) findViewById(R.id.details_layout);
TextView transportationText = (TextView) detailsLayout.findViewById(R.id.transportationText);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (IMGS.size() > 1) {
params.addRule(RelativeLayout.RIGHT_OF, IMGS.get(IMGS.size() - 1).getId());
params.addRule(RelativeLayout.ALIGN_TOP, IMGS.get(IMGS.size() - 1).getId());
params.addRule(RelativeLayout.END_OF, IMGS.get(IMGS.size() - 1).getId());
params.height = 5000;
params.width = 65;
} else {
params.addRule(RelativeLayout.RIGHT_OF, transportationText.getId());
params.addRule(RelativeLayout.ALIGN_TOP, transportationText.getId());
params.addRule(RelativeLayout.END_OF, transportationText.getId());
params.height = 65;
params.width = 65;
}
methodImage.setLayoutParams(params);
detailsLayout.addView(methodImage);
Picasso.with(getApplicationContext()).load(img_url).fit().centerCrop().placeholder(R.drawable.ic_launcher).into(methodImage);
}
}
}
XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:orientation="vertical"
android:weightSum="1"
android:id="#+id/details_layout">
<ImageView
android:layout_width="match_parent"
android:layout_height="275dp"
android:id="#+id/image" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/name2"
android:layout_below="#id/image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/date2"
android:layout_below="#id/name2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/creator_name2"
android:layout_below="#id/date2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/venues"
android:layout_below="#id/creator_name2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/transportationText"
android:layout_below="#id/venues"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/categories"
android:layout_below="#id/transportationText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
For debugging purposes, I set the image height obnoxiously large. The gray is part of an image that is supposed to go to the right side of the ImageView that correctly aligns next to "Transportation:"
The problem is with this line:
methodImage.setId(j + 100);
If there's more than one Object in methods array there will be conflict in ImageViews ids. You're generating your ids based on only the index of the inner array (j).
For example if:
There are two items in methods
There are three items in image_urls
Your ImageViews would have ids: [101, 102, 103, 101, 102, 103]
Therefore you will add multiple Views with the same ids.
To solve the issue either generate ids based on both i and j, or generate them in more conventional way.
EDIT:
Well, the other issue is that you're trying to set RelativeLayout Rules that are aligning a particular View to the same View.
Take a look at your code:
You're creating an ImageView, setting its id and immediately adding it to the IMGS list, with this:
IMGS.add(methodImage);
And then you're trying to set your RelativeLayout Rules, that should align your ImageView, but you're referencing to the last item of the IMGS list, with:
IMGS.get(IMGS.size() - 1)
And the last item in the array is the same View, you're trying to set up!
To solve the issue either:
a) Move IMGS.add(methodImage); after the if-else, and change the if condition to if (IMGS.size() > 0) {
or
b) Change the if body to:
params.addRule(RelativeLayout.RIGHT_OF, IMGS.get(IMGS.size() - 2).getId());
params.addRule(RelativeLayout.ALIGN_TOP, IMGS.get(IMGS.size() - 2).getId());
params.addRule(RelativeLayout.END_OF, IMGS.get(IMGS.size() - 2).getId());

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 "

How write text on image in RelativeLayout while it always keeps the same position (even if image is resized)?

As the title says. I need to write some text at ImageView. For that I was advised to use RelativeLayout. BUT there is not possible to use alignParentBottom (or maybe it is but I cant use margin then).
Problem is: I need to keep text at exactly some part of image even though it is resized or it is shown on different screen resolution etc. Is that possible?
CODE:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:gravity="center" >
<!-- Speaker image -->
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="#ffffff"
android:src="#drawable/authorimg" />
<!-- Time -->
<TextView
android:id="#+id/timeTVid"
style="#style/TextWithShadow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="2:59" />
</RelativeLayout>
I want the TextView to be somewhere in the middle but not exactly there.
EDIT: After first response tried (not working):
Horizontal position http://i59.tinypic.com/o76omg.png
Vertical position http://i59.tinypic.com/20tf7ky.png
I want to have "Your text" to be at the same position at the picture.
I found out that this is the solution. It's a shame that XML in android does not support percentage padding/margin so you have to do it programmatically as shown below. I just got the image width, width of frame and calculated it so the text is always on the same place on the image.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int paddingLeft;
frameHeight = imgFrameLayout.getHeight();
frameWidth = imgFrameLayout.getWidth();
imgWidth = image.getWidth();
imgHeight = image.getHeight();
// getting the difference of img width and frame width
int diff = frameWidth - imgWidth;
// if frame is bigger than image then set additional value to padding
// 20% image width + (diff/2)
if (diff > 0) {
paddingLeft = imgWidth / 100 * 20 + diff / 2;
}
// else set padding 20% of the image
else {
paddingLeft = imgWidth / 100 * 20;
}
timeTV.setPadding(paddingLeft, 0, 0, 0);
}
Use this example but it will only work for the Relativelayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<ImageView android:id="#+id/full_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/myImageView"
android:layout_alignTop="#+id/myImageView"
android:layout_alignRight="#+id/myImageView"
android:layout_alignBottom="#+id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Your Text"
android:textColor="#000000" />
Might want to try using a FrameLayout. In any event, set textview to match_parent (both ways)
use android:gravity="center" (not layout_gravity). now if you want to adjust the centered position to make it offset a bit, you can use paddingLeft, paddingTop, etc to adjust your center.
I think you should create two separate xml files. the first one for the image and 2nd one for the overlayed text. then in your activity class you should use LayoutInflater. I created an example and I hope it is what you are looking for.
JavaCode:
private ImageView imgView;
private TextView tv00, tv01, tv02, tv03;
private LayoutInflater mInflater = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_overlayed_with_text00);
imgView = (ImageView) findViewById(R.id.imgview);
tv00 = (TextView) findViewById(R.id.tv00);
tv01 = (TextView) findViewById(R.id.tv01);
tv02 = (TextView) findViewById(R.id.tv02);
tv03 = (TextView) findViewById(R.id.tv03);
//imgView.setImageBitmap(BitmapFactory.decodeFile("c:\\pic.jpg"));
imgView.setImageResource(R.drawable.pic);
mInflater = LayoutInflater.from(getApplicationContext());
View overView = mInflater.inflate(R.layout.textoverlay, null);
addContentView(overView, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
ImageviewXML:
<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="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.imageoverlayedwithtext00.ImageOverlayedWithText00$PlaceholderFragment" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imgview" />
TextOverlayXML:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:id="#+id/tv00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="text0"/>
<TextView
android:id="#+id/tv01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="text1"/>
<TextView
android:id="#+id/tv02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="text2"/>
<TextView
android:id="#+id/tv03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="text3"/>
</LinearLayout>

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