My eyes and my fingers are telling me different things - android

I programatically moved a layout with several child views. The child views all display in their new positions. However, the click and touch events are still working as if the views were in their old positions. What do I do?
Edit:
I used translate and scale to move the layout. Here is the code from a class extending LinearLayout:
#Override
protected void onDraw(Canvas canvas) {
canvas.translate(getWidth(), 0);
canvas.scale(-1, 1);
super.onDraw(canvas);
}
I use this new layout in multiple activities, but the issue is the same for all of them. Here is one of the simpler layouts:
<?xml version="1.0" encoding="utf-8"?>
<com.example.myapp.NewLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="#+id/chapter_gridview"
style="#style/GridStyle" />
</com.example.myapp.NewLayout>
Any solutions?
Update
One way may be to update the LayoutParams but I am not sure how to do that yet. These are some related questions I found:
OnClickListener issues after ImageButton moved after TranslateAnimation
How can I dynamically set the position of view in Android?
http://www.clingmarks.com/how-to-permanently-move-view-with-animation-effect-in-android/400
Edit
No solutions? I give up then.

If you are working with AnimationTranslation :
Your LinearLayout had been moved seemingly,but it's just visually moved.The LinearLayout is still where it is before the animation.
try this to your layout :
mylayout.layout(x,x,x,x);
I took my answer from here
you can also see this question
EDIT
If you are working with Canvas.translate :
try padding in place of translate : canvas.setPadding(getWidth(), 0);

Related

Set height of children in RecyclerView?

I am trying to use RecyclerView in a similar way to ViewPager (Vertical).
I have setup fling and scroll gestures so the views are scrolled in exactly the same way as ViewPager but now I would like to setup children views (LinearLayoutManager) and LinearLayout used for each child view to fill screen instead of wrap_content. I have made all views to match_parent and fill_parent but that does not work.
I did this:
holder.layout.setMinimumHeight(this.recyclerView.getMeasuredHeight());
Which is nice but when the orientation changes then layout overflows the screen which makes smooth scrolling/gestures flicker etc. I don't want it to overflow the screen.
Any good tips how to make it properly so all children are laid out to maximum height of the screen but do not overflow?
Thanks
Edit: I changed setting minimum height to this:
holder.layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, this.recyclerView.getHeight()));
which solves flickering issue but is there a better way?
I have low reputation so unable to comment down. thats why answering here .
to over come the overflow issue lets say you have XMl of single item like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:id="#+id/mainLayout" >
<-- some views --->>
</RelativeLayout>
now in your adapter get this Layout in viewHolder
and make a boolean variable where you are setting adabpter.. which you can use for knowing orientation changed . when orientation get changed then change its value .
in your adapter make a function
public void orientaiotnChange(){
notifyDataSetChanged();
}
in your function
#Override
public void onBindViewHolder(final ViewHolder viewHolder, final int arg1) {
if(isOrientationHorizontal){
viewHolder.Mainlayout.setOrientation(horizontal);
}
else{
viewHolder.Mainlayout.setOrientation(vertical);
}
also change the orientation of RecyclerView in activity from where you set adapter and from where you are checking orientation is changed or not .
hopefully it will work so :)

Android TextView cannot setBackground Color or Drawable

wonder if anyone has came across this problem before. I have an app which detects faces and places touchable squares around the faces, all in a RelativeLayout. When touched I want to add some text into the View which is all working nicely, but when I go to simply add a background to the TextView it just does nothing. I've tried a standard background setBackgroundColor(Color.WHITE); instead of the background I really want to use (setBackgroundResource(R.drawable.nametag);) and still nothing.
TextView nameLabelView = new TextView(activity);
nameLabelView.setText(fullname);
nameLabelView.setTextColor(Color.BLACK);
nameLabelView.setBackgroundColor(Color.WHITE); //TODO <-- wth??
//nameLabelView.setBackgroundResource(R.drawable.nametag);
nameLabelView.setGravity(Gravity.CENTER_HORIZONTAL);
//duplicate layout params from active face View so label sits inside it
ViewGroup.LayoutParams lp = selectedFaceView.getLayoutParams();
nameLabelView.setLayoutParams(lp);
facePreviewLayout.addView(nameLabelView);
Strange one, hope it's obvious to someone out there, thanks in advance!
I have tested your code and it works ok for me. It is necessary to provide more information so I can help more with it. The code below works fine.
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView nameLabelView = new TextView(getApplicationContext());
nameLabelView.setText("Test");
nameLabelView.setTextColor(Color.WHITE);
nameLabelView.setBackgroundColor(Color.BLACK); //TODO <-- wth??
//nameLabelView.setBackgroundResource(R.drawable.nametag);
nameLabelView.setGravity(Gravity.CENTER_HORIZONTAL);
//duplicate layout params from active face View so label sits inside it
FrameLayout selectedFaceView = (FrameLayout)findViewById(R.id.frame_layout);
ViewGroup.LayoutParams lp = selectedFaceView.getLayoutParams();
nameLabelView.setLayoutParams(lp);
RelativeLayout facePreviewLayout = (RelativeLayout)findViewById(R.id.relative_layout);
facePreviewLayout.addView(nameLabelView);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#00FF00" >
</FrameLayout>
</RelativeLayout>
Thank you janzoner for your test really appreciate it, good to know I wasn't going mad. I've found a way to work around it though, I have no idea why this method works and the setup I had before didn't, perhaps I'd been staring at it too long and missed something somewhere else.
Basically beforehand I was adding the name label TextView into the main RelativeLayout (whose children were the actual image and all the face Views etc).
Now I have moved the name label TextView into its own RelativeLayout which sits around the face, and that's magically done the trick. This is a cleaner way of doing things anyway as you can't iterate through child Views of Views but you can children of RelativeLayout's and I will need this later!
Phew!

