I am developing an app and have successfully added a floating action button using the library shown here. The floating button displays well but when i navigate to another fragment through the navigation drawer the button still displays instead i want the button to only display in the activity i created it. I checked for those that had similar issues online and i saw comments...saying that i have to set the View by modifying this line of code found within the method show below.
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
Please can tell me how to achieve this, thanks in advance.
Library Method
public FloatingActionButton create() {
final FloatingActionButton button = new FloatingActionButton(activity);
button.setFloatingActionButtonColor(this.color);
button.setFloatingActionButtonDrawable(this.drawable);
params.gravity = this.gravity;
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
return button;
}
if you can access the FAB view using findViewById you can simply do:
button.setVisibility(VISIBILITY.GONE);
to hide it, that way you can set visibility back to visible when you go back to that activity.
or if you can access view called 'root' (or can access button via getActivity().findViewById for Fragments)
root.removeView(button);
Set the FloatingActionButton to be invisible by default in the XML file:
<ImageButton android:id="#+id/button" android:visibility="invisible"/>
and make it visible by using the following code in the required activity class:
button.setVisibility(View.VISIBLE);
Related
I am trying to create a like and dislike button. I use Checkboxes to do so.
In the XML code I have two checkboxes one called like and the other dislike
I'm trying to toggle between the like and dislike buttons. Such that they both cannot be switched on at the same time.
public void onLike(View view) {
if (dislike.isChecked()) {
dislike.setChecked(false);
}
Toast.makeText(this,"liked",Toast.LENGTH_SHORT).show();
}
The issue that I am having is that set setChecked(true) is not doing anything.
For more context, the XML for the checkbox is defined inside a fragment that has a cardview. Each item in the card view has the checkboxes.
the way I initialized the checkbox in the main activity is as follows: -
View cardViewLayout = getLayoutInflater().inflate(R.layout.text_row_item,null);
like = (CheckBox) cardViewLayout.findViewById(R.id.like);
dislike = (CheckBox) cardViewLayout.findViewById(R.id.dislike);
any ideas what's going on?
ok, I've figured out the solution. Since I am using a recycler view with a custom adapter I need to bind the onClick listener via an interface.
Here is a link to another post that will show the necessary steps to implement click listeners in adapters: https://stackoverflow.com/a/49969478/11379938
I am trying to build a Floating Action Button that will show in every activity that is being ran. How would I accomplish to get the layout to show in each activity.
I have seen some methods with WindowManager but I am not a fan of it sitting on top of the app and needing the overlay aspect to it. Is there a better way with ZIndex or adding to the view from a service when a new activity is transitioned, etc. to accomplish this?
Thanks.
You can create a separate class with a static method, which will create a FabButton and will attach it to your root view.
public class FabButton {
public static void init(AppCompatActivity activity){
View root = root.getWindow().getDecorView();
FloatingActionButton fab = new FloatingActionButton(getContext());
fab.setId(R.id.fab);
fab.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
));
root.addView(fab);
}
}
And you can use this wherever you want to add. In your activity's onCreate, call:
FabButton.init(this);
Edit: I found a better answer according to your requirement
in your application class'onCreate, add
registerActivityLifecycleCallbacks(this);
And then implement methods of ActivityLifecycleCallbacks.
Now in
#Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
FabButton.init(activity);
}
create a layout fab_layout:
<android.support.design.widget.FloatingActionButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_fab_24dp"
android:layout_marginRight="#dimen/fab_margin"
android:layout_marginEnd="#dimen/fab_margin"
android:layout_marginBottom="#dimen/fab_margin"
android:layout_gravity="bottom|end"
app:backgroundTint="#00ff00"/>
in all activities add this tag:
<include layout="#layout/fab_layout />
You can create a base activity "FABActivity" in which you can add the FAB button to the root layout at runtime in the onCreate() method.
Every activity which extends this activity will have the same FAB this way.
There are a myriad of ways said here that you can achieve what you want.
Here is another one:
You can have all your activities as fragments. You can create one MainActivity with xml layout with Relative layout in which you will add your FAB there and a container(with width and height=match_parent for the fragments(your activities).
This is what I'm hoping to achieve:
Step 1: Click on the Green Button to open View1.xml on the ViewPager. (Works)
Step 2: Click on the Orange Button to open SubView.xml on the ViewPager. (Doesn't work)
So, I have set the onClick event for the orange button the following:
public void openForm(View view) {
ViewGroup item = (ViewGroup)findViewById(R.id.vp_horizontal_ntb); //this is the view pager
View child = getLayoutInflater().inflate(R.layout.activity_post_form, null);
item.addView(child);
}
Results:
Nothing. Nothing happens.
If I switch viewpager for linearlayout it opens (and breaks tab navigation). What I wanna do is open it on the viewpager, so it looks and behaves as if it's on a tab, until it's removed by clicking on another tab.
I also thought about putting a linear layout there and make viewPager gone while the linearLayout is set to visible. But I feel like there's a better solution than this.
I've got an activity where there is a button which opens an AlertDialog.
My dialog works and I decided to add a layout to it which contains a spinner.
So I have 3 documents :
mainActivity.java : Its role is to open a Dialog
activity_main.xml
dialog_main.xml : The dialog's layout containing the spinner
I try to retrieve in mainActivity.java the spinner declared in dialog_main.xml (in order to add it an adapter) :
Spinner mySpinner = (Spinner)findViewById(R.id.mySpinner);
However mySpinner = NULL, I can't find mySpinner. What is the problem ?
findViewById
as a method of the Activity class finds Views in your Activity's layout. When you are showing a Dialog, it is not part of your layout, so you have to findViewById in the Dialog's layout.
You are probably inflating a View for the Dialog that you show, you cou can use this view to find your Spinnner.
It probably looks like
View view = layoutInflater.inflate (...);
then you do
view.findViewById(...);
Post the code of how how show the Dialog and I'll show you if you can't do it.
Problem Description and Question
I'm using tab activity with action bar. In the HOME tab I have GridView which contains some items, like it is shown in the image below (Hot Lines, Taxi, Restaurants etc)
Wen user click on the items in the grid view I want my application to take following actions:
Change icon of application to grid view item image on which I pressed.
Change the test near the icon
Add standard back button near icon, which will go back to grid view screen.
Change the tab fragment to the one which I specify.
Like it is shown in the image below:
As I never deal with this kind of problems can you please give me example or share some link with me of how I can do this? and can I do this at all?
This might help:
Android studio - is possible to add tabs pointing to fragments from designer?
It is not exactly what you want, but a good start. If you are willing to put a bit of work in it, you should be able to get what you want. If you have a basic Frame to work with and more specific problems with this matter I will gladly help you out ^^
John
The first link you can check is THIS.
And you should read more about ActionBar.
The last thing is it's better if you google it first and try to write a code and when you got stuck somewhere share your code with us and ask for help.
You have to use actionbarsherlock library for it.
use android.support.v4.app.FragmentTabHost and TabWidget in the xml as shown below :
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
style="#style/yourstyle"/>
<FrameLayout
android:id="#+id/realtabcontent"
style="#style/realtabFrameContentStyle" />
<TabWidget
android:id="#android:id/tabs"
style="#style/yourtabstyle" />
</android.support.v4.app.FragmentTabHost>
Use SherlockFragmentActivity for showing the tabs.
In the activities code use following code (preferably in a function) to set the ctionbar icon and text :
activity.getSupportActionBar().setDisplayShowCustomEnabled(true);
activity.getSupportActionBar().setCustomView(R.layout.your_action_barlayout);
((TextView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title))).setText("your title");
ImageView homeButton = ((ImageView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.your_icon)));
homeButton.setVisibility(View.VISIBLE);
homeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, YourHOmeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
activity.finish();
}
});
ActionBar mActionBar = activity.getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
mActionBar.show();
then call this function with your icon and your text in your fragments onResume() method.