Android Studio - Change default application layout load - java

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

Related

Trying to Make Pop-Up Window/Overlay in Android Studio (Java)

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"

Recycler view onClick produces another Recycler view

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.

Android Activity action bar label not being set properly?

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);

Getting null on object reference for Up navigation

This seems to work but not sure if I have to add it to every Activity?
android:theme="#style/Theme.AppCompat"
along with extending the Classes with ActionBarActivity
Here is what I did in the manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.shmira.shmira.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=".Book"
android:theme="#style/Theme.AppCompat"
android:label="Shmira" >
</activity>
The class looking like this:
public class Book extends ActionBarActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
// lets user travel back to where they came from
case android.R.id.home:
finish();
return true;
If you're using a support lib i.e. ActionBarActivity add the following line after calling setContentView:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and if you're not using a support lib i.e. Activity, then simply change getSupportActionBar() to getActionBar().. and don't forget to add android:theme="#style/Theme.AppCompat" to AndroidManifest.xml
To go back to the MainActivity instead of up through the stack, define ActivityA as parent for ActivityB in AndroidManifest.xml like this:
<activity
android:name=".Book"
android:label="Shmira">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.shmira.shmira.MainActivity" />
</activity>
Try adding ParentActivity of your Activity in AcivityTag in AndroidManifest.xml
<activity
android:name=".Book"
android:label="#string/book"
android:parentActivityName="ParentActivity">
Try Adding below code in your oncreate
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In your code your using getActionBar() while base class is ActionBarActivity. You should use above code instead.

Android - Preventing WebView reload on Rotate

When I rotate my screen, the WebView reloads the whole page. I can't have this since some of my content contains dynamic/random material. Currently when rotated the screen reloads the original URL from the loadUrl() method.
Any idea what's wrong with my code?
MainActivity.java
package com.mark.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView web;
String webURL = "http://www.google.co.uk/";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null)
((WebView)findViewById(R.id.web)).restoreState(savedInstanceState);
web = (WebView) findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl(webURL);
web.setPadding(0, 0, 0, 0);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setSupportZoom(true);
web.getSettings().setBuiltInZoomControls(true);
web.setWebViewClient(new HelloWebViewClient());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class HelloWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView web, String url) {
web.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mark.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden">
<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>
I think the main problem is that you call web.loadUrl(webURL); also when savedInstanceState != null
EDIT
Try:
if (savedInstanceState == null)
{
web.loadUrl(webURL);
}
EDIT2: You also need the onSaveInstanceState and onRestoreInstanceState override.
#Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
web.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);
}
Note: Please also add in your AndroidManifest.xml in your Activity
android:configChanges="orientation|screenSize"
Thanks
EDIT: Found a more suitable solution.
NEW:
In AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Example.WebviewSample">
<application
<activity android:name=".WebViewActivity"
<!-- ADD THIS -->
android:configChanges="orientation|screenSize">
</activity>
</application>
</manifest>
In WebViewActivity.java file add this to onCreate() method
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
wView = (WebView) findViewById(R.id.webView);
if (savedInstanceState == null) {
wView.loadUrl("https://google.com");
}
}
also update onSaveInstanceState
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
wView.saveState(outState);
}
and onRestoreInstanceState
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
wView.restoreState(savedInstanceState);
}
OLD:
No java coding needed.
use this in your manifest file.
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
like:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.Example.WebviewSample.webviewsample"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
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>
in tag (manifest)
android:configChanges="orientation|screenSize"
Adding android:configChanges="orientation|screenSize" in manifest works for me
<activity
android:name="com.example.HelloWorld.WebActivity"
android:label="#string/title_activity_web"
android:configChanges="orientation|screenSize" >
</activity>
I don't believe this will work anymore. In my case, restoring state using WebView.restore(Bundle savedInstanceState) still triggers a reload of the url.
Looking at the docs for restoreState() you'll see it says:
If it is called after this WebView has had a chance to build state (load pages, create a back/forward list, etc.) there may be undesirable side-effects.
and
Please note that this method no longer restores the display data for this WebView.
Props to #e4c5 for pointing me in the right direction in his answer
And of course, the extreme course of action would be to prevent orientation changes from triggering activity destruction/creation. Documentation for how to do this is here
Add this code in Manifest
<activity
...
android:configChanges="orientation|screenSize">
Try this in your manifest file:
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
Place this in the Manifest.xml file activity:
android:configChanges="orientation|screenSize"
Here is an example:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="orientation|screenSize">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Override the onConfigChange method to avoid reloading data on orientation change
on your activity in AndroidMainfest file.
android:configChanges="orientation|keyboardHidden"
and have these as well in your WebView settings
webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webview.loadUrl("Your URL To Load");
As quoted here,
Caution: Beginning with Android 3.2 (API level 13), the "screen size"
also changes when the device switches between portrait and landscape
orientation. Thus, if you want to prevent runtime restarts due to
orientation change when developing for API level 13 or higher (as
declared by the minSdkVersion and targetSdkVersion attributes), you
must include the "screenSize" value in addition to the "orientation"
value. That is, you must decalare
android:configChanges="orientation|screenSize". However, if your
application targets API level 12 or lower, then your activity always
handles this configuration change itself (this configuration change
does not restart your activity, even when running on an Android 3.2 or
higher device).
setting android:configChanges="orientation|screenSize" on your activity will resolve this issue.
Also, take note of the following
Remember: When you declare your activity to handle a configuration
change, you are responsible for resetting any elements for which you
provide alternatives. If you declare your activity to handle the
orientation change and have images that should change between
landscape and portrait, you must re-assign each resource to each
element during onConfigurationChanged().
This solution works well for me:
(1) In AndroidManifest.xml add this line
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
Like this (and like the answers above)
<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=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
(2) Then in MainActivity.java verify savedInstanceState
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainContext = getApplicationContext();
----
myWebView = (WebView) findViewById(R.id.webView);
prepareWebView();
myWebView.addJavascriptInterface(myJavaScriptInterface, "WEB2Android");
if (savedInstanceState == null) {
myWebView.post(new Runnable() {
#Override
public void run() {
myWebView.loadUrl("http://www.appbiz.ro/foto_konta");
}
});
}
----
}
(3) and then:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
myWebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
myWebView.restoreState(savedInstanceState);
}
Add this in Androidmanifest.xml:
<activity android:name=".MyActivity"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
Now, when one of these configurations change, MyActivity does not restart. Instead, MyActivity receives a call to onConfigurationChanged().
Add this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}

Categories