Do this in your onCreate()
method.
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.your_layout_name_here);
this
refers to the Activity
.
Question 1:
Unfortunately the one in which you you say is most intuitive is the least used in Android. As I understand, you should separate your UI (XML) and computational functionality (Java Class Files). It also makes for easier debugging. It is actually a lot easier to read this way and think about Android imo.
Question 2:
I believe the two mainly used are #2 and #3. I will use a Button clickButton as an example.
2
is in the form of an anonymous class.
Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
***Do what you want with the click here***
}
});
This is my favorite as it has the onClick method right next to where the button variable was set with the findViewById. It seems very neat and tidy that everything that deals with this clickButton Button View is located here.
A con that my coworker comments, is that imagine you have many views that need onclick listener. You can see that your onCreate will get very long in length. So that why he likes to use:
3
Say you have, 5 clickButtons:
Make sure your Activity/Fragment implement OnClickListener
// in OnCreate
Button mClickButton1 = (Button)findViewById(R.id.clickButton1);
mClickButton1.setOnClickListener(this);
Button mClickButton2 = (Button)findViewById(R.id.clickButton2);
mClickButton2.setOnClickListener(this);
Button mClickButton3 = (Button)findViewById(R.id.clickButton3);
mClickButton3.setOnClickListener(this);
Button mClickButton4 = (Button)findViewById(R.id.clickButton4);
mClickButton4.setOnClickListener(this);
Button mClickButton5 = (Button)findViewById(R.id.clickButton5);
mClickButton5.setOnClickListener(this);
// somewhere else in your code
public void onClick(View v) {
switch (v.getId()) {
case R.id.clickButton1: {
// do something for button 1 click
break;
}
case R.id.clickButton2: {
// do something for button 2 click
break;
}
//.... etc
}
}
This way as my coworker explains is neater in his eyes, as all the onClick computation is handled in one place and not crowding the onCreate method. But the downside I see is, that the:
- views themselves,
- and any other object that might be located in onCreate used by the onClick method will have to be made into a field.
Let me know if you would like more information. I didn't answer your question fully because it is a pretty long question. And if I find some sites I will expand my answer, right now I'm just giving some experience.
Best Answer
No, that is not possible via code. Android just implements the
OnClickListener
for you when you define theandroid:onClick="someMethod"
attribute.Those two code snippets are equal, just implemented in two different ways.
Code Implementation
Above is a code implementation of an
OnClickListener
. And this is the XML implementation.XML Implementation
In the background, Android does nothing else than the Java code, calling your method on a click event.
Note that with the XML above, Android will look for the
onClick
methodmyFancyMethod()
only in the current Activity. This is important to remember if you are using fragments, since even if you add the XML above using a fragment, Android will not look for theonClick
method in the.java
file of the fragment used to add the XML.Another important thing I noticed. You mentioned you don't prefer anonymous methods. You meant to say you don't like anonymous classes.