Set visibility visible but set enable false Android XML Java - java

I have a button named HideIt:
android:id="#+id/HideIt"
android:onClick="HideIt_onClick"
I have another button named HelloWorld:
android:id="#+id/HelloWorld"
android:onCick="HelloWorld_onClick"
And the third button named VisibleIt:
android:id="#+id/VisibleIt"
android:onCick="VisibleIt"
What is the Scenario?
First of all I click on the HideIt button, so it's function will Invisible and Disable the HelloWorld button as codes below:
public void HideIt_onClick(View v) {
Button DisableHide = findViewById(R.id.HelloWorld);
DisableHide.setVisibility(View.GONE);
DisableHide.setEnabled(false);}
Then I click on the VisibleIt button, so its function will Visible the HelloWorld button BUT it will NOT Enable the HelloWorld Button as codes below:
public void VisibleIt_onClick(View v) {
Button VisibleHelloWorld = findViewById(R.id.HelloWorld);
VisibleHelloWorld.setVisibility(View.VISIBLE);}
What is the problem?
The problem is when I click on VisibleIt, I expect that my HelloWorld button set Visible but still disabled, but it will never set visible and it's still hidden.
I added a line of code to VisibleIt codes to text as below:
DisableHide.setEnabled(true);}
So VisibleIt codes are as below:
public void VisibleIt_onClick(View v) {
Button VisibleHelloWorld = findViewById(R.id.HelloWorld);
VisibleHelloWorld.setVisibility(View.VISIBLE);
DisableHide.setEnabled(true);}
In this case when I click on VisibleIt Button, it comes Visible BUT NOT disabled as what I wanted, so I set the setEnable(false) in VisbileIt codes above, but I had the same problem as before, button HelloWorld is still HIDDEN.
So I tried a private void as codes below but still that problem:
public void VisibleIt_onClick(View v) {
Button VisibleHelloWorld = findViewById(R.id.HelloWorld);
VisibleHelloWorld.setVisibility(View.VISIBLE);
DisableHide.setEnabled(true);
DisableItNow();
}
private void DisableItNow() {
Button DisableItPlease = findViewById(R.id.HelloWorld);
DisableItPlease.setEnabled(false);
}
I need that HelloWorld button, which had been Disabled and Gone by another Java fuction , get visible by this fucntion BUT still disabled

Maybe this below code can help you.
public class MainActivity extends AppCompatActivity{
Button HideIt;
Button HelloWorld;
Button VisibleIt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HideIt = findViewById(R.id.HideIt);
HelloWorld = findViewById(R.id.HelloWorld);
VisibleIt = findViewById(R.id.VisibleIt);
HideIt.setOnClickListener(v -> {
HelloWorld.setVisibility(View.INVISIBLE);
HelloWorld.setEnabled(false);
});
VisibleIt.setOnClickListener(v -> {
HelloWorld.setVisibility(View.VISIBLE);
HelloWorld.setEnabled(true);
});
//in xml code and in Your activity java file you can now safely delete the
//function that you created for
//hiding or showing
}
}

Maybe just use an if statement inside the onClick() to check the visibility as follows:
public void VisibleIt_onClick(View v) {
//Button VisibleHelloWorld = findViewById(R.id.HelloWorld);
//v is already your button
if( v.getVisibility() == View.INVISIBLE)
v.setVisibility(View.VISIBLE);
else if(v.getVisibility() == View.VISIBLE)
v.setVisibility(View.INVISIBLE)
else{;/*the view is View.GONE*/}
}
so that if the view (your button) is visible it sets the visibility to View.INVISIBLE and if is invisible to View.VISIBLE.
For what you are trying to do there is no point in changing the button .setEnabled() property.

Related

revised background image comparison

