I have that code:
public class GeolocationActivity extends ActionBarActivity implements SensorEventListener {
{
//...
private void setUpCompass()
{
isCompass = true;
setContentView(R.layout.subactivity_compass);
//...
}
private void setUpMain()
{
//...
btn2 = (Button)findViewById(R.id.geoloc_compass);
btn2.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// here I want to call method setUpCompass();
}
});
//...
}
}
I want to call setUpCompass() method from onClick() function in setUpMain().
I tried:
Context c = getBaseContext();
((GeolocationActivity)c).setUpCompass();
but I get:
java.lang.ClassCastException: android.app.ContextImpl cannot be cast to com.myname.myapp.GeolocationActivity
What am I doing wrong?
Instead simply use GeolocationActivity.this.
Example:
GeolocationActivity.this.setUpCompass();
Related
I wrote a compound component and was adding a custom listener to react.
Inside the class for the compound component which uses an xml file.
public class VerticalCounterBlock extends LinearLayout {
public interface VerticalCounterBlockListener {
public void onCountChanged(int newCount);
}
private VerticalCounterBlockListener mVerticalCounterBlockListener = null;
public void setVerticalCounterBlockListener(VerticalCounterBlockListener listener){
mVerticalCounterBlockListener = listener;
}
// ... Other functions
}
I got my interface, I got the listener and I got the setter and I engage the listener like this in the button I have in the compound component. I can see that toast that is showing there when I test
addBtn = (Button)findViewById(R.id.btn_addcount);
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
count++;
counttv.setText(String.format("%1$d", count));
Toast.makeText(getContext(), "VCB", Toast.LENGTH_SHORT).show();
if(mVerticalCounterBlockListener != null) {
mVerticalCounterBlockListener.onCountChanged(count);
}
}
});
In my main activity
m20_vcb = (VerticalCounterBlock) findViewById(R.id.vcb_m20);
m20_vcb.setVerticalCounterBlockListener(new VerticalCounterBlock.VerticalCounterBlockListener() {
#Override
public void onCountChanged(int newCount) {
increasePreachCountTotal();
Toast.makeText(CounterActivity.this, String.format("%1$d", newCount), Toast.LENGTH_SHORT).show();
}
});
I do not see that toast nor does it engage the function call. What am I missing?
I can suggest you several improvement scope here mainly restructuring the current format.
Lets not keep the interface as a inner class. So here's your VerticalCounterBlockListener.java
public interface VerticalCounterBlockListener {
public void onCountChanged(int newCount);
}
Now implement this interface in your MainActivity
public class MainActivity implements VerticalCounterBlockListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
m20_vcb = (VerticalCounterBlock) findViewById(R.id.vcb_m20);
m20_vcb.setVerticalCounterBlockListener(this);
}
// ... Other methods
// Override the onCountChanged function.
#Override
public void onCountChanged(int newCount) {
increasePreachCountTotal();
Toast.makeText(CounterActivity.this, String.format("%1$d", newCount), Toast.LENGTH_SHORT).show();
}
}
You might consider removing the Toast from the addBtn click listener which might create exception.
addBtn = (Button)findViewById(R.id.btn_addcount);
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
count++;
counttv.setText(String.format("%1$d", count));
if(mVerticalCounterBlockListener != null) {
mVerticalCounterBlockListener.onCountChanged(count);
}
}
});
This was good there was something wrong with my system. i uninstaklled app and restarted computer and it worked as expected.
I have searched but could not find answer of my question.
This is what I have:
private class BoxView extends View {
private String caption;
private OnClickListener bvClickListener = null
public BoxView(Context context) {
super(context);
this.bvClickListener = new this.OnClickListener(){
public void onClick (View v){
/*v.setCaption("X"); view don't have this method */
}}
}
public void setCaption(String s){
this.caption=s;
invalidate();
}
}
This is what I want to have:
private class BoxView extends View {
private String caption;
private OnClickListener bvClickListener = null
public BoxView(Context context) {
super(context);
this.bvClickListener = new this.OnClickListener(){
public void onClick (BoxView bv){
bv.setCaption("X");
}}
}
public void setCaption(String s){
this.caption=s;
invalidate();
}
}
I may need custom methods for my custom views. And I want to be able to pass my custom view instead of view version of it when onclick is triggered so I can access to it directly.
Updated
And I want to have access to real object not a converted one. So I want to avoid this:
public void onClick (View v){
((BoxView)v).setCaption("X");
}
Call setCaption method as in onClick :
public void onClick (View v){
((BoxView)v).setCaption("X");
}
Try this
class Main extents Activity
{
BoxView boxView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// if view is used using layout then
boxView = (BoxView)findViewByID(id);
//else if directly used
boxView = new BoxView(this);
box.setOnClickListener(new onClickListener()
{
#Override
public void onClick(View view) {
boxView.setCaption("X");
boxView.invalidate();
}
});
}
}
Calling onBackPressed method of Activity class on a View Class? is it possible? and so how can I? thanks for help
UPDATE: I just created the GameView of my Game which is extends to View Class. I create a variable that increments whenever I finish every level so it will limit a levels per game. And so I want to implement a onBackPressed method which I can set the incrementing variable back to zero whenever the player press the back key.
FULL CODE: MAIN ACTIVITY
public class MainActivity extends Activity {
private GameView mGameView;
#Override
public void onBackPressed() {
if (mGameView.interceptBackPressed()) {
return;
}
// TODO Auto-generated method stub
super.onBackPressed();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mGameView = (GameView) findViewById(GameView.countmaze);
Afterwards it goes to Menu Class
public class menu extends Activity implements OnClickListener {
static int nextmaze;
int countmaze = 0;
GameView gameView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnplay = (Button) findViewById(R.id.btnPlay);
btnplay.setOnClickListener(this);
}
#Override
public void onBackPressed() {
//I have here an AlertDialog builder
}
#Override
public void onClick(View v) {
case R.id.btnPlay:
Toast.makeText(getApplicationContext(), "LEVEL 1",
Toast.LENGTH_SHORT).show();
startnextmaze();
}
}
void startnextmaze() {
Random rand = new Random();
Intent game = new Intent(menu.this, Game.class);
nextmaze = rand.nextInt(5) + 1;
Maze maze = MazeCreator.getMaze(nextmaze);
game.putExtra("maze", maze);
startActivity(game);
}
Then on my GameView Class
public class GameView extends View {
static int countmaze;
//Big codes here......
public boolean interceptBackPressed() {
// TODO Auto-generated method stub
countmaze = 0;
return true;
}
}
Create a callback on the Activity onBackPressed that the View class will implement
Example
public void interface onBackPressedHandler {
public void onBackPressed();
}
Activity Class
public onBackPressedHandler mHandler;
#Override
public void onCreate(....) {
GameView game... //Inflate or create.
game.setActivity(this);
}
public void setListener(onBackPressedHandler handler) {
mHandler = handler;
}
#Override
public voind onBackPressed() {
if(mHandler != null) {
mHandler.onBackPressed();
}
super.onBackPressed();
View Class
public GameView extends View implements onBackPressedHandler {
public void setActivity(Activity activity) {
activity.setListener(this);
}
public void onBackPressed() {
//your code here.
}
}
This example shows when you press the back Button it will call the onBackPress on the View.
Hope it Helps.
Update your code with the following changes:
Activity
public class MyActivity extends Activity {
private GameView mGameView;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);
//View reference to object defined in XML
mGameView = (GameView) findViewById(R.id.gameview);//id must be specified in XML
//I assumed that GameView is a part of your XML layout
//View created programmatically.
mGameView = new GameView(this);
//Use one of the above initialisation techniques of mGameView.
}
#Override
public void onBackPressed() {
//here we are calling method on mGameView which cannot be null
//that's why we initialised it in onCreate() method
if (mGameView.interceptBackPressed()) {
return;
}
super.onBackPressed();//finish your Activity
}
}
GameView
public class GameView extends View {
public boolean interceptBackPressed() {
//TODO handle your game logic here and return true if you don't
//want your game to be shutdown, otherwise return false
return true;
}
}
how can I make listener for multiple buttons in Android with for loop? Like in java :
private class Akcija implements ActionListener {
public void actionPerformed(ActionEvent e){
for(int r=0;r<brDugm;r++){
if (e.getSource() == b[r]) {
....
}
}
}
for(int r=0;r<brDugm;r++)
{
// Assuming b[r] is your button as object
// Assuming your action has is a function in your class Currentclass
b[r].addOnClickListener(new OnClickListener() {
public void onClick()
{
Currentclass.this.actionOnClick();
}
});
}
Your activity should implement the interface View.OnClickListener, and override the OnClick(View view) method.
And then you do something like this:
for ( int i=0;i<numButtons; i++ )
{ buttons[i].setOnClickListener(this);
}
And in your OnClick(View view) method you implement your code.
I want to create by code an array of objects that are subclasses of Button.
public class MyButton extends Button {
private Context ctx;
private int status;
public MyButton(Context context) {
super(context);
ctx = context;
status = 0;
}
private click() {
status = 1;
// OTHER CODE THAT NEEDS TO STAY HERE
}
}
In the main activity I do this:
public class myActivity extends Activity {
private MyButton[] myButtons = new MyButton[100];
#Override
public onCreate(Bundle si) {
super.onCreate(si);
createButtons();
}
private void createButtons() {
for (int w=0; w<100; w++) {
myButtons[w] = new MyButton(myActivity.this);
myButtons[w].setOnClickListener(new View.onClickListener() {
public void onClick(View v) {
// ... (A)
}
});
}
}
}
Now I want the click() method inside MyButton to be run each time the button is clicked.
Seems obvious but it is not at my eyes.
If I make the click() method public and run it directly from (A), I get an error because myButtons[w].click() is not static and cannot be run from there.
In the meantime, I an not able to understand where to put the code in the MyButton class to intercept a click and run click() from there. Should I override onClick? Or should I override onClickListener? Or what else should I do?
How can I run click() whenever one of myButtons[] object is clicked?
Thanks for the help.
You can cast View v you got in listener to MyButton and call click on it:
private void createButtons() {
View.OnClickListener listener = new View.onClickListener() {
public void onClick(View v) {
((MyButton) v).click();
}
};
for (int w=0; w<100; w++) {
myButtons[w] = new MyButton(myActivity.this);
myButtons[w].setOnClickListener(listener);
}
}
you can add:
View.onClickListener onclick = new View.onClickListener() {
public void onClick(View v) {
((MyButton)v).click();
//since v should be instance of MyButton
}
};
to your Activity
then use:
myButtons[w].setOnClickListener(onclick);
//one instance of onclick is enough, there is no need to create it for every button
in createButtons()
but ... why, oh why array of buttons we have ListView in android ...