This is weird. I had recently gotten into Android programming. I made a fresh install but when I create a new project there is this new option called Fragment Layout Name at the very end of the page where you specify your Activity name. I haven't had this happen before but when I open my main activity this is what I get. Also half of it is filled with errors according to Android. Is there anyway I can go back and avoid this?
package com.example.quizactivity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Do as follows, this works for me..
Step-1:
Right Click on your Project -> Properties -> Android -> In Library panel, remove appcompat_v7 library, Apply and Ok
Step-2:
In Project goto res -> values -> style.xml
In line <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> change parent value from Theme.AppCompat.Light to android:Theme.Light
Step-3:
In Project goto res -> values-v11 -> style.xml
In line <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> change parent value from Theme.AppCompat.Light to android:Theme.Holo.Light
Step-4:
In Project goto res -> values-v14 -> style.xml
In line <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar"> change parent value from Theme.AppCompat.Light.DarkActionBar to android:Theme.Holo.Light.DarkActionBar
Step-5:
In Project goto menu -> main.xml remove these lines in menu tag:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.test.MainActivity"
and in item tag change this line from app:showAsAction="never" to android:showAsAction="never"
In project, goto res -> layout -> delete fragment.xml
Step-6:
In MainActivity extends Activity not ActionBarActivity and finally your MainActivity.java after remove unnecessary code, looks like this:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Enjoy:)
When creating a new project Select Minimum SDK version as upper or higher as u can . and then u will get rid of app compact .
I have chosen Miminum SDK 4.0 and get rid of it .
Related
I have been practicing in Android Studio as per the android app development free course in UDACITY. And wherever there is a R.menu or R. stuff it cannot be resolved
package com.example.android.courtcounter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.R;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
}
Plz help how to solve this Issue.
Clean and rebuild your project by going to Build>Clean Project.
Go to File>Invalidate Caches/Restart.
Make sure you built your project, and make sure there aren't any errors (red lines) in the res directory.
remove import android.R; clean and rebuild it.
if it still doesn't work try this
import com.example.android.courtcounter.R;
Check you AndroidManifest.xml, check the package name there.
use that as import .R; in code.
I've been going through the Android training tutorials and I can't seem to get a simple blank activity to apply the theme Theme.Holo.Light.
This is what I've done so far:
Set the minSdkVersion to 11 in gradle script and synced the project.
Applied the theme in the AndroidManifest.xml by setting:
android:theme="#android:style/Theme.Holo.Light"
Tried applying it at the application/activity level and by creating custom theme and setting the parent.
When running the app in the emulator it crashes with the error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test.themetest3/com.example.test.themetest3.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
According to the documentation the default theme for 11+ API levels is Theme.Holo but I can't get it to work and I'm obviously missing something.
My activity insists on using AppCompat themes only. Do I need to extend some other class within my activity?
This is the activity code (generated by creating a blank activity).
package com.example.test.themetest3;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thanks for your assistance :).
Your activity Extends AppCompatActivity, so you have to use AppCompatActivity themes. Use this in your styles.xml
parent="Theme.AppCompat.Light"
And if you also want to use Toolbar, you can get rid of ActionBar by using :
parent="Theme.AppCompat.Light.NoActionBar"
I am new to programming Android. When I start a new Android Application Project in Eclipse it automatically opens 2 files for me:
MainActivity.java and fragment_main.xml
package com.example.testprogram;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
and
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.testprogram.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
I wonder what all this stuff is. The Tutorials I watch always have a different code when they start a new project. For example the .xml file that Eclipse opens for them is named activity_main.xml (unlike mine which is named fragment_main.xml). Also their MainActivity.java has less methods integrated then mine.
Can Someone tell me wether that makes a huge difference and how i can maybe change that.
You are using latest one so it get like this. Don't worry about it.
public class MainActivity extends ActionBarActivity
instead of
public class MainActivity extends Activity
Remove all code in your MainActivity except OnCreate(). Then you follow your tutorial.
You onCreate should be
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Those are probably old tutorials (on Android platform even after couple of months an tutorial can become old), Android ADT is constantly changing. It now automatically adds an Activity and a Fragment to your project. Instead of just an Activity. You can still use knowledge from those tutorials and fit it into Fragments. Or delete Fragments and use only Activities. But Android is promoting the use of Fragments with Activities, not just mere Activities so you should learn how to use them.
Create New Project Like This:
Start `File->New->Android Application Project->Next->Next->Next->Select-> Empty Activity->Next->Finish
this code is about the default code of MainActivity.java
having problem to understand and also there is a another thing that is the fragmentaion_main.xml file which is likely another buden instead of activity_main.xml why it so ?
package com.example.onehello;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
What is appcompat_v7?
It is the support library added for backwards compatibility. Android has many versions and each new version brings many changes to the API. So, for the older versions of Android to be compatible with the code written for the newer versions, the appcompat_v7 library is helpful.
Why is it being added every time I create a new project?
I assume that you are using Eclipse IDE for the development. Ideally, it would suffice to create one appcompat_v7 folder and make all the projects link to that folder for support library. But, there seems to be some bug in Eclipse. It creates a new appcompat_v7 folder each time a new project is created.
'fragment_main.xml' is like another burden?
Fragment class was introduced in Android API 11. It is useful in creating different layouts for tablets and phones. It is more of a good support to take advantage of the bigger screens on tablets. If you want to develop your application for a smart phone only, you need not even worry about it. So, it's more of an advantage than a burden.
For more reading on fragments, go to http://developer.android.com/reference/android/app/Fragment.html
This appears after installation "Android Support Library":
https://developer.android.com/tools/support-library/features.html
I'm newbie in Android programming.
I've read many tutorial in the Internet about Sliding Menu on https://github.com/jfeinstein10/SlidingMenu.
1. Import library in my project.
2. Create an .xml in layout folder.
3. Modify MainActivity like this:
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends SlidingActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.copyactivity_main);
getSlidingMenu().setBehindOffset(100);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
It have no error. But when I debug it in emulator. It stopped and have the error: Source not found.
I don't know what it is.
Any help, thanks.