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 trialWei-Ting Chen
7,632 Points[HELP!!!!] Action Bar not show up in RecipientsActivity and EditFriendsActivity.
Hi everyone, I am using Android Studio 1.0, my action bar can't show up in the RecipientsActivity and EditFriendsActivity on both emulator and my Htc M8. The code from menu recipient.xml is down below:
``Android <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="co.sensenow.ribbit.RecipientsActivity">
<item android:id="@+id/action_send" android:title="@string/action_send_label"
android:orderInCategory="100" app:showAsAction="ifRoom"
android:visible="true"/>
//app:showAsAction="always" didn't work </menu>
``
The code from RecipientsActivity.java is down below:
``Android public class RecipientsActivity extends ListActivity {
public static final String TAG=RecipientsActivity.class.getSimpleName();
protected List<ParseUser> mFriends;
protected ParseRelation<ParseUser> mFriendRelation;
protected ParseUser mCurrentUser;
protected MenuItem mSendMenuItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipients);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
/some code had been omitted/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_recipients, menu);
mSendMenuItem=menu.getItem(0);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.string.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
mSendMenuItem.setVisible(true);
}
}
``
Any help will be appreciated, I've been stuck on this for three days ˊ____ˋ
Wei-Ting Chen
7,632 Points*HTC M8: Android 4.4.4 with HTC Sense 6.0 *emulator: Nexus5 with Android 5.0.1 thanks a lot
Wei-Ting Chen
7,632 Points@Ben Jakuben could you give some help?
7 Answers
Yin Sheng Ng
10,515 PointsHi, i have google some information and made some changes on it, and it is working now. Currently, i'm still learning, so it probably not a very good solution. I posted my code here for reference. Hope it helps !
In activity_recipients.xml, i changed the @android:id/list to @+id/RecipientsList .
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RecipientsList"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
In RecipientsActivity.java,
- First, i changed to extend ActionBarActivity instead of ListActivity.
- Then, declare the ListView by using ButterKnife or findViewById.
- Change all the getListView() method to the member variable of ListView you have declared.
- In onCreate method, add setOnItemClickListener method and copy and paste the content from the onListItemClick method. Add getCheckedItemCount() method and use it in if statement.
- Comment the onListItemClick method because it is no more functioning without ListActivity.
public class RecipientsActivity extends ActionBarActivity {
public static final String TAG = RecipientsActivity.class.getSimpleName();
protected List<ParseUser> mFriends;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected MenuItem mSendMenuItem;
@InjectView(R.id.RecipientsList) ListView mRecipientListView;
@InjectView(R.id.empty_friend_list) TextView mEmptyFriendList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipients);
ButterKnife.inject(this);
mRecipientListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(getCheckedItemCount() > 0){
mSendMenuItem.setVisible(true);
}else{
mSendMenuItem.setVisible(false);
}
}
});
mRecipientListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
@Override
public void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATIONS);
ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
query.addAscendingOrder(ParseConstants.KEY_USERNAME);
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> friends, ParseException e) {
if (e == null) {
mFriends = friends;
String[] usernames = new String[mFriends.size()];
int i = 0;
for (ParseUser user : mFriends) {
usernames[i] = user.getUsername();
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
mRecipientListView.getContext(),
android.R.layout.simple_list_item_checked,
usernames);
mRecipientListView.setAdapter(adapter);
mRecipientListView.setEmptyView(mEmptyFriendList);
} else {
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
public int getCheckedItemCount() {
ListView listView = mRecipientListView;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return listView.getCheckedItemCount();
}
SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
int count = 0;
for (int i = 0, size = checkedItems.size(); i < size; ++i) {
if (checkedItems.valueAt(i)) {
count++;
}
}
return count;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_recipients, menu);
mSendMenuItem = menu.getItem(0);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
.
switch(item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_send :
return true;
}
return super.onOptionsItemSelected(item);
}
}
Andre Du Plessis
4,874 PointsThanks this will help a lot of people.
Slava Fleer
6,086 Pointshi. thanks for help. maybe you also found how to add spinner for ActionBarActivity? Cause till I don't change it to extends Activity, I have crushing on opening Intent. But if I change it, I am loosing the 3 dots of ActionBar =(
Winthrop Granville
24,373 PointsSee Billy Ashmall's answer here: https://teamtreehouse.com/forum/action-bar-not-show-up-in-recipientsactivity-and-editfriendsactivity
These are the only changes I needed to make, that differed from Ben's code to get things to work:
1) After setContentView in onCreate() make sure you have setupActionBar(), or if you don't have this method in your class you can can instead use getActionBar().setDisplayHomeAsUpEnabled(true), which is simply the code that gets executed when setupActionBar() is called. Do the same for EditFriendsActivity if you are having the same issue there.
2) in the manifest for RecipientsActivity add android:theme="@android:style/Theme.Holo.Light". Once again, include this line for EditFriendsActivity if needed.
3) Finally, as Devanshu mentions in this post, the item in menu_recipients.xml should read: android:showAsAction="always" (This is the same code Ben uses in the video as well). I know Android Studio underlines this in red and suggests using app, but the icon only appears android.
Unsubscribed User
12,721 PointsHi Wei-Ting Chen ,
It seems that you need to correct your menu recipient.xml.
It should say -
android:showAsAction = "always"
and not -
app:showAsAction = "always"
(or "ifRoom" for that matter).
Update :
- Also add setupActionBar() in onCreate() after setContentView().
Unsubscribed User
12,721 PointsDoes this work Wei-Ting Chen ?
Wei-Ting Chen
7,632 Pointsafter adding setupActionBar(), it showed "Cannot resolve method "setupActionBar();" The reason for this might be "RecipientsActivity extends ListActivity" I thought.
Unsubscribed User
12,721 PointsYes, you might want to extend Activity in that case.
To show an action-bar for ListActivity , this thread might be helpful : http://stackoverflow.com/questions/21157660/listactivity-with-actionbar-support-v7
peter doherty
6,690 PointsI also experienced this issue and made some progress by defining and calling setupActionBar() - as suggested by Wei-Ting Chen - using the code sample for this step as a reference.
Unfortunately, that caused other issues that I wasn't able to fix.
I appreciate that Treehouse was in an awkward position when creating this tutorial, as Android Studio was still in beta, yet looming on the horizon. However, I'm of the opinion that they should have insisted that all users use Eclipse and not provide any support, teachers' notes, etc. for Android Studio.
Wei-Ting Chen
7,632 Pointsyep still no idea how to fix this. If the action bar doesn't show up,the following steps can't be execute because the Ribbit will crash.
Andre Du Plessis
4,874 PointsYes, this is rather frustrating....
Kevin Parks
5,475 PointsHere's what I ended up doing to work around this, using EditFriendsActivity as the example:
1) Change to extending AppCompatActivity in EditFriendsActivity.java
public class EditFriendsActivity extends AppCompatActivity {
2) Add a ListView as a member of EditFriendsActivity.java
private ListView mListView;
Add it in the layout activity_edit_friends.xml. Note I'm no longer using the android namespace for the ID.
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_friends_listview"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
While you're in activity_edit_friends.xml, update the empty textview, we'll use this later. Note again, not using android namespace for the ID:
<TextView
android:id="@+id/edit_friends_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/edit_friends_empty"/>
3) Next, let's use these in a similar fashion as before in EditFriendsActivity.java:
3a) Initialize the ListView
mListView = (ListView) findViewById(R.id.edit_friends_listview);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
3b) We were overriding the onListItemClicked method of the ListActivity previously, so we need to do something similar for our new ListView. I did this at the end of the onCreate method:
...
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mListView.isItemChecked(position)) {
mFriendsRelation.add(mUsers.get(position));
}
else {
//remove friend
mFriendsRelation.remove(mUsers.get(position));
}
mCurrentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
//success
} else {
Log.e(TAG, e.getMessage());
}
}
});
}
});
3c) Finally, I found this method that seems to work for setting an empty view when the ListView has no elements. I placed this right after the previous step, also in the onCreate method:
mListView.setEmptyView((View) findViewById(R.id.edit_friends_empty));
3d) Now, do some cleanup of the remainder of the class. For example:
- Change any getListView() to use member variable mListView instead. Many of the method calls are the same.
- Change setListAdapter to mListView.setAdapter
- Comment out the Overridden onListItemClick method
TheBigoso /\
3,217 PointsHi kevin,
I followed your steps is this for editfrinedsactivity or recipientsactivity?
my program compiles fine. I can take a picture or a 10 sec video. Then I press ok on the picture or video, Im able to choose my friends from a list of my added friends.
The problem is that there is no actionbar displayed and with no actionbar there is no send button displayed as it is involved with the actionbar. Since the release of this video things have been deprecated and the course is outdated.
Do you have any informantion on how to deal with my problem so i can move onto the next part.
Thank you, TheBigOso
here is my github https://github.com/TheBigOso/RibbitSendButton
RecipientsActivity.Java
package com.fartyou.thedirtyappstore.ribbit2;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import java.util.List;
public class RecipientsActivity extends ListActivity {
public static final String TAG = RecipientsActivity.class.getSimpleName();
protected List<ParseUser> mFriends;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected MenuItem mSendMenuItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_recipients);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
@Override
public void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> friends, ParseException e) {
if (e == null) {
mFriends = friends;
String[] usernames = new String[mFriends.size()];
int i = 0;
for (ParseUser user : mFriends) {
usernames[i] = user.getUsername();
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getListView().getContext(),
android.R.layout.simple_list_item_checked,
usernames);
setListAdapter(adapter);
} else {
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
activity_recipients.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.fartyou.thedirtyappstore.ribbit2.RecipientsActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/empty"
android:text="@string/empty_recipients_list_message"
/>
</RelativeLayout>
Kevin Parks
5,475 PointsTheBigOso,
I checked out your most recent commit on your github and I'm wondering if you're having trouble because of this commented out line:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_recipients, menu);
mSendMenuItem = menu.getItem(0);
return true;
}
I don't think the options menu can be properly created without that inflater method being invoked. You can check out my source on github here if you'd like: https://github.com/parkskevin/android
Unsubscribed User
12,721 PointsUnsubscribed User
12,721 PointsHi, which Android version do your emulator and HTC M8 run currently?