i have two activity associated with two buttons
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Subactivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:taskAffinity="com.example.start_cs.sub">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".sub"
>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" android:taskAffinity="com.example.start_cs.main"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".main"
>
</activity>
</application>
MainActivity code
package com.example.start_cs.myapp;
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.main_text_view);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, main.class);
startActivity(intent);
}
});
}
layout code
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/main"
android:background="#drawable/main"
android:layout_centerHorizontal="true"
android:id="#+id/main_text_view"
android:layout_marginTop="17dp"
/>
in this situation
Subactivity button opened sub
but MainActivity button did not open main
but if i put MainActivity above Subactivity
Subactivity button did not open sub
but MainActivity button opened main
ok, based on your responses to my comments, I guess all I can say is that you need to follow a tutorial on how to create an app for android. There are excellent tutorials available all over the internet.
You have two activities, so you should have two layout files. You only posted one.
You should also have two source files, one for each activity. You only posted one.
Every android element in a layout that you would like to reference in your code needs an ID. your code references R.id.main_text_view, but your layout file has no such id in it. I'm surprised your code even compiles.
However, to answer your specific question, what you need is the following:
1) The <name> tags in your manifest file must match the name of your java class source files for each of your activities. So your activity class files appear to be called "MainActivity" and "Subactivity" according to your manifest file. However, see my comment on your onClickListener code below.
Also, your manifest indicates that both of your activies are "LAUNCHER" activities. You only need that tag for activities which you want to be able to launch from the Android application launcher (i.e. the list of all the apps installed on your phone). Seems like you would only want this on your main activity, but you could specify more than one if you like.
2) Your activities are duals of each other (i.e. they sound like they do exactly the same thing - each has a button that launches the other) so the code will be very similar. Your code for MainActivity should look something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.main_text_view); //<-- This tries to find a button in this activity (using the activity's layout file that was used in the call to setContentView() in onCreate(). However, the id you specify doesn't exist in your layout file. This should either not compile or return null.
//This is fine.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, main.class); //<--"main.class" doesn't match either of the activity names declared in your manifest. It should match one of the names declared in the <name> tag of one of your <activity> tags.
startActivity(intent);
}
});
}
Your layout file needs to contain an id for the button that wish to find using findViewById(). Modify your layout file as follows (and create one for each activity - although you could, technically, reference the same layout for each activity. But for now, it is conceptually easier to have separate files).
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/main"
android:background="#drawable/main"
android:layout_centerHorizontal="true"
android:id="#+id/main_text_view" <!-- Here is the line that identifies the button for your app. The format is "#+id/some_name", and is reference as "R.id.some_name" in your code. -->
android:layout_marginTop="17dp"
/>
Now you must do the same in your sub-activity code, but your onClickListener will call the Main Activity instead of your subactivity. So the onClickListener code looks like this for your MainActivity (which launches your subactivity):
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subactivity.class);
startActivity(intent);
}
});
and like this in your subactivity (which launches your Main Activity)
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
}
});
Related
I am working on a simple project which is responsible to launch and quit another installed application (let's call it "abc") on the tablet.
The other application ("abc") is already installed in tablet. I can launch it using Intent. But need to find a way to quit it as well.
Here is the template of my mainActivity.java file.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout parent = findViewById(R.id.parent);
Button launchApp = (Button) findViewById(R.id.button);
Button quitApp = (Button) findViewById(R.id.button2);
launchApp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.abc");
if (launchIntent != null) {
startActivity(launchIntent);
} else {
Toast.makeText(MainActivity.this, "There is no package available in android", Toast.LENGTH_LONG).show();
}
}
});
quitApp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Add code to close the installed app on tablet ("com.abc")
}
});
}
}
This is also my activitymanifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.launcherQuitterApp">
<!--uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /!-->
<!--uses-permission android:name="android.permission.com.abc"
tools:ignore="ProtectedPermissions" /!-->
<uses-permission android:name="android.permission..com.abc"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.LauncherQuitterApp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<queries>
<package android:name="com.abc"/>
</queries>
</manifest>
I have tried the suggestions in the following threads, but none of them worked for me:
How to get PID from package name?
Followed the suggestion of this thread, but when I tried to list all running applications, it only reported the running application (the one through which i want to close the other application)
Android Permission Denial: forceStopPackage()
I am trying to create a pop-up window/overlay in Android Studio with 2 activities. When a certain button (the green plus button in the first picture below) is pressed, it will start a second activity at a smaller size with a different layout (.xml) file. When the second activity is declared in the AndroidManifest.xml, it uses a custom theme I have created to make the first activity appear under it, however, it is not working properly.
Here are pictures of both activites:
First Activity -
Second Activity -
Here is the code:
MainActivity -
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Util.initScreenRes(this); // initializes screen resolution for later pop-up window sizing
}
public void ibAddOnClick(View view){
startActivity(new Intent(MainActivity.this, AddPopUpActivity.class));
}
}
AddPopUpActivity -
public class AddPopUpActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_popup_activity);
getWindow().setLayout((int) (Util.screenRes.x * 0.8), (int) (Util.screenRes.y * 0.5));
}
}
Util -
public class Util {
public static Point screenRes;
public static void initScreenRes(Activity a){
final DisplayMetrics dm = new DisplayMetrics();
a.getWindowManager().getDefaultDisplay().getMetrics(dm);
screenRes = new Point(dm.widthPixels, dm.heightPixels);
}
}
Lastly, here is the AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="App.ProgressTracker">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.ProgressTracker">
<activity
android:name="App.FrontEnd.MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.ProgressTracker.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="App.FrontEnd.AddPopUpActivity"
android:label="ProgressTracker"
android:theme="#style/Theme.ProgressTracker.PopUp">
</activity>
</application>
</manifest>
For reference, I have used this tutorial on YouTube for the way I went about this:
https://www.youtube.com/watch?v=fn5OlqQuOCk&ab_channel=FilipVujovic
I'd appreciate any help on this. Thank you in advance!
In the AndroidManifest.xml set the theme of the pop up activity to
android:theme="#android:style/Theme.Dialog"
I have a problem with Activities. My MainActivity is a first activity. After succesful loggin in, i want to come back to MainActivity. When login is not succed, user stayed on LoginActivity. MainActivity looks like this:
public class MainActivity extends AppCompatActivity {
Intent activityIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityIntent = new Intent(this, LoginActivity.class);
startActivity(activityIntent);
setContentView(R.layout.main_activity);
}
And AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.keystroke_dynamics">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activities.LoginActivity" android:theme="#style/AppTheme.Dark" />
<activity android:name=".Activities.SignupActivity" android:theme="#style/AppTheme.Dark" />
</application>
I close LoginActivity by method finish();, when login credentials are correct. When user pass wrong data into EditTextes, User stay in Login Page.
My question is, how to close app to tab on Back button press, when user is not login in? For now, when User is not logged, on back button press i come back to MainActivity. I want to close app to work in background, if noone is loged in.
You can use startActivityForResult() mechanism to start LoginActivity with a requestCode.
startActivityForResult(new Intent(this, LoginActivity.class), <Request-Code>);
Then in LoginActivity you need to call setResult() if the user logs in successfully before finishing your activity.
setResult(Activity.RESULT_OK);
finish();
To consume this result, you need to override onActivityResult() in your MainActivity -
public void onActivityResult(int requestCode, int resultCode, Intent data){
// check if the request code is same with which you requested LoginActivity
if(requestCode == <Request-Code>){
if(resultCode != Activity.RESULT_OK) {
// now finish your activity
finish();
}
}
}
try something like this..
if{
//CODE FOR SUCCESSFULLY LOGIN
}else{
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
I have an app I'm designing where I have an activity with several buttons in it. This activity uses "setTitle()" to set the action bar title to the name of the user, where the default was set to a blank string in the AndroidManifest via android:label. One of the buttons leads to another activity that has a tab layout with fragments with different pieces of data. I use android:label in the manifest to set the title of this tab layout activity to a raw string. However, when entering this activity, it shows the user's name which was in the title of the activity that launched it. Why is this? Is there something wrong with how I'm defining the label of the activities? Here is my AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.uml.android.adventurersarchive">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Preferences"
android:parentActivityName=".MainActivity"
android:theme="#android:style/Theme.Holo" />
<activity android:name=".CreateCharacterActivity"
android:parentActivityName=".MainActivity"
android:label="Create a Character" />
<activity android:name=".CharacterMainActivity"
android:parentActivityName=".MainActivity"
android:label="" />
<activity android:name=".CharacterSheetActivity"
android:parentActivityName=".CharacterMainActivity"
android:label="" />
<activity android:name=".CharacterEquipmentActivity"
android:parentActivityName=".CharacterMainActivity"
android:label="Equipment" />
<activity android:name=".CharacterSpellbookActivity"
android:parentActivityName=".CharacterMainActivity"
android:label="Spellbook" />
</application>
</manifest>
The activity in question which is displaying the wrong title is "CharacterSpellbookActivity". The one that sets the title to the user name is "CharacterMainActivity".
Here is the onCreate() method of CharacterMainActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_main);
Intent intent = getIntent();
myCharacter = (CharacterInfo) intent.getParcelableExtra("character");
setTitle(myCharacter.getCharacterName());
}
Here is where I launch the CharacterSpellbookActivity:
public void openSpellbook(View v) {
Intent intent = new Intent(this, CharacterSpellbookActivity.class);
intent.putExtra("character", myCharacter);
startActivity(intent);
}
And here is the onCreate() method of the CharacterSpellbookActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_spellbook);
Intent intent = getIntent();
myCharacter = (CharacterInfo) intent.getParcelableExtra("character");
setTitle(myCharacter.getCharacterName());
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
SpellbookTabAdapter adapter = new SpellbookTabAdapter(myCharacter, getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
Did I do something wrong here?
However, when entering this activity, it shows the user's name which was in the title of the activity that launched it. Why is this?
Because that's what you told it to do
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_spellbook);
Intent intent = getIntent();
myCharacter = (CharacterInfo) intent.getParcelableExtra("character");
setTitle(myCharacter.getCharacterName());
Empty labels automatically picks up their value if they are null from their parent Activity.
Lets solve this :
remove the label attribute from manifest
If you want to change only the title why don't you ease down on your code and try this:
1
getActionBar().setTitle(R.string.your_title);
After it, you can call:
2
getActionBar().setDisplayShowTitleEnabled(true);
Bear with me, this is my first Android project.
I've been making a simple Java class, Event, and made the CRUD functions for it. Now I'm doing the layout and can't figure out how I launch these activities after a button is clicked. This is the code I'm working with.
activity_main.xml;
<Button
android:id="#+id/newEventButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="New Event" />
MainActivity.java;
public void addEvent(MenuItem item) {
Intent i = new Intent(this, AddEventActivity.class);
startActivity(i);
}
AndroidManifest.xml;
<activity
android:name="com.example.eventmanager.AddEventActivity"
android:label="#string/title_activity_add_event"
android:parentActivityName="com.example.eventmanager.MainActivity"
android:noHistory="true">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.eventmanager.MainActivity" />
</activity>
What do I need to include into these segments that will link the newEventButton to launch AddEventActivity.class?
In your main activity in onCreate after setContentView add the following code
// get newEventButton
Button addEvent = (Button) findViewById(R.id.newEventButton);
// Listen for button click and start AddEventActivity
addEvent.setOnClickListener(new onClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(this, AddEventActivity.class);
}
});
You need to add an OnClickListener to the Button in your Activity's Java code. Normally you do this in onCreate().
public class MainActivity extends Activity implements OnClickListener {
public void onCreate(Bundle saved) {
super.onCreate(saved);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.newEventButton);
button.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.newEventButton:
Intent i = new Intent(this, AddEventActivity.class);
startActivity(i);
break;
}
}
}
<activity
android:name="com.example.eventmanager.MainActivity"
android:label="#string/title_activity_add_event" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.eventmanager.AddEventActivity"
android:label="#string/title_activity_add_event"
<intent-filter>
<action android:name="android.intent.action.ADDEVENTACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Here u can work like this also
EDIT
Dont you set your onclick listener if no copy this in oncreate
Button b = (Button) findViewById(R.id.newEventButton);
b.setOnClickListener(new onClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent("android.intent.action.ADDEVENTACTIVITY");
startActivity(i);
}
});
}