I have two images overlapping each other, i am animating(sliding) the above image to the right of the screen up to some part of it remaining on the screen so that i can animate it back to its original position, Now i want to get click of the below image to start my new activity but i am getting the click of above image only, and i know that animating the upper image will not move its layout.
But i have tried to move the layouts of upper image but after moving the layouts the above image disappears.
Any help?
After translating the above image,set its clickability to false,like
aboveImage.setClickable(false);
Try the below code ,it will work
AnimationActivity.java:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class AnimationActivity extends Activity {
ImageView aboveImage;
ImageView belowImage;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
aboveImage=(ImageView) findViewById(R.id.image_above);
belowImage=(ImageView) findViewById(R.id.image_below);
aboveImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
/*belowImage.setVisibility(View.VISIBLE);
overridePendingTransition(R.anim.move_to_right,R.anim.move_to_left);*/
Animation animation = new TranslateAnimation(0, 100,0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
aboveImage.startAnimation(animation);
aboveImage.setVisibility(0);
aboveImage.setClickable(false);
}
});
belowImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(getApplicationContext(),Activity2.class));
}
});
}
}
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" >
<ImageView
android:id="#+id/image_below"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/cherry" />
<ImageView
android:id="#+id/image_above"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/amalapaul" />
</RelativeLayout>
Related
I'm new to Android app.. I was trying to change the button color on first click and to change back to default on the next click and this will work no matter how many times user clicks...
Can anyone help me to understand this
Create global variable clickStatus
boolean clickStatus=true;
final Button running_app=(Button)findViewById(R.id.running_app);
running_app.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clickStatus){
clickStatus=false;
running_app.setBackgroundColor(Color.parseColor("#3F51B5"));
}else {
clickStatus=true;
running_app.setBackgroundResource(android.R.drawable.btn_default);
}
}
});
Simple, Just give two different tags to button and keep toggling it on button click. That would help you to change background colour alternatively. Here is the complete code for it. Hope this helps.
Here is main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Click Me" />
</LinearLayout>
Here is MainActivity.java
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnClick;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnClick = (Button) findViewById(R.id.btnClick);
btnClick.setOnClickListener(this);
btnClick.setTag("1");
btnClick.setBackgroundColor(Color.BLUE);
}
#Override
public void onClick(View view) {
if (view.getTag().equals("1")) {
view.setTag("0");
btnClick.setBackgroundColor(Color.RED);
} else {
view.setTag("1");
btnClick.setBackgroundColor(Color.BLUE);
}
}
}
I want to slide views which are within ViewAnimator from right to left for previous views.
This is my layout
<?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:orientation="vertical">
<LinearLayout
android:id="#+id/btn_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="#+id/btn_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PREVIOUS" />
<Button
android:id="#+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT" />
</LinearLayout>
<ViewAnimator
android:id="#+id/view_animator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/btn_layout">
</ViewAnimator>
</RelativeLayout>
This is my Activity
package com.example.ronem.testing;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewAnimator;
/**
* Created by ronem on 3/17/16.
*/
public class ViewAnimatorActivity extends AppCompatActivity {
Button btnPrev, btnNext;
ViewAnimator viewAnimator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_animator_layout);
btnPrev = (Button) findViewById(R.id.btn_previous);
btnNext = (Button) findViewById(R.id.btn_next);
viewAnimator = (ViewAnimator) findViewById(R.id.view_animator);
addImageView();
Animation inAnimation = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
Animation outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
viewAnimator.setInAnimation(inAnimation);
viewAnimator.setOutAnimation(outAnimation);
btnPrev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewAnimator.showPrevious();
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewAnimator.showNext();
}
});
}
private void addImageView() {
int[] drawable = new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.images};
for (int i = 0; i < drawable.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(drawable[i]);
imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
viewAnimator.addView(imageView);
}
}
}
Whenever I click on Next button imageView slides from left to right that works fine, But when I click on previous button it shows the previous imageView which seems fine but only problem is that the image view is sliding from left to right again but I want to slide image from right to left when I click on previous button. What attribute should I add to ViewAnimator ? any suggestions will be helpful.
In the next button click listener, set the in/out animations the way you did originally. In the previous button click listener, set the in/out animations the opposite way. That way they will go the desired direction based on which button you click.
hello I want put one button . when click that button from this page go to another page. I think I use Relative layout but I don't know exactly how to use it. please put complete code from "main.xml" and "activity" because I'm an amateur!!! thanks
Oh silverboy...
MainActivity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
}
activity_main.xml
<?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">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to second page"/>
</RelativeLayout>
I have managed to get a map to show on the screen. I am trying to create buttons above the map and then add actions to this. E.g. the button will change the location of the map. I am basically trying to adapt my code to this: https://www.youtube.com/watch?v=awX5T-EwLPc. However, I am using "extends Fragment Activity" which messes everything up.
My problem: I cannot create a button above the map, it is placed inside the map. I am trying to create a button above the map, and assign code so I can move to a location on the map etc. Here is my code:
MainActivity.java
package com.example.theapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener
{
Button button;
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
text=(TextView)findViewById(R.id.textView1);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text.setText("The button worked!");
}
}
activity_main.xml
<?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=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/map"
android:layout_alignTop="#+id/map"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_alignRight="#+id/map"
android:layout_marginRight="39dp"
android:text="TextView" />
</RelativeLayout>
From the code, my app displays a button on the map and a text view. When you click the button, its meant to change the text, but it doesn't do anything. I have researched tutorials and by the looks of it, I will have to create multiple fragments?
Question: How would I be able to create a button on that page, that will then pin point the user on the map?
Thank you in advance.
You are not setting any functionality to the Button... You should use
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text.setText("that's how you do it!");
}
});
You need not to worry about "FragmentActivity" until and unless you use fragment class.
Just use this as normal Activity. it will work well.
package com.example.theapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener
{
Button button;
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
text=(TextView)findViewById(R.id.textView1);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text.setText("The button worked!");
}
}
This is an example of facebook sliding menu.
When sliding, there is 20% space user can see which is similar with facebook. Facebook implements it with clicking anywhere of this 20% and the menu is slide back.
How to implement this?
One way of doing that is as following with OnTouchListener on your activity. You can detect exactly where is touched on the screen.
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.Toast;
public class AndroidTestActivity extends Activity implements OnTouchListener {
LinearLayout main;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
main = (LinearLayout) findViewById(R.id.main_layout);
main.setOnTouchListener(this); // you need to set the touch listener for your view. And every element around the detection area.
}
public boolean onTouch(View v, MotionEvent e) {
if(e.getX() <= main.getWidth() / 5) {
Toast.makeText(this, "In the %20..", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
You also need to id your main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >