How do I edit properties of components? - java

Let's say I have a button. How would I programmatically edit its properties? I don't want to change the properties using XML code in the layout.

Declare your Button variable:
Button button;
...
Then write the following in your onCreate() method:
button = (Button) findViewById(R.id.<Your buttons ID>);
button.setHeight(50);
Please rather do research before asking questions.

Related

How to post a message in android studio

I want to make a button which will post a message once the button is hit. Here is my main activity.java :
enter image description here
But I confuse why I cannot choose the onclick option for the button :
enter image description here
or I dont have to choose the onClick button option again ?
Hope you undestand my question, and here is my video reference from youtube :
"Android Tutorial for Beginners 8 # wrap_content, fill_parent, Password Field and Toast in Android" Uploaded by : ProgrammingKnowledge
Replace the onCreate() method as
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListnerOnButton(); //Add this line to your code.
}
you are doing wrong in android you can set click listener by two ways
1. In Java code
2. In XML
in your example you are doing runtime in java code so it will not show in XML editor
in your case just call your addListenerOnButton in onCreate it will work
if you want it in xml
add onClick function in activity class as below
public void buttonClick(View v)
{
//do what you want
}
then this buttonClick will be shown in editor or you can directly add in xml button onClick attribute
<Button ...
android:onClick="buttonClick"/>

Making a button 'disappear' when pressed

So I'm trying to make a kind of Wheel of Fortune game or Hangman. I have 33 buttons which represent the alphabet, 1 button = 1 letter. When a user presses one, it has to 'dissapear' (become disable and invisible). I created all the buttons in the SceneBuilder so they are located in the FXML file.
How do I actually do that? I created this method for the first button. But it doesn't work properly, no matter what button I press the first one dissapear. Is there an easier way to do it wIthout writing 33 different methods for each button?
public void letterChosen (ActionEvent evt) {
b1.setDisable(true);
b1.setVisible(false);
The Button that was clicked is available as the source of the ActionEvent.
Additionally userData could be attached to the Button, in case you cannot get the necessary information to handle the button click from other properties of the Button:
public void letterChosen(ActionEvent event) {
Button source = (Button) event.getSource();
source.setVisible(false);
System.out.println("pick: "+source.getUserData());
}
FXML
<Button onAction="#letterChosen" userData="a" text="A"/>
<Button onAction="#letterChosen" userData="b" text="B"/>
Note that it isn't necessary to disable a Node that isn't shown, since a Node that is not visible cannot be interacted with. a disabled Button will appear "faded" by default, but could also be shown differently, e.g. using CSS.

Android - Get Other Layout Widget

I've been trying to get a widget from another layout for past hours but still no luck. Can someone tell what is wrong with my current code?
public void refreshClicked(View view){
tab1 = LayoutInflater.from(view.getContext()).inflate(R.layout.tab_1, null);
Button b = (Button) tab1.findViewById(R.id.refresh);
b.setText("Updating...");
}
I've hooked my button onClick to refreshClicked(View view) method.
This method is under my MainActivity.java and I'm trying to get the Button with id refresh from tab_1.xml layout but the button text is not updating.
Thanks!
You seems to inflate a layout but never attach it to a parent view as the ViewGroup parameter is null. Then this layout inflated must not even been displayed anywhere.
If the button b already exists, no need to inflate the layout and retrieve the button form there, it will a different button.
Just retrieve the button from the actual existing layout.

Change logo programmatically in Fragment

How to change the title bar inside a fragment? Somewhat I am not able to succeed.
setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);
I think the problem is that you can't access the ActionBar APIs from the fragment. So, what you need is an access to the Parent Activity.
Try : getActivity().setTitle("My new title");

SmartGWT- how to change filter button icon in listGrid?

I want to change this filter icon to another one, I have tried this method setFilterButtonProperties(newButton);
but it doesn't work.
here the image which I want to change.
Try this one
Button newButton=new Button("");
newButton.setSize("18px", "18px");
newButton.setIcon("[SKIN]/RecordEditor/add.png");
listGrid.setFilterButtonProperties(newButton);
Note: Change the icon path and size as per your requirement.

Categories