Rectangle 27 65

Save this code in drawable folder with "bg_button.xml" and call "@drawable/bg_button" as background of button in your xml:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#004F81" />
            <stroke
                android:width="1dp"
                android:color="#222222" />
            <corners
                android:radius="7dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#89cbee"
                android:endColor="#004F81"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#4aa5d4" />
            <corners
                android:radius="7dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

Thanks man, using style you can reduce application size. good idea to not use drawable

On the other hand, xml vector-based drawables are usually worse for performance than images, especially on older devices. Don't optimize prematurely, but keep that in mind as well.

How to Change color of Button in Android when Clicked? - Stack Overflo...

android
Rectangle 27 65

Save this code in drawable folder with "bg_button.xml" and call "@drawable/bg_button" as background of button in your xml:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#004F81" />
            <stroke
                android:width="1dp"
                android:color="#222222" />
            <corners
                android:radius="7dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#89cbee"
                android:endColor="#004F81"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#4aa5d4" />
            <corners
                android:radius="7dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

Thanks man, using style you can reduce application size. good idea to not use drawable

On the other hand, xml vector-based drawables are usually worse for performance than images, especially on older devices. Don't optimize prematurely, but keep that in mind as well.

How to Change color of Button in Android when Clicked? - Stack Overflo...

android
Rectangle 27 28

1. Using the onClick attribute of the Button. (Beginner)

Current responses are great but a more comprehensive answer is needed for beginners. There are 3 different ways to start a new activity in Android, and they all use the Intent class; Intent | Android Developers.

  • Using the onClick attribute of the Button. (Beginner)
OnClickListener()
  • Activity wide interface method using the switch statement. (Pro)

Buttons have an onClick attribute that is found within the .xml file:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="goToAnActivity"
    android:text="to an activity" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="goToAnotherActivity"
    android:text="to another activity" />

Advantage: Easy to make on the fly, modular, and can easily set multiple onClicks to the same intent easily.

This is when you set a separate setOnClickListener() to each button and override each onClick() with its own intent.

Disadvantage: there will be a lot of anonymous classes which will make readability difficult when reviewing.

This is when you use a switch statement for your buttons within the onClick() method to manage all the Activity's buttons.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.button1:
            Intent intent1 = new Intent(this, AnActivity.class);
            startActivity(intent1);
            break;
        case R.id.button2:
            Intent intent2 = new Intent(this, AnotherActivity.class);
            startActivity(intent2);
            break;
        default:
            break;
    }

Advantage: Easy button management because all button intents are registered in a single onClick() method

android - How to start new activity on button click - Stack Overflow

android android-intent android-activity android-button android-lifecycle
Rectangle 27 27

1. Using the onClick attribute of the Button. (Beginner)

Current responses are great but a more comprehensive answer is needed for beginners. There are 3 different ways to start a new activity in Android, and they all use the Intent class; Intent | Android Developers.

  • Using the onClick attribute of the Button. (Beginner)
OnClickListener()
  • Activity wide interface method using the switch statement. (Pro)

Buttons have an onClick attribute that is found within the .xml file:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="goToAnActivity"
    android:text="to an activity" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="goToAnotherActivity"
    android:text="to another activity" />

Advantage: Easy to make on the fly, modular, and can easily set multiple onClicks to the same intent easily.

This is when you set a separate setOnClickListener() to each button and override each onClick() with its own intent.

Disadvantage: there will be a lot of anonymous classes which will make readability difficult when reviewing.

This is when you use a switch statement for your buttons within the onClick() method to manage all the Activity's buttons.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.button1:
            Intent intent1 = new Intent(this, AnActivity.class);
            startActivity(intent1);
            break;
        case R.id.button2:
            Intent intent2 = new Intent(this, AnotherActivity.class);
            startActivity(intent2);
            break;
        default:
            break;
    }

Advantage: Easy button management because all button intents are registered in a single onClick() method

android - How to start new activity on button click - Stack Overflow

android android-intent android-activity android-button android-lifecycle
Rectangle 27 19

private Bitmap mBitmap;
private Canvas mCanvas;
private Rect mBounds;

public void initIfNeeded() {
  if(mBitmap == null) {
    mBitmap = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
    mBounds = new Rect();
  }
}

