I have a TextView that I created in the main.xml. In my app.java I am dynamically positioning that TextView based on where the user taps the screen. The problem I am having is that when I call myTextView.setPadding(100,100,0,0), it moves the actual Text of the TextView, but does not move the Colored Background of the TextView.
Ideas?
The padding only affect the contents of the View, you could embed your View inside a LinearLayout and change the padding of the LinearLayout to move the TextView.
If you want to move the entire contents you might want to use margins instead.
You may use animation to move a view.
Animation a = new TranslateAnimation((oldPos),
newPos, 0.0f, 0.0f);
a.setDuration(300);
a.setStartOffset(100);
a.setRepeatMode(Animation.ABSOLUTE);
a.setFillAfter(true);
a.setInterpolator(AnimationUtils.loadInterpolator(this.getContext(),
android.R.anim.accelerate_decelerate_interpolator));
View.startAnimation(a);
Related
I've been making all of my views dynamically, and now I've come to the point where I want to add an EditText for people to write in.
I've been able to accomplish this for the most part, but it doesn't look right. I have a linear layout that I'm adding a relative layout to. I'm making the relative layout have a white background, then adding the EditText. Problem is, it always adds it to the direct center of the relative layout, and options to align it vertically to the top have so far failed.
I also need to be able to pull the text from it later when a separate button was pressed (I know how to make the button work, it's the pulling text from it part I'm a bit iffy on). Here's my code so far:
public void addEditText(LinearLayout L){
EditText myEditText = new EditText(c);
myEditText.setSingleLine(false);
RelativeLayout l1 = new RelativeLayout(c);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(scWidth1, 300);
lp.gravity=Gravity.CENTER;
l1.setLayoutParams(lp);
l1.setBackgroundColor(Color.WHITE);
l1.setGravity(Gravity.CENTER_VERTICAL);
l1.addView(myEditText);
L.addView(l1);
}
l1.setGravity(Gravity.CENTER_VERTICAL); places the EditText in center vertical of the parent container i.e RelativeLayout, remove that line.
I'm trying to apply a TranslateAnimation to an ImageView inside of a LinearLayout. As soon as the ImageView(marked "1" in the image below) crosses the bounds of the LinearLayout that contains the ImageView, it goes "black"/disappears. This does NOT happen if I animate the entire green LinearLayout, so I don't think it has to do with it's z-value. Rather, I believe that the ImageView can't visually "escape" its container layout (green). What can I do to make the ImageView display in front of everything when the animation is being performed? I've already tried .bringToFront()(followed by .requestLayout/.invalidate of the root view).
Try to set android:clipChildren="false" in a parent container
I have my ListView with image background (it's actually a fragment) but still i have set the background with: getListView().setBackgroundResource(R.drawable.table); I noticed that every time this list shows up bacground image is allways like "fill_parent" also i noticed that listview is fill_parent too but i can fix that with getListView().setPadding() but i cant crop background image. Is there any other way to do it? I need background view with aditional buttons...
the Method setPadding is just affecting the content of the ListView, but not it's container e.g. the Background. So basically the padding produces unused space inside the ListView. If you want to restrict the ListView you can use the margin attribute:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams (LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Another and in my opinion better way to hava a ListView with a Button below is:
Use a LinearLayout with orientation vertical. Add the ListView and the Button to this layout.
The ListViews height is fill_parent and the Button's wrap_content. Now add to both the layout_weight with value 0 for the Button and 1 for the ListView. The result is that the Button is drawn at the bottom of your screen and the ListView just can expand to the rest of the screen (also its background).
I'm trying to make a tab bar in android using a RelativeLayout and a RadioGroup that has an indicator which will slide to indicate the active RadioButton within the RadioGroup.
These are customized radio buttons that are effectively rectangles with an icon over text, all contents centered, which uses a custom state-selector for the background.
Here's my current hierarchy:
<RelativeLayout> // Main view of the tab bar
<RadioGroup> // The buttons
<RadioButton />
<RadioButton />
<RadioButton />
</RadioGroup>
<ImageView /> // The indicator
</RelativeLayout>
The idea I had was when a radio button was clicked, I would align the indicator to the top & bottom of the selected radio button (and animate it). However, It seems that layout_align<Edge> only works with sibling elements, not with members of another view group, i.e. I can align to the RadioGroup itself, but not to a RadioButton inside it.
I thought of putting the indicator as a member of the RadioGroup, but since RadioGroup is an extension of LinearLayout, there doesn't seem to be a way to place it at the edge of a given RadioButton.
Can anyone think of how I might solve this issue? Or perhaps there's a better solution than my animate-align-to-button technique?
UPDATE
With #superjos help, I managed to work this out fairly well.
I had to give each button a known height instead of using wrap_content (not ideal, but for this project it should probably work fine). Make the indicator height match that of the buttons. Make sure the RadioGroup is set to wrap content and be centered vertically in the parent view. Set indicator to alignTop and toRightOf the RadioGroup. Then, for the button click listener:
int prevY, newY;
int prevButtonIndex, newButtonIndex;
public void moveIndicator(button, indicator, index) {
prevY = newY;
newY = prevY + (index - prevButtonIndex) * button.getHeight();
TranslateAnimation animation = new TranslateAnimation(0, 0, prevY, newY);
animation.setDuration(500);
animation.setFillAfter(true); // stay at final animation position
indicator.setAnimation();
animation.start();
}
Here's my idea about it. Following are some more detailed steps.
The idea is to have the ImageView has a RadioGroup sibling, as it is in your sample. The ImageView is initially positioned on the right of the RadioGroup, and its horizontal middle line is aligned with the one of the first (or whatever chosen) RadioButton.
Then, upon a RadioButton click, a TranslateAnimation is built/configured at runtime in order to let the ImageView shift vertically until its middle line gets aligned with the one of the clicked button. The animation is set with fillAfter so to stay firm after it completes.
Steps:
Create your resources so that ImageView and RadioButtons have the same height, or else work with vertical paddings so that they result having the same height.
To initially align the ImageView to the first RadioButton, some XML could suffice: in ImageView attach layout_toRightOf and layout_alignTop to the RadioGroup. In order to take into account the top-padding from RadioGroup, you could add that to the initial ImageView top-padding XML attribute. Or better as top-margin.
When a RadioButton is clicked, create a TranslateAnimation that will be applied to the ImageVew, changing only the Y coordinate starting from 0 to its destination toY value (while from/toX attribute will be 0). That is given by a formula such as:
toY = (ClickedRadioButton.Index - LastClickedRadioButton.Index) * RadioButton.Heigth
Here Index is the ordinal position of the RadioButton within the group (e.g. from 0 to 2) (Warning: that is pseudo-code, not necessarily an existing Java field named Index).
Apply the animation to the ImageView and start it.
You could customise the RadioButton style to include a 9patched version of the indicator image as a background for the selected (might be checked for radios..) state. Or as drawable
However, it depends what you want the animation to look like.
For example.
<style name="TabRadioButton" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:background">#drawable/tab_bg</item>
<item name="android:drawableLeft">#drawable/tab_indicator</item>
</style>
i use this a s surface view in my application and now i want to add text view to this programmatically. How can i do that.
"<com.csfcse.udrawer.UdrawerDrawingSurface
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/drawingSurface"
android:layout_gravity="left" />"
Thanks......
An option may be to have a FrameLayout with two children, first the SurfaceView, then whatever you want to overlay on the SurfaceView. In the sample below, the second View in the FrameLayout is a horozontil LinearLayout with a Button and a TextView. The FrameLayout displays all its children as piled in Z order with the first child at the bottom and all children positioned at the upper left corner. In this case the LinearLayout has Gravity.CENTER_VERTICAL (I think you could do the same thing with padding on the LinearLayout.
I have never found much (good) documentation about how SurfaceViews (or any of the screen drawing) works. There may be a problem with flickering or refresh, but I don't see much problem on my Froyo Evo. (This test app draws 'twirling' lines on the SurfaceView below the Button and TextView.
If the question is simply: How do you programmatically add a TextView to a Layout that was created by a XML inflated Layout, then, get a reference to the Layout instance and call addView() to it.
Layout lay = parentLayo.findViewById(R.id.drawingSurfaceParent);
TextView tv = new TextView(this);
tv.setText("New textview");
lay.addView(tv);
You'll need to use the Canvas and a corresponding text draw method:
drawText(String text, int index, int count, float x, float y, Paint paint).
This is not possible, a SurfaceView cannot contain other Views.