How to set zoom controls on TextView and ImageView - android

i want to set zoom controls on TextView and ImageView. where, when zoom in pressed then all the textview and imageview are being zoomed in, and when zoom out pressed all the textview and imageview are being zoomed out with the function of scrollable left & right.
or, is it possible pinch to zoom only in imageview?
my try is below, but not worked perfectly.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity_main"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center"
android:orientation="vertical"
tools:context="sample.app"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:orientation="vertical"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textIsSelectable="true"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="something in text view"
android:textColor="#ff0000"
android:textIsSelectable="true"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="another textview"
android:textColor="#000000"
android:textIsSelectable="true"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Parts list"
android:textColor="#ff0000"
android:textIsSelectable="true"
android:textSize="20dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#drawable/melody" />
</LinearLayout>
</ScrollView>
<ZoomControls
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/zoomControls"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
public class melodygenerator extends AppCompatActivity {
ZoomControls zoomit;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.melodygenerator);
zoomit = (ZoomControls)findViewById(R.id.zoomControls);
imageView = (ImageView)findViewById(R.id.imageView);
zoomit.setOnZoomInClickListener(new View.OnClickListener(){
#Override
public void onClick(View V){
float x = imageView.getScaleX();
float y = imageView.getScrollY();
imageView.setScaleX ((int) (x+1));
imageView.setScaleY((int) (y+1));
}
});
zoomit.setOnZoomOutClickListener(new View.OnClickListener() {
#Override
public void onClick(View V) {
float x = imageView.getScaleX();
float y = imageView.getScrollY();
imageView.setScaleX ((int) (x-1));
imageView.setScaleY((int) (y-1));
}
});
}}

Try using this code.
public class melodygenerator extends AppCompatActivity {
ZoomControls zoomit;
ImageView imageView;
boolean isZoomeed;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.melodygenerator);
zoomit = (ZoomControls)findViewById(R.id.zoomControls);
imageView = (ImageView)findViewById(R.id.imageView);
zoomit.setOnZoomInClickListener(new View.OnClickListener(){
#Override
public void onClick(View V){
float x = imageView.getScaleX();
float y = imageView.getScrollY();
if(isZoomeed) {
isZoomeed = false;
imageView.setScaleX ((int) (x-1));
imageView.setScaleY((int) (y-1));
}else {
isZoomeed = true;
imageView.setScaleX ((int) (x+1));
imageView.setScaleY((int) (y+1));
}
}
});
}}

Related

Android click listener on View work in strange manner

I have the following layout.xml containing one ScrollView with some elements(picture and text)
<?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"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/clickable"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="#drawable/crown" />
<TextView
android:id="#+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="some text" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="#drawable/medal" />
<TextView
android:id="#+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="some text" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="#drawable/star" />
<TextView
android:id="#+id/n3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="some text" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity{
LayoutInflater inflater;
RelativeLayout MyLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater = LayoutInflater.from(this);
MyLayout = (RelativeLayout) inflater.inflate(R.id.layout, null, false);
MyLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TAG"," I'm clicked! ");
}
});
Click Listener above does not work when I click screen!!!
But if I change code to this - everything is working
MyLayout.findViewById(R.id.clickable).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TAG"," I'm clicked! ");
}
});
So why should I set click listener to LinearLayout inside ScrollView inside parent RelativeLayout?
Why click listener does not work for root xml element RelativeLayout???
The problem is that your ScrollView is intercepting the touch events and not propagating them to parent layouts (in this case your RelativeLayout).
This thread might have some useful information about this topic.
Just add android:clickable="false" in your ScrollView. Then change your MainActivity like below:
public class MainActivity extends Activity {
LinearLayout myLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myLayout = (LinearLayout) findViewById(R.id.layout);
myLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("TAG"," I'm clicked! ");
}
});
}
}
Note: activity_main.xml is xml file which you are loading.
Now your OnClick will work as expected.

Buttons Overlap

