How to set a button clickable when clicking another button? - android-studio

I'm new to programming and I've been trying to build a Skilltree for my tablerpg, I wanted to have the Skill buttons unclickable until you click the previous Skill button, so far I've got this
Button btn_for = (Button) findViewById(R.id.Fortificacao);
Button btn_ten = (Button) findViewById(R.id.Ten);
//btn_for.setOnClickListener(btn_ten.setClickable(true));
the commented line is not working and I have no idea why if someone can give me some advice I'd be grateful <3

You can initially set the button disabled either in xml or through code.
android:enabled="false"
Then on click of the button you can make it enabled
Button btn_for = (Button) findViewById(R.id.Fortificacao);
Button btn_ten = (Button) findViewById(R.id.Ten);
btn_for.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn_ten .setEnabled(true);
}
});

Related

Dialog box close not working in primefaces

I am opening dialog through
public void openDialogRegionList() {
RequestContext.getCurrentInstance().openDialog("RegionList");
}
I have a commandLink onClick of that and i need to close dialog box.
public void setId(ActionEvent event){
UIComponent component = event.getComponent();
String value = (String) component.getAttributes().get("value");
this.regionId = value;
RequestContext.getCurrentInstance().closeDialog("RegionList");
}
But the dialog box is not closing.
Thanks in advance
You can add oncomplete="PF('dialogName').close()" attribute to your html button. Wether it doesn't work, other solution is to call the hide() method.

While typing in a text field enable a keystroke ENTER to activate a button in JavaFX

Such as typing into the Google search box the hitting ENTER activates the search
I've just been introduced to JavaFX and Scene Builder a few days ago so I'm learning the basics here. I have the latest version of JavaFX and am using Scene Builder to facilitate action events. Also, any pointers to relevant tutorials would be helpful. At one point in the day I was focused on the Keyboard section of the coding panel of Scene Builder, especially with the "On Key Released" event with no results. Thanks in advance
Here's a rough idea of what I'm trying to do:
#FXML
Text Field theTextField;
#FXML
Button theButton;
#FXML
void ButtonPressed() {
//do stuff here
}
#FXML
//when ENTER is pressed the button is activated
void textFieldEnterPressed() {
ButtonPressed();
}
In your FXML file, add a onKeyPressed handler
<TextField fx:id="yourTextField" onKeyPressed="#handleEnterPressed">
Implement the handler in you Controller
#FXML
public void handleEnterPressed(KeyEvent event)
if (event.getCode() == KeyCode.ENTER) {
// do some actions
}
}
In TextField, when you press Enter, you get notification through onAction. In your Java code you can add:
#FXML
private void handleTFAction(ActionEvent event) {
TextField source = (TextField)event.getSource();
System.out.println("You entered: "+source.getText());
}
In your FXML (or through JavaFX SceneBuilder designer) hook it up to your TextField's OnAction event. In FXML it looks something like this:
<TextField onAction="#handleTFAction" ... />

Android Fragment Interface Unresponsive

I am having a strange issue with my program that I cannot explain and I have thus far not been able to find a solution. I have a simple activity that will switch between fragments and run the user through an initial setup of the app. The first fragment is just a text view at the top, with a button on the bottom with an onClickListener set to call a method on the parent activity, however in testing, when I click on the button, nothing happens. It does not change color like a normal button would, and no click seems to be registered.
Here is the XML for the layout:
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/setup_intro" />
<Button
android:id="#+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:clickable="true"
android:width="72dp"
android:text="#string/next_button" />
</RelativeLayout>
And this is the fragment code where I implement the onClickListener
public class SetupFragmentInitialScreen extends SherlockFragment
{
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View parentView = null;
parentView = inflater.inflate(R.layout.setup_fragment_initial_screen,
container,
false);
Button nextButton = (Button)parentView.findViewById(R.id.next_button);
nextButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.v("ButtonPressed", "You Pressed the button!");
((InitialActivity)getActivity()).onInitialScreenNextPress();
}
});
return parentView;
}
}
And lastly, here is my code for my activity so far
public class InitialActivity extends SherlockFragmentActivity
{
private SetupFragmentInitialScreen initialScreen;
private SetupFragmentPreferenceOneScreen preferenceOneScreen;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
initialScreen = new SetupFragmentInitialScreen();
preferenceOneScreen = new SetupFragmentPreferenceOneScreen();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.replace(android.R.id.content,
initialScreen);
fragmentTransaction.commit();
}
public void onInitialScreenNextPress()
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.replace(android.R.id.content,
preferenceOneScreen);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
So far the code to me seems correct, but as I said, there is no reaction from the interface when I try to press the button.
Edit: I have added the following code to my Activity to check for touch events
#Override
public boolean onTouchEvent(MotionEvent event)
{
super.onTouchEvent(event);
Log.v("Touch Detected", "You are touching the screen.");
return false;
}
It logs events all over the screen, except for when I'm touching the button, so the activity is receiving touch events, but the UI itself is not. I also tried loading another interface which has a pair of radio buttons, and they too were unresponsive. Is there something I'm doing wrong with initializing the fragments?
Unfortunately none of the code you posted seems to point to what the issue is.
A button does not need an onClick listener in order to change color when pressed, so I wouldn't worry about that part. More importantly:
Is it possible that any transparent view is lying on top of the button and taking the click? DDMS has a "Dump View Hierarchy for UI Automator" button that may help you check on this.
Does your activity override dispatchTouchEvent(), onInterceptTouchEvent(), or related API's and could one of these be preventing the touch from reaching the button?
If you are applying custom theming to the button, do you have separate visuals for the pressed state?
I figured out what it was. For whatever reason the program didn't like me trying to do the fade out-fade in animation when loading the first fragment into the activity. I removed that line from the onCreate() method and it works fine now.

