I'm using Eclipse for Android development and I have already set up my code formatting style but still have the anonymous methods that I couldn't figure out how to format in Eclipse. This is how Eclipse formats anonymous methods now:
// The BroadcastReceiver that listens for discovered devices and
// changes the title when discovery is finished
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Utils.Log.i("BLUETOOTH: " + action);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the
// BluetoothDevice
// object from the
// Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already
// paired, skip it,
// because it's been
// listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
if (mNewDevicesArrayAdapter.getCount() == 0) {
mNewDevicesArrayAdapter.add(device);
}
btDevicesUpdateList.add(device);
}
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
mNewDevicesArrayAdapter.setItems(btDevicesUpdateList);
mNewDevicesArrayAdapter.notifyDataSetChanged();
btDevicesUpdateList.clear();
mBtAdapter.startDiscovery();
}
else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (mBtAdapter.getState() == BluetoothAdapter.STATE_ON) {
switchToView(viewBluetoothOn);
firstTimeDiscover();
}
else if (mBtAdapter.getState() == BluetoothAdapter.STATE_OFF) {
switchToView(viewBluetoothOff);
}
}
}
};
See? Its very crappy. What is the correct setting to format the anonymous method declaration to stay in the left side and don't go under the =
equal character?
You can use Format Document command to format a Java file. If you didn't specify a formatter profile before, the Java file will be formatted using default settings.
I believe the setting that causes this bad formatting is "Align fields in columns", if you turn this off, the class/interface implementation should be indented from the start of the line instead of the equals.
I opened a bug at eclipse to either fix the default behavior or to add another setting for class/interface implementations.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=385901
The work-a-round for this issue is to slightly change your coding style. Cut and paste the following and run your formatter. Style #2 will look less crappy.
// **Style #1** - Formatter handles poorly
JDialog jDialog1 = new JDialog(new JFrame()) {
public void setBackground(final Color c) {
super.setBackground(c);
}
};
// **Style #2** - Formatter handles well.
JDialog jDialog2;
{
jDialog2 = new JDialog(new JFrame()) {
public void setBackground(final Color c) {
super.setBackground(c);
}
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With