I have an ImageView that it's a background with an arrow. And I have another ImageView that it's the same background but with another different arrow.
I want to show one of the ImageView, and when I press a button, it shows the another ImageView and the first one. I.e. overlap them.
Is there any function to overlap 2 ImageView without use canvas or another?
Thank you
XML
<LinearLayout android:name="myView">
<ImageView android:name="rightArrow"
android:src"#+id/drawable/image1">
</ImageView>
<ImageView android:name="leftArrow"
android:src"#+id/drawable/image2">
</ImageView>
<Button android:name="myButton">
</Button>
</LinearLayout>
Code:
Button button= (Button) findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (imageView1.getVisibility == View.INVISIBLE ) {
imageView1.setVisibility(View.VISIBLE) ;
imageView2.setVisibility(View.INVISIBLE);
} else {
imageView1.setVisibility(View.INVISIBLE) ;
imageView2.setVisibility(View.VISIBLE);
}
}
});
You can call onclick :
if (view1.getVisibility == View.INVISIBLE ) {
view1.setVisibility(View.VISIBLE) ;
view2.setVisibility(View.INVISIBLE);
} else {
view1.setVisibility(View.INVISIBLE) ;
view2.setVisibility(View.VISIBLE);
}
I don't think there is a function to overlap directly...
Related
I have this button that should change its appearance when I click the button from
state1 to state2 and vice versa.
the heart is 2 different drawables (#drawable/ic_fav_dish_color & #drawable/ic_fav_dish_grey) and the text is 2 different strings (#string/dish_faved & #string/dish_not_faved)
I made the button in xml with that code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/fav_dish_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_fav_dish_color"
android:gravity="start|center_vertical"
android:text="#string/dish_faved"
android:textAlignment="center"
android:layout_margin="8dp"/>
</LinearLayout>
you can use this , you should have two images one that is fill and other is
not
final Button btn = (Button)(findViewById(R.id.fav_dish_button));
final Drawable drawable = getResources().getDrawable(R.drawable.your_fill_heart_image_name);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null);
}
});
You should make a click listener on your button like below:
Button mButton=(Button)findViewById(R.id.fav_dish_button);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mButton.setText(getResources().getString(R.string.dish_not_faved););
mButton.setBackgroundResource(R.drawable.ic_fav_dish_grey);
}
});
Update:
Try below code if you want to change drawable left:
// Left, top, right, bottom drawables
Drawable[] drawables = mButton.getCompoundDrawables();
// get left drawable.
Drawable leftCompoundDrawable = drawables[0];
// get desired drawable.
Drawable img = getContext().getResources().getDrawable(R.drawable.ic_fav_dish_grey);
// set image size (don't change the size values)
img.setBounds(leftCompoundDrawable.getBounds());
// set new drawable
mButton.setCompoundDrawables(img, null, null, null);
I've searched the forums, but not have found any specific or understandable answers for my problem.
I'd like to change my Imagebutton image to a picture, selected from the gallery. Prefferrably the image should stay changed after closing the application.
My XML for the button is here:
<ImageButton
android:id="#+id/eat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:adjustViewBounds="true"
android:background="#drawable/eat"
android:clickable="true"
android:longClickable="true"
android:scaleType="fitCenter" />
The java code for playing the sound is here with the OnClick method.
ImageButton eat = (ImageButton) findViewById (R.id.eat);
eat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.start();
}
});
I would like to add the OnLongClick method here too, (since to OnClick is allready taken and the image replacing should be a little different), but havent found the right way. Can You please guide me a little bit?
You need to return true from image's onLongClickListener.
Like this:
eat.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//do something
return true;
}
});
eat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.start();
}
});
This will not cause the image's onClickListener to be called as it means that the action has already been handled in longClickListener.
My Xml file:-
<?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:background="#drawable/pagefw">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="51dp"
android:layout_marginTop="140dp"
android:src="#drawable/cross" />
</RelativeLayout>
My activity class:-
int turn=1;
iv1 = (ImageView)findViewById(R.id.imageView1);
//iv1.setVisibility(View.INVISIBLE);
//iv1.setImageAlpha(0);
iv1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"Image View 1 selected", Toast.LENGTH_SHORT).show();
if(turn%2!=0)
{iv1.setVisibility(View.INVISIBLE);}
else
{iv1.setVisibility(View.VISIBLE);}
turn++;
}
});
What I Did:-
I added an ImageView in the activity having an image, which has onClickListener assigned to it.
What I Want:-
To my image set in the imageview to completely become invisible and visible after each click, i.e. after first click it becomes invisbile and after another click visible and so on.
What I Get:-
After first becoming invisible the image never becomes visible nor am I getting the toast when I click the invisible imageView. I also tried doing it with iv1.setAlpha(); method but also with no result.
Try something like this , using this u'll not need any extra variable
if(iv1.getVisibility()==View.INVISIBLE){
iv1.setVisibility(View.VISIBLE);
}else{
iv1.setVisibility(View.INVISIBLE);
}
Edit:
I have tried and worked using a Button or removing background.
You are applying click on same ImageView, thats the reason why not working for u.You should apply onClick on a diff button. If want on same ImageView than u have to remove Background instead making it invisible.as ex, u have several method to remove bg/src which u can use.one of them
iv1.setBackgroundResource(null);
Edit-2
Ur imageview must have a min height and width as after removing bg ,imageview clicking area is very small.
<ImageView
android:id="#+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:minHeight="48dp"
android:minWidth="48dp"
android:onClick="onClick" />
and a boolean for handle image during onClick
boolean avail=false;
and onClick code.
if (avail) {
iv1.setBackgroundResource(0);
avail = false;
} else {
iv1.setBackgroundResource(R.drawable.ic_launcher);
avail = true;
}
Make it more simple by using one boolean paramater that hold the button state, either pressed or not as following:
static final boolean isPressed= false;
iv1 = (ImageView)findViewById(R.id.imageView1);
iv1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"Image View 1 selected", Toast.LENGTH_SHORT).show();
if(!isPressed){
isPressed= true;
iv1.setVisibility(View.INVISIBLE);
} else {
isPressed= false;
iv1.setVisibility(View.VISIBLE);
}
}
});
Set the tag on the image. Set tag 1 or 0 and check the tag to change the visibility of the image.
if(v.getTag()==0)
{
iv1.setVisibility(View.INVISIBLE);
v.setTag() == 1;
}
else
{
iv1.setVisibility(View.VISIBLE);
v.setTag() == 0;
}
I suggest you do it this way:
in your xml you provide the actual width and height of a drawable and do not provide source:
<ImageView
android:id="#+id/imageView1"
android:layout_width="_some_value_"
android:layout_height="_some_value_"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="51dp"
android:layout_marginTop="140dp">
</ImageView>
That is for your view not to become of a size 0*0 when you set your background to something like null or transparent or whatever.
Then, in your activity:
protected void onCreate(Bundle savedInstanceState) {
.....
iv1.setBackgroundResource(R.drawable.cross);
...
}
And onClick():
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),
"Image View 1 selected", Toast.LENGTH_SHORT).show();
if(turn%2!=0)
{iv1.setBackgroundResource(Color.TRANSPARENT);}
else
{iv1.setBackgroundResource(R.drawable.cross);}
turn++;
}
Pretty sure you can simply it like this too:
iv1.setVisibility(!iv1.getVisibility());
Know what I mean?
if(iv1.getVisibility()==View.INVISIBLE){
iv1.setVisibility(View.VISIBLE);
}else{
iv1.setVisibility(View.INVISIBLE);
}
I am trying to create an android app. I have two buttons next and back in my android app. I want when i click on next button its open same activity with different background. Next time i again click next new background image. And on press on back button its show previous image. And if no previous image its shows menu on press. Similarly if background with last image its hide next button. I have no idea how to do this.
I have tried this:
#Override
public void onCreate(Bundle savedInstanceState)
{
onCreate(savedInstanceState);
back = (Button) findViewById(R.id.back);
next = (Button) findViewById(R.id.next);
back.setOnClickListener(this);
next.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.back)
{
startActivty(new Intent(this,));
}
else if(v.getId()==R.id.next)
{
startActivity(newIntent(this,));
}
}
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"
android:background="#drawable/back">
<Button
android:id="#+id/back"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="25dp"
android:background="#drawable/ques"
android:text="Back" />
<Button
android:id="#+id/next"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/back2"
android:layout_alignBottom="#+id/back2"
android:layout_alignParentRight="true"
android:background="#drawable/ques"
android:text="Next" />
</RelativeLayout>
In layout as you can see i am using image back for background. I want when i click next new background image then next and so on.
But i dont know how to start same activity with differene backgroud.
Don't start new Activity just change the background:
Keep an array of background resources in your activity like:
int[] backgroundResId;
and one int variable to store current background index:
int currentIndex=0;
now inside your onCreate initialize this array with resource id's of all the backgrounds drawables:
backgroundResId=new int[]{R.drawable.a,R.drawable.b,R.drawable.c};
changeBackground()
create function changeBackground in activity:
private void changeBackground(){
findViewById(R.id.root_layout).setBackgroundResource(backgroundResId[currentIndex]);
}
Now onClick of next button increase currentIndex:
currentIndex++;
if(current<=backgroundResId.length){
changeBackground();
}else{
// setVisibility of next button to invisible
}
onBackButton Click
currentIndex--;
if(current>=0){
changeBackground();
//// setVisibility of next button to visible
}else{
//show menu
}
Make an images array and post your data to the next activity:
Intent intent = getIntent();
intent.putExtra("background", imageIdInTheImageArray);
startActivity(intent);
//finish();
and in your onCreate function :
Bundle b = getIntent().getExtras();
if (b != null) {
int background = b.getInt("background");
//set your background
}
You can add an ImageView in your xml file.
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
you can change background using this
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setBackgroundResource(resId);
Try this..
Global:
int[] backgrounds = new int[]{ images in drawable as int array };
int count = 0;
Button back,next;
RelativeLayout img_backn_lay;
JAVA:
setContentView(R.layout.activity_main);
back = (Button) findViewById(R.id.back);
next = (Button) findViewById(R.id.next);
back.setOnClickListener(this);
next.setOnClickListener(this);
img_backn_lay = (RelativeLayout) findViewById(R.id.main_lay);
img_backn_lay.setBackgroundResource(backgrounds[count]);
count += 1;
ClickListener:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.next)
{
if(backgrounds.length != count){
img_backn_lay.setBackgroundResource(backgrounds[count]);
count += 1;
}else{
Toast.makeText(MainActivity.this, "No images", Toast.LENGTH_LONG).show();
}
}
else if(v.getId()==R.id.back)
{
if(count != 0){
img_backn_lay.setBackgroundResource(backgrounds[count]);
count -= 1;
}else{
Toast.makeText(MainActivity.this, "No images", Toast.LENGTH_LONG).show();
}
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_lay"
android:layout_width="match_parent"
android:layout_height="match_parent" >
I have a button which is called Check, I want it to be invisible and visible as I click each time on it, as If its visible and I clicked it will become invisible and verse vies !
But my code doesn't work ! any ideas ?
Button Check ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Check = (Button)findViewById(R.id.checkButton);
Check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View View) {
if (View.getVisibility() == android.view.View.VISIBLE)
View.setVisibility(android.view.View.INVISIBLE);
else if (View.getVisibility() == android.view.View.INVISIBLE)
View.setVisibility(android.view.View.VISIBLE);
}
});
In my activity its visible at the beginning and when I click on it, it become invisible, BUT when I click again it stays invisible !
Change your code to this,
Check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (v.isShown())
v.setVisibility(View.INVISIBLE);
else
v.setVisibility(View.VISIBLE);
}
But i think problem is, when button goes invisible, you are not getting any click event on it. First make sure that onClick method get call when button is invisible.
An invisible button will not dispatch any interaction event. So instead of setting button's visibility to the invisible, you can set a transparent or blank background or something like that.
But i personally believe, you should change your use-case because why one will click on the invisible button.
Try This:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="abcd" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="abc"
android:text="Button" />
</LinearLayout>
public void abc(View v) {
v.setVisibility(View.INVISIBLE);
}
public void abcd(View v) {
v.findViewById(R.id.button1).setVisibility(View.VISIBLE);
}
Invisible Items don't receive on-click event. So the only way you can receive a click on invisible is by receiving on some other view in place of the invisible view. The above solution wraps the button in a layout, so when button is invisible the on-click is passed on to layout, which handles the event and do accordingly. If you have a high usage of such layout you can also create a custom button with above mechanism.