Get position of the view - android

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

Related

EditText width not increasing at runtime

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

Android views tate changes resets layout

I have a list with Swipe Actions on it using IOnTouchListener, as dragging occurs I am changing the Left of a LinearLayout to reveal a second LinearLayout behind it. That layout has two buttons, one is hidden. If the user does a "quick" swipe then I move the Left of the top view to a specific value and fire some code. This code changes the invisible button to be visible. When the ViewState of that button changes it makes the top view's Left snap back to 0 instead of staying where I place it. Any idea why it would do this or how to work around it.
The XML looks like... (But will change slightly based on the answer to another question I posted)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill_vertical"
android:background="#color/black"
android:id="#+id/Swipe"
>
<LinearLayout
android:id="#+id/LeftContentView"
android:layout_width="175dp"
android:layout_height="match_parent"
android:background="#color/yellow"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
>
<Button
android:id="#+id/ApproveButton"
android:layout_width="0dp"
android:layout_weight=".72"
android:layout_height="match_parent"
android:background="#2796C3"
android:text="Approve"
android:layout_alignParentLeft="true"
/>
<Button
android:id="#+id/ApproveUndoButton"
android:layout_width="0dp"
android:layout_weight=".28"
android:layout_height="match_parent"
android:background="#215681"
android:text="Undo"
android:layout_toRightOf="#id/ApproveButton"
/>
</LinearLayout>
<LinearLayout
android:layout_alignParentRight="true"
android:id="#+id/RightContentView"
android:layout_width="175dp"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="horizontal"
>
<Button
android:id="#+id/DenyButton"
android:layout_width="0dp"
android:layout_weight=".72"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="Deny"
/>
<Button
android:id="#+id/DenyUndoButton"
android:layout_width="0dp"
android:layout_weight=".28"
android:layout_height="match_parent"
android:background="#860000"
android:text="Undo"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/TopContentView"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1F1F1F">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="match_parent" >
<ImageView
android:id="#+id/UnreadImage"
android:layout_height="match_parent"
android:layout_width="7dp"
android:src="#drawable/vertical_blue_bar"
android:background="#2796C3"/>
<LinearLayout
android:id="#+id/ListText"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:padding="12dp">
<TextView
android:id="#+id/Text_Line1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dip"
/>
<TextView
android:id="#+id/Text_Line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dip"
/>
<TextView
android:id="#+id/Text_Line3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="11dip"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
I am not using Java, I am using Xamarin with C#. Here are the relevant pieces of code...
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
OriginalTouchX = Math.Ceiling(e.RawX);
index = e.ActionIndex;
pointerId = e.GetPointerId(index);
VelocityTracker.AddMovement(e);
break;
case case MotionEventActions.Move:
VelocityTracker.AddMovement(e);
ChangeX(newX);
VelocityTracker.ComputeCurrentVelocity(1);
float velocity = Math.Abs(VelocityTracker.GetXVelocity(pointerId));
if (velocity > SlowDrag && Math.Abs(distanceMoved) > (DragThreshold / 3))
{
QuickApprove();
}
break;
}
}
void QuickApprove()
{
ToggleUndoButton(true, true);
WaitingForUndo = true;
ChangeX(DragThreshold);
this.ApproveTimer.Start(true);
}
private void ToggleUndoButton(bool ShowUndo, bool LeftSide)
{
this.ApproveUndoButton.Visibility = ViewStates.Visible;
this.ApproveButton.Text = "Approving...";
}
private void ChangeX(int newX)
{
int width = this.TopContentView.Right - this.TopContentView.Left;
this.TopContentView.Left = newX;
this.TopContentView.Right = this.TopContentView.Left + width;
}
If in the ToggleUndoButton method I comment out the line
this.ApproveUndoButton.Visibility = ViewStates.Visible;
Everything works fine. The Left of TopContentView changes to be equal to my DragThreshold, the text changes to Approving... the timer starts, It stays this way for 2 seconds, then the code in my timer tick fires and the TopContentView is moved back to a Left of 0. If I leave in the visibility change then the TopContentView is immediately moved back to 0 instead of waiting until the timer is done.

how to overlap a layout with image view and text over the other layout as a stack/pile

I am in a weird situation in my existing project. In my project i want to inflate one layout over the other as a stack dynamically. At present in my project i managed to show the layouts one below the other vertically by appending them, this approach has been chosen by me because even though i am trying to show it as stack only one image is appearing at the front during runtime instead of a bunch. In order to understand my problem please go through the following image links and code
desired result
acquired_result_when_tried_to_acheive_desired_result
approached way
Parent layout:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imagegallery"
android:orientation="vertical"></LinearLayout>
child layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/relativeImage"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/rl1"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp" >
<RelativeLayout
android:id="#+id/parent"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:orientation="vertical" >
<Button
android:id="#+id/left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/close" />
<Button
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/right" />
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:adjustViewBounds="true"
android:background="#drawable/deelpizza"
android:paddingBottom="15dp"
android:paddingEnd="0dp"
android:paddingStart="0dp"
android:scaleType="fitCenter" />
<RelativeLayout
android:id="#+id/left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/image"
android:background="#drawable/text_bg" >
<ImageView
android:id="#+id/imageprew"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:background="#drawable/icon_3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageprew"
android:text="This is an exclusive Deel good for today only clime this deel before its close!"
android:textColor="#FFFFFF" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_below="#+id/imageprew" >
</RelativeLayout>
</RelativeLayout>
<!--
<Button
android:id="#+id/left"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:background="#drawable/text_bg"
android:layout_alignTop="#+id/image"
/>
-->
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeImage"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/rectangle" >
<TextView
android:id="#+id/locationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/descText"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:text="woodfire pizza, Los Gatos CA\nLimited time offer, redeemable today only."
android:textColor="#000000" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="#+id/locationText" />
<TextView
android:id="#+id/descText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:text="1 large, 2 topping pizza"
android:textColor="#000000"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="$15"
android:textColor="#FF0000"
android:textSize="25dp" />
</RelativeLayout>
JavaCode:
private void addImagesToThegallery() {
LinearLayout imageGallery = (LinearLayout)findViewById(R.id.imagegallery);
//imageGallery.setPadding(0, 0, 0, 30);
LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
List views=new ArrayList();
View view;
for(int j=0; j<=5; j++)
{
view=layoutInfralte.inflate(R.layout.newdeels, null);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
views.add(view);
}
for(int i = 0; i<views.size(); i++)
{
imageGallery.addView((View) views.get(i));
}
Please go through my code and let me know where am i going wrong and let me know if i am unclear anywhere
Change the margin top to make views come down a bit
for(int j=0; j<=5; j++)
{
view=layoutInfralte.inflate(R.layout.newdeels, null);
LinearLayout.LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.setMargins(left,5*j, right, bottom);
view.setLayoutParams(lp);
views.add(view);
}
for(int i = 0; i<views.size(); i++)
{
imageGallery.addView((View) views.get(i));
}
Checkout following
Github : Material Recent Layout
Usage
RecentsList recents = (RecentsList) findViewById(R.id.recents);
recents.setAdapter(new RecentsAdapter() {
#Override
public String getTitle(int position) {
return "Item "+position;
}
#Override
public View getView(int position) {
ImageView iv =new ImageView(RecentsActivity.this);
iv.setImageResource(R.drawable.mazda);
iv.setBackgroundColor(0xffffffff);
return iv;
}
#Override
public Drawable getIcon(int position) {
return getResources().getDrawable(R.mipmap.ic_launcher);
}
#Override
public int getHeaderColor(int position) {
return colors[random.nextInt(colors.length)];
}
#Override
public int getCount() {
return 10;
}
}
Note
You need to modify as per your requirement.
Try FrameLayout in your parent layout. Set different margins in child views to achive an effect of stack.
Child views are drawn in a stack, with the most recently added child on top
check out : github.com/kikoso/Swipeable-Cards
As per your response :
hi thank you for your response ur response actually worked. But now i am dealing with another situation where-in i would like to update details based on swipe for eg: if a card is swiped right it should be gestured as like where as if a card is swiped left it should be gestured as dislike so how to actually code these swipe gestures can you please help me out
#ashwin for that you should have a like and dislike drawable or a custom view.
card.setOnCardDimissedListener(new CardModel.OnCardDismissedListener() {
#Override
public void onLike() {
Log.d("Swipeable Card", "I liked it");
}
#Override
public void onDislike() {
Log.d("Swipeable Card", "I did not liked it");
}
});
Use above snippet to show or hide the view of like or dislike in respective methods that is onLike() and onDislike().
Edit 1:
I figured out the problem what you are facing, If you are simply implementing the Sample Code. Then it clearly shows that CardContainer has SimpleCardStackAdapter as adapter and CardModel are added to it using new operator. Except the last element of CardModel that is added separately and cardModel Listener is applied on last element only. If you want to apply it on all elements. Then you should have to add the elements to SimpleCardStackAdapter by ArrayList of CardModel.

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>

different values in onProgressChanged() and onStopTrackingTouch in SeekBar

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);

Categories

Resources