Manifest merging in Android Studio - android

Is there any way to use ADT's manifest merging feature (manifestmerger.enabled=true in project.properties) in Android Studio?

1. create sample project
2. add new module
3. Module Setting
4. Remove files in App Module
Move app/~~~/values/style.xml to common/~~~/values/style.xml
edit 1.
<!-- app/~~~/AndroidManifest.xml -->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.susemi99.manifestmergerforandroidstudio" >
<application >
<activity
android:name="kr.susemi99.common.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
edit 2.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"> <!-- add this line -->
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
edit 3.
package kr.susemi99.common;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_settings)
{
Toast.makeText(getApplicationContext(), "test toast", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
}
See all http://susemi99.kr/2368

Related

How we start an activity in MainActivity which extends AppCompatActivity [duplicate]

This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 4 years ago.
I used startActivity() method but it is not working.I think this method is not working because my MainActivity extends app compat actvity.
So please help me to start activity in an app compat activity.
I try various times but when I start my apk and click on button then app crashes.
Here my code
main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E96050"/>
<TableRow><Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainButton"
android:text="Open Second Activity"
android:onClick="yas"/></TableRow> </TableLayout>
MainActivity.java
package com.mycompany.myapp;
import android.app.Activity;
import android.os.*;
import android.support.v7.app.*;
import
android.support.v7.widget.Toolbar;
import android.view.*;
import android.widget.Toast;
import android.content.Intent;
import android.support.v7.app.*;
public class MainActivity extends
AppCompatActivity {
#Override
protected void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO: Implement this method
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}
private void CreateMenu(Menu menu) {
MenuItem mnu1 = menu.add(0,0,0,"item1");
{
mnu1.setIcon(R.drawable.ic_launcher);
mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
}
public void yas(View vv) {
Intent iiii = new
Intent(MainActivity.this,SecondAct.class);
startActivity(iiii);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="secondActivity"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="SecondActivity" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
use this
Intent intent=new Intent(this,Target_Activity.class);
startActivity(intent);
And open your manifest file add this line
<activity android:name=".Target_Activity"
android:parentActivityName=".MainActivity">
</activity>
I hope you try this code..
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
Please add second activity into android manifest file.
<activity android:name=".SecondActivity"/> // define second activity name.
add this code in android manifest file..
<activity
android:name=".SecondActivity"
android:label="secondActivity" >
</activity>

Activity Working on emulator but doesn't load on tablet

I am developing an android application, the problem is that all activities work fine on emulator but one of the activity doesn't load on tablet (Running 4.4 kitkat) and stopes the Application, MainPage.Class is not working.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smarttrack"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme" >
<activity
android:name="com.smarttrack.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.LoginActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.MainPage"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.AskActivity"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.AskLocation"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.Options"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.smarttrack.Settings"
android:label="#string/app_name"
android:theme="#style/ActionBarTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver android:name="com.smarttrack.MsgBroadcast">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
MainPage.Class
package com.smarttrack;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainPage extends Activity implements OnClickListener{
ImageView img;
TextView statusText;
String status ;
Button askActivity;
Button askLocation;
Button addActivity;
Button optionsMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
initVars();
img.setOnClickListener(this);
askActivity.setOnClickListener(this);
askLocation.setOnClickListener(this);
addActivity.setOnClickListener(this);
optionsMenu.setOnClickListener(this);
}
private void initVars() {
// TODO Auto-generated method stub
img = (ImageView)findViewById(R.id.switchingImage);
statusText = (TextView)findViewById(R.id.appStatus);
img.setImageResource(R.drawable.switch_off);
status = "active";
askActivity = (Button) findViewById(R.id.AskActivity);
askLocation = (Button) findViewById(R.id.btAskLocation);
addActivity = (Button) findViewById(R.id.addActivity);
optionsMenu = (Button) findViewById(R.id.optionsButton);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
switch(id){
case R.id.switchingImage:
Runnable swap = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
if(status == "active"){
img.setImageResource(R.drawable.switch_off);
statusText.setText("Status = Running");
status = "unactive";
}else {
img.setImageResource(R.drawable.switch_on);
statusText.setText("Status = Stopped");
status = "active";
}
}
};
img.postDelayed(swap, 100);
break;
case R.id.AskActivity:
Intent i = new Intent(this,AskActivity.class);
startActivity(i);
break;
case R.id.btAskLocation:
Intent loc = new Intent(this,AskLocation.class);
startActivity(loc);
break;
case R.id.addActivity:
break;
case R.id.optionsButton:
Intent opt = new Intent(this,Options.class);
startActivity(opt);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_item, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.ActionBarHelp:
AlertDialog.Builder bldr = new AlertDialog.Builder(this);
bldr.setTitle("SmartTrack").setMessage("Cool application");
Dialog dlg = bldr.create();
dlg.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
in your manifest xml your target sdk is 18 but 4.4 can run on 19

Activity and android manifest

i created an activity that holds all my activities in a list and declared them in the android manifest. But i keep getting an error "class exception not found". don't know what to do. here is the code and the android manifest.
package com.activityexample.cookbook;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ExtendsListActivity extends ListActivity implements OnItemClickListener{
static final String[] Possible_Choices = new String[]{
"Open Website Example",
"Open Contacts",
"Open Dialer",
"Search Google Example",
"Start Voice Command"
};
final String searchTerms="supeman";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Possible_Choices));
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
getListView().setTextFilterEnabled(true);
getListView().setOnItemClickListener(this);
//getListView().setOnItemClickListener(new OnItemClickListener() {
// });
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
switch (arg2){
case 0:
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com/")));
break;
case 1:
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("content://contacts/people/")));
break;
case 2:
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("tel://08160212611/")));
break;
case 3:
Intent i = new Intent(Intent.ACTION_WEB_SEARCH);
i.putExtra(SearchManager.QUERY, searchTerms);
startActivity(i);
break;
case 4:
startActivity(new Intent(Intent.ACTION_VOICE_COMMAND));
break;
default: break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.activityexample.cookbook"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:maxSdkVersion="19" android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".TheMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityEx"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.activityexample.cookbook.ActivityEx" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity
android:name=".ExtendsListActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
Without seeing the specific error, it looks like this is not the error that you expect. It looks like Android is trying to launch your "MAIN" activity TheMenu, but you have not created that activity. If you don't have it, don't declare it. And then you should probably modify the manifest so that ExtendsListActivity is the MAIN:
<activity
android:name=".ExtendsListActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If you do not declare activity in Manifest you will get the next error:
unable to find [Activity name]. Have you declared the Activity in your
Android Manifest
java.lang.ClassNotFoundException will usually occur when you are missing jar file, or declared the wrong package name for your custom view in XML file, like this guy.

