I'm programming an android app with Android Studio where Imagebutton opens a new screen so that the user can proceed to the next step. Imagebutton is working and new screen opens, but it isn't opening second activity but the same one.
I'm new to android and java programming, so please tell me if other codes are necessary for solving the problem and sorry if I shared unnecessary code.
I have created startActivity with intent.
Here are my current codes of MainActivity java-file and AndroidManifest xml-file:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
mTextMessage = findViewById(R.id.message);
navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
final ImageButton imgButton = findViewById(R.id.simpleImageViewHowdoglearnspackage);
imgButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mobidogi">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SecondActivity"
android:label="#string/title_activity_second" />
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Related
Good morning everybody, I'm trying to implement search features in a single activity with search features by implementing SearchView. From UI-side, everything is working.
But unlike almost of example I found, SearchView does not start another activity but is supposed so send request to the single running activity named HomeActivity.
Following some documentation I found on Android developers website and other sources, I try to do like below. Normally, I would supposed to handle search request in the event "onNewIntent" but it's never called. Click on the button after entering text has no effect (it just closes the keyboard).
I'm beginner in Android development and probably missed something simple.
Can you please help me ?
HomeActivity
public class HomeActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private FrameLayout myFrameLayout;
private Fragment fragmentContracts, fragmentHardware, fragmentMaps, fragmentUsers;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
// Switch to fragment ...
}
return loadFragment(fragment);
}
};
private void handleIntent(Intent intent) {
Log.i("LOG_myapp", "Called new intent !!!");
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
String msg = MessageFormat.format("Search query = {0}", query);
Log.i("LOG_myapp", msg);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(query)
.setTitle("Search request");
AlertDialog dialog = builder.create();
dialog.show();
}
}
#Override
protected void onNewIntent(Intent intent){
// *** NEVER CALLED :( ***
Log.i("LOG_myapp", "Event onNewIntent raised");
handleIntent(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
myFrameLayout = (FrameLayout)findViewById(R.id.MyFrameLayout);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
Fragment defaultFragment = new places();
defaultFragment.setRetainInstance(true);
loadFragment(defaultFragment);
Toolbar toolbar = (Toolbar)findViewById(R.id.activity_main_toolbar);
setSupportActionBar(toolbar);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.application, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.MenuItemSearch).getActionView();
searchView.setSubmitButtonEnabled(true);
ComponentName componentName = new ComponentName(this, HomeActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.MenuItemSettings:
Intent intent = new Intent(this, preferences.class);
startActivity(intent);
break;
}
return true;
}
#Override
public boolean onQueryTextChange(String s){
Log.i("LOG_myapp","Search query: " + s);
return true;
}
#Override
public boolean onQueryTextSubmit(String s){
Log.i("LOG_myapp","Submitted search query: " + s);
return true;
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
try {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.MyFrameLayout, fragment);
transaction.addToBackStack(null);
transaction.commit();
return true;
}
catch (Exception ex)
{
return false;
}
}
return false;
}
}
Menus.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/MenuItemSearch"
android:title="Search"
android:icon="#drawable/baseline_search_white_18dp"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/MenuItemSettings"
android:icon="#drawable/baseline_settings_white_18dp"
app:showAsAction="always"
android:title="Settings"/>
</menu>
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vermilionenergy.myapp">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".HomeActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<intent-filter>
<!--action android:name="android.intent.action.MAIN" /-->
<action android:name="android.intent.action.SEARCH"/>
<category android:name="android.intent.category.DEFAULT" />
<!-- category android:name="android.intent.category.LAUNCHER" / -->
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable"/>
<meta-data android:name="android.app.default_searchable" android:value=".HomeActivity" />
</activity>
<meta-data android:name="android.app.default_searchable" android:value=".HomeActivity" />
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".maps"
android:label="#string/title_activity_maps" />
<activity
android:name=".preferences"
android:label="Preferences" />
<!-- activity android:name=".users" / -->
<activity android:name=".SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TextActivity"></activity>
</application>
</manifest>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="myapp"
android:hint="Search items"
>
</searchable>
I finally found the solution myself.
It looks like the direct strings in searchable.xml don't work. I replaced them by variable as below and it worked :)
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_title"
android:hint="#string/search_hint"
>
</searchable>
I have imported a package which was downloaded from the link "https://github.com/moritz-wundke/android-page-curl", then I created a new project and imported the downloaded package by clicking on "import module". I have added a button inside my new package to open the downloaded package. But its not working. Can someone help. Here is the code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage
("fi.harism.curl");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
});
}
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="android:icon">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Layout:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="press"/>
My manifest.xml raises error for ".RoleActivity". But If I replace my ".roleActivity" with others for checking, they all are okay. Here is my manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zobaed.androidlogin" >
<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=".RoleActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DoctorLoginActivity">
</activity>
<activity android:name=".PatientLoginActivity">
</activity>
</application>
</manifest>
Here is my RoleActivity. Tried to write switch case here.
public class RoleActivity extends AppCompatActivity {
private Button btnPatient;
private Button btnDoctor;
private Button btnGuest;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_in_role);
btnPatient = (Button) findViewById(R.id.btpatient);
btnDoctor = (Button) findViewById(R.id.btdoctor);
btnGuest = (Button) findViewById(R.id.btguest);
btnPatient.setOnClickListener((View.OnClickListener) this);
btnDoctor.setOnClickListener((View.OnClickListener) this);
btnGuest.setOnClickListener((View.OnClickListener) this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btdoctor: {
Intent i = new Intent(getApplicationContext(), DoctorLoginActivity.class);
startActivity(i);
break;
}
case R.id.btpatient: {
Intent i = new Intent(getApplicationContext(), PatientLoginActivity.class);
startActivity(i);
break;
}
}
}
}
implement onClickListner in RoleActivity class and change code to
btnPatient.setOnClickListener( this);
btnDoctor.setOnClickListener(this);
btnGuest.setOnClickListener(this);
Your activity is not implementing View.OnClickListener. Unless you implement View.OnClickListener on your activity you cannot cast the activity as an OnClickListener. That is why you are getting error, probably a ClassCastException
btnPatient.setOnClickListener((View.OnClickListener) this);
btnDoctor.setOnClickListener((View.OnClickListener) this);
btnGuest.setOnClickListener((View.OnClickListener) this);
implement View.OnClickListener on your activity. Change
public class RoleActivity extends AppCompatActivity
to
public class RoleActivity extends AppCompatActivity implements View.OnClickListener
and then you can remove that casting
btnPatient.setOnClickListener(this);
btnDoctor.setOnClickListener(this);
btnGuest.setOnClickListener(this);
if you are not implemeting View.OnClickListener on your activity you can add the click listener as an anonymous inner class to handle clicks on views
My app crashes when I try to navigate to another activity. Why does that happen?
I'm able to start the other activity when I launch it at first so there's no problem in the CheckUsernameActivity.
public class CheckNumberActivity extends AppCompatActivity {
EditText phoneNumberEditText;
Button countryCodeButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_number);
Button button = (Button) findViewById(R.id.okButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
countryCodeButton = (Button) findViewById(R.id.countryCodeButton);
phoneNumberEditText = (EditText) findViewById(R.id.phoneNumberEditText);
Log.v("areaCode", countryCodeButton.getText().toString());
Log.v("phoneNumber", phoneNumberEditText.getText().toString());
Intent k = new Intent(CheckNumberActivity.this, CheckUsernameActivity.class);
startActivity(k);
}
});
}
}
Try This way. I think it will help you.
public class CheckNumberActivity extends AppCompatActivity {
EditText phoneNumberEditText;
Button countryCodeButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_number);
Button button = (Button) findViewById(R.id.okButton);
countryCodeButton = (Button) findViewById(R.id.countryCodeButton);
phoneNumberEditText = (EditText) findViewById(R.id.phoneNumberEditText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v("areaCode", countryCodeButton.getText().toString());
Log.v("phoneNumber", phoneNumberEditText.getText().toString());
Intent k = new Intent(CheckNumberActivity.this, CheckUsernameActivity.class);
startActivity(k);
}
});
}
}
I didn't have my new activity defined in my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dimsumdev.runk" >
<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=".activity.CheckNumberActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.CheckUsernameActivity" >
<!--Default Intent Filter-->
<intent-filter>
<action android:name="android.intent.action.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".activity.HomeActivity" >
</activity>
</application>
</manifest>
I have the following code, but when I run it, it tells me that "http://www.google.com" is unavailable, which is total rubbish. If I run the code without the whole HelloWebViewClient class, it opens google properly, except in the pre-installed Android browser. I want to open it in-app. Thanks.
My Code:
public class MainActivity extends Activity {
Button btnGo;
Editable guiNumber;
WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGo = (Button) findViewById(R.id.btnGo);
webView = (WebView) findViewById(R.id.viewWeb);
webView.setVisibility(View.GONE);
webView.setWebViewClient(new HelloWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
btnGo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
guiNumber = enterGUI.getText();
if (guiNumber.length() == 8) {
guiNumber.replace(4, 8, "");
gui = Integer.parseInt(enterGUI.getText().toString());
System.out.println(gui);
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
webView.setVisibility(View.VISIBLE);
webView.requestFocus();
webView.loadUrl("http://google.com");
}
else {
enterGUI.setError("Your GUI/ILGU number must be 8 digits long");
}
}
});
}
private class HelloWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
And the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.frostplant.clublink" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="6" android:targetSdkVersion="15"/>
<uses-permisssion android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:theme="#style/AppTheme">
<activity android:name=".MainActivity" android:label="#string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Give your application the permission for accessing internet: android.permission.INTERNET This will allow your application to use network.
You have mistakenly typed 3 "s"s in your permission line:
<uses-permisssion android:name="android.permission.INTERNET"/>
This should be:
<uses-permission android:name="android.permission.INTERNET"/>