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

  • Home
  • SEARCH
  • 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 7750683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:21:14+00:00 2026-06-01T11:21:14+00:00

I’ve been following a tutorial that explains how to use background for a button

  • 0

I’ve been following a tutorial that explains how to use background for a button with different states but it doesn’t seem to work :S

Here is my code :

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/boutonn" android:state_window_focused="false"/>
    <item android:drawable="@drawable/boutonnpousse" android:state_pressed="true"/>
    <item android:drawable="@drawable/boutonnpousse" android:state_focused="true"/>
    <item android:drawable="@drawable/boutonn" android:state_focused="false" 
    android:state_pressed="false" />

</selector>

This is an xml code that I’ve placed in my drawable folder, here is a part of the xml of the activity that uses these buttons :

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/backgrounddd"
    android:orientation="vertical" >

            <Button
                android:id="@+id/bNoteRemind"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:background="@drawable/imagebutton1" /> 
    ...

And here is the java class :

public class MenuPrincipal extends Activity {

    Button NoteRemind;          

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        //on lui associe le layout menuprincipal.xml
        setContentView(R.layout.menuprincipal);

        NoteRemind = (Button) findViewById(R.id.bNoteRemind);     

        // Si on choisit de rédiger une nouvelle task on va être rediriger sur l'activité NoteReminder

        NoteRemind.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                //On créé l'Intent qui va nous permettre d'afficher l'autre Activity
                //Mettez le nom de l'Activity dans la quelle vous êtes actuellement pour le premier parametre
                v.setPressed(true);

                Intent intent = new Intent(MenuPrincipal.this, NoteReminder.class);
                //Intent intent = new Intent(MenuPrincipal.this, Teste2.class);
                //On démarre l'autre Activity
                startActivity(intent);


            }
        }); ....

The button displays well but when I press it it doesn t show the pressed image :s I don’t understand what I am doing wrong !

Does anyone see an error somewhere ???


Where should I put those lines ? I ve put them in my button xml

    <Button
        android:id="@+id/bNoteRemind"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="@drawable/imagebutton1"
        android:focusable="true"
        android:focusableInTouchMode="true" />

But now my button background changed to the pressed image without me pressing it :p and it doesn’t change

  • 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-06-01T11:21:16+00:00Added an answer on June 1, 2026 at 11:21 am

    Is the Button the only thing you have displayed in your Activity? If so, then it will be focused (triggering the third item in your selector) when the window loads, and you won’t be able to navigate away from it. If you want to change only when pressed, delete that third line. While you’re at it, delete the first line, as the button will never be pressed when the window isn’t focused.

    In fact, I suggest this code:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/boutonnpousse" android:state_pressed="true"/>
        <item android:drawable="@drawable/boutonn"/>
    </selector>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

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.