How can i after animated view move a views below? (without using Animation class)

For animation i use a ViewPropertyAnimator.
mAnimatedView.animate().translationYBy(50).start();
Before translation:
After translation:
I want buttons to stay below AnimatedView (according to the mentioned above illustrations it is not right now). There is only one idea that comes to my mind: animate buttons too, but i am sure it is not the best solution. Maybe there is a method that recalculates all layout while animation, that i have missed?
Add Buttons to xml before AnimatedView:
<Button ...>
<AnimatedView ...>

Resize parent LinearLayout when adding Buttons Programatically

so what i want to do is add buttons to a LinearLayout programaticalls, this works fine. I want them to apper as one line of buttons in a horizontal order so i set the LinearLayout like this:
<LinearLayout
android:id="#+id/button_frame"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
Then i add the buttons programatically:
for (String text: textlist) {
Button cbut = new Button(context);
cbut.setText(text);
cbut.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
cbut.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
Log.d(LOGTAGNAME, "TEST: " + buttonText);
}
});
button_frame.addView(cbut);
button_frame.invalidate();
}
This works until the buttons extend the screen width. So what i want to happen is that there is a horizontal scollbar if the buttons extend the screen width. As an alternative there could be a "line break" for the buttons inside the LinearLayout.
I've tried different setups including a scrollview around the listview but i never saw a scrollbar.
So maybe my problem is that the LinearLayout is not resizing correctly? What do i have to do to make the LinearLayout recalculate width every time after a view is added? invalidate() has no effect at all.
Thanks for any help.
Try putting the linearlayout inside a horiztonalscrollview. That should provide the scrollbar once the button exceeds the screen width.
You trying a simple scrollview or http://developer.android.com/reference/android/widget/HorizontalScrollView.html ? If you don't trying HorizontalScrollView, I think that must help.
try HorizontalScrollView and add Buttons to it . this will scroll automatically whenever needed . for more help share complete layout code
LinearLayout provide only "linear" positioning of views ;) I mean you can do like this:
[btn1][btn2][btn3][btn4]
or like this:
[btn1]
[btn2]
[btn3]
[btn4]
Difference between two variants is in android:orientation param. For more complicated views you should use TableLayout or RelativeLayout.
If you want to do scrollable variant of linear layout creta this structure:
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
...any other params...>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horisontal"
...any other params.../>
</HorizontalScrollView>
and add buttons to linear layout like you do it now.

Help with my first Android UI

I'm having trouble developing the UI for my first lame "game".
Here is a screenshot.
I'm using a LinearLayout that contains a TableLayout with TableRows. It seems so tedious and hard to control the position of elements.
For example, to get things to line up, I've inserted empty TextViews to "push" other elements into place.
I've also added padding to the buttons to get them to be the size I want.
Is there a better way of doing this?
Thanks!
You definitely want to be using a Relative Layout for this.
You would be able to specify where each button is in relation to other buttons.
Absolutely AVOID developing your UIs the way you are currently trying. The TextViews will be different sizes for different distributions of Android, and will likely only look right on the device you tested them for.
EDIT:
If you need empty space, use the XML attribute android:weightSum="x" in the parent view and android:layout_weight="y" in the child. This will make the child take up (y/x) of the space allotted to it in the layout_height and layout_width.
EDIT:
I think another good bit of advice for this would be to use individual layouts for things like your "direction" buttons. You'll be able to handle where they are on the screen as a group, instead of having to move each individually.
You should use RelativeLayout to solve this problem. I've gone through a similar problem once...
I didn't use the the Android's default buttons, for I had my own images for the pressed and unpressed behaviors...
Let suppose you want to place the east "button". You could use a function like:
public void addEastImageView(RelativeLayout myBackgroundLayout, ImageView center, ImageView east, int leftPadding, int topPadding, int rightPadding, int bottomPadding){
RelativeLayout.LayoutParams rightSide = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rightSide.addRule(RelativeLayout.RIGHT_OF, center.getId());
east.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
myBackgroundLayout.addView(east, rightSide);
}
The ImageView called "center" would be the one you called "i" in your image. The padding parameters would allow you to control the distance between the ImageViews. You can create functions like this one to add the "west", "south" and "north" buttons also: you just have to change the parameter "RelativeLayout.RIGHT_OF" to "RelativeLayout.LEFT_OF", "RelativeLayout.BELLOW" and "RelativeLayout.ABOVE" accordingly.
If you want some behavior for your ImageViews, you just have to set it in the setOnClickListener. You can then change your ImageView's "image" with setBackgroundResource, for example, and set the others logic behaviors you want.
Hope it helps :D
Use an AbsoluteLayout - it lets you state exactly where to put every element
http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:layout_width="188px"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="126px"
android:layout_y="361px"
/>
<Button
android:layout_width="113px"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="12px"
android:layout_y="361px"
/>

Categories

Resources