Android: Not able to change image in Window Title - android

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

Related

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.

Navigation drawer extending base activity

I have created a baseactivity with a navigation drawer and searchview. I am trying to extend this in other activities, but also have different content. However if I extend the base activity, and try to set a different content view, I don't get the navigation drawer/search view.
How do i extend the baseactivity, but still customize the ui for each activity?
Thanks for you help!
The best way to do this would be to use Fragments. However, to use multiple activities, extend the base activity and then use setContentView() with the layout for that activity. Ensure that the xml for that layout contains the navigation drawer layout too.
Sounds like you want to do something like this. You'll see that any of the Activities that inherit from this one and call setContentView will now be adding their content to the content frame in your already inflated navigation layout.
public class BaseActivity extends FragmentActivity
{
private FrameLayout mContentFrame;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// notice we call super.setContentView here
super.setContentView(R.layout.activity_main);
// and other nav stuff here
mContentFrame = findViewById(R.id.content_frame);
}
#Override
public void setContentView(int layoutResID)
{
setContentView(getLayoutInflater().inflate(layoutResID, mContentFrame, false));
}
#Override
public void setContentView(View view)
{
mContentFrame.removeAllViews();
mContentFrame.addView(view);
}
#Override
public void setContentView(View view, LayoutParams params)
{
mContentFrame.removeAllViews();
addContentView(view, params);
}
#Override
public void addContentView(View view, LayoutParams params)
{
mContentFrame.addView(view, params);
}
}

Fragment placed on top of NavigationDrawer

I'm trying to add a PreferenceFragmentin my application. The problem is, it's auto placed on top of my NavigationDrawer.
public class SetPreferenceActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
navigationDrawer(); // Loads the NavigationDrawer
getFragmentManager().beginTransaction().replace(android.R.id.content,
new Settings()).commit();
}
As you can see I load the "SettingsFragment" and replace the content with it? (I'm unsure) but it places it on top of everything else.. Here's my Settings fragment.
public class Settings extends PreferenceFragment {
static final String TAG = "MAIN";
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
Everything work as expected, BUT the PreferenceFragment are loaded in front, covering up the NavigationDrawer slideout, I tried calling bringToFront(); on the listview, with no luck.
A picture for reference :
Is it possible to tell the fragment to load behind the listview? I also tought about loading the fragment in a ViewPager, but I get an error that the Pager Adapter wont accept fragments of type PreferenceFragment.
Don't replace android.R.id.content, use the the id of the FrameLayout you have in the layout that contains your DrawerLayout.
In addition to what adneal said (in case others have the same problem):
The Activity which calls the PreferenceFragment needs to have the setContentView() method if you extend the Activity with your NavigationDrawer:
public class SetPreferenceActivity extends MyNavigationDrawer {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
getFragmentManager().beginTransaction().replace(R.id.drawer_frame_layout,
new Settings()).commit();
}
And the settings.xml should only contain a FrameLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
I had same issue and i resolved it by replacing android.R.id.content to R.id.container.

hide activity name from action bar just in time of creation

Just in time of creation and for about one second or less the action bar shows the name of activity as declared in manifest. Is there any way to avoid this? Of course when activity is created i change programmatically the title.The manifest:
<activity
android:name="com.abc.HomeActivity"
android:screenOrientation="portrait"
android:label="Home"
android:icon="#drawable/x_logo">
</activity>
and code from fragment which is shown by activity. I change the title of action bar based on what fragment is shown.
((ActionBarActivity) getActivity()).getSupportActionBar().setTitle("variable name");
from activity:
super.onCreate(savedInstanceState);
getSupportActionBar().setTitle("");
If hiding the activity name means simply avoiding the text, then call the following in onCreate of your Activity:
getActionBar().setTitle(null); //if using ActionBar theme
setTitle(null); //if using standard theme
Update
#Override
protected void onCreate(Bundle savedInstanceState) {
...
final CharSequence title = getActionBar().getTitle();
getActionBar().setTitle(null);
//sets the title again after 1 second delay
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
getActionBar().setTitle(title);
}
}, 1000);
}
Use this line to hide a name of activity this.requestWindowFeature(Window.FEATURE_NO_TITLE);
just after onCreate.
Example.
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.serverfilelist);

custom title bar is not working 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.

Categories

Resources