Merry Christmas and Happy Holidays everyone!
I'm trying to setup a listener on the image icon that appears on the left side of the default title bar, but so far not having any luck.
Here's my Activity's onCreate:
#Override public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_LEFT_ICON);
super.onCreate(savedInstanceState);
findViewById(Window.FEATURE_LEFT_ICON).setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
System.out.println("It works!");
}
});
}
Any suggestions? I'm hoping to not see the answer "it's not possible" :)
There doesn't seem to be an id for the left icon, however for the classic title bar, there is an id available: android.R.id.title Here is a sample Activity using this id. The requestWindowFeature(Window.FEATURE_LEFT_ICON); should force the classic title bar regardless of theme.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.activity_main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);
View v = findViewById (android.R.id.title);
v.setClickable(true);
v.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
Toast.makeText(MainActivity.this, "Works!", Toast.LENGTH_SHORT).show();
}
});
}
}
Basically, what this does, is it finds the id of the title bar (android.R.id.title) then assigns an onClickListener to it.
This will not work with ActionBars, only classic window title bars.
You should use the ActionBar. Use ActionBarSherlock to have it also for Android versions less than 3.0. To make the Icon clickable, see the ActionBar API Docs (see link below). It's very easy, you just activate the behaviour and then it works like a menu-item with a special item-id.
http://developer.android.com/guide/topics/ui/actionbar.html#Home
Related
I'm looking at adding a custom view for the HomeAsUpIndicator whenever there are new message. I want to bring attention to the hamburger icon when there are new messages, and then use the default hamburger icon when there aren't new messages.
I have two functions to display custom icon and display default, but I want to use a custom View.
protected void setCustomIcon() {
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_menu_hamburger);
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout.openDrawer(mDrawerList);
}
});
}
protected void setDefaultIcon() {
mDrawerToggle.setDrawerIndicatorEnabled(true);
}
Is there a way to use a custom View instead of a resId or a drawable?
I'm new to android, and I'm facing a problem where I'm trying to make my ImageView, which is playing an animation through startAnimation(), to become Invisible as the user clicks on it. what happens however is that only after the animation is done the Imageview becomes invisible. my assumptions was it's because they both run on the UI thread. surprisingly however, when I changed my event handling from setVisibility to startAnimation() it actually listened to the click, interrupted the animation and restarted it!
To override my problem, which did turn out nice, I made a new Animation object that opposite to the first, which shows the Imageview, hides the Imageview and it worked perfectly, but I'm still curious as to why my Imageview was selectively responsive to different event handling?
This code change visibility only after the animation is done
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
face = (ImageView)findViewById(R.id.face);
final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
face.startAnimation(showAnimation);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
face.setVisibility(View.INVISIBLE);
}
});
}
this code changes the animation "while" the original is playing
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
face = (ImageView)findViewById(R.id.face);
final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
face.startAnimation(showAnimation);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation hideAnimation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation1);
face.startAnimation(hideAnimation);
}
});
}
You should not call face.setVisibility(...), instaed tt should be:
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setVisibility(View.INVISIBLE);
}
});
I am busy creating an app. I succeeded in creating the button with a custom font and all. Now what I'd want is that when I click the button, it must disapear, the background color of the view must randomely change and text must be loaded from an database.
How does one go about this?
Matthew
Well, the disappearing can be make like this:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click - Disappear in your case...
v.setVisibility(View.INVISIBLE); //can be View.GONE as well...
}
});
}
}
Then for the background I guess you can create an array with all the colors you want (or something that generates a random HEX code) and then do setBackground(X) where X is the HEX code that you just generated... You need to specify more about the database part though.
I'd like to add an onclick listener to my launcer icon in the title bar of my app.
Since i'm also supporting API level 8 i do not have an Action bar.
The following code works great, however the menu is set back to default (white background, white text, small icon etc.)
The code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.activity_main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);
View v = findViewById (android.R.id.title);
v.setClickable(true);
v.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
Toast.makeText(MainActivity.this, "Works!", Toast.LENGTH_SHORT).show();
}
});
}
}
source: adding click listener to titlebar image
How can I keep my standard layout of my title bar (black background color, white text and large icon), while also implement this onclick listener?
Here are two shots of the different layouts:
good: http://gyazo.com/40d1cdd5302de3cd28b698b68164a556
bad: http://gyazo.com/3cee42524ec4167392baec6cc2369584
(Note that the good one is also has a greater height)
I'm new to android and am making a simple app. I'm trying to change the image (in an imageview) on a button click.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frown = (ImageView)findViewById(R.id.imageView1);
}
public void action(View view)
{
Toast.makeText(getApplicationContext(), buttontest, Toast.LENGTH_SHORT).show();
frown.setImageResource(R.drawable.chimpy);
}
"action" is being called via XML with the "android:onClick"[insert method here]" for my button
The button works fine and I get my toast, but the image stays the same.
Try changing the drawable to something standard e.g. android.R.drawable.btn_default.
Does it chnage Now?
I am sure you are having some issues with R.drawable.chimpy.
You must use .png image and you can go with the following code snippet:
frown.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
frown.setBackgroundDrawable(R.id.chimpy);
}
});
If it is not working, just tell me...!