Action Bar Title Clickable - android

I want Action bar title to be made clickable.
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
here the home button and backbutton perform same intent but if we want to do different action over this that is suppose up button take to home activity and home button (title) take us to home2 activity how this can be done

You can use Resources.getIdentifier to call View.findViewById, then attach a View.OnClickListener to the TextView that contains the ActionBar title.
Here's an example:
final int abTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
findViewById(abTitleId).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
});

Related

Android: how to hide title bar on button click

App crashing when using requestWindowFeature(Window.FEATURE_NO_TITLE);
When using the button to make it full screen which when tries to hide the Title bar the app crashes
static int vari = 0;
public void fsc(){
ib = (ImageButton) findViewById(R.id.fulls);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Full-Screen", Toast.LENGTH_LONG).show();
if(vari == 0)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
vari = 1;
}
});
I wanted to make it full screen (hiding both status and title bar) on the button press
Please note: this is also to be called to fragments
Check if you have the fullscreen theme set in the manifest?
//if not add this in your manifest
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
// Hide status bar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Show status bar
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

How to replace the actionbar by a popup menu?

I want to achieve 1 of these options for my EditText :
Replace the actionbar that appear on the top by a popup menu instead. Something like this for exemple:
Or make the actionbar floating and child of my current view (in some way same a first option)
i need this because i add my view via windowManager.addView(view, Layout_Params); and in this way i have some trouble with the actionbar on the top (it is displayed blank)
actually i do this to show the actionbar :
#Override
public ActionMode startActionMode(ActionMode.Callback callback) {
Activity host = (Activity) this.getContext();
return host.getWindow().getDecorView().startActionMode(callback);
}
but it's don't work, it's show me an empty white actionbar on the stop instead :( i think i need to create myself the ActionMode but i don't know how to do it.
Ok. You can hide the actionbar when your edittext is gained focus. Then you need to show the popup menu where ever you want.
EditText t = new EditText(this);
t.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b){
getSupportActionBar().hide();
//now show your popup menu at the top position
}else{
getSupportActionBar().show();
//here you dismiss the menu.
}
}
});

Close floating action button Android

I used the following library https://github.com/futuresimple/android-floating-action-button and everything works fine.
How do I close the menu automatically after clicking on one of the buttons on the same menu?
Its open-source lib, You can read the source code and find that by your self.
FloatingActionsMenu.collapse(); // close the menu
FloatingActionsMenu.toggle(); // toggle the menu
FloatingActionsMenu.expand(); // open the mneu
In the Click listener of the Menu Item call .collapse()
FloatingActionMenu fAM = (FloatingActionMenu) findViewById(R.id.fAM_ID);
FloatingActionButton fAB1 = (FloatingActionButton)findViewById(R.id.fAB_ID);
floatingActionButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Action
Toast.makeText(MainActivity.this, "FAB Click", Toast.LENGTH_SHORT).show();
fAM.collapse();
}
});

How to detect Action bar "Sub-Title" click event in Android Fragment..?

In my Android app I want to detect action bar "Sub-Title" click event but I don't know how to do it. I have successfully done Action bar "Title" click as shown in following code---
// Get action bar title ID
final int abTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
//Set OnClick event on action bar title
getActivity().findViewById(abTitleId).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// Done
}
});
Referring above code, I have tried to detect action bar "Sub-Title" click event as follows, but there is something wrong and I'm getting "Sub-Title Id" null and it is throwing Null-Pointer Exception.
//Set OnClick event on action bar sub-title
final int abSubTitleId = getResources().getIdentifier("action_bar_sub_title", "id", "android");
getActivity().findViewById(abSubTitleId).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
Hope you understand. Thanks..!
The best to handle this scenario is to use custom actionbar view, So create a simple actionbar layout with title textview and subtitle textview and implement respective listener. :)
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.action_bar, null);
// Set up your ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
// You customization
final int actionBarColor = getResources().getColor(R.color.action_bar);
actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));
final TextView actionBarTitle = (TextView) findViewById(R.id.action_bar_title);
final TextView actionBarSubTitle = (TextView) findViewById(R.id.action_bar_sub_title);
The Action Bar subtitle is non clickable as far as i know. Instead you can add the icon there and make it clickable. You can get the click event of icon in onOptionsItemSelected().

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)

Categories

Resources