Hide "refresh" action bar item while showing indetermidate progress bar in action bar [duplicate]

How to show Indeterminate ProgressBar when Refresh button is pressed in ActionBarSherlock and again show Refresh Button when ViewGroup on refreshed?
Update 1:
I have a answer here which is incomplete. I am placing a bounty on question so that more developers can help build a good answer which can useful to others in future.
How can we show a Indeterminate ProgressBar which looks like the one shown in the image below
It seems like ActionBarSherlock doesn't provide specific method to animate a refresh MenuItem.
What you can do (by using classic android API) is to use the setActionView(int resId) method and give the id of a layout with a ProgressBar in it.
At the beginning of your refresh action just call :
item.setActionView(R.layout.refresh_menuitem);
And when your refresh action is finished call :
item.setActionView(null);
Here is a sample of what your layout file refresh_menuitem.xml can have :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:addStatesFromChildren="true"
android:focusable="true"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:gravity="center"
style="?attr/actionButtonStyle">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
style="#android:style/Widget.ProgressBar.Small"/>
</LinearLayout>
Here is how you add this kind of indeterminate ProgressBar with a ActionBarSherlock object : (actually it's easier the other one but the progressBar is shown alone and not above a MenuItem)
1 - Put this line in the onCreate() method before the setContentView() call :
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
-> This line specify that you will use the indeterminate ProgressBar function.
2 - Enable the indeterminate ProgressBar by calling :
setSupportProgressBarIndeterminateVisibility(true);
3 - Disable the indeterminate ProgressBar by calling :
setSupportProgressBarIndeterminateVisibility(false);
Remark : Have a look in the sample folder of the ActionBarSherlock folder. I found this code in the following file :
JakeWharton-ActionBarSherlock-9598f2b\samples\demos\src\com\actionbarsherlock\sample\demos\IndeterminateProgress.java
Here is a complete code:
private static final int menuItemIdRefresh = 10; //class constant
private boolean refresh;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem refreshItem = menu.add(0, menuItemIdRefresh, 0,
getString(R.string.action_refresh)).setShowAsActionFlags(
MenuItem.SHOW_AS_ACTION_ALWAYS);
if (isRefreshing) {
refreshItem.setActionView(R.layout.indeterminate_progress);
} else {
refreshItem.setActionView(null);
refreshItem.setIcon(R.drawable.ic_refresh);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case menuItemIdRefresh: {
//the user has pressed the refresh button
if (!isRefreshing) {
isRefreshing = true;
new RefreshMyViewAsyncTask().execute("");
}
}
break;
default:
break;
}
return false;
}
One last note, in order to get the above code working you´ll need also call supportInvalidateOptionsMenu(). You can add that to the RefreshMyViewAsyncTask's onPreExecute() method.
Hope this helps to somebody.

ChangeBackground of TableLayout when i click on a button on the same layout

i have created a linearlayout having 2 table layouts.in the 1st table i have button .now i want to change the 2dn table layout background when i click on the button.not the whole layout ,only the 2nd tablelayout background will be changed.
please help me with some sample code.
In your onCreate() method you need to add a listener to the button and in that listener you can retrieve the view (table layout) and modify its properties. To find the button and the view you're changing you'll need to specify IDs for them in the XML (assuming you're inflating it from XML). Try something like this (untested) code:
Button button = (Button) findViewById(R.id.mybutton);
button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(final View view)
{
TableLayout tableLayout = (TableLayout) findViewById(R.id.mytablelayout);
tableLayout.setBackgroundResource(R.drawable.mybackgroundimage);
}
});

Resources