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);
Related
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 am trying to start a new Recycler view upon clicking a container view from another recycler view. Essentially I have list of NBA teams presented as a recycler view in "NBA_Adapter"; when each team is clicked it will bring the user to another recycler view showing the players in the team. I am trying to do this using two classes that extend recycler view. The issue is that I am unable to start the second class, which I suspect is because the second class does not extend "AppCompatActivity" and instead extends RecyclerView, which the manifest file is asking me to do since it does not recognize my second class.
Manifest below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nba">
<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=".Second_Adapter">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Main Activity below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//instantiates the parameters
recyclerView = findViewById(R.id.recycler_view);
adapter = new NBA_Adapter(getApplicationContext());
layoutManager = new LinearLayoutManager(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);
}
}
Part of 1st Class below:
#Override
public void onClick(View v) {
TEAMS current = (TEAMS) containerView.getTag();
Intent intent = new Intent(v.getContext(), Second_Adapter.class);
intent.putExtra("id", current.getId());
v.getContext().startActivity(intent);
}
My second class is essentially the same as my first class. The only reason I do not use the same recycler view is that I am having a hard time implementing it as I am also using a search view for each recycler view. So at the moment I am separating them. Any hints or links are appreciated showing how I can do this is appreciated.
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);
}
});
sometimes the application does run but its very slow and messes up. I attached the main activity and the manifest, I think it is one of those that is making the application mess up. Sometimes it gives me this error : Session 'MainActivity': error.
Any help would be greatly appreciated.
Thank you.
Main Activity
import Graphics.MyGLSurfaceView;
import gameInfo.GameDatabase;
public class MainActivity extends ActionBarActivity {
//Runs before the application is created
private Button mCampaignButton;
private Context context = this;
private GLSurfaceView mGLView;
//When the application is created
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//New thread to perform database creation / filling
Runnable runnable = new Runnable(){
public void run(){
//Instantiate a GameDatabase object (this activity)
final GameDatabase gDB = new GameDatabase(context);
gDB.fillGameDatabase();
}
};
Thread myThread = new Thread(runnable);
myThread.start();
mGLView = new MyGLSurfaceView(this);
setContentView(mGLView);
//Keeps screen on so it doesn't fall asleep
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Finding button by button id after application is created
mCampaignButton = (Button)findViewById(R.id.campaignButtonID);
//Checks if the campaign button is clicked
mCampaignButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Intent to go from main activity to campaign Level Select Activity
final Intent intent = new Intent(MainActivity.this, CampaignSelectLevel.class);
startActivity(intent);
}
});
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tekvision.codedecrypter">
<!-- Tell the system this app requires OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CampaignSelectLevel"
android:label="#string/title_activity_campaign_select_level" >
</activity>
<!-- Anagrams.Modern Era is to get java class from different package-->
<activity android:name="anagrams.Modern_Era"
android:label="Modern_Era">
</activity>
</application>
Make sure that you are declaring all the permissions in manifest file required by your application i.e.
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
for more details check this: uses-permission
I have a android studio project,
and I have got few layouts which are connected to their .java files
note "I couldn't post images as of my low reputation"
I have got:
layout1.xml
layout2.xml
layout3.xml
layout4.xml
layout5.xml
and
Layout1.java
Layout2.java
Layout3.java
Layout4.java
Layout5.java
Android Manifest is:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#drawable/blablabla"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:screenOrientation="portrait"
android:name="com.icetea09.demomaterialdesigndrawermenu.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
layout1.xml code is this: "activity_main.xml is my layout1"
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mLvDrawerMenu = (ListView) findViewById(R.id.lv_drawer_menu);
List<DrawerMenuItem> menuItems = generateDrawerMenuItems();
mDrawerMenuAdapter = new DrawerMenuItemAdapter(getApplicationContext(), menuItems);
mLvDrawerMenu.setAdapter(mDrawerMenuAdapter);
mLvDrawerMenu.setOnItemClickListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if(savedInstanceState == null){
setFragment(0, BikeFragment.class);
}
}
The default layout load view is "layout1.xml with Layout1.java"
The question is how do I change the start view layout from layout1 to layout2.
Like when I install the app first, It will show the "layout2.xml" view instead of default one which is layout1.xml in my case.
Thank you for your time.
One way to do it, is when you add a new activity. You can click on "Launcher Activity". Then Android Studio will make this new activity the Launcher Activity.
When you already have added both activities there is another way.
The best way is to use the
AndroidManifest.xml
file. To make an activity seen as a launcher activity you add the following attributes to your activity in the manifest:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This question may be a duplicate: change application's starting activity - Android