Which is the best way to add a button?
I think you are confused. The examples you give are two different things.
Adding a Button
This line
Button b1=(Button) findViewById(R.id.button1);
doesn't add a Button
. It declares and initializes an instance of Button
which refers to a Button
in your currently inflated xml which has an id
of button1
So in your xml you would have somewhere
<Button
android:id="@+id/button1"
<!-- other properties -->
/>
You can add a Button
programmatically with
Button bt1 = new Button(this);
// give it properties
But it is generally easier to do in xml because here you have to programmatically give it parameters, properties, and add it to an inflated layout
OnClick
As far as the onClick()
it depends on what you feel is the easiest and best in your situation. I like to declare it in the xml like that often but you can do it several ways. Using this method you just have to be sure that you have a function like this that is public
and takes only one parameter and that parameter must be a View
public void clickEvent(View v)
{
// code here
}
I also changed the name so your xml would be like
<Button
android:id="@+id/button1"
<!-- other properties -->
android:onClick="clickEvent"/>
You also can set onClick()
in your Java with something like
Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// code here
}
});
or
Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
@Override
public void onClick(View v)
{
// code here
}
Note that the last way you will need to add implements OnClickListener
in your Activity
declaration
public class MyActivity extends Activity implements OnClickListener
{
You can also create your own click Listener
by changing it to something like
b1.setOnClickListener(myBtnClick);
then create an instance of it with something like
public OnClickListener myBtnClick = new OnClickListener()
{
@Override
public void onClick(View v)
{
// click code here
}
};
You can use this for multiple Button
s and switch on the id
or check the View
param to know which Button
was clicked or create separate Listeners
for different Button
s.
Place a button right aligned
Which alignment technique you use depends on your circumstances but the basic one is float: right;
:
<input type="button" value="Click Me" style="float: right;">
You'll probably want to clear your floats though but that can be done with overflow:hidden
on the parent container or an explicit <div style="clear: both;"></div>
at the bottom of the container.
For example: http://jsfiddle.net/ambiguous/8UvVg/
Floated elements are removed from the normal document flow so they can overflow their parent's boundary and mess up the parent's height, the clear:both
CSS takes care of that (as does overflow:hidden
). Play around with the JSFiddle example I added to see how floating and clearing behave (you'll want to drop the overflow:hidden
first though).
How do I create an HTML button that acts like a link?
HTML
The plain HTML way is to put it in a <form>
wherein you specify the desired target URL in the action
attribute.
<form action="https://google.com">
<input type="submit" value="Go to Google" />
</form>
If necessary, set CSS display: inline;
on the form to keep it in the flow with the surrounding text. Instead of <input type="submit">
in above example, you can also use <button type="submit">
. The only difference is that the <button>
element allows children.
You'd intuitively expect to be able to use <button href="https://google.com">
analogous with the <a>
element, but unfortunately no, this attribute does not exist according to HTML specification.
CSS
If CSS is allowed, simply use an <a>
which you style to look like a button using among others the appearance
property (it's only not supported in Internet Explorer).
a.button {
-webkit-appearance: button;
-moz-appearance: button;
appearance: button;
text-decoration: none;
color: initial;
}
<a href="https://google.com" class="button">Go to Google</a>
Looking for the best way to add an icon to a button on css
You can use it like this:
input[type='button'] {
background-image: url(/media/colour/group.png);
background-position: 7px 7px;
background-repeat: no-repeat;
}
adding button to top right of block level elements
(1) One way is to float the button
right at the top of the container.
<div><button>My Button</button>some content here</div>
div + button {
float: right;
}
(2) Another way is to position the button
absolutely inside the div
container and give the div
container position (so that elements postioned absolutely inside it are relative to this container). This way the button can be anywhere in the markup, providing it is inside the container.
div {
position: relative;
}
div button {
position: absolute;
top: 0;
right: 0;
}
However, the button will now be on top of other content inside the container so you might have to adjust this with padding etc.
What's the best way to add a bunch of Button to a ScrollPane?
Sounds like you want your buttons laid out within a grid within a scroll pane.
The appropriate layout for that would be a GridPane
.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class ButtonLinesInScrollPane extends Application {
private static final double BUTTONS_PER_LINE = 8;
private static final double NUM_BUTTON_LINES = 8;
private static final double BUTTON_PADDING = 5;
@Override
public void start(Stage stage) {
GridPane grid = new GridPane();
grid.setPadding(new Insets(BUTTON_PADDING));
grid.setHgap(BUTTON_PADDING);
grid.setVgap(BUTTON_PADDING);
for (int r = 0; r < NUM_BUTTON_LINES; r++) {
for (int c = 0; c < BUTTONS_PER_LINE; c++) {
Button button = new Button(r + ":" + c);
grid.add(button, c, r);
}
}
ScrollPane scrollPane = new ScrollPane(grid);
stage.setScene(new Scene(scrollPane));
stage.show();
}
public static void main(String[] args) { launch(args); }
}
Getting the button into the top right corner inside the div box
Just add position:absolute; top:0; right:0;
to the CSS for your button.
#button {
line-height: 12px;
width: 18px;
font-size: 8pt;
font-family: tahoma;
margin-top: 1px;
margin-right: 2px;
position:absolute;
top:0;
right:0;
}
jsFiddle example
How to add a button into a nother application's window using C#
Try this (tested using Skype for Windows Desktop version 6.11.0.102)
public void AttachButtonToSkype() {
// find skype main window (className = tSkMainForm)
var mainHandle = NativeMethods.FindWindow("tSkMainForm", null);
if (mainHandle != IntPtr.Zero) {
// find child window to inject (className = TMyselfControl)
var parentHandle = NativeMethods.FindWindowEx(mainHandle, IntPtr.Zero, "TMyselfControl", null);
if (parentHandle != IntPtr.Zero)
{
var button = new Button { Text = "Click Me!", Left = 150, Top = 5, Width = 75, Height = 25 };
button.Click += (o, args) => { MessageBox.Show("You've clicked me"); };
NativeMethods.SetParent(button.Handle, parentHandle);
}
}
}
NativeMethods
internal class NativeMethods {
[DllImport("User32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string ClassN, string WindN);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
}
Tips: Use Spy++ to find window className
Related Topics
What Characters Allowed in File Names on Android
Getting View's Coordinates Relative to the Root Layout
Firebase Analytics Custom Events Params
How to Access Call Log for Android
Android - Implementing Search Filter to a Recyclerview
Adb' Is Not Recognized as an Internal or External Command, Operable Program or Batch File
App Is Misconfigured for Facebook Login: Android Facebook Integration Issue
Remove All Unused Resources from an Android Project
Date and Time Change Listener in Android
Android Activity Over Default Lock Screen
Nullpointerexception:Println Needs a Message in Android
Find Distance Between Two Points on Map Using Google Map API V2
Use Picasso to Get a Callback with a Bitmap
How to Decompile an Android .Apk File
Webview Load Website When Online, Load Local File When Offline