0% found this document useful (0 votes)
26 views4 pages

Android Activity for Orientation Change

The document contains code for an Android application with a main activity that handles orientation changes by saving and restoring text input. It includes XML layouts for both portrait and landscape modes, featuring a TextView, Button, and EditText. The AndroidManifest.xml defines the application and its main activity, allowing it to be launched from the home screen.

Uploaded by

ieshaaved
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Android Activity for Orientation Change

The document contains code for an Android application with a main activity that handles orientation changes by saving and restoring text input. It includes XML layouts for both portrait and landscape modes, featuring a TextView, Button, and EditText. The AndroidManifest.xml defines the application and its main activity, allowing it to be launched from the home screen.

Uploaded by

ieshaaved
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Main_activity.

java:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private EditText editText;


private String savedText;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

editText = findViewById([Link]);
Button button = findViewById([Link]);

// Restore saved text if available


if (savedInstanceState != null) {
savedText = [Link]("text");
[Link](savedText);
}

[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]([Link], "Button Clicked!", Toast.LENGTH_SHORT).show();
}
});
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
[Link](outState);
// Save the current text input
[Link]("text", [Link]().toString());
}
}

Main_activity.Xml:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="[Link]

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Portrait Mode"

android:textSize="24sp"

android:layout_gravity="center"/>

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Click Me"

android:layout_gravity="center"

android:layout_marginTop="16dp"/>

<EditText

android:id="@+id/editText"

android:layout_width="match_parent"
android:layout_height="wrap_content"

android:hint="Enter some text"/>

</LinearLayout>

Second [Link]:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="[Link]

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Landscape Mode"

android:textSize="24sp"

android:layout_gravity="center"/>

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Click Me"

android:layout_gravity="center"

android:layout_marginStart="16dp"/>

<EditText
android:id="@+id/editText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter some text"/>

</LinearLayout>

[Link]:
<manifest xmlns:android="[Link]
package="[Link]">

<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/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>

You might also like