Image View moves after image changes(after pressing it) - java

ok so i'm currently learning android and i am trying to make a memory app where you have to find 2 matching pictures.
I have almost finished it but there is one problem that is bugging me, i looked it up and someone else had the same problem but the only answer he got was to make the image button a fixed dp size, but i don't think that's a solution if you are making an app for a variety of different android devices.
I made the images image views and they are all the same image but every time someone presses one of the image views it changes to a certain image that is fixed for that spot for that round.
The problem is every time an image is pressed the whole layout moves slightly but enough to be noticed and get annoyed by it, how can i fix this without giving them a fixed dp size.
Thanks :).
i made it with table layout inside a linear layout, this is one of the rows.
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/ImageView1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="5dp" />
<ImageView
android:id="#+id/ImageView2"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="5dp" />
<ImageView
android:id="#+id/ImageView3"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="5dp" />
<ImageView
android:id="#+id/ImageView4"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="5dp" />
</TableRow>

Related

Android: how to animate height to wrap_content?

I need to use a ValueAnimator to make a custom "drop field" appear when the user drags a certain view. (I want to change the field from gone, height = 0 to visible, height = wrap_content).
I've tried the solution of this question: How to animate to wrap_content?
The answer there worked when I used it on a single TextView, but when I tried to apply it to a LinearLayout with multiple text views it animated to a too large height value, then, when the animation finished, snapped back to the correct one. The layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="gone"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="match_parent"
android:paddingVertical="8dp"
android:gravity="center"
android:textSize="18sp"/>
<TextView
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:paddingVertical="8dp"
android:gravity="center"
android:textSize="18sp"/>
</LinearLayout>
Using animateLayoutChanges didn't work for me either, and I want to use a custom animator anyway.
I'm using C# in Xamarin, but answering with Android Studio Java code is acceptable too, I'll translate it to C#.
This should be caused by measuring the children views during Measure, so you can change your xaml like this ,change your children views's width to wrap_content:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="gone"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_weight="5"
android:layout_height="match_parent"
android:paddingVertical="8dp"
android:gravity="center"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_weight="2"
android:layout_height="match_parent"
android:paddingVertical="8dp"
android:gravity="center"
android:textSize="18sp"/>
</LinearLayout>