i know the brainy people wont like my petty questions but im trying to learn
I,m trying to make a pairs game i have been using int so far on my apps but this game needs a different approach ive created the pairs game with ints but confusing code and a floor that pushing same button twice will delete the pair as below ive been trying with tags the code all looks clean as in no errors
public class MainActivity extends Activity {
//added Tag here for the if (pic2.getTag()==(beck));
Tag beck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton pic1 = (ImageButton ) findViewById(R.id.imageButton1);
final ImageButton pic2 = (ImageButton ) findViewById(R.id.imageButton2);
pic1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic1.setBackgroundResource(R.drawable.becks);
pic1.setTag(R.drawable.becks);
if (pic2.getTag() == pic1.getTag()){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);}
}});
pic2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic2.setBackgroundResource(R.drawable.becks);
pic2.setTag(R.drawable.becks);
if (pic1.getTag() == pic2.getTag()){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);
}
}});
}}
ive tried since my original post to work out how to do ive shown code for 2 buttons all i want to do is compare and make invisible after the second button is clicked
if (pic1.getTag().equals(pic2.getTag())){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE); }
the .equals crashes the app
pic1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic1.setBackgroundResource(R.drawable.becks);
pic1.setTag(beck);
if (pic2.getTag()==(beck));{
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);}
}});
this works with or without semi but both buttons dissapear when either button clicked
if (pic1.getTag()==(pic2.getTag())){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE); }
this changes the image but the buttons don,t disappear when second image clicked trying to not use ints if possible
this line works with comma to
if (pic1.getTag()==(pic2.getTag()))
if (pic1.getTag()==(pic2.getTag()));
with effect of both button disappear on 1 click of either button dread moving to the else if lol
Also can a Tag be removed if the pair of images compared if false eg
if no match remove the button tag and reset all remaining images to Default image as when i put all 24 buttons on i need a reset method
i,m finding the semi colon at end of if statement has different effects to not having can anyone point correct way when and when not to use semi colons
Use tags for saving your image-id:
pic1.setTag(R.drawable.becks);
pic2.setTag(R.drawable.becks);
You can then check and compare those by calling getTag() on the buttons that have been clicked:
public boolean isMatch(View x, View y) {
return x.getTag() == y.getTag();
}

Android - How to make buttons listen more than one time

I have an app with a home screen and a bunch of buttons on it, and therefore listeners for each. After the user clicks on one of the buttons, a new layout is brought up and that layout has a back button with a listener.
The problem is that whenever the user presses the back button, the home screen layout is brought back up but none of the listeners work for the buttons anymore.
Here is some sample code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // return to home screen
// sets up a listener for when the GCF main screen button is clicked.
GCFButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.gcf); // change to the gcf layout
Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
back.setOnClickListener(new View.OnClickListener() // put a listener on back button
{
public void onClick(View v)
{
setContentView(R.layout.main); // return to home screen
}
});
Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
{
public void onClick(View v)
{
// do stuff
}
});
}
});
}
You should not change a screen with setContentView(). Screens are changed in Android by starting a new Activity with startActivity(new Intent(...)) or with Fragments like recommended by Malimo (which is a bit more difficult to do but much nicer). You call two times setContentView() where one is destroying the other one.
in my opinion you should use fragments for your contentviews. so every fragment will be responsible for its contentview and can add listeners each time it is displayed...
http://developer.android.com/guide/components/fragments.html
I'm sure that there is a method built into Android that allows you to do this, but my first thought is recursion.
The problem is that your listeners are in the onCreate method, which means that after they are run through, they won't repeat. In the back button listener,
when you set the content view to be the home screen again, that won't set up the listeners again, that will just change the content view.
To fix that, you would have to call the onCreate method again, once the back button is clicked, because then it would run your whole code with all the listeners
from the home screen again.
I suggest putting all of the listeners in a listeners() method, and then calling that method recursively when needed. It would need to be called in onCreate(...),
as well as when the back button is clicked:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
listeners(); // set up all the listeners for the buttons
}
public void listeners()
{
setContentView(R.layout.main); // return to home screen
// sets up a listener for when the GCF main screen button is clicked.
GCFButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.gcf); // change to the gcf layout
Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
back.setOnClickListener(new View.OnClickListener() // put a listener on back button
{
public void onClick(View v)
{
listeners(); // recursively call the listeners again to 'start over'
}
});
Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
{
public void onClick(View v)
{
// do stuff
}
});
}
});
}
I would also recommend putting the back button listener in its own method, so that it can be called every time the layout is changed.

Can't reach my (final) button inside my OnClickListener function

