Using Java, how can I show a Tab Widget on each and every Activity, even if that Activity is a subActivity of FirstActivity? If possible, please provide me with some code or examples.
Use THis to start the new Activity
View view = getLocalActivityManager().startActivity("tab1", new Intent(this,tab1.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
setContentView(view);
Create Another GroupActivity and putExtra info along with your intent so when it gets it ,the group activity can check what tab should be opened.Use My code to open the new Tab.
Related
I am working with an android project for which I want to create a widget. Using this widget I have to control some actions of the control (that is by pressing the stop button from widget the app should stop working of some action). For that I have to pass some value to app from the widget on a button click. So far I am able to do the following:
open an activity on button click
open an URL on button click
make a toast on home screen on button click
Any help is appreciated,thank you.
I have successfully completed this part of my project.There are two scenarios
When we are passing values the app will be launched. That is we are passing the values directly to an activity
With out launching the app we can pass the values.Here we are using service
In both situation we can use two method
Using sharedpreferences
Using Intent(putExtra())
If you want pass the value directly to an activity(the App will be launched while passing the value) you can use second method above mentioned and call the activity(which activity you wanted to be launched) as follows:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
Intent intent = new Intent(context,MainActivity.class);
PendingIntent pendingIntent = PendingIntent(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.PunchIn, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
this code to launch MainActivity from widget button click. In Inten you can use putExtra() to pass values.better to not use sharedpreference here.
For passing value with out launchingthe app,you can call service from widget button then you can add whatever code you want to add in the service.class file. you can call the service as follows:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
Intent intent = new Intent(context,MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.PunchIn, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
from this service class you can pass values to the application using SharedPrefernce or putExtra().
if you want to make a button widget which work same as button click of the app you can use second method. Here you can call a service on on button click as mentioned above, Then write code for the button click in the service class.
i am new to android,i worked with widget in my project for that researched a lot and it took lots of time. so thought about posting this which will help anyone like me. if you found any error/mistake please correct me.happy to correct my mistake.
Thank you
I'm not sure with this problem because I have not try widget yet.
When button click, send a boardcast message. And your app needs to run a service to receive the message. You can put your values in the message?
Wish this link may give you some ideas:
http://rxwen.blogspot.com/2012/10/communication-between-android-widget.html
There is a similar question:Android - Communications between a widget and its app
Good luck!
I am learning how to develop under Android. I've made a new project, main activity, and I wanted to design a new window. I've genereted the new Activty as it it described here
Best way to add Activity to an Android project in Eclipse?
But i can't get the visual editor for that new activity. I know that I suppose to create new layout but how to do it and connect it with that second Activity?
How to properly go back from the secondActivity(close it? minimize it? how?) to the mainActivity and don't loose information gathered why we were using secondActivity(for example what options user has made?
This is how i invoke the second Acitivity and it works fine.
Intent intent = new Intent(this,DrugieOkno.class);
startActivity(intent);
For question 1:
Here is a basic tutorial on how to create a new Activity. For a more comprehensive one containing more information about Android development you can see here.
For question 2:
For tansfering data between Activities here is a nice tutorial.
Hope that helps.
To add a new activity, follow the methods answered in this question. This way you will create a new activity without adding it to the Manifest manually. [every activity needs to be listed in the AndroidManifest.xml].
Say you create a new activity names Activity2.java. To add the new layout to the new activity, add a new xml file to res/layout folder, say activity2.xml [where you define your new activity's layout]
To link the new layout to the new activity, include this line in your newly created Activity2.java
setContentView(R.layout.activity2);
So it will look like this:
public class Activity2 extends Activity{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
}
}
2 . Now if you want to send some data from your Activity1.java to Activity2.java, you need to use Bundles.
So, if you want to send say a String from Activity1, do the following in Activity1.java:
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent
To recieve data in your Activity2.java do the following (say, onCreate())
Bundle params = this.getIntent().getExtras(); //gets the data from the Intent
String firstName = params.getString("fname"); //gets value of fname
I have 3 tabs (act1,act2,act3) and i have activities without tabs(A,B), if the user open activity A and press button OK then alarm will start and after 10 seconds it will go to act2
this all done, but i tried many thing :
1- when i go to act2 it does not display the tabs. just act2 activity
so i change the code and tried to :
2- when i go to activity tabs it show me the first tab(act1) but i wanna act2
how can i do it
i wanna display act2 with tab
Give me any reference or hint.
Thanks in Advance.
Try this: Send an intent (via startActivity() as usual) to bring the activity to the front which contains the tabs. Send an extra parameter with the activity containing the TAG or some identifier for tab, you want to be opened. Evaluate the extra parameter in the activity, which contains the tab and let it switch to the tab as indicated by the parameter.
EDIT
To start the tab activity with a parameter:
final Intent i = new Intent(this, YourTabActivity.class);
i.putExtra(TAB_TAG, tag); // TAB_ID see comment below, define some tags for the tabs
this.startActivity(i);
To extract the parameter from the intent:
Overwrite onNewIntent() in the tab activity and introduce a field lastIntent, set this.lastIntent = this.getIntent() there. (Otherwise you will always access the intent which started the activity in the first place, not the most recently sent intent!)
in onResume process the last intent:
final Bundle extras = this.lastIntent.getExtras();
final String tabTag = extras.getString(TAB_TAG); // define the key TAB_TAG as static string
Now use tabTag to set the current tab.
I have some tabs in my app and I want the last tab to launch google in the default system browser. I thought this would work:
Uri uri = Uri.parse("http://www.google.com/");
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Google", res.getDrawable(R.drawable.google)).setContent(new Intent(Intent.ACTION_VIEW, uri)));
But it results in a force close error. Any tips on getting this working?
EDIT
I solved this. Basically what I do is add an onClick event handler to capture when the tab is clicked in the first place (only this tab in question) and then from within that I prevent the default action by returning true (for handled) after launching a new Intent in the regular fashion.
You can start an Activity from Tab Host(that you have mention as last Tab Host).Then from that activity you can launch external Browser.As i think its not possible to launch default activity from TabHost.
Edited
I have checked it.It give ActivityNotFound Exception.Conclusion is that TabHost look for the activity that is registered in Android manifest.If you want to achieve it then go with my first suggestion
I am making a keyboard ( InputMethodService ), which needs to launch a dialog.
As I found out, a service can not launch a dialog. So I made a separate activity which is called from the service by
Intent dialogIntent = new Intent(getBaseContext(), dialog.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
and show a dialog. The problem is that this activity replaces the previous one, where the user was typing something.
What do you think would be the best way to make it "transparent" ( i.e. not to push away the previous activity ) and also what would be the best way for this activity to talk back to the service, saying that dialog option was picked.
Thanks! :)
If this is an Activity (not a Dialog), you can add a dialog theme in the activity section of your AndroidManifest:
android:theme="#android:style/Theme.Dialog"
As for getting back what the user pressed, you should use startActivityForResult(...)
You should NOT launch an activity from an IME. This is a huge break in the IME flow -- the activity comes along and does an app switch from the current app, taking focus from it, and breaking your connection with its current editor.
Also there is no way to get a result back from it, because you can only use startActivityForResult() from an activity.
To show a Dialog in your IME, just use Dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG before showing the dialog.
To resume in code what have been said, let me share some code for those who need to test the solution:
// 1. CREATE THE DIALOG
val builder: AlertDialog.Builder = AlertDialog.Builder(this, R.style.Theme_AppCompat_Light)
builder.setTitle("Title").setMessage("This is the message for the user. ")
val mDialog = builder.create()
// 2. SET THE IME WINDOW TOKEN ATTRIBUTE WITH THE TOKEN OF THE KEYBOARD VIEW
mDialog.window?.attributes?.token = this.mKeyboardView.windowToken
// 3. SET THE TYPE OF THE DIALOG TO TYPE_APPLICATION_ATTACHED_DIALOG
mDialog.window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG)
// 4. SHOW THE DIALOG
mDialog.show()