HomeAsUpIndicator Custom View - android

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?

Related

Android - Change Hamburger/Back icons color programmatically during runtime

I'm trying to change the style attribute "colorControlNormal" of my app programmatically and during runtime, but I didn't have any results.
This property is the color that will tint the hamburger & back icons of the new Toolbar viewGroup. Beside, I'm using the v7 compatibility library.
I heard that we cannot change app theme during runtime, but I'm looking for an answer, even if it's not so clean way.
Edit:
I just figured that gmail is doing what i want, when you click on the search icon, the white hamburger icon turn into grey back.
Waiting for more.
I spent one day, played with different implementation. So my opinion, the best way todo that it copy paste DrawerArrowDrawable from AppCompat v7 library.
https://gist.github.com/IstiN/5d542355935fd7f0f357 - take a look on the code with some optimization
than you can use it in your main activity with code below
DrawerArrowDrawable drawable = new DrawerArrowDrawable(this, this);
ImageView menuButton = (ImageView) findViewById(R.id.arrow);
menuButton.setImageDrawable(drawable);
menuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((DrawerLayout)findViewById(R.id.drawer)).openDrawer(Gravity.START);
}
});
when you start new fragment, you need to create one more view on the same place and add second code to your fragment
private DrawerArrowDrawable mArrowDrawable;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mArrowDrawable = new DrawerArrowDrawable(getActivity(), getActivity());
ImageView topButton = (ImageView) view.findViewById(R.id.arrow);
topButton.setImageDrawable(mArrowDrawable);
topButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
closeSearch();
}
});
//run animation from hamburger to arrow
animate(0, 1, null);
....
private void animate(int startValue, int endvalue, Animator.AnimatorListener listener) {
ValueAnimator anim = ValueAnimator.ofFloat(startValue, endvalue);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
mArrowDrawable.setProgress(slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(300);
if (listener != null) {
anim.addListener(listener);
}
anim.start();
}
to make animation from arrow to hamburger handle back button and execute code
animate(1, 0, null);
you also need to wait in your fragment while animation will not finish, but it another questions.
If you have any questions ask in comments.

Android: Title bar onclick launcher icon

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)

Clear the previous selected View Background and change the background of newly selected View in Onclick - Android

I want to change the background view when it's pressed (its working). My problem is, If i press the other view (not the same one) in the list, i want to set my background to Black of the newly selected view and change the background to White of the previous selected view. Here is my Implementation
for(final TotalPlayers player : this.playerData){
final ArrayList<View> addedPlayerViews1 = getPlayerView(player);
dropPlayersListView.addView(addedPlayerViews1.get(0));
addedPlayerViews1.get(0).setOnClickListener(new OnClickListener() {
boolean highlight = false;
#Override
public void onClick(View v) {
if (!highlight)
{
addedPlayerViews1.get(0).setBackgroundColor(Color.BLACK);
highlight=true;
}
else {
addedPlayerViews1.get(0).setBackgroundColor(Color.WHITE);
highlight=false;
}
}
});
}
}
addedPlayerViews1.get(1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addedPlayerViews1.get(0).setBackgroundColor(Color.WHITE);
highlight=false;
}
});
I assumed that you can get the other view by get(1).
The problem of your code was you only handled the onClick event of your view where you want to change the background. but you also need to handle the onClick of other view too.

Setting visibility of ListView

I want to show my list view when the user clicks on a button and hide it again when they click on a button. This is the onClick listener for the button in question:
connectBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(open){
mDbAdapter.close();
connectBtn.setText("Open Database");
open = false;
hideUI();
}else{
mDbAdapter = new ContactsDbAdapter(v.getContext());
mDbAdapter.open();
connectBtn.setText("Close Database");
open = true;
showUI();
//retrieve data
fillData();
}
}
});
This is the showUI() method:
protected void showUI() {
fName.setVisibility(View.VISIBLE);
lName.setVisibility(View.VISIBLE);
fNameBox.setVisibility(View.VISIBLE);
lNameBox.setVisibility(View.VISIBLE);
createBtn.setVisibility(View.VISIBLE);
this.setVisible(true);
createBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDbAdapter.createContact(fNameBox.getText().toString(), lNameBox.getText().toString());
fillData();
}
});
}
and the hideUI() method:
protected void hideUI() {
fName.setVisibility(View.INVISIBLE);
lName.setVisibility(View.INVISIBLE);
fNameBox.setVisibility(View.INVISIBLE);
fNameBox.clearComposingText();
lNameBox.setVisibility(View.INVISIBLE);
lNameBox.clearComposingText();
createBtn.setVisibility(View.INVISIBLE);
this.setVisible(false);
}
It works fine when I set the visibility to true. However when I set it to false I get a black screen but no crash or error. Any idea?
NOTE: this.setVisible(false);. My class extends ListActivity.
setVisibility(View.INVISIBLE);
Just makes you view invisible but the space taken by view will be their itself
use setVisibility(View.GONE); so that the size of view will be lapsed
Use this and let me know if it is helpful
ListActivity is hold list view
if u do this.setVissiblity(false);
it hide the list view and its contents so u r seeing background color in ur case it is black.
Good way is take Listview in xml and get id make vissible nad invissible of that view u feel very confortable with this apprch
http://www.vogella.com/articles/AndroidListView/article.html read this u will get clear idea. and make changes accordingly

adding click listener to titlebar image

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

Categories

Resources