android: create a new activity with xml page - java

I want to create a new activity. My default has two files MainActivity.xml with MainActivity.java. But, If i created a new activity only .java files are created in /src/ path no .xml files are created in res/layout folder.
Help me to create an activity with java and xml as well ?

First create a manually a newactivity.xml in the res/layout folder then after that you can create a newActivity.java like this :
public class NewActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newactivity);
}
}

On Mac:
Right click on your package
New -> Other...
( or Command + N )
Choose: Android -> Android Activity, click Next
Select a template (i.e Blank Activity), click Next
Enter Activity Name (i.e NewActivity), click Finish or Next
By this way, you can create java class, xml file, add information to Manifest file, create new string in strings.xml, create new menu, etc...
I hope this will help :)

Related

How do I change the main xml file from another activity?

I am very new to Java. I am doing a school project at the moment and I have my main activity, then I have a settings activity. I am trying to modify the xml from the main activity with the settings activity. I am able to modify the settings xml file with the settings.java, but I would like to modify the main activity xml with settings.java
public class Settings extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Get the Intent that started this activity and extract the string
Switch switchButton;
final RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.activity_settings);
final RelativeLayout mRelativeLayoutMain = (RelativeLayout) findViewById(R.id.activity_main);
switchButton = (Switch) findViewById(R.id.switch1);
switchButton.setChecked(true);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if (bChecked) {
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
mRelativeLayout.setBackgroundColor(Color.GRAY);
} else {
mRelativeLayoutMain.setBackgroundColor(Color.WHITE);
mRelativeLayout.setBackgroundColor(Color.WHITE);
}
}
});
if (switchButton.isChecked()) {
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
mRelativeLayout.setBackgroundColor(Color.GRAY);
} else {
mRelativeLayoutMain.setBackgroundColor(Color.WHITE);
mRelativeLayout.setBackgroundColor(Color.WHITE);
}}
public void toast1(View view) {
android.widget.Toast.makeText(this, "Created by Cody Walls and Tommy Serfas", android.widget.Toast.LENGTH_LONG).show();
}
/*public void switch1(View view) {
ScrollView mScrollView = (ScrollView) findViewById(R.id.scrollView);
mScrollView.setBackgroundColor(Color.GRAY);
}*/
}
In the Code I am trying to change the background of the main activity xml with :
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
and when I run the app and click the intent it will crash with the error:
"java.lang.NullPointerException: Attempt to invoke virtual method
'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null
object reference"
I think the easiest way is to create an PreferenceManager.SharedPreferences, in which I recommend you to store current app data. This will help you not to loose any changes in app after you exit the it. Here is short instructions:
Create button in settings activity which will change something in main activity.
Create onClickListener for your button.
Use .SharedPreferences to store was you button clicked or not. (I recommend storing boolean variables, this way you can store was button clicked or not.)
I both of your activities in onCreate method call .getSharedPreferences to read saved app values. (I mean to read was the button clicked or not.)
Use app values you got from 4. to change any element in activity. (For example if you stored that button was clicked, then change some TextView text or etc.)
I hope you understood the idea.
Link to the Android developer tutorial about App key values storing & saving
Link to the StackOverflow much easier explanation & examples
There are a couple of ways of doing this (Some of which depends on how you are switching back and forth from each activity). It also depends on what things you are changing.
From your settings page, as you are changing different settings, you'll save this content within Preferences. (You can see more how to use Preferences here: https://examples.javacodegeeks.com/android/core/ui/settings/android-settings-example/ or by just Googling it).
On you main activity, depending on how you come back to it (onStart most likely), you can setup the things you need to programmatically.
So, you may need to do a little research on the Android lifecycle and how each cycle works (https://developer.android.com/guide/components/activities/activity-lifecycle.html), how to program the UI programmatically through Java (http://startandroid.ru/en/lessons/220-lesson-16-creating-layout-programmatically-layoutparams.html), and the Preferences Android library to save certain settings.
The xml isn't meant to be "altered". You can change the UI programmatically. It's possible to build an Android app without any xml. When Android was first built, it didn't use the xml to create the UI. It was all done through Java. It was then added to use xml to create your activities or fragments or any UI component. This made things easier for more static activities or activities with very little dynamic content.

Android Studio Start Activity from Modul

I have created an *.aar File from a Android Studio project. This file I have successfully imported to an another Android Studio project file. Now I want start the activity from the included libraries. But i am hanging and I try hour to hour...
In wich file I have to include the statement like this?
public void button_to_start_the_activity(View view)
{
Intent intent = new Intent(StartActivity.this, MainActivity.class);
startActivity(intent);
}
Should I implement this in the modul java file or in the application java file?
(Please check this out, I have not found a activity java file in my module :-/ glglgl)
Steps bevor, i have created a new activity in my main-project and i was able to start the activity without trouble.
Okay... Next question:
In the Main Manifest,...what to hell I shout declare in android:name"....?...." like this:
<activity
android:name="....??????...."
android:label="DLC"
android:screenOrientation="portrait"
android:theme="#style/Theme.AppCompat.NoActionBar">
</activity>
Maybe, or for sure, simple question... But i try to learn a little bit AS since a week... For your understanding, i will try to explain my project with simple words....:
Try to merge two app projects in one app!
I have created a app, then i have created an another app. Now i want to merge. Simple startpage (allready exist) with the function to call the both activitys... :-/
My final solution to start a modul activity in the main app (For poeple with less experience like me)
Create in Android Studio a modul from the existing app
Open Main-Project an insert the new modul (File-->New-->Import Modul)
Right click on project folder. Search App (left side), then dependencies (right side) --> Add new from modul --> Add your modul
Check your build.cradle (Main-App) for compile project(':yourmodul')
Add this in your activity (from the Main-App)
public class StartActivity extends AppCompatActivity {
public void gotocalc(View view)
{
Intent intent = new Intent(StartActivity.this, YourModulClass.class);
startActivity(intent);
}
gotocalc is for start with a button android:onClick="gotocalc" set in your layout.xml in the Main App.
Clean, Rebuild

How to put a MapsActivity inside another activity

I created a "Google Maps Activity" (G) and "Navigation Drawer Activity" (N) from the wizard of Android Studio, when I use like Main Activity (Launch) the G activity, this show a mark (from the java source sample created on onMapReady method). But when a use the N activity like the main activity, and I copy the content from the "activity_maps.xml" inside "content_main.xml" to put the map inside the G activity, this don't show the mark. I guess the Java source for MapActivity of the G activity doesn't execute.
This is the directory tree:
This is the XML files:
And the Java code (I don't change nothing):
I tried to change in MapsAcivity "setContentView(R.layout.activity_maps);" by "setContentView(R.layout.activity_main);" thinking the activity_maps is copied inside content_main that is inside activity_main. Also, change in content_main "tools:context="com.example.ws02.myapplication.MapsActivity"" by "tools:context="com.example.ws02.myapplication.MainActivity"" because this work in the main_acivity.

Error caused when converting android activity based app to fragment based

I am new to Android Development and I have a simple list app which I have been asked to create.I have had no problems having the app as activity based however I have to extend the functionality and use fragments for a 'universal' app. My main activity is:
I was able to successfully compile your code by taking the following steps:
It looks like this line is the problem (inside Main.java):
contactCursor = contactDBAdapter.getAllContactsCursor();
I looked at how your contactDBAdapter gets initialized and it turns out you initialize it after you setContentView for your activity. However, your view involves calls to contactDBAdapter. So in Main.java you need to move the following two lines to the TOP of the onCreate window:
public void onCreate(Bundle savedInstanceState)
{
contactDBAdapter = new ContactDBAdapter(this);
contactDBAdapter.open();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
....
}
Furthermore, the following line in Main.java needs to be removed (or commented out):
contact.clear();
Also, I had to make two further changes to how you call ListView
In list_view.xml, the way you identify a ListView for Android is :
android:id="#+id/android:list"
In ContactListFragment.java, then call the ListView this way :
parent.myListView = (ListView)v.findViewById(android.R.id.list);
have you not just tried using the Eclipse Template which set up everything for you just copy in your existing code?
File>New>Android Application Project then under the Create Activity Step select
Your Fragment class needs an empty default constructor. See Android Reference

Main layout not recognized, immediately after starting new Android project

The below code was automatically generated when I started a new android project.
I'm getting an error on the "R.layout.main" saying it does not exist.
I do in fact have a main.xml and I can see the layout change as I edit it in the Graphical Layout tab.
How can I fix this so I can run my application?
public class ComplimentGeneratorActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
The package name of the R file and the package name of your source code may not match. Make sure the package your code is in is the same as the package defined in the manifest file. Otherwise you will need to import the R file with the full package name (ex: com.example.R.layout.main).
If they match, for some reason your R file was not generated properly. Try cleaning your project.
Also, start accepting some answers. I almost didn't answer this because of your horrible acceptance rate.
This may be a very simple answer, but it has happened to me before. Try to restart eclipse. File -> Restart
Try to import the R.java file with the full path inside the main activity class file...
import com.example.packagename.R;
Hope, this will solve your query.

Categories