Android Orientation tag error

I am using eclipse android
I want to know about the orientantion changes ,when orientation changes gget a log message in logid ,Below is our code
package com.example.orientation;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.util.Log;
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;
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.d("Android","orientation changes"+newConfig.orientation);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.orientation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.orientation.MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
here we don't get the message in the log cat .I am trying to log the integer value of the orientation .there is no error in the code
Use
<activity
android:name="com.example.orientation.MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize">
Instead of this....
<activity
android:name="com.example.orientation.MainActivity"
android:label="#string/app_name" >
android:configChanges="orientation|screenSize">
see the difference

Open app on number of clicks of power button using broadcast receiver

I am trying to generate some log and put up a toast on screen after power button click.
But it doesn't seem to work. Here's the code:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.pbtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACTION_SHUTDOWN" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.test.pbtest.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.test.pbtest.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"></action>
<action android:name="android.intent.action.SCREEN_ON"></action>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
<action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
</intent-filter>
</receiver>
</application>
</manifest>
MyReceiver.java class:
package com.test.pbtest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.v("Hurray!", "Power button was clicked!");
Toast.makeText(arg0, "power button clicked",Toast.LENGTH_LONG).show();
}
}
MainActivity.java class:
package com.test.pbtest;
import android.os.Bundle;
import android.app.Activity;
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.activity_main, menu);
return true;
}
}
As per my knowledge, i need not register my receiver explicitly as i have used it in manifest file. Please guide me where i am going wrong.
you will need to add ACTION_SHUTDOWN permission in AndroidManifest.xml :
<uses-permission android:name="android.permission.ACTION_SHUTDOWN" />
for some reason you have to register your receiver on runtime.. you can register it via service

Categories

Resources