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 :
Related
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.
I'm trying to add button on other button exactly on same position with same width,and height.
I thought i can do that like these codes,
public class MainActivity extends AppCompatActivity {
Button b1;
LinearLayout root;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = (LinearLayout)findViewById(R.id.activity_main);
b1 = (Button)findViewById(R.id.b1);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
ViewGroup.LayoutParams size = new ViewGroup.LayoutParams(b1.getWidth(),b1.getHeight());
int[] locations = new int[2];
b1.getLocationInWindow(locations);
Button a = new Button(this);
a.setLayoutParams(size);
a.setText("new Button");
a.setX(locations[0]);
a.setY(locations[1]);
root.addView(a);
}
}
and XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="semo.msoft.myapplication.MainActivity">
<Button
android:id="#+id/b1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"/>
</LinearLayout>
but, result was like this,
resultImage
I thought it should be correct but it seems be wrong, new button had little bit smaller width and height. Additionally , the position of new button was totally wrong.. I don't know why,, someone who know about this, please help
The following code is working perfectly fine on my machine. I basically just changed the way you are setting LayoutParams:
int[] locations = new int[2];
b1.getLocationInWindow(locations);
Button a = new Button(this);
a.setLayoutParams(b1.getLayoutParams());
a.setX(locations[0]);
a.setY(locations[1]);
a.setText("new Button");
frameLayout.addView(a);
PS: You need to change the root to <FrameLayout>. LinearLayout just isn't made to handle overlapping views.
Hello this is my xml file
<RelativeLayout
android:id="#+id/tutorialBox"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
i have made an on click listener for it
final Button closeBt = (Button) findViewById(R.id.closeBen);
closeBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
closeBt.setText("Im a button");
}
});
for some reason when i click this button nothing happens it doesnt look like it has been clicked.
when i took the button out of the realtive layout everything worked fine
any suggestions?
Instead of this add onClick attribute to the button tag in xml.
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:onClick = "close_clicked"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
Then in Main Activity just make a new method like this.
public void close_clicked (View v){
// Your code
}
No need to add on click listner.
Your RelativeLayout does not look good, is it your main container? Maybe try it this way
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
And a good practice is to declare your widgets globally then instantiate them in theOnCreate
public class Foo extends AppCompatActivity {
private Button closeBenny;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
closeBenny = (Button)findViewById(R.id.closeBen);
closeBenny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
closeBenny.setText("Im a button");
}
});
}
}
I know there are some questions about this topic but neither did work for me. I have created a simple button in a RelativeLayout and when I click on it I'd like the button to jump in the middle of the screen. So I have to set the button to CenterHorizontal and CenterVertical programatically.
Here is my code to set the button centerHorizontal:
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
final RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)btn.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,0);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setLayoutParams(layoutParams);
}
});
}
And here is my 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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
When I press the button nothing happens. If anyone has an idea how to fix it please response.
So i found a few problems in your code:
1. You have to add the rule after a button click because you create a reference betwen the variable "layoutParams" and the object.
2.You need to get rid of the existing rules.
Sample code:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
btn.setLayoutParams(layoutParams);
}
});
You have to remove your align left rules which you have defined in the xml.
Button btn = (Button)findViewById(R.id.button);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) btn.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
lp.removeRule(RelativeLayout.ALIGN_PARENT_START);
lp.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
btn.setLayoutParams(lp);
keep in mind that RelativeLayout.LayoutParams.removeRule is only supported for API >= 17
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);
}