I'm trying to resize ImageView on button click. I don't know why but setLayoutParams works when I call it in onCreate method, but not working when use it inside button's OnClickListener. Did I miss something?
ImageView myImageView;
Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
myImageView = findViewById(R.id.viewtest);
myButton = (Button)findViewById(R.id.buttontest);
resizeImageView(); // resize when called here
myButton.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
resizeImageView(); // NOT resize when called here
}
});
}
private void resizeImageView() {
Toast.makeText(getApplicationContext(), "Resize", Toast.LENGTH_LONG).show();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)myImageView.getLayoutParams();
params.height = 300;
params.width = 300;
myImageView.setLayoutParams(params);
}
XML layout. I'm using LinearLayout inside of RelativeLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ResultActivity">
<android.widget.LinearLayout
android:id="#+id/lineartest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="#+id/viewtest"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerCrop"
android:layout_centerVertical="true"
app:srcCompat="#mipmap/ic_launcher_round"
android:background="#444"
/>
</android.widget.LinearLayout>
<android.support.v7.widget.AppCompatButton
android:id="#+id/buttontest"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="test"/>
</android.widget.RelativeLayout>
I just checked. This works fine in case of ImageView.
Maybe you are resizing it multiple times. Means If you have already called resresizeImageView() in onCreate method and you call it again in OnClick method , the resizing won't be visible because it already has the size you are resizing to.
In xml, you have AppCompatButton but in java you have defined as Button. Change AppCompatButton to Button in xml Or in java, Button to AppCompatButton.
I used your code, to create a new project, and just commented out the first call to resizeImageView() and it works fine when resizeImageView() is called in onClick()
As Derek answered, maybe you're already setting the same bounds to the ImageView during onCreate() which you wish to update in button click.
Also, just a suggestion:
Try adding the property android:layout_below="#id/lineartest" to your AppCompatButton.
Change android:layout_height="wrap_content", so you can view the changes happening to the ImageView.
Try this one
public class SomeActivity extends Activity {
public static final float screenWidth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.the_company);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
}
}
And after declare this implementation:
Button mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
float scale = SomeActivity.screenWidth / view.getWidth();
if(view.getScaleX() == 1) {
view.setScaleY(scale);
view.setScaleX(scale);
} else{
view.setScaleY(1);
view.setScaleX(1);
}
}
});
OK. I've found solution. I use some async task that affects ImageView (loading image). When I disabled it resize button started to work as excepted. Thanks for your help.
Related
I've a requirement in which I need to rotate a wheel dynamically when I receive a value from the server.
I'm using WheelMenu library which can be fou.
wheelMenu.setAlternateTopDiv(int);
using this line of code doesn't make any difference in the wheel rotation.
Here is the wheel I want to rotate automatically.
Thanks in advance.
Hi I downloaded the library and read the documentation. The thing is that library doesn't do what you want to do. The method wheelMenu.setAlternateTopDiv(int) just change the referential selection of the wheel.
If you want to rotate automatically an image use animation and do a rotation of certain angle to reach the position that you want.
EDIT :
You can try this code which turn the image for 90 degrees each time you click on the button but with no animation. Try it and let me know if its what you want.
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private Button button;
private float rotation = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.image);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rotation = rotation + 90;
imageView.setRotation(rotation);
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
Layout :
<?xml version="1.0" encoding="utf-8"?>
<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.gohidoc.realmtest.MainActivity">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/wheel"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="turn"/>
</RelativeLayout>
image :
I am programming a android app with ImageView and when I try to change the width and height dynamically, the app stops (abort). I am using the following commands:
RelativeLayout.LayoutParams icon_horizontal = new RelativeLayout.LayoutParams(50, 50);
mec21h_icon2.setLayoutParams(icon_horizontal);
The layout is Relative, so what is wrong?
It would be nice to have a Stack trace in order to understand what the error is.
Without this, I can just guess:
you're trying to use the wrong parent layout params
the ImageView ref is null
...
I guess you are trying to change the image dimensions after it is visible (already displayed), if so you should first call for method requestLayout();
i.e.
mImageView.requestLayout();
Re-size image on Click of a button.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rel"
tools:context="com.testingpp.scannerbarcode.MainActivity">
<ImageView
android:id="#+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="button_one"
android:src="#drawable/btn_back"
/>
</FrameLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
FrameLayout layout;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageview);
layout = (FrameLayout) findViewById(R.id.rel);
}
public void button_one(View view)
{
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(50,50);
layout.setLayoutParams(params);
}
}
if you are using Framents, then don't use onClick of xml. use onClickListener in java.
I have a simple button in my layout. Setting leftMargin to the view actually showing different results.
my_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="hello pandora"/>
</RelativeLayout>
In my activity, I'm setting the leftMargin property to the Button.
Button leftBtn = (Button) findViewById(R.id.left_btn);
LayoutParams params = (LayoutParams) leftBtn.getLayoutParams();
params.leftMargin = 550;
If I set leftMargin as negative value or 0, its working fine, but If I set the value greater than the width of screen, it just resizing/compressing the button. I am expecting the button to go out of bounds like negative value.
I am expecting the button in the 3rd image to go out of bounds like the button in 1st image.
Please don't say to set the button layout_alignParentRight="true" in layout and rightMargin = -50in activity(this works) because I want to move the button from left to right.
I assume assigning a specific width larger than the screen size (eg. 1000 dp) to the parent RelativeLayout should solve your problem.
Also why do you want to make out-of-screen UI elements? What is the desired behaviour? Perhaps a transition animation would be better?
EDIT
I've tried the animation + storing the measured width of the Button. It seems to work.
Can you try this on GB?
MainActivity.java
public class MainActivity extends Activity {
final Context context = this;
Button mButton;
int mButtonWidth; // Measured width of Button
int amountToMove; // Amount to move the button in the x direction
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
amountToMove = 600;
mButton = (Button) findViewById(R.id.button);
// Measure Button's width
mButton.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
mButtonWidth = mButton.getMeasuredWidth();
// Simple onClick listener showing a Toast
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"Hello Pandora clicked!",Toast.LENGTH_SHORT).show();
}
});
// Onclick listener for the other button
Button toggle = (Button) findViewById(R.id.toggle);
toggle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Animate the other button
TranslateAnimation a = new TranslateAnimation(0, amountToMove, 0, 0);
a.setDuration(1000);
// Finalize movement when animation ends
a.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mButton.getLayoutParams();
// Restore measured width and change left margin
lp.width = mButtonWidth;
lp.leftMargin = lp.leftMargin + amountToMove;
mButton.setLayoutParams(lp);
amountToMove = -amountToMove;
}
#Override
public void onAnimationStart(Animation animation) { /* Do nothing */ }
#Override
public void onAnimationRepeat(Animation animation) { /* Do nothing */ }
});
mButton.startAnimation(a);
}
});
}
}
activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Pandora"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Move the other button"
android:id="#+id/toggle"/>
</LinearLayout>
EDIT 2
It works on a GB Emulator too (the Button gets clipped, is clickable).
u can use max line=1 to show complete text in one line on button when you use leftMargin = 550;
try this
<Button
android:id="#+id/left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:maxLines="1"
android:text="hello pandora"/>
Hello Edit your button property like this,
android:layout_gravity="center_horizontal"
android:singleLine="true"
and change parent layout to frameLayout
I am having a button in my activity and using a TranslateAnimation to change its position.
I am using anim.setFillAfter(true); to keep the resulting translation, the button shifts its position but the touch area remains same. Now when i touch on the button it doesn't work rather touching on the previous location of the button calls its listener..
Here is my code:
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Animation anim=new TranslateAnimation(0, 100, 0, 0);
anim.setDuration(500);
anim.setFillAfter(true);
Button mybtn = (Button)findViewById(R.id.btn);
mybtn.startAnimation(anim);
}
public void myFunc(View v){
Toast.makeText(this, "btn clicked", Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
android:onClick="myFunc"/>
</RelativeLayout>
I found the answer to my query at this page
vogella
and here android has explained it, buttons require a property animation not just the view animation:
Here is the code that I used:
Button mybtn = (Button)findViewById(R.id.btn);
mybtn.animate().translationX(400).setDuration(500);
Thanx for your help...
I am leaving this post if anyone else require assistance.
I have re-generated your case and it seems like only 50% of the button shows the selected color. Is that right ? Try this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sampleButton = (Button)findViewById(R.id.btn);
sampleButton.animate().translationX(somevalue).setDuration(someduration);
}
I have a simple demo with two buttons. They are laid out with a RelativeLayout at the top and bottom of the screen.
When I click one of them, I want them to switch places.
What is the best way to do that?
This is my res/layout/main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:id="#+id/android_button_1"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:layout_alignParentTop="true" />
<ImageButton
android:id="#+id/android_button_2"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:src="#drawable/icon2"
android:layout_alignParentBottom="true" />
</RelativeLayout>
And this is my Activity:
public class HelloButtons extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageButton button1 = (ImageButton) findViewById(R.id.android_button_1);
final ImageButton button2 = (ImageButton) findViewById(R.id.android_button_2);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
Toast.makeText(HelloButtons.this, "Beep Bop", Toast.LENGTH_SHORT).show();
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
Toast.makeText(HelloButtons.this, "Beep Bop", Toast.LENGTH_SHORT).show();
}
});
}
}
Use something along these lines
RelativeLayout.LayoutParams lp1 = (LayoutParams) b1.getLayoutParams();
RelativeLayout.LayoutParams lp2 = (LayoutParams) b2.getLayoutParams();
lp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
lp1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
b1.setLayoutParams(lp1);
b2.setLayoutParams(lp2);
(and the opposite to revert them again) in you OnClickListeners
I would say the best way to do that is not to switch the actual ImageButton locations, but to instead switch the ImageButton images and keep track of the state inside your application so it can react to onClicks correctly.