I made a stopwatch using chronometer with four buttons but when i use the visibility modes to make stop and pause button appear they overlap.... Pls explain why...Below is the code.....
Assume the layout file with buttons in relative layout...
public class StopWatchFragment extends Fragment {
Chronometer chronometer;
Button startStopWatch;
Button stopStopWatch;
Button resetStopWatch;
Button pauseStopWatch;
Button resumeStopWatch;
private long lastPause;
//RelativeLayout relativeLayout;
private int check = 0;
public StopWatchFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.stopwatch_layout,container,false);
chronometer = (Chronometer) rootView.findViewById(R.id.stopwatch);
startStopWatch = (Button) rootView.findViewById(R.id.startStopWatch);
stopStopWatch = (Button) rootView.findViewById(R.id.stopStopWatch);
resetStopWatch = (Button) rootView.findViewById(R.id.resetStopWatch);
pauseStopWatch = (Button) rootView.findViewById(R.id.pauseStopWatch);
resumeStopWatch = (Button) rootView.findViewById(R.id.resumeStopWatch);
relativeLayout = (RelativeLayout) rootView.findViewById(R.id.parentRelativeLayout);
pauseStopWatch.setVisibility(View.GONE);
//final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(relativeLayout.getLayoutParams());
startStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
startStopWatch.setVisibility(View.GONE);
pauseStopWatch.setVisibility(View.VISIBLE);
if(check == 1){
resumeStopWatch.setClickable(true);
}
else{
resumeStopWatch.setClickable(false);
}
//params.setMargins(16,16,16,16);
//pauseStopWatch.setLayoutParams(params);
}
});
pauseStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
check = 1;
lastPause = SystemClock.elapsedRealtime();
chronometer.stop();
}
});
resumeStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
chronometer.start();
resumeStopWatch.setClickable(false);
}
});
stopStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.stop();
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
resumeStopWatch.setClickable(false);
}
});
resetStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.stop();
resumeStopWatch.setClickable(false);
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
}
});
return rootView;
}
}
This is the layout file pls refer for this....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
This is the way buttons work when click on start button
You are hiding the button, but you are still listening with the OnClickListener. Try adding:
pauseStopWatch.setOnClickListener(null);
Then, when you make your button visible:
pauseStopWatch.setOnClickListener(whatever);
Try this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:visibility="gone"
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</LinearLayout>
</LinearLayout>
In the above, the pause button will be set to Gone. You make it visible later when user clicks start button

Android Auto Scroll when a Button is pressed

I have a XML Layout that puts a button at the bottom of the screen. I'd like to show a text under this button, when it is pressed, with a sort of auto scroll.
I really don't know how to do it. Any suggestion?
My XML file:
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/activity_main"
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.example.thefe.newsmartkedex.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/pkmn"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/tmpPkmn" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/tipo"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/tmpPkmn"
android:layout_marginStart="19dp"
android:layout_marginTop="89dp"
android:id="#+id/tipo1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/tipo"
android:id="#+id/tipo2"
android:layout_marginTop="20dp"
android:layout_below="#+id/tipo1"
android:layout_alignParentEnd="true"
android:layout_alignStart="#+id/tipo1" />
<TextView
android:text="Tipo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tipo"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="36dp"
android:textSize="20dp"
android:textAlignment="center"
android:layout_alignStart="#+id/tipo1" />
<TextView
android:text="Forte contro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:textSize="20dp"
android:id="#+id/forteContro" />
<TextView
android:text="Debole contro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/forteContro"
android:layout_marginTop="108dp"
android:textSize="20dp"
android:id="#+id/deboleContro" />
<!--tsf = tiposmallforte-->
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/forteContro"
android:id="#+id/tsf1"
android:src="#drawable/tipoSmall"/>
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/forteContro"
android:layout_toRightOf="#id/tsf1"
android:id="#+id/tsf2"
android:src="#drawable/tipoSmall"/>
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/forteContro"
android:layout_toRightOf="#id/tsf2"
android:id="#+id/tsf3"
android:src="#drawable/tipoSmall"/>
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/forteContro"
android:layout_toRightOf="#id/tsf3"
android:id="#+id/tsf4"
android:src="#drawable/tipoSmall"/>
<!--tsd = tiposmalldebole-->
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/deboleContro"
android:id="#+id/tsd1"
android:src="#drawable/tipoSmall"/>
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/deboleContro"
android:layout_toRightOf="#id/tsd1"
android:id="#+id/tsd2"
android:src="#drawable/tipoSmall"/>
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/deboleContro"
android:layout_toRightOf="#id/tsd2"
android:id="#+id/tsd3"
android:src="#drawable/tipoSmall"/>
<ImageView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/deboleContro"
android:layout_toRightOf="#id/tsd3"
android:id="#+id/tsd4"
android:src="#drawable/tipoSmall"/>
<Button
android:text="Descrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:id="#+id/descrizione" />
</RelativeLayout>
First wrap everything inside a ScrollView
and now place your button and a textview inside a LinearLayout like this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<Button
android:text="Descrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/descrizione" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:visibility="gone" />
</LinearLayout>
Now in button click action set your text to the textView and don't forget to change its visibility to visible. Finally scroll to the textView with scrollView.fullScroll(View.FOCUS_DOWN)
-----For Auto Scrolling webview click on Toggle Button ----
public class MainActivity extends AppCompatActivity {
TimerTask mTimerTask;
final Handler handler = new Handler();
Timer t = new Timer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggleButton = (ToggleButton)findViewById(R.id.toggleButton);
final WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("https://en.wikipedia.org/wiki/Blog");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {
if (ischecked) {
Toast.makeText(MainActivity.this,"Start AutoScrolling", Toast.LENGTH_SHORT).show();
doTimerTask(); }
else{
stopTask();
Toast.makeText(MainActivity.this,"Stop AutoScrolling", Toast.LENGTH_SHORT).show();
}
}
private void stopTask() {
mTimerTask.cancel();
}
private void doTimerTask() {
mTimerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
webView.post(new Runnable() {
public void run() {
if (webView.getContentHeight() * webView.getScale() >= webView.getScrollY()) {
webView.scrollBy(0, 5);
}
}
});
}
});
}};
// public void schedule (TimerTask task, long delay, long period)
t.schedule(mTimerTask, 0, 500);
}
});
}
}

