custom title bar is not working android - android

Hi i have set up a custom title bar but the app crashes and i don't get any error log in LogCat, i'm going crazy. Here's is some code, can you experts see what's wrong?
boolean isCustomTitleSupported;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isCustomTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.about);
customizeTitleBar("My Title");
public void customizeTitleBar(String title){
if(isCustomTitleSupported){
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
TextView customTitleText = (TextView)findViewById(R.id.customtitle);
customTitleText.setText(title);
customTitleText.setTextColor(Color.WHITE);
}
}
customtitlebar.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/customtitle"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textStyle="bold"
android:padding="3px"
/>
</LinearLayout>
some help will be appreciated
Thanks!!
EDIT: i noticed that i wasn't extending Activity but BaseActivity a superclass i created to have the menu available in all of my activities. So i changed back to extend Activity and it's working but this is a problem because i need menus too. Is there any tricks so i could keep extending BaseActivity and even get the title bar to work?

Have you tried setting the title bar before setting the content view ?
customizeTitleBar("My Title");
setContentView(R.layout.about);

You are trying to extract the customtitle TextView from the wrong layout. When you use findViewById it defaults to your current activity's layout, which you set to R.layout.about. You need to use a layout inflater to inflate R.layout.customtitlebar and then call findViewById from that (since the customtitle view is in the customtitlebar layout).
Something like this:
View view = getLayoutInflater().inflate(R.layout.customtitlebar);
TextView customTitleText = (TextView)view.findViewById(R.id.customtitle);

It seems to be half working now, but no title is being set, i mean that i get an empty title bar here's is what i got:
About.java
public class About extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customizeTitleBar("APP TITLE");
setContentView(R.layout.about);
}
}
BaseActivity.java
public class BaseActivity extends Activity {
boolean isCustomTitleSupported;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isCustomTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
customizeTitleBar("MY TITLE");
}
public void customizeTitleBar(String title){
if(isCustomTitleSupported){
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
View view = getLayoutInflater().inflate(R.layout.customtitlebar, null);
TextView customTitleText = (TextView)view.findViewById(R.id.customtitle);
customTitleText.setText(title);
customTitleText.setTextColor(Color.WHITE);
}
}
}

Request the windowfeature --> setContentView --> customize Title
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Boolean customTitle = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.listviewoffers);
customTitle(R.string.dashboard_offers, 0, R.id.listViewTitle, customTitle);
This are the first lines of my onCreate.
The customTitle() is in my superclass.
public void customTitle(int middle, int right, int altTitle, Boolean customTitle) {
if (customTitle) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebarlayout);
TextView titleMiddle = (TextView) findViewById(R.id.middleTitleBar);
titleMiddle.setText(getResources().getString(middle));
TextView titleRight = (TextView) findViewById(R.id.rightTitleBar);
if (right != 0) {
titleRight.setText(getResources().getString(right));
} else {
titleRight.setVisibility(View.GONE);
}
TextView title = (TextView) findViewById(altTitle);
title.setVisibility(View.GONE);
} else {
setTitle(getResources().getString(R.string.dashboard_offers));
}
}
It is a layout with 2 textviews, one in the middle, one in the right. The right can be set as gone.

Related

Android Hide or Show Title Bar from the Activity and Fragment

I have a Activity as below:
// My Activity Holding Fragment
public class MyActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity_main);
}
}
// My Fragment
public class MyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.my_fragment, container, false);
return view;
}
}
I wan to have the Title Bar for my Activity.
I want to Hide or Show Title Bar from Activity and Frgment on some conditions.
I tried below solutions, but as my activity does not derive from ActionBarActivity it does not work.
// getSupportActionBar returns null
getSupportActionBar().hide();
getSupportActionBar().show();
// getActionBar returns null
getActionBar().hide();
getActionBar().show();
// findViewById returns null
findViewById(android.R.id.title).setVisibility(View.GONE);
findViewById(android.R.id.title).setVisibility(View.VISIBLE);
// Are deprecated
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
None of the solutions did work for hiding Title Bar from Activity or Fragment.
Basically I want to hide the Activity title bar for the Activity.
First Change your App theme from res/value/styles.xml to this
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
//leave everything as it is
</style>
After that Add toolbar in your Activity's XML Layout
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:background="#color/colorAccent"
app:titleTextColor="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
//add your toolbar design here
</toolbar>
Now finally set up your toolbar in Activity Class
public class MyActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
//need to set toolbar before calling getSupportActionBar
setSupportActionBar(toolbar);
//Now show or hide according to your need
getSupportActionBar().hide();
getSupportActionBar().show();
}
}
Same would be applicable for fragment but for fragment you need to call view first
Toolbar toolbar = getView().findViewById(R.id.toolbar);

