Making ImageView visible and invisibile - android

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);
}

Related

Custom View Snackbar from XML file will not display bottom of screen

We have tried two ways to display a Custom Snackbar (1) as a masquerading Dialog which will not move to the bottom of the screen It does however not dismiss the current Activity view just makes it opaque. I know why it is in the center of the screen but I am not able to move it to the bottom. (2) next is a view that takes over the entire screen because it is a new content view that I am guessing dismisses the current Activity view BUT it is at the bottom of the screen.
So my question is how to use design number 1 and move the Dialog to the bottom of screen?
Second question how to stop the new view in design number 2 from dismissing the view of the current Activity? After careful reading and little thought and extreme testing I do not think this is possible! I have posted the code for my two methods below. The XML file uses a Relative Layout as the base container.
public void seeSB(){
setContentView(R.layout.custom_snackbar);
// Line of Code above shows XML file
// Line of code tested but no control over the "viewMyLayout"
//LayoutInflater inflater = LayoutInflater.from(ListActivity.this);
//final View viewMyLayout = inflater.inflate(R.layout.custom_snackbar, null);
//viewMyLayout.setEnabled(true);
Button btnAB = (Button) findViewById(R.id.btnAB);
btnAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// viewMyLayout.setEnabled(false);
// Line above does not function
// CODE BELOW WORKS BUT FAR FROM elegant
setContentView(R.layout.activity_list);
//Intent intent = new Intent(ListActivity.this, ListActivity.class );
//startActivity(intent);
Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show();
}
});
}
public void displaySB(){
final Dialog openSnack = new Dialog(context);
openSnack.setContentView(R.layout.custom_snackbar);
Button btnAB = (Button)openSnack.findViewById(R.id.btnAB);
TextView tvSB =(TextView)openSnack.findViewById(R.id.tvSB);
//Dialog dialog = new Dialog(ListActivity.this);
//dialog.setContentView(Bottom);
// if YES delete Master Password from TABLE_MPW
btnAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openSnack.dismiss();
Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show();
}
});
openSnack.show();
}
This is far from functional in my book because the method design has just one Custom Snackbar to look at so you need to work on how to have multiple fixed Custom Snackbars. One suggestion might be to have multiple sub views in your parent view and call the sub view you want. I will post just the sub view I added to the parent XML file and the not so real dynamic method to implement which is implemented in this case with a button click. For this to work in a real application the code would need be called from some method or event.
You might consider a switch statement for multiple views ? ? ?
TAKE NOTE THE RELATIVE LAYOUT has its visibility set to GONE at the start
<RelativeLayout
android:id="#+id/hold_snackbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_Black"
android:visibility="gone"
android:orientation="vertical">
<TextView
android:id="#+id/tvSB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:text="#string/snackbar_text"
android:textColor="#color/color_Yellow"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="350dp"
android:layout_marginTop="5dp"
android:background="#color/color_Transparent"
android:focusable="false"
android:text="#string/snackbar_action"
android:textColor="#color/color_Red"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
Notice the View subViewGroup is declared when the Activity starts
View subViewGroup;
public void makeSB(View view) {
subViewGroup = findViewById(R.id.hold_snackbar);
subViewGroup.setVisibility(View.VISIBLE);
seeSB();
}
public void seeSB(){
Button btnAB = (Button) findViewById(R.id.btnAB);
btnAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
subViewGroup.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show();
}
});
}
Countdown Timer to close a Snackbar with no Action Button
public void makeCDT(View view) {
cdt = new CountDownTimer(5000, 100) {
// 5 sec 5000,100
// 10 sec 10000,100
#Override
public void onTick(long secsUntilFinished) {
etCPW.setText(String.valueOf(secsUntilFinished / 1000));
//etCPW.setText("seconds remaining: " + secsUntilFinished / 1000);
subViewGroup = findViewById(R.id.SB_NO_ACTION);
subViewGroup.setVisibility(View.VISIBLE);
}
#Override
public void onFinish() {
etCPW.setText("Counter Done");
subViewGroup.setVisibility(View.GONE);
if(cdt!=null){
cdt.cancel();
}
}
};
cdt.start();
}

Changing imagebutton image(from gallery) with longclick

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.

Keep on clearing text present in edit text when I hold the button

I have a text view in which the user can enter data at run time using the custom buttons that I have created.
My delete button is able to delete one character at a time but if i hold the button then it stops.
I want the text field to get cleared when i hold the button.
Is there any solution to this....??
Please help me out.
This is my xml,
<RelativeLayout
android:id="#+id/btnClear"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
>
<ImageView
android:id="#+id/imgClear"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/img_clear" />
</RelativeLayout>
This is my code,
imgClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getNumber;
if(isFirstNum){
getNumber = txtFirstNumber.getText().toString();
if(getNumber.length() > 0)
txtFirstNumber.setText(getNumber.substring(0, getNumber.length()-1));
} else if(!isFirstNum){
getNumber = txtSecondNumber.getText().toString();
if(getNumber.length() > 0)
txtSecondNumber.setText(getNumber.substring(0, getNumber.length()-1));
}
}
});
You can use the onLongClickListener to check if the delete button is pressed for a long time
imgClear.setOnLongClickListener(new OnLongClickListener()
{
#Override
public boolean onLongClick(View v)
{
txtFirstNumber.setText("");
return true;
}
});
This will set your txtFirstNumber to blank but the onClickListener will not be called.
Your question is not clear. But as per my understanding you want to clear the text of the text view then in your button click listener just set the text for textview to empty.
eg:
public void onClick(View v){
textview.setText("");
}

One activitiy with different backgrounds in android

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 want to overlap 2 ImageView so I can see both

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...

Categories

Resources