I'm new android developper. In my application I have a main activity with toolbar, that contains a title, and a recycler view.
The recycler view contains some items. I want to open a activity on click on them. My code is able to open the activity but the toolbar disappear.
I open the activity like this:
public VHolder(final View itemView){
super(itemView);
title = ((TextView) itemView.findViewById(R.id.articleTitle));
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(itemView.getContext(), ArticleActivity.class);
intent.putExtra("title", currentNews.title);
intent.putExtra("content", currentNews.htmlContent);
itemView.getContext().startActivity(intent);
}
});
}
Have you any ideas and advices ?
Sorry about my poor english ;).
You are doing everything in the correct way. The problem is that toolbar is just another widget on your Activity and cannot be shared between multiple activities. So you should add toolbar view to the layout of the Activity which you are starting (ArticleActivity).
As another option you can show Fragment over RecyclerView instead of starting new Activity. Similar to this: how to open a different fragment on recyclerview OnClick
I have found the cause of my problem of toolbar. The opened activity (ArticleActivity) not extends "AppCompatActivity" like the main activity but, "Activity".
Related
When i click on a button in recycler view adapter then one popup is appeared from service then it will go to next fragment.i write code like this it shows error how to solve this?
dialog_confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ConfirmAsyncTask().execute();
Intent intent = new Intent(context,OngoingFragment.class);
v.getContext().startActivity(intent);
}
});
If your are using Activity beside Fragment then add your activity in Manifest file. and if your are using Fragment then use FragmentManager to switch your fragment instead of Intent.
You can not start a Fragment like starting an Activity.
See this.
I'm trying to build a search bar similar to Flipboard. This search bar animates from below the toolbar to cover the toolbar. This GIF shows it better than I can explain:
Does anyone know if this is standard Material Design? And if so, are there any libraries or standard widgets I can use to do this? Soundcloud also does this so just wanted to ask if there was anything already out there. If not I'll just have to implement it myself.
Thanks!
To solve this I'd recommend looking into android.support.v4.app.ActivityOptionsCompat.makeSceneTransitionAnimation().
Here are the snippets from the code I remember:
I have ActivityA which contains an EditTextA.
The EditTextA has a View.OnClickListener:
View.OnClickListener() {
#Override
public void onClick(View view) {
ActivityB.launch(getActivity(), view);
}
}
which when invoked calls a static method: ActivityB.launch(Activity a, View transitionView):
public static void launch(Activity activity, View transitionView) {
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
activity, transitionView, EXTRA_TRANSITION_VIEW);
Intent intent = new Intent(activity, ActivityB.class);
ActivityCompat.startActivity(activity, intent, options.toBundle());
}
The code above is basically creating an Intent that will launch ActivityB. That Intent also includes a Bundle (options.toBundle()) which has a bunch of stuff, including the view that is transitioning from ActivityA to ActivityB. In my case this is the EditTextA.
In ActivityB.onCreate() all we need to do is "connect" that view to the new view it's transitioning to. So in my case, EditTextA is transitioning to another EditTextB that lives in ActivityB.
ActivityB.onCreate() {
....
mEditTextB = (EditText) findByViewId(...);
ViewCompat.setTransitionName(mEditTextB, EXTRA_TRANSITION_VIEW);
}
If I remember everything correctly that should be it :).
I took over this android project from an outsourced developer. I do iOS development so I am still trying to get up to speed with android. The app has a top tab navigation which appears on all views. The home view is a fragment which has icons which when clicked on produce webviews that replace the home view, which the navigation tab still in view. I want to replace one of the webviews with a list view which is a fragment which I already have coded (EHallSchedFragment.java), so that when the icon is clicked on the list view replaces the home view with the navigation tab still in view. See the images below.
HOME VIEW
VIEW I WANT WHEN EXHIBIT HALL SCHEDULE ICON IS CLICKED ON
Here is the code for the onClick event for that button as it exists. It calls a webView:
ivExhall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
openInternalWebview("http://www.web.org/m/content.aspx?id=7006");
//Need code here to open EhallSchedFragment.java
}
});
SO what I need is the onClick code that will open the fragment in the existing view, leaving the navigation tab in place.
Try this approach: Define an interface inside your fragment, implement it in your activity and then override the method there. Then finally, from your fragment, when a user clicks an item from a list, call the interface method to notify the activity of the event.
From Your Fragment
#Override
public void onClick()
{
//when a user selects an event from your list
switch(position)
{
((OnScheduleSelectedListener) getActivity()).switchFragment(new DetailsFragment());
}
}
public interface OnScheduleSelectedListener
{
void switchFragment(Fragment frag);
}
You can then do the following in your activity:
public class ScheduleAppActivity extends Activity implements OnScheduleSelectedListener
{
.............................
#Override
switchFragment(Fragment frag)
{
//check if fragment is in view here and if,
replaceFragment(fragment)
//else
addFragment(frag);
commit();
}
}
So at this point, the right fragment will be added to view.
I hope this helps you!
I have setup a new android project with the default navigation drawer implemented for me. I created a custom NavagationDrawerAdapter which will return a more complex RelativeLayout for the first item of the navigation drawer. In this RelativeLayout, I have some buttons.
I have assigned the onclick attribute in the xml for these buttons to be addPhoto.
I then declared addPhoto thus in the MainActivity:
public void addPhoto(View view) {
Intent intent = new Intent(this, AddPhotoActivity.class);
startActivity(intent);
}
As I understand it, the navigation drawer is only a fragment and it resides in the MainActivity?
When I run the app it gives:
Could not find a method addPhoto(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.ImageView with id 'imageView3'
It looks like it is using the activity ContextThemeWrapper or something...
How should I handle the onclick for an item in navdrawer?
Any help would be appreciated.
I am trying to make an application which have 4 tabs at the bottom of the screen.
All of them contain Activity (Intent).
And I want to navigate any of the Activity to another activity. But want to keep the TabWidget visible.
Let me know as quickly as possible if you know about it.
Shaiful
The problem of error occuring due to the replacement of activities can be solved in the following manner.
First Let us understand the flow:
We have in a Tab host , activity (say a list) from which we need to go to the next Activity (say details for the clicked item) under the same tab. For this we can use the concept of replacing the activity.Also setting the flags for the tab selected and other for knowing that details are being shown now
When we press back we should get the previous activity under the same tab.For this instead of again replacing the activity we can refresh the tab while using the particular flag for tab which was selected. Also if flag for show details is true we'll go the the list in the same tab or else we will go the activity before the tabwidget (normal use of onBackPressed)
The code can be as follows..
For going from list to details...
(This can be in the onClickListener)
private OnClickListener textListener = new OnClickListener() {
#Override
public void onClick(View v) {
Constants.SHOW_DETAILS = true;
Intent intent = new Intent(context, DetailsActivity.class);
replaceContentView("activity3", intent);
}
};
public void replaceContentView(String id, Intent newIntent) {
View view = ((ActivityGroup) context)
.getLocalActivityManager()
.startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
((Activity) context).setContentView(view);
}
When back pressed is done we override on BackPressed in each of the Activity under the tab to go to the list again from the details screen
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
if (MathHelper.SHOW_DETAILS) {
Log.e("back", "pressed accepted");
Constants.LIST_ACTIVITY = 1;
Constants.SHOW_DETAILS = false;
Intent intent = new Intent(this, Tab_widget.class);
startActivity(intent);
finish();
}
}
The most important part here is
Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc
Again to load the correct list (Activty) when the tab activity is refreshed we have to include this in the TabWidget onCreate after the creation of the tabs
tabHost.setCurrentTab(Constants.LIST_ACTIVITY);
This is implemented in Tabs with multiple activities in a single tab.
However when multiple times activities are called StackOverFlow error arises. Tried very hard but unable to solve it.. Please someone tell a method to solve this problem
Also need to Replace an activity in a tab, However from child activity. How is that to be done?
At any one moment there may only be one activity. Docs about this here