Get reference to LinearLayout in fragment

In the XML file for my layout I added a fragment.
The layout for my fragment is a LinearLayout and it contains a text view.
I want to get a reference to the LinearLayout object in my fragment class, but despite trying everything I can think of or find online, it is always null.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
text = (TextView) getActivity().findViewById(R.id.text);
layout = (LinearLayout) getActivity().findViewById(R.id.layout);
if(layout == null){
text.setText("null");
}
}
This code is in my fragment class and results in the TextView's text being set to "null" since the layout is null. How could this be since I am trying to get the references to the layout and the textview in the exact same way?
onCreate in activity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
manager = getFragmentManager();
MyFragment = (MyFragment) manager.findFragmentById(R.id.fragment);
}
Method 2
I tried getting a reference to the LinearLayout in my activity. Here is the activity's onCreate with this attempt.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
manager = getFragmentManager();
MyFragment = (MyFragment) manager.findFragmentById(R.id.fragment);
LinearLayout ll = (LinearLayout) findViewById(R.id.layout);
TextView tv = (TextView) findViewById(R.id.text);
if(ll == null){
tv.setText("null");
}
}
This also changed the text to "null."
The problem is fixed in both cases by saying layout = (LinearLayout) getView() in the fragment class or layout = (LinearLayout) fragment.getView() in the activity. This doesn't answer why what I was doing didn't work (which could be helpful) but this solves the problem.

click home button in fragment page to disappear action bar activity title

I am facing weird issue.I am using PageActivity inside three
fragments swipes one after another and so on.
My problem is,when I am in FragmentA page,on click the home
button ,again come back to the application, my action bar title name is
Chapter,which I was added the action bar title name in PageActivity and
after 2 seconds the fragment title was loading with the help of
query.
My only problem is,on home button click in fragment page instead of
activity title name,Fragment page title name have to be shown
directly after enter the application.
Below I am posted the code related to this:
PageActivity.java:
public class PageActivity extends FragmentActivity {
static TextView mTitleTextView;
.......
.......
#Override
protected void onResume() {
super.onResume();
ActionBar(Constants.Titlebarcolor);
........
........
}
public void ActionBar(String color) {
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mTitleTextView = (TextView) mCustomView.findViewById(R.id.textviewHeading);
mTitleTextView.setText("Chapter");
}
FragmentPageA.java:
#Override
public void onResume() {
super.onResume();
....
....
}
#Override
public void onPause() {
super.onPause();
....
....
}
#Override
public void setMenuVisibility(final boolean visible) {
//get action bar title for every fragment page
PageActivity.mTitleTextView.setText(DatabaseQueryHelper.getInstance().getPagename(getArguments().getInt(Constants.PAGEID)));
......
......
}
Anyone can give me any suggestion regarding to this.Thank you.
I just moved this ActionBar(Constants.Titlebarcolor); line inside onResume() method to onCrate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar(Constants.Titlebarcolor);
}
So that When I entering into the activity,the action bar activity title will not resume.

Android: Not able to change image in Window Title

