Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:51:45+00:00 2026-05-28T04:51:45+00:00

package woot.wat.wen; import android.app.Activity; import android.os.Bundle; import android.text.Layout; import android.util.Log; import android.view.KeyEvent; import android.view.View;

  • 0
   package woot.wat.wen;

import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class HmmActivity extends Activity implements OnKeyListener  {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //RelativeLayout Relay=(RelativeLayout) getResources().getLayout(R.layout.main);
        ViewGroup group = (ViewGroup) findViewById(R.id.relativeLayout1);
        TextView tv=(TextView) findViewById(R.id.tv1);
        MarginLayoutParams mapara = new MarginLayoutParams(tv.getLayoutParams());
        mapara.setMargins(225, 260, 120, 120);


        LayoutParams params=new RelativeLayout.LayoutParams(mapara);
        tv.setLayoutParams(params);
        group.setFocusable(true);
        group.setOnKeyListener(this);

        int children = group.getChildCount();
        for (int i = 0; i < children; i++) {

            View child = group.getChildAt(i);
            child.setFocusable(true);
            child.setOnKeyListener(this);


            }



        //tv.setOnKeyListener(this);


    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK &&  event.getRepeatCount() == 0) {
              //    back=true;
                    // do something on back.
            Toast.makeText(this, "Back key pressed", Toast.LENGTH_SHORT);




            System.out.println("WTF");


                    return true;
                }



        else    

            return super.onKeyDown(keyCode, event);
    }


    public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK /*&& event.getRepeatCount() == 0*/) {
              //    back=true;
                    // do something on back.
                    Log.d("backk", "goingoin backback");






                    return true;
                }

                else
                // TODO Auto-generated method stub
                return super.onKeyDown(keyCode, event);
                //return false;
            }






    }

As you can see in the code i am trying to do something when the back key is pressed but the onkey codes never seem to be used.
The program never seems to go into onKey method whenever back is pressed,both in emulator and the device.Any idea what i am doing wrong..?

I’ve put the updated code.

Ok I’ve created a new clean project.Heres the code

package you.packag.namespac;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class BlActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
    if(keyCode==KeyEvent.KEYCODE_BACK){
        Toast.makeText(this, "Back key pressed", Toast.LENGTH_SHORT);
        return true; //that is important
    }else 
    return super.onKeyDown(keyCode, event);
}
}

Still the same problem.The toast is not displayed.The program flow/control goes to both the return true and return super statements each time back is pressed.Dont really see how that should be happening.Really driving me crazy.Must be something small I am doing wrong.Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:51:46+00:00Added an answer on May 28, 2026 at 4:51 am

    Try to

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK){
            Toast.makeText(this, "Back key pressed", Toast.LENGTH_SHORT).show();
            return true; //that is important
        }else 
        return super.onKeyDown(keyCode, event);
    }
    

    Or in your code:

    1) Put @Override before your onKey method

    2) change return false to return super.onKey(keyCode, event)

    Also – it’s not neccesary to implement OnKeyListener

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package com.rest; import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button;
package com.commonsware.android.layouts; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import java.util.Date; public class
package com.ebonybutler.cexample3; import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener;
package com.example.android.home; import android.app.Activity; import android.os.Bundle; import android.widget.*; import android.view.*; public class HomeActivity extends
package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.view.View; import android.view.View.OnClickListener; //Create an
package com.aviyehuda.test.multithreading; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.TextView;
package com.ave; import com.ave.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageButton;
package com.parseador.prueba; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class main extends
package com.ustr.eMIRnew; import java.util.ArrayList; import java.util.HashMap; import android.R; import android.app.Activity; import android.os.Bundle; import android.widget.ListView;
package com.duncan.hello.world; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.