I'm learning Android to do some projects for the college. I'm doing an app to look data on a web service. But my activity don't catch the search intent (or I thought it). I was following the Android Developer Guide: https://developer.android.com/guide/topics/search/search-dialog. I followed it, but my app doesn't work. I let here my code:
SearchActivity.java
package com.example.javierortiz.pprog2_ac4;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class SearchActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())){
String asdf = intent.getStringExtra(SearchManager.QUERY);
Log.d("asdfasdfasdf", asdf);
}
}
}
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:label= "#string/app_name"
android:hint = "#string/search_hint">
</PreferenceScreen>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.javierortiz.pprog2_ac4">
<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=".SearchActivity">
<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" />
</activity>
</application>
</manifest>
activity_search.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SearchActivity">
<SearchView
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Thank you in advance
If you want to implement a search view listener try:
SearchView search = findViewById(R.id.search);
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
Log.d("onQueryTextChange", "query" + query);
// Do your task here
return false;
}
});
Related
I was trying to do this Tutorial
But there's an error then try to do with the top comment seen at below
Why do I still got an error Error:(26, 105) error: illegal character: '\ufeff'
Here is my code
package com.pete.mseuf.intent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static Button Button_Submit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickButtonListener();
}
public void OnClickButtonListener(){
Button_Submit = (Button)findViewById(R.id.button);
Button_Submit.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this ,
Main2Activity.class);
startActivity(intent);
}
}
);
}
}
Added my xml file from AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pete.mseuf.intent">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="com.pete.mseuf.intent.Main2Activity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Here is my Layout xml activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pete.mseuf.intent.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="168dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I am new here at stackoverflow and in android studio. Thankyou for your patience
Possibilities :you have miss typed the character '\ufeff' in your XML
Open build.gradle (Module: app) form Gradle Scripts section in your android studio project
Add this line
compileOptions.encoding = 'windows-1251'
inside android {//add line hare} like this
android {
compileOptions.encoding = 'windows-1251'
}
clean the project and run..
I hope it will solve your problem...
feel free to ask..
When pressing the button, new activity should open but app instantly crashes. I have searched for answer and making a few adjustments to my code and only thing i dont know how to do is Manifest, can u help me with that?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Starting.Programm"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
<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="InformationActivity"
android:label="#string/app_name"
>
</activity>
</application>
MainActivity:
package Starting.Programm;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends InformationActivity
{
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startJourney(View view) {
Intent intent = new Intent(this, InformationActivity.class);
startActivity(intent);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity"
>
<Button android:id="#+id/button_StartApp"
android:layout_width="100dp"
android:layout_height="60dp"
android:text="#string/button_StartApp"
android:layout_gravity="center"
android:layout_marginTop="70dp"
android:layout_marginLeft="115dp"
android:onClick="startJourney"
/>
</LinearLayout>
And next activity which should be opening
package Starting.Programm;
import android.app.Activity;
import android.os.Bundle;
class InformationActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
}
}
<activity> android:name=".InformationActivity" </activity>
Remove android:label="#string/app_name" too. I don't think it is needed.
Add dot to end of pacakager name
package="Starting.Programm."
I've started learning programming for Android and the tutorial I'm following says that if I add 2 activities to the manifest xml with the same intent I should get a dialog that makes me select one of them, but it doesn't. My Android version is 2.3.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.frisodenijs.usingintent" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".main"
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="#string/title_activity_second" >
<intent-filter>
<action android:name="com.frisodenijs.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ThirdActivity"
android:label="Third Activity">
<intent-filter>
<action android:name="com.frisodenijs.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Main activity:
package com.frisodenijs.usingintent;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
public class main extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
startActivity(new Intent(main.this, SecondActivity.class));
}
}
Second activity:
package com.frisodenijs.usingintent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class SecondActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
Main xml:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To second button"
android:onClick="onClick"/>
</RelativeLayout>
Second Activity xml:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.frisodenijs.usingintent.SecondActivity">
<TextView
android:text="This is the second activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Replace your onClick with this…
public void onClick(View view) {
startActivity(new Intent("com.frisodenijs.SecondActivity"));
}
My Application name is timepass
The below file is MainActivity.java
package com.example.timepass;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
int counter;
Button add, sub;
TextView Display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add= (Button) findViewById(R.id.badd);
sub=(Button) findViewById(R.id.bsub);
Display=(TextView) findViewById(R.id.tvdisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
Display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
Display.setText("Your total is " + counter);
}
});
}
#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;
}
}
The below file is activity_main.xml which is realted to the above file MainActivity.java
<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=".MainActivity"
android:background="#drawable/pic_two">"
<Button
android:id="#+id/badd"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvdisplay"
android:layout_alignTop="#+id/bsub"
android:text="#string/but1"
android:textSize="20sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvdisplay"
android:layout_alignRight="#+id/tvdisplay"
android:text="#string/hello_world"
android:textSize="#dimen/asa" />
<TextView
android:id="#+id/tvdisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:gravity="center"
android:text="#string/str1"
android:textColor="#layout/activity_main"
android:textSize="30sp" />
<Button
android:id="#+id/bsub"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tvdisplay"
android:layout_below="#+id/tvdisplay"
android:layout_marginRight="38dp"
android:layout_marginTop="23dp"
android:text="#string/but2"
android:textSize="20sp" />
</RelativeLayout>
Then I have created another activity which is below which is Splash.java
package com.example.timepass;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity
{
#Override
protected void onCreate(Bundle tpsavedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(tpsavedInstanceState);
setContentView(R.layout.splash);
Thread timer =new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch(InterruptedException e)
{ {
e.printStackTrace();
}
finally
{
Intent openMainActivity =new Intent("com.example.timepass.MAINACTIVITY");
startActivity(openMainActivity);
}
}
};
timer.start();
}
}
Then I have created Xml file related to Splash.java which is splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/pic_background">
</LinearLayout>
NOW I WANT TO RUN FIRST Splash.java file then MainActivity.java file so I have make some changes in Androidmanifest.xml file
here are the changes, and after running my program I get error "Unfortunately timepass has stopped" where timepass is my app name . please solve this issue
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timepass"
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.timepass.Splash"
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.example.timepass.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.timepass.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
//just do it like this
Intent openMainActivity =new Intent(this,MainActivity.class);
startActivity(openMainActivity);
instead of
Intent openMainActivity =new Intent("com.example.timepass.MAINACTIVITY");
startActivity(openMainActivity);
MainActivity in your manifest is used in two places. In one place it is in fully capital letter. Change that and give a try
You have an extra { before e.printStackTrace(); in your Splash.java. Remove that and you'll be good to go.
Also post log for furthur errors in the future
I have edited your AndroidManifest.xml update your code after your application work properly.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timepass"
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.timepass.Splash"
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.example.timepass.MainActivity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
change your code in splash.java
Intent i = new Intent(splash.this, MainActivity.class);
startActivity(i);
I have a problem with an Android aplication. When I try to swich from main activity (PretvornikValut) to another one (Pretvornik) via buttun I get the "Unfortunately aplication stopped working" error. Can anyone help?
PretvornikValut.java
package pretvornik.valut;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class PretvornikValut extends Activity implements OnClickListener
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View gumbPretvori = findViewById(R.id.gumb_pretvornik);
gumbPretvori.setOnClickListener(this);
View gumbTecaji = findViewById(R.id.gumb_tecaji);
gumbTecaji.setOnClickListener(this);
}
public void sendMessage(View view) {
Intent intent = new Intent(this, Pretvornik.class);
startActivity(intent);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.gumb_pretvornik:
Intent i = new Intent(this, Pretvornik.class);
//i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
break;
case R.id.gumb_tecaji:
Intent tecaj = new Intent(this, Tecaji.class);
tecaj.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(tecaj);
break;
}
}
}
Pretvornik java
package pretvornik.valut;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
/**
*
* #author FAKS
*/
public class Pretvornik extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.pretvornik);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#99CCCC"
>
<Button
android:id="#+id/gumb_pretvornik"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="Pretvornik"
/>
<Button
android:id="#+id/gumb_tecaji"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_toRightOf="#+id/OK"
android:layout_alignParentBottom="true"
android:text="Izpis tecajev"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Izberi zeljeno opcijo"
/>
</LinearLayout>
pretvornik.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#99CCCC"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="glavni program"
/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pretvornik.valut"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name="PretvornikValut"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity
android:name="Pretvornik"
android:label="#string/Pretvornik_valut" >
<meta-data
android:name="PretvornikValut"
/>
</activity>
<activity
android:name="Tecaji"
android:label="#string/Izpis_tecajev" >
<meta-data
android:name="PretvornikValut"
/>
</activity>
</activity>
</application>
</manifest>
Change your manifest xml,
add action for your activities
action android:name="android.intent.action.ACTIVITY"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pretvornik.valut"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="PretvornikValut"
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="Pretvornik"
android:label="#string/hello_world" >
<action android:name="android.intent.action.ACTIVITY" />
</activity>
<activity
android:name="Tecaji"
android:label="#string/hello_world" >
<action android:name="android.intent.action.ACTIVITY" />
</activity>
</application>
</manifest>