Sunday, January 26, 2014

Android activity title background color/image change


For changing Title bar background, add the following code in the activite's onCreate() function,

 ActionBar bar = getActionBar();
//for color
 //bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#DDAAD")));
//for image
 bar.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_bg3));
bar.setTitle("add your own title here");

For changing the Title bar title's color,

In the onCreate() Function,

this.setTitleColor(getResources().getColor(R.color.black));

Saturday, January 18, 2014

Android Button Click Event

There are three ways to do it,

First Method,

In the onCreate function,


Button button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener()
    {
      public void onClick(View v)
      {
         ImageView iv = (ImageView) findViewById(R.id.imageview1);
         iv.setVisibility(View.VISIBLE);
      }
    });

Second Method,

Import the following package,
import android.view.View.OnClickListener;

In onCreate method add the following Listener,

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();

}

Implement the Listener,

 
public void addListenerOnButton() {
 
button = (Button) findViewById(R.id.button1);
 
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent browserIntent = 
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vijaynetwork.com"));
startActivity(browserIntent);
}
});

Third Method,

<Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"

     android:onClick="displayMap" />

when a user clicks the button, the Android system calls the activity's displayMap(View) function.

And the function declaration would be,

public void displayMap(View myview)
{
//made your own code for button click
}