public int getBackgroundColor(View view) {
  // The actual color, not the id.
  int color = Color.BLACK;

  if(view.getBackground() instanceof ColorDrawable) {
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
      initIfNeeded();

      // If the ColorDrawable makes use of its bounds in the draw method,
      // we may not be able to get the color we want. This is not the usual
      // case before Ice Cream Sandwich (4.0.1 r1).
      // Yet, we change the bounds temporarily, just to be sure that we are
      // successful.
      ColorDrawable colorDrawable = (ColorDrawable)view.getBackground();

      mBounds.set(colorDrawable.getBounds()); // Save the original bounds.
      colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds.

      colorDrawable.draw(mCanvas);
      color = mBitmap.getPixel(0, 0);

      colorDrawable.setBounds(mBounds); // Restore the original bounds.
    }
    else {
      color = ((ColorDrawable)view.getBackground()).getColor();
    }
  }

  return color;
}

Get the background color of a button in android - Stack Overflow

android button background-color
Rectangle 27 5

Use a selector in your button.

<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:gravity="center" android:focusable="true"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:background="@android:drawable/list_selector_background" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/green"/> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused -->
    <item android:drawable="@color/black"/> <!-- default -->
</selector>

How to change color of Button on button tap in android studio - Stack ...

android
Rectangle 27 5

Use a selector in your button.

<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:gravity="center" android:focusable="true"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:background="@android:drawable/list_selector_background" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/green"/> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused -->
    <item android:drawable="@color/black"/> <!-- default -->
</selector>

How to change color of Button on button tap in android studio - Stack ...

android
Rectangle 27 78

I ended up overriding the text to be empty string and then defining the setBackgroundResource of the button to my image (didn't need the dynamic login/logout text functionality)

<com.facebook.widget.LoginButton
        xmlns:fb="http://schemas.android.com/apk/res-auto"
        android:id="@+id/login_button"
        android:layout_width="249dp"
        android:layout_height="45dp"
        android:layout_above="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="30dp"
        android:layout_marginTop="30dp"
        android:contentDescription="@string/login_desc"
        android:scaleType="centerInside"
        fb:login_text=""
        fb:logout_text="" />

And in code I defined the background resource :

final LoginButton button = (LoginButton) findViewById(R.id.login_button);
button.setBackgroundResource(R.drawable.facebook);

Kind of a workaround, but I preferred this over changing Facebook SDK code (although it's very straight forward as well) and worry about updating each time I update the their version.

It still shows ' f ' icon of Facebook, how to remove that?

add button.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); to remove that 'f' icon of Facebook.

Is it possible to change Facebook login button image in Facebook Andro...

android facebook-android-sdk
Rectangle 27 5

If you want to change the backgorund image or color of the button when it is pressed, then just copy this code and paste in your project at exact location described below.

<!-- Create new xml file like mybtn_layout.xml file in drawable -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/pressed" /> <!--pressed --> 
<item android:drawable="@drawable/normal" /> <!-- Normal -->
</selector>
  <!-- Now this file should be in a drawable folder and use this 
  single line code in    button code to get all the properties of this xml file -->

    <Button
      android:id="@+id/street_btn"
      android:layout_width="wrap_content"
      android:background="@drawable/layout_a" > <!-- your required code -->
    </Button>

How to Change color of Button in Android when Clicked? - Stack Overflo...

android
Rectangle 27 5

If you want to change the backgorund image or color of the button when it is pressed, then just copy this code and paste in your project at exact location described below.

<!-- Create new xml file like mybtn_layout.xml file in drawable -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/pressed" /> <!--pressed --> 
<item android:drawable="@drawable/normal" /> <!-- Normal -->
</selector>
  <!-- Now this file should be in a drawable folder and use this 
  single line code in    button code to get all the properties of this xml file -->

    <Button
      android:id="@+id/street_btn"
      android:layout_width="wrap_content"
      android:background="@drawable/layout_a" > <!-- your required code -->
    </Button>

How to Change color of Button in Android when Clicked? - Stack Overflo...

android
Rectangle 27 3

Clicking the "TODAY" and other buttons..

Here you go--- Its not that difficult actually. Click on the Cal1 day and the Cal2 Changes!

$('#mycalendar1').fullCalendar(
{
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    dayClick: function( date, allDay, jsEvent, view ) {
        dayClicked(date);
    },
    events: [                         
        {
            title  : 'event2',
            start  : '2011-03-10',
            end    : '2011-07-25'
        }
    ]
}); 


$('#mycalendar2').fullCalendar(
            {
             header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,agendaWeek,agendaDay'
                    },

                defaultView: 'agendaDay',

                events: [                         
                        {
                            title  : 'event2',
                            start  : '2011-03-10',
                            end    : '2011-07-25'
                        }
                    ]
           }); 


