Reputation: 65
So, I'm trying to wrap my head around Android Studio, but I find something very confusing. When you make a new activity it also makes a Java class, yet coding anything into that java class doesn't seem to do anything. Yet coding something into the MainActivity does.
Example names
MainActivity SecondActivity main_activity second_activity
Adding an onClick event for a (Button for example) in second_activity, it checks the MainActivity class for the pubic void onClickName(){ } rather than the SecondActivity class. Is there something somewhere that I have to change?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:text=""
android:hint="Player name here"
android:layout_alignParentTop="true"
android:id="@+id/txtName"
android:layout_toStartOf="@+id/btnAddPlayer" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back to Main menu"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:onClick="backToMain"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spnCharacters"
android:layout_below="@+id/txtName"
android:layout_marginTop="56dp"
android:layout_alignParentEnd="true"
android:background="@drawable/bg_key"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Player"
android:id="@+id/btnAddPlayer"
android:layout_alignBottom="@+id/txtName"
android:layout_alignParentEnd="true"
android:onClick="addPlayer"/>
Yet neither addPlayer nor backToMain in SecondActivity does nothing, yet if in MainActivity they do whatever I've coded in
SecondActivity onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
}
Upvotes: 3
Views: 13882
Reputation: 1
In order to solve this issue simply go to your activity_second and use any of the palette tools before referencing your activity_second in your java file(SecondActivity).
Upvotes: 0
Reputation: 5214
An Activity
"is a single, focused thing that the user can do.". The internal of make an Activity
when developing is actually the bundle of several actions:
AppCompatActivity
).Activity
inside AndroidManifest.xml
, with a new tag like <activity android:name=".NecessaryActivityName" android:label="@string/optional" android:icon="@drawable/optional"/>
, or you may specify more details of this Activity
, for example the <intent-filter>
.So,
When you make a new activity it also makes a Java class.
Yes. A better description is, "When you make a new Activity
, you are making a Java class that extends Activity
".
yet coding anything into that java class doesn't seem to do anything.
"An activity is a single, focused thing that the user can do.". When you click on an application icon on Launcher ("desktop" of Android), you are starting a corresponding Activity (to be more precisely, sending an Intent
to that Activity).
When you create another Activity, you are willing to have an Intent
to start it, by adding a Launcher icon or by redirecting from another Activity (for example, MainActivity
). The way of adding it to Launcher is posted by @J0B, by adding an <intent-filter>
as the child of <activity>
inside AndroidManifest.xml
. The way of redirecting from another Activity is to build an Intent
then call startActivity()
, for example in Java:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
Upvotes: 2
Reputation: 1648
Each Android Application contains Activities. Each activity is declared in AndroidManifest.xml e.g. main_activity.xml and MainActivity.java is an 'Activity'. java classes, interfaces... can be used as normal in the java controller part of an activity.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Should be part of the activity that launches first.
Upvotes: 0
Reputation: 93
\ make sure that the Java file is connected to the XML file
the first line of the Java file should show something like this
public class balance extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balance);
}
this means that my class(Java file) balance is connected to activity (layout) activity_balance.
if this is not the case, make sure that you are adding new activities from (file >new>activity> (blank activity for example)
Hope this helps
Upvotes: 1