Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Inside the XML snippet, below activity_main.xml, add two lines of XML so that

ID: 3736783 • Letter: 1

Question

1. Inside the XML snippet, below activity_main.xml, add two lines of XML so that the dimensions (width and height) of the TextView are just enough to hold the text inside the

TextView.

    <TextView

     />

2.Inside the onCreate method, fill in the code to set the layout and GUI defined in activity_main.xml.

public void onCreate( Bundle savedInstanceState )

{

            // Your code goes here

}

3. Inside an activity element of the AndroidManifest.xml file, write the code so that this activity is supported only in vertical orientation.

        <activity

            android:name = ".MainActivity"

            <!-- Your code goes here -->

        

       </activity>

4. Inside activity_main.xml, we define a Button element; when the user clicks on the button, we want the method go (from the MainActivity class, whose content view is defined in activity_main.xml) to execute. Write the code for it.

<Button

<!--your code goes here -->

5. Inside an Activity class, write a line of code to get a SharedPreferences reference.

6. Inside an Activity class, write a statement that creates and shows a Toast that lasts 5 seconds and says HI.

Explanation / Answer

1.

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

layout_width defines the width of text view

layout_height defines the height of text view

wrap_content means that the view wants to be just big enough to enclose its content.

2.

public void onCreate( Bundle savedInstanceState )

{

setContentView(R.layout.activity_main); // sets the activity content from a layout resource (here activity_main)

//The resource will be inflated, adding all top-level views to the activity.

}

3.

<activity

android:name = ".MainActivity"

<!-- The orientation of the activity's display on the device.portrait means that the activity is supported only in vertical orientation. -->

android:screenOrientation="portrait"

</activity>

4.

<Button

<!-- when the user clicks on the button, the method go (defined in MainActivity class, whose content view is defined in activity_main.xml is executed) -->

android:onClick="go"

/>

5.

//Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values.

// gets the reference of sharedPreferences file  
preferences = getSharedPreferences(<name of the sharedPreferences file(String)>, mode in which the file is opened(int));

6.

// makeText - Make a standard toast that just contains a text view.

// show is used to display the toast

Toast.makeText(getApplicationContext(), "HI", 5).show();