function dayClicked(date){
    $('#mycalendar2').fullCalendar( 'gotoDate', date );
}
$('.fc-button-today').click(function() { 
     alert($('#mycalendar2').fullCalendar( 'getDate' ));
   //dayClicked($('#mycalendar2').fullCalendar( 'getDate' ))

        });

You might have to use the viewDisplay event. But it gets a bit complicated in there. Here is a document how to use it.

You can open fullcalendar.js and goto line about... 3220 or search for Slot/Day clicking and binding

/* Slot/Day clicking and binding
-----------------------------------------------------------------------*/


function dayBind(cells) {
    cells.click(slotClick)
        .mousedown(daySelectionMousedown);

   //OVER HERE INSERT YOUR CUSTOM CALL!
     setMyOtherCalendar();
}

and in your page somewhere you would havbe a public function of setMyOtherCalendar and it would have this simple code in it.. init mate?

function setMyOtherCalendar(){

  $('#mycalendar2').fullCalendar( 'gotoDate',      $('#mycalendar1').fullCalendar( 'getDate' ) );

};

brilliant! One other thing, if I click the today button how do I get the other calendar to 'sync' to that date? I don't know how to hook into that event.

I edited- but i am not sure how to bind to the today button propery - it think you must use viewDisplay event somehow to call dayClicked in there

How do I set the date of a calendar view from another view using Jquer...

jquery fullcalendar
Rectangle 27 6

How to do with Batch file run from Button

To add or change shortcuts open: Windows->Preferences->General->Keys. Here you can find all the Eclipse commands and their associated shortcut keys.

You can add a custom binding in eclipse to export your jar using a keyboard shortcut.

If you want the JAR to be generated automatically you can look at the following question:

You can use the combination of the following two answers:

Basically create a batch file that makes the JAR... add a button that executes it.

Is there any way to then make it so that it when I press those keys is names the file the project name and saves it in the previous location

So you mean do a key combination and export directly in one step?

Maybe this will work for you: stackoverflow.com/a/12200309/1688441 . Otherwise, a custom eclipse plugin OR a button in your GUI that calls a bat file that does the jar export.

Yes that exactly what I mean

java - Eclipse: Is there a way to quickly export JARs - Stack Overflow

java eclipse jar
Rectangle 27 107

One approach is to create an XML file like this in drawable, called whatever.xml:

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

    <item
        android:state_focused="false"
        android:state_pressed="true"
        android:drawable="@drawable/bgalt" />

    <item android:drawable="@drawable/bgnorm" />
</selector>

bgalt and bgnormare PNG images in drawable.

If you create the buttons programatically in your activity, you can set the background with:

final Button b = new Button (MyClass.this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));

If you set your buttons' style with an XML, you would do something like:

<Button
  android:id="@+id/mybutton"
  android:background="@drawable/watever" />

And finally a link to a tutorial. Hope this helps.

You can also replace those drawables in the XML file with colors definitions if you want to avoid using images.

@jshin47 an empic amount of trial and error. I've written two books on Android development now :-)

@haseman - Can you post the links of those 2 books or share to me at brk0018@gmail.com. Would be glad to go through those and learn better

What if just colors not images?

How to Change color of Button in Android when Clicked? - Stack Overflo...

android
Rectangle 27 107

One approach is to create an XML file like this in drawable, called whatever.xml:

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

    <item
        android:state_focused="false"
        android:state_pressed="true"
        android:drawable="@drawable/bgalt" />

    <item android:drawable="@drawable/bgnorm" />
</selector>

bgalt and bgnormare PNG images in drawable.

If you create the buttons programatically in your activity, you can set the background with:

final Button b = new Button (MyClass.this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));

If you set your buttons' style with an XML, you would do something like:

<Button
  android:id="@+id/mybutton"
  android:background="@drawable/watever" />

And finally a link to a tutorial. Hope this helps.

You can also replace those drawables in the XML file with colors definitions if you want to avoid using images.

@jshin47 an empic amount of trial and error. I've written two books on Android development now :-)

