I am replacing the ActionBar and integrating the Material Design ToolBar within my app. One of my activity's extends ListActivity.
In the onCreate() method, whenever I try to add the ToolBar, the setSupportActionBar() method is producing an error saying it cannot be resolved.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_route_recipients);
// Attaching the toolbar layout to the toolbar object
toolbar = (Toolbar) findViewById(R.id.tool_bar);
// Setting toolbar as the ActionBar with setSupportActionBar() call
setSupportActionBar(toolbar);
// get the default list view associated with this activity
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // we can now check and uncheck multiple friends
// get the array of LatLng points passed in from the map intent
markerPoints = getIntent().getParcelableArrayListExtra("markerPoints");
}
What is the best solution to this problem?
You wrote that your activity extends ListActivity but sadly ListActivity doesn't implement setSupportActionBar. You have to base your activity on AppCompatActivity.
You'll say "but hey, but I really need ListActivity as I've ListView in my Activity" -- consider using RecyclerView as well. I'd say that ListView is a dead end now -- one should port code to RecyclerViews
And as a help, two sample projects showing all new features of Material Design
https://github.com/antoniolg/MaterializeYourApp
https://github.com/chrisbanes/cheesesquare
Related
I was coding this program the other day, hoping that the fragment was doing okay, but it isn't. My project is a Relative Layout app where the GPS map will show up on the screen first thing the activity pops up, and then the choices to view some of the Javanese culture of Indonesia (in the form of Grid - Card View combination), will be available for selection.
Here, in this line, I have a class fragment inflation error, according to Logcat :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java_island); // Erroring line
KerisCard = (CardView) findViewById(R.id.KerisCard);
TradClothesCard = (CardView) findViewById(R.id.TradClothesCard);
TradHouseCard = (CardView) findViewById(R.id.TradHouseCard);
TariMerakCard = (CardView) findViewById(R.id.TariMerakCard);
SupportMapFragment javaMapFragment = (SupportMapFragment) getSupportFragmentManager().
findFragmentById(R.id.java_island_map);
javaMapFragment.getMapAsync(this); . . . }
In retrospect, I'm not very familiar to the concept of fragments as the classes I've been attending aren't going too detailed on the usage of fragments. Also, I have to note without the declaring the object references ((CardView) declarations on the onCreate() function), this error would not show up, and the app will be executable as normal.
Instead of making support fragment reference create fragment reference directly and call method from fragment.
SampleFragment fragment=new SampleFragment()
Fragment .someMethod
Here is a confusion
Do you want to open fragment in activity ?
Because you are trying to find fragment from id ,so have you created in xml if not then there is problem.
I'm trying to update the subtitle of my toolbar from an adapter class. I've tried the accepted answer from here.
((OrderActivity) context.getSupportActionBar().setSubtitle("Bla Bla"));
Order Activity is the calling activity and this line of code is in the adapter. The context being used here is passed from the calling/parent activity.
I'm getting a 'cannot resolve method getSupportActionBar()'.
try ((OrderActivity) context).getSupportActionBar().setSubtitle("Bla Bla");
When I place "setContentView" above the "NumbersClickListners" line the app works as expected.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListners numbersClickListners = new NumbersClickListners();
TextView numbers = (TextView) findViewById(R.id.numbers);
numbers.setOnClickListener(numbersClickListners);
}
But as soon as the "setContentView" is placed below the three lines starting with "NumbersClickListners" the app crashes. The code looks like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NumbersClickListners numbersClickListners = new NumbersClickListners();
TextView numbers = (TextView) findViewById(R.id.numbers);
numbers.setOnClickListener(numbersClickListners);
setContentView(R.layout.activity_main);
}
I'm pretty much unsure of the reason for this behavior. Can anybody help me with that please?
Let's look at the life of layout.
First of all, you have to create it by declaring XML file, where you do your design in a user-friendly way and name the elements according to your needs.
Now your layout is just a file, that Android doesn't really care about for performance reasons.
Next thing you wanna do is use your layout. To do that your file needs to be converted to internal structure of objects known as ViewGroup and Views.
This process is called inflating. That's the point where the system can find the views for you by calling findViewById().
So in the second snippet you ask activity to find you a button, which is not inflated. That leads to throwing exception.
Generally speaking, first thing you want to do in the onCreate is to call setContentView().
I am working on a big app that has recently adapted the drawer navigation style and now I'm trying to customize some of the elements.
Because of its great size and complexity it was decided to adapt the navigation drawer to Activities and Fragment Activites instead of Fragments as the extended standard use.
For that I created a base Drawer activity called NavDrawer, which is inherited/extended by all of those activities that need to navigate with the Drawer. Every Activity that extends NavDrawer Activity calls NavDrawer onCreate method by calling super.onCreate(savedInstanceState, R.activity_layout) adding its layout_id instead of executing setContentView(R.layout_id).
This base Activity also handles all the intent calls to the other different activities whenever the user clicks on one of the sections/textviews.
I managed to make it work pretty well so far but I'm facing now a weird issue when customizing the Typeface of the TextViews inside the drawer. First time it is launched the app, I can see all the links without the proper style, but only after selecting one of the sections of the drawer, navigating to it and opening again the drawer, I see the textviews properly formated with my custom font.
I have been struggling with it for a few days already, and I am sure it has to be a stupid rookie mistake but I can´t figure it out what it is.
This is the NavDrawer class OnCreate method:
protected void onCreate(Bundle savedInstanceState, int resLayoutID) {
super.onCreate(savedInstanceState);
setContentView(resLayoutID);
AC = (ApplicationController)getApplicationContext();
mModel = AC.getModel();
//Setting up controls for the navigation drawer
if(AC.getModel().hasRatedApp() != null && AC.getModel().hasRatedApp()){
mLinksTitles = getResources().getStringArray(R.array.menu_links_has_rated);
}
else
mLinksTitles = getResources().getStringArray(R.array.menu_links_has_not_rated);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerContainer = (LinearLayout) findViewById(R.id.left_drawer_cont);
mDrawerList = (ListView) findViewById(R.id.menu_links);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(AC, R.layout.drawer_list_item, mLinksTitles){
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Typeface mFaceR = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Lato-Regular.ttf");
View v = super.getView(position, convertView, parent);
((TextView) v).setTypeface(Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Lato-Light.ttf"));
return v;
}
});
I would really appreciate any help or code you could provide me since I´m still quite new in Android development.
Finally I just found out what was going on, actually I set by mistake another adapter for my mDrawerList later in the code. As I said, stupid rookie mistake, but the code above still worked for me and hope it can help other people to customize their Navigation Drawers too.
I am working on an android project in which i have to add links i am following the given below example.. But when i am clicking on the link neither it is highlighting nor i am able to access the link. Clicking on the link is causing an exception which says
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I want to open the link in the browser when i am clicking on the link.How to do it efficiently?. I am expecting an answer soon !Thank you
NB: i am using a dynamic textview in my actual project
public class StackOverflowActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView link = new TextView(getApplicationContext);
String linkText = "Visit the <a href='http://stackoverflow.com'>StackOverflow</a> web page.";
link.setText(Html.fromHtml(linkText));
link.setMovementMethod(LinkMovementMethod.getInstance());
}
Use android:autoLink="web" in your TextView xml. And if it does not work, try android:linksClickable="true"
Based on your error, if using dynamic TextView make sure you refer your view with this object as
TextView yourView = new TextView(this);
this refers to your Activity context where I think you might have made a little mistake.
You should not use getApplicationContext for creating a view with Activity context.
I believe you have to inflate the layout that contains your TextView before you can use it, like so:
getActivity().getLayoutInflater()