Android Animation disabled EditText

i add the Bounce effect to Linear layout.This linear layout contain 2 EditTexts and one button.but when animation complete Edittexts and Button are Disabled.I cant use them.how can i enable Edittexts and Button.
XML
<LinearLayout android:background="#drawable/login_grd" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_marginTop="50dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:textSize="55sp"
android:gravity="center"
android:layout_gravity="center"
android:textColor="#fff"
android:id="#+id/_title"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="AFF"/>
<Button
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="200dp"
android:textColor="#000"
android:background="#fff"
android:id="#+id/animloginButton"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Login"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="480dp"
android:background="#color/white"
android:id="#+id/lnr_do_login"
android:layout_marginTop="280dp">
<android.support.design.widget.TextInputLayout
android:theme="#style/TextLabel2"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:id="#+id/input_layout_username"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:textSize="16sp"
android:textColor="#color/black"
android:id="#+id/input_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:theme="#style/TextLabel2"
android:id="#+id/input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
<EditText
android:textSize="16sp"
android:textColor="#color/black"
android:inputType="textPassword"
android:id="#+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password" />
</android.support.design.widget.TextInputLayout>
<Button
android:textColor="#000"
android:background="#fff"
android:id="#+id/loginButton"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Sign In"/>
</LinearLayout>
<TextView
android:textColor="#color/white"
android:layout_gravity="bottom|right"
android:gravity="bottom|center"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/app_name"
android:layout_marginBottom="10dp" />
</LinearLayout>
</LinearLayout>
LoginActivity.class
private LinearLayout lnrLoginView;
#Override
public void onCreate(Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.before_login_screen);
animloginButton = (Button) findViewById(R.id.animloginButton);
lnrLoginView = (LinearLayout) findViewById(R.id.lnr_do_login);
TextView title = (TextView) findViewById(R.id._title);
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/title_font.ttf");
title.setTypeface(type);
animloginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
lnrLoginView.clearAnimation();
TranslateAnimation translation;
translation = new TranslateAnimation(0f, 0F, 0f, -getDisplayHeight());
translation.setStartOffset(10);
translation.setDuration(1000);
translation.setFillAfter(true);
translation.setInterpolator(new BounceInterpolator());
lnrLoginView.startAnimation(translation);
animloginButton.setVisibility(View.INVISIBLE);
}
private int getDisplayHeight() {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics.widthPixels;
}
});
}
Try to use ObjectAnimator.onFloat instead.
animloginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(lnrLoginView.getTag() instanceof Animator){
((Animator)lnrLoginView.getTag()).cancel();
}
ObjectAnimator translateY = ObjectAnimator
.ofFloat(target, "translationY", -transY)
.setDuration(1000)
;
translateY.setStartDelay(10);
translateY.setTarget(lnrLoginView);
translateY.setInterpolator(new BounceInterpolator());
translateY.start();
animloginButton.setVisibility(View.INVISIBLE);
}
private int getDisplayHeight() {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics.widthPixels;
}
});
Use a animation listener to malipulate your views before and after the animation.
translation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
//Disable the views.
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
//Enable the views
}
});

Layering of views during an objectanimation

I have a relative layout with a LinearLayout as a child viewgroup and a button as a child view. so when i press the button the linearlayout falls down the screen but it goes under the button , i want it to go over the button .
<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"
android:id="#+id/relative"
>
<LinearLayout
android:layout_width="365dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout"
android:background="#drawable/background">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Name"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:id="#+id/radioButton"
android:layout_below="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/radioButton2"
android:layout_below="#+id/linearLayout"
android:layout_toRightOf="#+id/radioButton"
android:layout_toEndOf="#+id/radioButton" />
</RadioGroup>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
android:id="#+id/button"
android:layout_gravity="center"
android:translationZ="1dp"
android:layout_below="#+id/linearLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp" />
my java code is :
public class MainActivity extends Activity {
ValueAnimator va;
LinearLayout linearLayout;
Button button;
ValueAnimator objectAnimator;
RelativeLayout relative;
boolean b=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout=(LinearLayout) findViewById(R.id.linearLayout);
button=(Button) findViewById(R.id.button);
setbuttonclick();
relative=(RelativeLayout) findViewById(R.id.relative);
relative.bringChildToFront(linearLayout);
}
public void setbuttonclick()
{
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(b==false) {
objectAnimator = ObjectAnimator.ofFloat(linearLayout, "TranslationY", 2600f);
objectAnimator.setDuration(1000);
objectAnimator.setInterpolator(new AnticipateOvershootInterpolator());
objectAnimator.start();
b=true;
}
else {
objectAnimator = ObjectAnimator.ofFloat(linearLayout, "TranslationY", 50f);
objectAnimator.setDuration(1000);
objectAnimator.setInterpolator(new AnticipateOvershootInterpolator());
objectAnimator.start();
b=false;
}
}
});
}
}

Categories

Resources