@haseman - Can you post the links of those 2 books or share to me at brk0018@gmail.com. Would be glad to go through those and learn better

What if just colors not images?

How to Change color of Button in Android when Clicked? - Stack Overflo...

android
Rectangle 27 2

You need to use onTouchListener to listen when user presses the button (ACTION_DOWN) and when the user releases it (ACTION_UP)

b.setOnTouchListener(new OnTouchListener() { 

public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
       // reset the background color here
        b.setBackgroundColor(Color.GREEN);

    }else{ 
       // Change the background color here 
        b.setBackgroundColor(Color.RED);

    } 
    return false; 
   } 
});

Instead of registering touch event like this, we can make selector drawable using multiple colors/drawables for different states.

How to change color of Button on button tap in android studio - Stack ...

android
Rectangle 27 2

You need to use onTouchListener to listen when user presses the button (ACTION_DOWN) and when the user releases it (ACTION_UP)

b.setOnTouchListener(new OnTouchListener() { 

public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
       // reset the background color here
        b.setBackgroundColor(Color.GREEN);

    }else{ 
       // Change the background color here 
        b.setBackgroundColor(Color.RED);

    } 
    return false; 
   } 
});

Instead of registering touch event like this, we can make selector drawable using multiple colors/drawables for different states.

How to change color of Button on button tap in android studio - Stack ...

android
Rectangle 27 12

Set background of Button as null by android:background="@null" give the same text color as given to other TextView's.

or You can take 3 TextView's and setOnClickListener to middle one.

how to have three textviews followed by one another in a multi line

Simple and Elegant! I needed the link text color to be same as default color of hyperlinks in web. So I put RGB(0, 122, 255), and looks the same! (Y)

How to make a part of text clickable like button in Android? - Stack O...

android button click textview
Rectangle 27 111

You can move to desired activity on button click. just add android:onClick="sendMessage"this line.

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendMessage"
        android:text="@string/button" />
public void sendMessage(View view) 
{
    Intent intent = new Intent(FromActivity.this, ToActivity.class);
    startActivity(intent);
}

And the most important thing: don't forget to define your activity in manifest.xml

<activity
      android:name=".ToActivity"
      android:label="@string/app_name">          
 </activity>

You must add > in the activity tag

This is not working for me. The View , Intent and the activity names are in red. Do I need to import anything for View?

@SrujanBarai yes, both import android.view.View; and import android.content.Intent; must be imported

How to open a second activity on click of button in android app - Stac...

android android-intent android-activity
Rectangle 27 70

The correct way of changing the button text if it was "buttonized" using JQuery is to use .button() method:

$( ".selector" ).button( "option", "label", "new text" );

This worked for me better than the other approaches, which stomped on the styling of the button (in particular the size of the button). I was using a button on a jQuery UI dialog, FWIW.

This is the correct answer for buttons created with jQuery, such as those on a dialog. All the others will destroy some of the jQuery styling. This also does it without depending on the HTML structure that jQuery generates, which is more future-proof.

This is the right answer to this question (it seems the question is about Jquery UI buttons, see comments), it should be promoted.

How to change the text of a button in jQuery? - Stack Overflow

jquery jquery-ui
Rectangle 27 15

This may not be the best solution, but you could place your own button over the map and handle it yourself. It would take the following:-

1) Put the map in a frameLayout and add your button on top. E.g.

<FrameLayout
    android:id="@+id/mapFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



    <fragment
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.MapFragment"
        map:mapType="normal"
        map:uiCompass="true" />

    <ImageButton
        android:id="@+id/myMapLocationButton"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_gravity="bottom|right"
        android:background="@drawable/myMapLocationDrawable"
        android:contentDescription="My Location" />

</FrameLayout>

2) Edit the maps UI settings so the button doesn't appear when you call setMyLocationEnabled(true). You can do this via map.getUiSettings(). setMyLocationButtonEnabled(false);

3) Handle the click of your new button to emulate what the supplied button does. E.g. call mMap.setMyLocationEnabled(...); and pan the map to the current location.

Hope that helps, or hope someone comes a long with a simpler solution for you ;-)

BTW for what it's worth I agree with CommonsWare, don't cover the map with an advert would be best!

java - Change position of Google Maps API's "My location" button - Sta...

java android google-maps-android-api-2 android-maps-v2