Mohammed
Mohammed

Reputation: 51

Android - Incorrect Main Activity Layout

I am currently using online resources. I've done some research but hardly found anything similar.

The problem is that during the first example, I ran the application right after creating it in Eclipse (no changes on my end). The only changes made were to the target SDK which had to be set to the latest Android version.

So when I run the application, the layout looks a bit raw, the text is not centered and the icon is not shown in the header label. I am also unable to see the application in the Main_Activity Graphical Layout.

The created app is called Silent Mode Toggle, I will update whatever necessary code when needed. I could not include the application output...

The label should have been preceded by the application logo or an image, and the "Hello World!" text should have appeared on the center of the screen.

P.S. I cannot include the screenshot due to my new profile here on SO.

Upvotes: 0

Views: 7495

Answers (2)

Angoranator777
Angoranator777

Reputation: 374

I had the same problem which I solved by changing this import statement as follows

import android.support.v7.app.AlertDialog;

to

import android.app.AlertDialog;

OR VISA VERSA!!!

You can alternatively delete your import statements and add them again by using "alt+Enter" You may find that there are in some cases more than one option in terms of the class to import. Your code should thus be in line with the class you choose!!!

I'm not a guru, but this seems logical to me, and I could not find any other reasonable explanation online...

Upvotes: 2

Mohammed
Mohammed

Reputation: 51

After doing some trial and error as well as searching around, I found a work around for this issue. My main problem is that I cannot use a theme for my app that is not a child of AppCompat, which means that the only theme that displays the icon in the action bar is the Theme.Holo.Light.DarkActionBar; the one in AppCompat does not have the same layout it seems and I was not allowed to use another. So below is what I've done to work around this:

  1. Create a theme.xml file in res ==> values, values-v11 and values-v14 as below:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="MyAppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <!-- Any customizations for your app running on pre-3.0 devices here -->
        </style>
    </resources>
    
  2. Change the theme set in the styles.xml file in res ==> values, values-v11 and values-v14 as shown below:

Previous:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">

New:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  1. In your MainActiviy.java file add the code below:

        setTheme(R.style.MyAppTheme);
    

The above code should be positioned before the super.onCreate(savedInstanceState); the entire code is shown below:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.MyAppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

This basically means that the activity is started without an action bar but then later on the theme changes to the Holo.Light.DarkActionBar. Not doing step (2) would result in having two action bars in the emulator.

Note: Solution above is what has worked for me based on my limited knowledge of Android development.

Upvotes: 3

Related Questions