Android adding containers to a horizonalscollview [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have to admit my java is rusty and this is my first native Android app. I used Titanium studio a few years ago to build the same app.
I have a HorizontalScroll view outlined in my main_activity.xml and I want to add LinearLayout containers to it with data, each time a user presses a button. So my scroll view will start with nothing, and each button push will add another entry to it.
I tried something with a separate layout for the containers to be added, that use inflator, but then saw indications that I just define it all in the main activity layout. Maybe that was the correct route Here is the pertinent part of my layout
<HorfizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ResultView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:onClick="onShowResult"
android:clickable="true"
android:visibility="gone"
android:id="#+id/ResultContainer">
<!--successes-->
<LinearLayout
android:orientation="horizontal"
android:weightSum="2"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="#string/success_label"
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/Success_Label"
android:layout_weight="1"
android:clickable="false" />
<TextView
android:text=""
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/Successes"
android:layout_weight="1"
android:clickable="false" />
</LinearLayout>
<!--tenss-->
<LinearLayout
android:orientation="horizontal"
android:weightSum="2"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="#string/tens_result_label"
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/Tens_Result_Label"
android:layout_weight="1"
android:clickable="false" />
<TextView
android:text=""
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/Tens_Result"
android:layout_weight="1"
android:clickable="false" />
</LinearLayout>
<!--ones-->
<LinearLayout
android:orientation="horizontal"
android:weightSum="2"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="#string/ones_result_label"
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/Ones_Result_Label"
android:layout_weight="1"
android:clickable="false" />
<TextView
android:text=""
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/Ones_Result"
android:layout_weight="1"
android:clickable="false" />
</LinearLayout>
</LinearLayout>
And here is the code that I started playing with to implement it in my OnClick for the button. (the entries themselves will eventually be clickable) This is the only part of the onClick pertinent as the rest is just doing calculations, not creating the view being added.
//create and insert results
//start by getting the result view we plan to insert to
HorizontalScrollView resultview = (HorizontalScrollView)findViewById(R.id.ResultView);
//pull up the container to insert
LinearLayout resultcontainer = (LinearLayout)findViewById(R.id.ResultContainer);
//get and set the results
//add the container to the resultsview
resultview.addView(resultcontainer);
resultcontainer.setVisibility(View.VISIBLE);
This causes my app to crash and the debug seems to indicate it's something with an OnClick, but I wasn't clear. Specifically it looks like the addview in the second to last line causes the crash.
I'm fairly certain I'm not understanding something very fundamental and basic about how to do this, but like I said, kinda just diving in and learning as I go.
I also need to edit the text of the TextViews in the containers I'm adding, but one thing at a time.
Any help or pointers in the right direction would be appreciated.
So, my question, since apparently there was some confusion. What is the best way to add LinearLayouts to a HorrizontalScrollView, such that I can access and modify TextViews within those LinearLayouts?
Ideally, I'd like to create the objects being added initially via an XML layout, and not built them entirely programatically. Once created and modified, I'll add them to my HorrizontalScroll View.

Android 3 Scrollviews for comparing

I'm working on an app that can compare three things at once. Normally they can be opened one at a time and loaded into a scrollview. How would I place 3 scrollviews on the same screen, that each take up exactly 1/3 of the screen, so that a profile could be loaded into each one and all 3 would be scrollable. I think this would make for a pretty nifty comparing layout.
Thoughts?
So basically, the whole activity wouldn't scroll, but would contain 3 small scrollviews that could be each individually scrolled.
Activity image
Use android:weightSum
Like This
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3">
<ScrollView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<ScrollView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<ScrollView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
</LinearLayout>

How to get this message bubble to be on the right side?

I have this as my layout for an item in my listview:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/msgbox_self_default" >
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingLeft="7dip" >
<ImageView android:id="#+id/pending" android:layout_height="fill_parent"
android:layout_width="wrap_content" android:layout_marginLeft="2dip"
android:src="#drawable/ic_sms_mms_pending"
android:visibility="gone"
/>
<TextView
android:id="#+id/body"
android:text="#+id/body"
android:singleLine="false"
android:paddingLeft="#dimen/message_item_text_padding_left_right"
android:paddingRight="#dimen/message_item_text_padding_left_right"
android:paddingTop="#dimen/message_item_text_padding_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:linksClickable="false"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ff000000"
android:textSize="16sp"
android:layout_alignParentRight="true" />
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#id/body"
android:id="#+id/picture_layout"
android:orientation="vertical"
>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/picture"
android:visibility="gone"
android:maxWidth="178dip" android:maxHeight="178dip"
android:layout_alignParentRight="true"
android:adjustViewBounds="true" android:background="#android:drawable/picture_frame"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<TextView
android:paddingRight="#dimen/message_item_text_padding_left_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/picture_layout"
android:paddingLeft="3dip"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/date"
android:text="#+id/date"
android:singleLine="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
I want the bubble though to be on the right side. This is a layout for a message bubble. I am trying to put it on the right side like the iPhone conversations. I have tried every possibly thing I can think of relating to putting it on the right side instead of the left. Does anyone see anything wrong with my layout? Please help! Thanks
** EDIT **
The green bubble is the one that I aiming for in this picture
http://getandroidstuff.com/wp-content/uploads/2010/12/GO-SMS-Android.jpg
* CHANGED LAYOUT *
this is so far what I have.. anymore ideas?
I want the grey bubble to look like the white one except on the right side.
Im not going to sift through your code but basically you just want something like this for each message
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="put the right amount of padding in">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="make this your background, make sure to add padding in so that the text view is in the right place, look at 9 patches"
android:layout_alignParentRight="true"/><!-- Or make this left -->
</RelativeLayout>
Don't over complicate things, just use a text view with a 9 patch and it will do what you want.
Just swap the 9 patch horizontally and it'll be around the other way

Android: Alignment of four squares

I am trying to align four equally sized squares on an Android Screen & I have now tried what feels like a million different approaches, yet none of them seem to work :(.
What I've got at the moment is the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/MasterLayout" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#FFFFFF">
<TableLayout android:layout_width="fill_parent" android:id="#+id/mainlay" android:background="#444444" android:layout_weight="0.2" android:layout_height="wrap_content" android:padding="0dip">
<TableRow android:layout_weight="1" android:background="#BBBBBB" android:padding="0dip">
<ImageView android:id="#+id/ImageView1" android:layout_marginLeft="10px" android:layout_weight="1" android:layout_marginRight="5px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="centerInside" android:src="#drawable/bigbox_new"></ImageView>
<ImageView android:id="#+id/ImageView2" android:layout_marginLeft="5px" android:layout_weight="1" android:layout_marginRight="10px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="centerInside" android:src="#drawable/bigbox_new"></ImageView>
</TableRow>
<TableRow android:layout_weight="1" android:padding="0dip">
<ImageView android:id="#+id/ImageView3" android:layout_marginLeft="10px" android:layout_weight="1" android:layout_marginRight="5px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="centerInside" android:src="#drawable/bigbox_new"></ImageView>
<ImageView android:id="#+id/ImageView4" android:layout_marginLeft="5px" android:layout_weight="1" android:layout_marginRight="10px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="centerInside" android:src="#drawable/bigbox_new"></ImageView>
</TableRow>
</TableLayout>
</LinearLayout>
This basically does the job. However, every one of those four Images has a huge padding above and under it. How do I get rid of that? Or do I need to use a different Layout type alltogether?
To help illustrate my problem, here's a picture.
On the left is what I got, on the right is what I need.
Image
Thank you very much!
Cheers, Markus!
Remove layout_weight from your TableRows and set their layout_height to wrap_content. Then add an empty TableRow below them with layout_height set to fill_parent.
Try using the scaleType as centerCrop, if the image size is greater then this will do the trick for you. But this is not the best way to do it, better would be to change the image resources.
HTH !
Try setting the ImageView height to fill_parent, I think that should do the trick. Anyway, it would help if your XML properties were split in lines, it's hard for the rest of us to read a single line of properties.
Also, why do you place a TableLayout inside a LinearLayout? Unless you have more elements inside that Linear, you can get rid of it.

Categories

Resources