How can I pass this code to my another activity, I created two activities one is home activity and another is video_activity I call this code in video_activity in bottom nav but I also want to call it on my home activity I tried too much by I am not getting the correct one and please help me ... I want to pass this code from video activity to home activity ...
case R.id.navigation_dashboard:
favourite = toolbar;
favourite.setTitle(item.getTitle().toString());
videos.clear();
fetch_db("favorit");
favouritList();
return true;
You can wrap it in a function and call it from other activities
Example:
public class A {
public void foo(){ //your code ... }
}
In other activities:
A namefunction = new A();
namefunction.foo();
Or you can use the static method
Here are several instructions you may want to look: link 1 , link 2
Related
My project operates around a navigation activity that uses fragments for the individual menu pages. I'd like to be able to open other fragments through clicking a button (as opposed to a navigation menu icon), however I can't figure out how to accomplish this. I've looked at both the following resources (and more), but I don't fully understand them.
How can I make my button to do something in Fragments,ViewPager
https://developer.android.com/training/basics/fragments/communicating.html
Furthermore, clicking a button would also have to call on code in the main activity that uses the app bar.
If someone can explain to me how to accomplish the desired effect, that would be great.
If you want to open a fragment by pressing a button from a fragment the call should go through the current activity.
Create a callback to the activity and call that callback when pressing the button. Then the Activity launches the second fragment when the callback is called.
This way the first fragment does not need to know anything about the second fragment. The Activity deals with the navigation between fragments in the same activity.
I think this is the cleanest and easiest solution.
edit: I'm basically saying the same as the link to the android documentation posted in the question. I think this is the best solution.
For simplicity: Activity = [A], Fragment 1 = [B], and Fragment 2 = [C]
Step 1: Create an interface inside [B] called IOnInitiate[C]Listener, and within that interface create a method called void Initiate[C]();.
Step 2: Create a private property within [B] of type IOnInitiate[C]Listener called (your-prefix-convention)OnInitiate[C]Listener.
Step 3: Create a public method within [B] called public void SetInitiate[C]Listener(Activity activity), and cast the activity parameter into the private property created in step 2.
Step 4: [A] must implement the interface [B].IOnInitiate[C]Listener and the method public void Initiate[C]().
Step 5: From [A], whenever you create a new instance of [B], be sure to call the method from step 3 and pass in this for the Activity parameter. Otherwise a null exception will occur.
Step 6: Now whenever you need to initiate [C] from [B] simply call the interface method from [B]'s private property. Example:(prefix)OnInitiate[C]Listener.Initiate[C]();
This is what mine looks like. I use C# for my development, so your syntax may be different.
public class A : Activity, B.IOnInitiateCListener
{
private void InitiateB()
{
B b = new B();
b.SetInitiateCListener(this);
b.Show(FragmentManager, "B");
}
public void InitiateC()
{
C c = new C();
c.Show(FragmentManager, "C");
}
}
public class B : Fragment
{
public interface IOnInitiateCListener
{
void InitiateC();
}
private IOnInitiateCListener _IOnInitiateCListener;
public void SetInitiateCListener(Activity activity)
{
_IOnInitiateCListener = (IOnInitiateCListener)activity;
}
private void InitiateC()
{
_IOnInitiateCListener.InitiateC();
}
}
I have an autocomplete textview. I disabled it and i want it to be enabled after a button is click which is from another activity. How will i do that?
if your activity doesn't destory you should make interface to aware activity to enable autocomplete textview
When you are changing activity by Intent . add This.
intent.putExtra("isButtonEnable",true);
And get That in Other Activity onCreate like This.
if(getIntent.getExtras.getBoolean("isButtonEnable")){
yourTextView.setEnable(true);
}
else{
yourTextView.setEnable(false);
}
Same as you can do it in fragment also
ActivityA
Intent intent = new Intent(ActivitA.this, ActivitB.class);
intent.putExtra("EditTextVisible", true);
startActivity(intent);
Then in ActivityB in onCreate():
if(getIntent().getBooleanExtra("EditTextVisible")) {
editTextB.setVisibility(View.VISIBLE);
}
first create a public method in your first activity where u want to manipulate the widget
public class methodEnable()
{
//your code
}
Then in your other class create a object of ur first class
FirstClass f1=new FirstClass();
Now on Button Click:
f1. methodEnable();
Hope it works :)
Make flag in your api-code and when second activity is closed - first activity will appear onResume function, where you can check you api and set enable TextView
I'm making my very first Android application but I ran into a problem.
I have over 8 different classes which all use the same actionbar.
Now in place of calling the method in every different class (and having a lot of double code) I would like to call the method of the main class in my other classes.
This is a part of my code for the onOptionsItemSelected in main.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.actionbar_new_income:
Intent newIncome = new Intent(this, NewIncome.class);
this.startActivity(newIncome);
return true;
}
}
Now I was wondering how I could call the method in another class (newIncome.java)
I have this so far, but it keeps saying I need to add arguments. And I ofcourse need to be able to detect which menuitem is clicked..
MainActivity main = new MainActivity();
main.onOptionsItemSelected();
Any help please?
Thanks!
You should not do this. If you have common code then put it in a class (not an activity) that is accessible by any activity that needs it.
You will still have some duplication but this is normal.
A good way of reducing activity launch code is to add a static method to each activity that you can call which launches the activity it is in.
E.g in your NewIncome Activity you could have
Public static void Launch(Context c) {
Intent newIncome = new Intent(c, NewIncome.class);
C.startActivity(newIncome);
}
You could then launch this activity from any other activity just by calling
NewIncome.Launch(this);
If required you can add parameters to the method and then add Extras to the Activity using these parameters.
You can do it like the following example if your menu entries are totally independent of the activity in which they are contained:
In each activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return CommonClass.HandleMenu(this, item.getItemId());
}
In a common class
public class CommonClass {
public boolean HandleMenu (Context c, int MenuEntry) {
switch (MenuEntry) {
case R.id.actionbar_new_income:
NewIncome.Launch(c);
etc....
...
}
}
If your 8 classes are activities you may define a base activity with the onOptionsItemSelected which is the one where you put the elements in the actionbar you want. Then make the other activities derive from it.
I must write a program with android which can find ssid and show it. My problem is how can i make a relation between an image button in first page and an activity or function in other page.
Buy the way i'm beginner and download the code of searching method because of that i can not recognize which one is the main method or function for pass it to setonclick method that i write for image button in first page?
please answer as soon as you can i need it immediately.
If you are trying to call another activity's method from your existing activity then you should use these steps.
First save the context or instance of the callee activity; for this you can use a class that holds your global data. In this class make the object of this activity.
FirstActivity first = null;
When the callee activity is first created, initialize this instance.
public void setFirstActivity(FirstActivity factivity)
{
first = factivity;
}
And when you do need to call this callee activity's method then access this instance from this global class and with the help of this instance, you will have access to the methods of the callee activity.
public FirstActivity getFirstActivity() {
return first ;
}
use it to get access to the methods or instances of the activity.
Click on the ImageButton, and add the name of the function in the On Click property (Simply the name, like myFunction)
In your code, copy-paste this function and replacemyFunction by the name of the function you chosed
public void myFunction(View v)
{
String ssid = ((EditText)findViewById(R.id.EditTextID)).getText().ToString();
Intent intent = new Intent(this, SecondView.class);
intent.putExtra("SSID_KEY", ssid);
startActivity(intent);
}
And replace SecondView by the class that displays your second page and EditTextName by the ID of your EditText.
In the OnCreate function of your second class, you can get your ssid using the following code
#Override
protected void onCreate(Bundle savedInstanceState) {
String ssid = getIntent().getStringExtra("SSID_KEY");
//Do other work here.
}
Also, on top of the class containing the ImageView, don't forget to add these lines:
import android.view.View;
import android.widget.EditText;
How to switch layouts? First, I have a class Main where is onCreate (setContentView(R.layout.main);) and then I call, another class with command:
setContentView(secondClass);
In this class, I draw with Canvas and this work just fine. I also create button to go back in first "class" (R.layout.main), but I don't know how to do it.
Now my program is basic a graph shower. In first class you type your function and them second class draw it. But how to go back in first class to type another function. This "back" button or arrow witch every Android phone have, send me out of program not back on insert part.
In secondClass I can't create onCreate method, but I also tried the following and they didn't work:
Intent abc = new Intent("bla.bla.bla.FIRSTCLASS");
startActivity(abc);
and
Intent abc = new Intent(SecondClass.this,FirstClass.class);
startActivity(greNaPrvoOkno);
If you want to use a custom view (as I understood, you are extending the View class), you can do it in the following way;
Consider you are showing the second class from your Main activity like this;
setContentView(new SecondClass(getApplicationContext(), MainActivity.this));
And you Second class is this (suppose);
// I am using onClickListener to go back to main view. You do whatever you like.
public class SecondClass extends View implements OnClickListener {
// This is needed to switch back to the parent activity
private Activity mParentActivity = null;
public SecondClass(Context context, Activity parentActivity) {
super(context);
mParentActivity = parentActivity;
setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Set the Main view back here.
mParentActivity.setContentView(R.layout.main);
}
}
Disclaimer: This code will do what you have asked for, but may cause other problems.
As advised by #Mudassir, you should use two different activities for two screens. It will give you better control, and your code will be easy to understand and maintain.
On the Onclick event of the button you have to write finish(); that's it..
Both of your classes are Activities yes? IF so then in your second activity you will simply call finish() and your activity will close revealing your first activity again.
When I have used multiple intents in my android application, I have created a new activity through:
Intent abc = new Intent(this, SecondClass.class);
startActivity(abc);
When the button is pressed in your second class, I would then either call finish(); on the class, or create a new intent like so:
Intent abc = new Intent(this, FirstClass.class);
startActivity(abc);
However, this method has the disadvantage that if a user wanted to use the back button, they may have to scroll through many layers of activities.
You should create another activity for your second class but not just set the main activity to a new view setContentView(secondClass).
For an easier modification, You could try to set the view back to setContentView(R.layout.main) first.
You still need to configure the widgets(e.g. TextView) on it when you set it back.
You don't have to startActivity again to go back.
Just call finish() in your second activity when you want to finish the current activity and go back:
e.g. When user press the back button in your second activity
mButtonBack.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
}