I have a parent activity (from which all other activities extend) - with a 'Navigation Drawer Toggle' icon in window title.
I want to change the 'Navigation Drawer Toggle' image everytime drawer icon is clicked ( to open/close drawer)
public class ParentActivity extends Activity {
private ImageView imgDrawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
....
setContentView(R.layout.window_title);
imgDrawer = (ImageView)findViewById(R.id.imgdrawer);
...
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.window_title);
}
...
public void openDrawer()
{
...
//Switch Image
imgDrawer.setImageDrawable(getResources().getDrawable( R.drawable.ic_close_drawer));
}
public void closeDrawer()
{
...
//Switch Image
imgDrawer.setImageDrawable(getResources().getDrawable( R.drawable.ic_open_drawer));
}
...
}
Problem is that the 'imgDrawer' is not changing, when openDrawer and closeDrawer is invoked.
Is this becz the 'imgDrawer' icon is in window title or something else?
Probably setContentView and setFeatureInt layout is same and calling getWindow().setFeatureInt before setting layout for Title. do it as:
#Override
protected void onCreate(Bundle savedInstanceState) {
....
setContentView(R.layout.window_title);
...
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.window_title);
imgDrawer = (ImageView)getWindow().findViewById(R.id.imgdrawer);
}
or pass a layout which contain ImageView with different is from setContentView layout

ActionBarCompat: How to hide/ remove the 'title' of the 'underlying' activity?

I'm using ActionBarCompat that is attached to 1 underlying activity. And there are a number of fragments that can be navigated with the navigation drawer.
And each fragment has a respective title set programatically.
Apps like the official Gmail app have a clean launcher activity, i.e the very first screen that you see for a brief moment has no icon, no title in the action bar. How can I achieve the same?
After the app is loaded with the following fragments, the ActionBar has been styled appropriately with the respective titles and icons programatically, as mentioned earlier.
I've tried the following code by putting it into the style.xml and calling it from the manifest. But this disabled Action Bar throughout the app, which I don't want.:
<style name="Theme.AppCompat.Light.NoActionBar" parent="#style/Theme.AppCompat">
<item name="android:windowNoTitle">true</item>
</style>
Just add setTitle(""); to your activity oncreate.
I think you could create your own ActionBar Layout like this:
In your BaseActivity (you have one, right?) do something like this:
public class BaseActivity extends ActionBarActivity {
private TextView mActionBarTextView;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View actionBarView = inflator.inflate(R.layout.ab_custom_view, null);
mActionBarTextView = (TextView) actionBarView.findViewById(R.id.title);
mActionBarTextView.requestFocus();
this.getSupportActionBar().setCustomView(actionBarView);
setTitle(Constants.EMPTY_STRING);
}
}
ab_custom_view.xml would look like (set your own TextView preferences, this is just an untested example):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:textColor="#color/white"
android:textSize="18sp"
android:maxLines="1"
android:ellipsize="marquee"
android:focusable="true"
android:singleLine="true"
android:focusableInTouchMode="true" />
</RelativeLayout>
Back in your BaseActivity, add the folowing convenience methods:
#Override
public void setTitle(final CharSequence title) {
if (mActionBarTextView != null) {
if (title != null) {
mActionBarTextView.setText(title);
} else {
mActionBarTextView.setText(null);
}
requestTitleFocus();
}
}
#Override
public void setTitle(final int titleId) {
if (mActionBarTextView != null) {
if (titleId > 0) {
mActionBarTextView.setText(getString(titleId));
} else {
mActionBarTextView.setText(getString(R.string.some_default_string);
}
requestTitleFocus();
}
}
public void requestTitleFocus() {
if (mActionBarTextView != null) {
mActionBarTextView.requestFocus();
}
}
Finally, in your activities (that inherit from BaseActivity), if you need to set a different than empty title:
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Your Title");
}
(I assume you store your mostly used Strings in a static final field for convenience and efficiency…)
public final class Constants {
public static final String EMPTY_STRING = "";
}
This should do the trick.
The perk you gain is that you could use a custom Font in the ActionBar (to match your App's font if you were using a custom one) because you have full control (and a reference) to your ActionBar's textview. ;)
Try using style for your activity in manifest file. Remember, in your onCreate() method you must call setStyle() or requestWindowFeature() before setContentView() when you want your action bar or app style back.
<style name="MyApp.Fullscreen" parent="android:Theme.Black.NoTitleBar">
<item name="android:windowBackground">#color/app_background</item>
</style>

Categories

Resources