I'm creating a menu where all of my activity will be on the list, and whenever i clicked an activity from that list, it should start that activity.
here is my menu.java
public class Menu extends ListActivity
{
String classnames[] = {"MainActivity","example1","example2","example3",
"example4","example5","example6","example7","example8","example9","example10"};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1,classnames));
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String classClicked = classnames[position];
try
{
Class ourclass = Class.forName("android.intent.action."+classClicked);
Intent intent = new Intent(Menu.this,ourclass);
startActivity(intent);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
here is my manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Menu"
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=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
It don't have any error but my MainActivity doesn't start even when I clicked it.
Here's what it looks like:
Use getListView().setOnItemClickListener instead of onListItemClick. Why? to be honest i also dont know, but it solved my problem.
You must declare all your activities in AndroidManifest.xml otherwise it won't work. Your updated manifest file should look like this:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Menu"
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=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".example1"
android:label="#string/app_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".example2"
android:label="#string/app_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".example3"
android:label="#string/app_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".example4"
android:label="#string/app_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".example5"
android:label="#string/app_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".example6"
android:label="#string/app_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".example7"
android:label="#string/app_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".example8"
android:label="#string/app_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".example9"
android:label="#string/app_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".example10"
android:label="#string/app_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
Menu.java
public class Menu extends ListActivity
{
String classnames[] = {"MainActivity","example1","example2","example3",
"example4","example5","example6","example7","example8","example9","example10"};
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1,classnames));
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l) {
String classClicked = classnames[position];
try {
Class ourclass = Class.forName("android.intent.action."+classClicked);
Intent intent = new Intent(getApplicationContext(),ourclass);
startActivity(intent);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
});
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
/*super.onListItemClick(l, v, position, id);
String classClicked = classnames[position];
try
{
Class ourclass = Class.forName("android.intent.action."+classClicked);
Intent intent = new Intent(Menu.this,ourclass);
startActivity(intent);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}*/
}
}
And now everything should work.
You must register the other activities in your manifest, just like the mainactivity. Otherwise Android wont start them.
Related
In this project I tried to make an animated splash screen which will appear before going to the main activity, but after I execute, the splash screen does not appear but instead goes directly to the main activity.
how to fix this?
this is my splash activity class
private ImageView container;
private AnimationDrawable animationDrawable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash)
container = findViewById(R.id.iv_icons);
container.setBackgroundResource(R.drawable.mysplash_animation);
animationDrawable = (AnimationDrawable) container.getBackground();
}
#Override
protected void onResume() {
super.onResume();
animationDrawable.start();
checkAnimationStatus(50, animationDrawable);
}
private void checkAnimationStatus(final int time, final AnimationDrawable animationDrawable) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (animationDrawable.getCurrent() != animationDrawable.getFrame(animationDrawable.getNumberOfFrames() - 1))
checkAnimationStatus(time, animationDrawable);
else finish();
}
}, time);
}
}
and this is my manifest.xml
<application
android:allowBackup="true"
android:exported="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<activity android:name=".SplashActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
You have to change this in your manifest(you have to give launcher activity to splashActivity)
<application
android:allowBackup="true"
android:exported="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<activity android:name=".SplashActivity">
// below code you have to add //
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
//Remove from here//
</activity>
</application>
Set your Splash Activity as the Launcher activity:
<activity android:name=".MainActivity"></activity>
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Please provide intentfilter to SplashActivity
<application
android:allowBackup="true"
android:exported="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
</application>
public class ListViewActivity extends AppCompatActivity
{
ListView lv;
String[] ListItems={"AutoCompleteTextViewActivity","CheckboxActivity","EditTextActivity","ListViewActivity","ProgressBarActivity","RadioButtonActivity","RatingBar2Activity","RatngBarActivity","SeekBarActivity","SpinnerActivity","SwitchActivity","TextViewActivity"};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter ad=new ArrayAdapter(ListViewActivity.this,android.R.layout.simple_list_item_1,ListItems);
lv.setAdapter(ad);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent i = new Intent(ListViewActivity.this, ListItems[position].getClass());
startActivity(i);
}
});
}
}
i am current learning android studio.i made an app through which i can open my previously created apps by choosing them from a listview widget.it shows an error in the intent line.please tell me where i've have gone wrong.
MY MANIFEST FILE
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.re1">
<application
android:allowBackup="true"
android:banner="#mipmap/ic_launcher_round"
android:icon="#mipmap/ic_launcher_round"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".TextViewActivity" />
<activity android:name=".EditTextActivity" />
<activity android:name=".RadioButtonActivity" />
<activity android:name=".CheckboxActivity" />
<activity android:name=".RatngBarActivity" />
<activity android:name=".RatingBar2Activity" />
<activity android:name=".ProgressBarActivity" />
<activity android:name=".SeekBarActivity" />
<activity android:name=".SwitchActivity" />
<activity android:name=".SpinnerActivity"/>
<activity android:name=".AutoCompleteTextViewActivity">
</activity>
<activity android:name=".ListViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Add your activity name to your manifest file
<activity android:name=".ListViewActivity"></activity>
Before my application was working fine. I just created a new class (within the same package) and also declared it in the manifest, still I get a ActivityNotFoundException and sometimes NoClassDefFoundException.
TestActivity.java
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.order_summary);
}
}
Activity_NewOrder.java
public class Activity_NewOrder extends Activity
try {
//Also tried Intent intent = new Intent(Activity_NewOrder.this, TestActivity.class);
Intent intent = new Intent(getApplicationContext(), TestActivity.class);
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mypackage">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Activities.Activity_OrderSummaryNew"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape" />
<activity
android:name=".Activities.Activity_Login"
android:configChanges="orientation"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activities.Activity_Orders"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape" />
<activity
android:name=".Activities.Activity_NewOrder"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape" />
<activity
android:name=".Activities.Activity_OrderHistory"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape" />
<activity android:name=".Activities.TestActivity" />
</application>
</manifest>
can't fire activity on onListItemClick() in ListActivity with buttons in list?
if i touch the button, i could not get into the referred activity. it just stays there in that page even on click.
public class Intro4 extends ListActivity{
String classes[] = {"Entertainment","TextPlay","Current-Affairs"};
public static int correct,wrong,marks;
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese= classes[position];
try {
Class ourClass = Class.forName("com.SVS."+cheese);
Intent ini= new Intent(Intro4.this,ourClass);
startActivity(ini);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Intro4.this,android.R.layout.select_dialog_item,classes));
}}
Manifest is:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Intro1"
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=".Intro2"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.SVS.INTRO2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Intro3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.SVS.INTRO3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Intro4"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.SVS.INTRO4" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Entertainment"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.SVS.ENTERTAINMENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TextPlay"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.SVS.TEXTPLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.SVS.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Here:
String classes[] = {"Entertainment","TextPlay","Current-Affairs"};
Current-Affairs is not a valid java identifier. see Using Java Naming Conventions for more details.
And make sure all activities Entertainment,TextPlay,... is declared in AndroidManifest.xml
i got the answer.. in java clas..
Class.forname("com.svs.blabla")
not
("com.SVS.blabla")
i.e., package name should be in lower case.
Hello i am new to android and i written a small app for "Simple web Browser".
but my browser is unable to open the page and it's showing error like no network available(Web Page not available) even wifi is on and normal browser is working on that device.
please check the code below::
SimpleBrowser.java
package com.thenewboston.travis;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class SimpleBrowser extends Activity implements OnClickListener{
Button bGo,bBack,bForward,bHistory,bRefresh;
EditText et;
WebView wb;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.simplebrowser);
bGo = (Button)findViewById(R.id.go);
bBack = (Button)findViewById(R.id.bBack);
bForward = (Button)findViewById(R.id.bForward);
bHistory = (Button)findViewById(R.id.bHistory);
bRefresh = (Button)findViewById(R.id.bRefresh);
et = (EditText)findViewById(R.id.editAddress);
wb = (WebView)findViewById(R.id.webView);
wb.setWebViewClient(new ourViewClient());
bGo.setOnClickListener(this);
bBack.setOnClickListener(this);
bForward.setOnClickListener(this);
bHistory.setOnClickListener(this);
bRefresh.setOnClickListener(this);
//wb.loadUrl("http://www.gmail.com");
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.go:
String website = et.getText().toString();
wb.loadUrl(website);
break;
case R.id.bBack:
if(wb.canGoBack())
wb.goBack();
break;
case R.id.bForward:
if(wb.canGoForward())
wb.goForward();
break;
case R.id.bHistory:
wb.clearHistory();
break;
case R.id.bRefresh:
wb.reload();
break;
}
}
}
ourViewClient.java
package com.thenewboston.travis;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ourViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
and ....
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thenewboston.travis"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="KCRaju"
android:theme="#style/AppTheme">
<activity
android:name="com.thenewboston.travis.Splash"
android:label="KCRaju"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StartingPoint"
android:label="KCRaju" >
<intent-filter>
<action android:name="com.thenewboston.travis.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="KCRaju" >
<intent-filter>
<action android:name="com.thenewboston.travis.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".prefs"
android:label="KCRaju" >
<intent-filter>
<action android:name="com.thenewboston.travis.prefs" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".About"
android:label="KCRaju"
android:theme="#android:style/Theme.Dialog" >
<intent-filter>
<action android:name="com.thenewboston.travis.About" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".TextPlay" android:label="#string/app_name" >
</activity>
<activity android:name=".Email" android:label="#string/app_name" >
</activity>
<activity android:name=".Camera" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".Data" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".GFX" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".GFXSurface" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".SoundStuff" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".Slider" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".Memory" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".OpenedClass" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".Tabs" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".SimpleBrowser" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
</application>
</manifest>
We need to provide url with https as suffix to loadUrl
In your code :
wb.loadUrl(website);
What you passed website.check the string it is valid website.Copyt the string and paste in android phone's browser then check it is display or not