My final goal of this snippet is to:
call a Dialog(Interface) from a button.
let the end user select an option (in a list of 5 options)
change the button text to the selected option
Currently I have this:
public void onCreate(Bundle savedInstanceState) {
setLayoutState();
// rest of code omitted
}
then the setLayoutState() that instatiates the button
public void setLayoutState() {
setContentView(R.layout.main);
Button rate = (Button) findViewById(R.id.ratebutton);
rate.setOnClickListener(onRatePress);
}
So here: setOnClickListener calls to a separate function (to keep things clean, the Activity has got a lot of buttons)
private final View.OnClickListener onRatePress = new View.OnClickListener() {
public void onClick(View v) {
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
context, R.array.rates, android.R.layout.select_dialog_item );
adapter.setDropDownViewResource(android.R.layout.select_dialog_item);
new AlertDialog.Builder(context).setTitle("Rate this item!")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Common.makeToast(context,adapter.getItem(which) + "", 3000);
Button rate = (Button) findViewById(R.id.ratebutton);
rate.setText(adapter.getItem(which)+"");
// TODO: user specific action
dialog.dismiss();
}
}).create().show();
}
};
While this works fine I was wondering if there is a way I can pull this off without redeclaring the Button rate inside the Dialog's onClick
I've already tried declaring the button as final in the top part, but that won't let me call the button in the Dialog's onClick.
A variable in Java has a scope. It is always available inside the block (a pair of braces {} ) that contains its declaration, and in any block contained by this block. But not ouside.
Thus if you declare your button inside a method, it is not accessible outside of this method.
You button is only accessible inside drupappSetUploadLayout.
if you want it to be accessible to all methods, then put it inside the class body directly. Such a variable is called a field and fields are accessible to all methods.
public class A {
private Button b;
public void foo() {
b=null;
}
}
b can be accessed by all methods.
Read more about the basics of java, you should consider making small J2SDK programs before starting with android.
The View v that is a parameter refers to the button that was clicked... so you could remove the redeclaration of the button and use
( (Button) v).setText(adapter.getItem(which)+"");

Which context do i need?

I'm creating a dialog box and using the (this) isnt working. Up until now its just been a button calling a dialogbox but now the button within the called dialogbox needs to call another dialog. The Dialog dialogdelcon is the one with problem.
Here is the code:
case R.id.delappt:
//rmvall();
final Dialog dialogdelsel = new Dialog(this);
dialogdelsel.setContentView(R.layout.delsel);
dialogdelsel.setTitle("What would you like to do?");
dialogdelsel.setCancelable(true);
Button btndelsel = (Button) dialogdelsel.findViewById(R.id.btndelsel);
btndelsel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete selected code here.
}
});
Button btndelall = (Button) dialogdelsel.findViewById(R.id.btndelall);
btndelall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete all code here.
final Dialog dialogdelcon = new Dialog();
dialogdelcon.setContentView(R.layout.delcon);
dialogdelcon.setTitle("Deletion Confirmation");
dialogdelcon.setCancelable(true);
Button buttoncnclok = (Button) dialogdelcon.findViewById(R.id.btndelcon);
buttoncnclok.setOnClickListener(new OnClickListener() {
// on click for cancel button
#Override
public void onClick(View v) {
dialogdelcon.dismiss();
}
});
dialogdelcon.show();
}
});
dialogdelsel.show();
break;
getApplicationContext() or use YourActictyName.this Because this refers the button click listner ,not your class Object
If this code is in the onCreate() method, or similiar, add getApplicationContext() instead of this and you should be fine. That's because this in a Button-context will refer to the button environment.
To improve the isolation between the two dialogs, it would be best to call showDialog(R.id.delapptcon) from the onClick handler. Then load the new dialog in the onCreateDialog of your activity. In this way, you can create more reusable dialogs and avoid the scoping issue you have now.

how to calculate the number of touches on a button in android

hi i am a new developer. i am trying to design an app. In my app i want to calculate the no of touches in a particular button. Is this can be calculated by onTouch process if yes can anyone give me an example or idea.
Try below code
First Create an Global variable
int numberOfClick = 0;
Now for your button try following code
clickButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
numberOfClick++;
}
}
now you can get the number of clicks by this variable
A click on a button is sent to the app via the onClick event. So if you have a Button:
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(myClickListener);
You can set up your onClickListener to do whatever you want when the button is clicked.
// Create an anonymous implementation of OnClickListener
private OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
// increment the counter on click
numberOfClicks++;
}
};

Categories

Resources