How to change the toolbar's title in android app? - java

My android app has a toolbar and I wan to change the title of it. I used toolbar.setTitle() but it is not working... Please help. What should I do? I tried getSupportActionBar().setTitle() but it doesn't work.

Try to use this.setTitle(title); in onCreate() method.

Use below code to set the title
Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("My title");

Related

How to post a message in android studio

I want to make a button which will post a message once the button is hit. Here is my main activity.java :
enter image description here
But I confuse why I cannot choose the onclick option for the button :
enter image description here
or I dont have to choose the onClick button option again ?
Hope you undestand my question, and here is my video reference from youtube :
"Android Tutorial for Beginners 8 # wrap_content, fill_parent, Password Field and Toast in Android" Uploaded by : ProgrammingKnowledge
Replace the onCreate() method as
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListnerOnButton(); //Add this line to your code.
}
you are doing wrong in android you can set click listener by two ways
1. In Java code
2. In XML
in your example you are doing runtime in java code so it will not show in XML editor
in your case just call your addListenerOnButton in onCreate it will work
if you want it in xml
add onClick function in activity class as below
public void buttonClick(View v)
{
//do what you want
}
then this buttonClick will be shown in editor or you can directly add in xml button onClick attribute
<Button ...
android:onClick="buttonClick"/>

How to change the color of the Toolbar text programmatically

My toolbar title needs to be able to change color depending on button click. For example, the default color for the toolbar title is white, but if I press a button, I want it to change to red for example.
I tried using the following:
SpannableString title;
title.setSpan(new ForegroundColorSpan(Color.argb(1, 255, 64, 129)), 0, remainingTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
But for some reason the color just does not want to change. It used to work with actionbar, but for some reason this same method does not work with toolbar.
You can use something like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Hello");
toolbar.setTitleTextColor(Color.RED);//for red colored toolbar title
Toolbar can change its text color. So, just call Toolbar.setTitleTextColor(int color).
If you want to span a text, you need to call TextView.setText(SpannableString). Here is an example:
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
//Handle with your Span here. Like change color of part text, bold or italic part text, add hypertext and something like that.
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar.setText(wordtoSpan);
Using the v7 appcompat library you can call setTitleTextColor() on the Toolbar
if (actionBarToolbar != null)
actionBarToolbar.setTitleTextColor(Color.RED);
couple letters out of the entire title :
getActionBar().setTitle(Html.fromHtml("<font color='#ff0000'>YourColorText</font>") + "tittle");

Change logo programmatically in Fragment

How to change the title bar inside a fragment? Somewhat I am not able to succeed.
setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);
I think the problem is that you can't access the ActionBar APIs from the fragment. So, what you need is an access to the Parent Activity.
Try : getActivity().setTitle("My new title");

How to remove defaut icon/title from top of main layout in Android?

Check out this picture:
WhoKnew title and WK? Icon http://puu.sh/5Ejo5.png
See the icon and title there? How do I go about removing that?
I'm not using any of the drag and drop or xml editing at all in eclipse for this project, because I have to dynamically create an arbitrary amount of buttons/text, and whatnot.
Basically, I've been going about it by doing something like this..
public createMainMenu(){
//make layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(backgroundColor);
//make/add text to layout
menuText.setText("Who Knew?");
menuText.setId(20001);
menuText.setTextSize(36);
menuText.setTextColor(textColor);
layout.addView(menuText);
//make a scrollview, then add the layout to the scrollview
ScrollView sc = new ScrollView(this);
sc.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
sc.setFillViewport(true);
sc.addView(layout);
//set content view to the scrollview
setContentView(sc);
}
Hopefully, I'm on the right track here on how I should be going about this. The app I have is working fine, I just need to get rid of that icon/title bar, if possible. I guess it's not a gamebreaker, but it's a bit annoying.
you can use this in your java class
requestWindowFeature(Window.FEATURE_NO_TITLE);
Add this to your manifest:
android:theme="#android:style/Theme.Light.NoTitleBar"

modify Action Bar Sherlock theme to have no action bar

I am using ABS for my application. But I do not want my start screen (the first activity) to have action bar. How should I modify the theme to achieve this?
Many thanks in advance.
You could set your activity's theme attribute in AndroidManifest.xml file to one of the following based on your requirement:
android:theme="#style/Theme.Sherlock.Light.NoActionBar"
android:theme="#style/Theme.Sherlock.NoActionBar
call hide() on your ActionBar object, inside the onCreate() of your FirstActivity
if you are using Sherlock
getSupportActionBar().hide();
add following code in your activity :
ActionBar ab = getActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowHomeEnabled(false);
ab.hide();

Categories