Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Set parse username as action bar title

Hi,

Just wondering if knows how to set the parse username as an action bar title.

The app i'm trying to create has a listView of users that once clicked opens a new conversation activity.

I want the title to reflect the user that has been clicked on.

Any help will be most appreciated.

Cheers

7 Answers

Try this! :)

ActionBar actionbar = getActionBar();
actionbar.setTitle("My Title");
actionbar.setSubtitle("sub-title");

Just get the user name via parse's methods (hint: think about previous queries we've done to recall usernames for a certain listView! [Ribbit project]) and apply that as your title!

I included sub-title so, in the case that you wanted to include what page they're on under it, you could. :)

EDIT

If this is a fragment, be sure to use getActivity().getActionBar()

Try something like this... Inside of your onListItemClick method, declare a variable as such:

String userName = l.getItemAtPosition(position).toString();

-- and from there, set up the new Intent, and before launching it, pass that string variable as the data, so you can use it in the new activity. Then, once retrieved in the activity, set that variable as the ActionBar title!

You can change android action bar title by doing this.

       //your parse username string
String username = "yourParseUsername";

// get the android action bar
getActionBar().setTitle(username);   
getSupportActionBar().setTitle() //provide compatibility support

Thanks for the replies guys it greatly appreciated.

Iv'e tried what you guys suggested but with no luck, keep getting an error message.

hopefully someone might be able to point me in the right direction with this.

public class MessagingActivity extends Activity implements ServiceConnection, MessageClientListener {

    private Button sendButton;
    private EditText messageBodyField;
    private String messageBody;
    private MessageService.MessageServiceInterface messageService;
    private ParseUser mCurrentUser;
    private ServiceConnection serviceConnection = new MyServiceConnection();



    protected List<ParseUser> mUsers;
    protected ParseRelation<ParseUser> mContactsRelation;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messaging);

        // Get the name from the exttra of the intent or howsoever you are getting the name

        doBind();
        bindService(new Intent(this, MessageService.class), serviceConnection, BIND_AUTO_CREATE);

        // setting the layout for text input
        messageBodyField = (EditText) findViewById(R.id.messageBodyField);


        //listen for a click on the send button
        sendButton = (Button) findViewById(R.id.sendButton);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendMessage();
            }
        });
    }

    private void sendMessage() {

        messageBodyField = (EditText) findViewById(R.id.messageBodyField);
        messageBody = messageBodyField.getText().toString();

        if (messageBody.isEmpty()) {
            Toast.makeText(this, "Please enter a message", Toast.LENGTH_LONG).show();
            return;
        }

        //Here is where you will actually send the message through to Sinch
        Toast.makeText(this, "Sending message! User id: " + mCurrentUser +  " Message: " + messageBody,
                Toast.LENGTH_LONG).show();
        messageBodyField.setText("");
    }


    @Override
    public void onResume() {
        super.onResume();

        mCurrentUser = ParseUser.getCurrentUser();
        mContactsRelation = mCurrentUser.getRelation(ParseConstants.KEY_CONTACTS_RELATION);

        setProgressBarIndeterminateVisibility(true);

        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.orderByAscending(ParseConstants.KEY_USERNAME);
        query.findInBackground(new FindCallback<ParseUser>() {

            @Override
            public void done(List<ParseUser> users, ParseException e) {
                setProgressBarIndeterminateVisibility(false);

                if (e == null) {
                    mUsers = users;

                    String[] usernames = new String[mUsers.size()];
                    int i = 0;
                    for(ParseUser user : mUsers){
                        usernames[i] = user.getUsername();
                        i++;


                    }

                    ActionBar actionbar = getActionBar();
                    actionbar.setTitle();



                }


            }

        });
    }

Look like you are quering parse data onResume(). So I am assuming in order to get the username from parse, you first need to interrupt the activity, then resume in order to call onResume() which intern fetches your data.

Also, your setTitle() seems null. You need to parse the username which would be a string of course, else it will crash.

Cheers for the reply Ronny, would it be better if i moved the Parse query from onResume into onCreate?

I'm not quite sure I understand how to implement the following

"You need to parse the username which would be a string of course, else it will crash."

Please could you show me an example.

Many thanks

Hey Tim, if you want to get the username from parse when you load the app, you will have to put the parse query object inside the onCreate() method.

As far as the action bar, you need to provide a string as a parameter to the method setTitle(); something like this actionbar.setTitle(parseUsername);