Android: how to hide title bar on button click - android

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);

Related

Action Bar Title Clickable

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
}
});

Changing color of action bar doesn't work in ICS

I am changing the colour of action bar using the following code.
actionBar= getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(backgroundColor));
This works perfectly fine in onCreate() method(i.e when my activity starts).
But when I am using the same code in Android 4.1 to change color of action bar in onClick() method(i.e when user clicks say a button) then it changes the color of action bar to 'white' ONLY whatsoever be the value of backgroundColor.
In Android 2.3 it works perfectly fine in both cases, whether called from onCreate or onClick().
It seems to be dead end for me.
It seems like for Android 4.0 up to Android 4.1 (4.2 is ok) you cannot change the action bar background once the activity has started.
So I guess a solution in those cases is to restart the activity with a different background color.
Not very elegant but thats the best I can come up with.
Something like this perhaps:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
if (getIntent().hasExtra("color")) {
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(getIntent().getIntExtra("color", 0)));
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent it = getIntent();
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
it.putExtra("color", Color.BLUE);
startActivity(it);
}
});
}
A workaround I used to solve this problem was to maintain a reference to the ColorDrawable that was used to set the background initially. Then, to change its colour, modify the colour of the Drawable, and then tell the activity to invalidate its options, which causes the ActionBar to be redrawn. Here's my code to set the action bar color:
ColorDrawable sBackgroundDrawable;
...
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void setActionBarColor(int color){
if(getSherlockActivity() != null && !mCallback.isNavDrawerShowing()){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN){
if(sBackgroundDrawable == null){
sBackgroundDrawable = new ColorDrawable(color);
getSherlockActivity().getSupportActionBar().setBackgroundDrawable(sBackgroundDrawable);
}else{
sBackgroundDrawable.setColor(color);
getSherlockActivity().invalidateOptionsMenu();
}
}else{
getSherlockActivity().getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
}
}
}
Based off of what steve_the_kid mentioned I was able to solve this by saving the color drawable.
ColorDrawable actionBarBackground = new ColorDrawable();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setBackgroundDrawable(actionBarBackground());
actionBarBackground.setColor(someColor);
}
void onSomethingClick() {
actionBarBackground.setColor(someOtherColor);
}
Sorry for such a late reply,But to help others I am replying to this question after so long.
Just set the Title of the action bar or make change in the icons or menu along with changing the colorDrawables.A simple change will recreate the action bar with new color.Even if you need same title or icon every time set same title or same icon.
I hope this will help others

How to HIDE/SHOW CUSTOM Title On button click In Android

I am working on an android project. And I want to show / hide the custom title bar on Button click. This button is for media player (full screen size / custom screen size ) please help me.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main1);
ctx=this;
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title1);
// FrameLayout frameLayout = (FrameLayout) findViewById(R.id.menu);
// FrameLayout frameLayout2 = (FrameLayout) findViewById(R.id.menu1);
for hide custom title i am using using following code onClick()
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
This is will hide the action bar:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this will make him back:
this.requestWindowFeature(Window.FEATURE_ACTION_BAR);

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)

Showing the title bar

I have an app that switches between full screen and normal screen for a certain condition.
I have successfully done this with the notification but the TITLE BAR still remains hidden after I set it back from full screen to normal screen mode. So HOW DO I SHOW THE TITLE BAR AFTER HIDING IT?
EDIT:
I have come across answers where they make a custom title bar and switch between its visibility but thats not what I want.
CODE:
if(ScreenReceiver.wasScreenOn) {
Toast toast=Toast.makeText(this, "screen was on", Toast.LENGTH_LONG);
toast.show();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.resume);
} else {
Toast toast=Toast.makeText(this, "screen was off", Toast.LENGTH_LONG);
toast.show();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.main);
}
try this
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/// custom tittle bar declaration
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
/// custom tittle bar creation ///....
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tittle_bar);
}
final TextView myTitleText = (TextView) findViewById(R.id.title_bar);
if (myTitleText != null) {
myTitleText.setText("Categories");
}

Categories

Resources