I have two classes in two different files, one is MainActivity and the other one is SearchResultsActivity.
For some reason when i press the search button it doesn't start SearchResultsActivity, I'm assuming this because i put several breakpoints (even some at the beginning of the class) and the debugger doesn't seem to reach them.
MainActivity.Java
package myapp.myapp;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
ProgressBar theProgressBar;
TextView stInstTxt;
public void sendMessage(View view) {
Intent intent = new Intent(this, MainActivity.class);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search_element).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item){
// Handle item selection
switch (item.getItemId()) {
case R.id.search_element:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//text view
stInstTxt = (TextView) findViewById(R.id.textView01);
//progress bar
theProgressBar = (ProgressBar) findViewById(R.id.progressBar01);
theProgressBar.setVisibility(View.INVISIBLE);
//toolbar
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
}
SearchResultsActivity.Java
package myapp.myapp;
public class SearchResultsActivity extends Activity{
TextView stInstTxt;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
searchItems(query);
}
}
public void searchItems(String query) {
stInstTxt.append(query);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myapp.myapp">
<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: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="myapp.myapp.SearchResultsActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value="myapp.myapp.SearchResultsActivity"/>
</activity>
</application>
</manifest>
activity_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"
tools:context=".MainActivity"
android:background="#FFF9C4">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:paddingTop="70dp"
android:layout_height="wrap_content">
<TextView android:text="#string/help_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView01"
android:scrollbars="vertical"
android:visibility="visible"/>
</RelativeLayout>
</RelativeLayout>
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search_element"
android:orderInCategory="200"
android:title="#string/search_hint"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
Could there be something wrong with AndroidManifest.xml?
After almost 6 hours of trying to implement this (starting with the android documentation as a guide), and trying dozens of different ways to do it, i feel i maybe could help someone else with a similar problem save some time, I came up to this tutorial
I read it all but what I ended up doing was downloading the code and comparing it with mine until i found that I was missing a couple of important things. The official android documentation is incomplete, and pretty much there was no way i could have came up with a solution without seeing a real and complete working example.
Related
I am trying to code an app where it connects to Bluetooth on the Main Activity Page and then on the Second Activity page it will turn on the silent ringer mode. Every time I click on the button to open up the next activity, the app closes down. There are no errors according to Android Studio.
Here is my SecondActivity.java file
package fonephree.fonephreeconnecttobluetooth;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends Activity {
Button button;
AudioManager audiomanager;
Context context;
TextView textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button = (Button)findViewById(R.id.button6);
textview = (TextView)findViewById(R.id.textView2);
context = getApplicationContext();
audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
textview.setText("Silent Mode Enable");
}
});
}
}
and here is my activity_second.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:background="#android:color/holo_orange_dark"
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="fonephree.fonephreeconnecttobluetooth.SecondActivity">
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Silent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button6"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:text="Silent Mode"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/phree"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
And just in case the error is in my AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fonephree.fonephreeconnecttobluetooth">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<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=".SecondActivity"
android:label="#string/title_activity_second"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="fonephree.fonephreeconnecttobluetooth.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
EDIT
I fixed the code but now the app only shuts off when I press the button and it wasn't in silent mode before opening the app. The button just reinforces silent mode, it doesn't actually turn it on.
From the information what you have given, you are setting your XML to be activity_main, while the button6 and textview2 are defined in activity_second.xml
So your onCreate should like below to solve the problem
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button = (Button)findViewById(R.id.button6);
textview = (TextView)findViewById(R.id.textView2);
context = getApplicationContext();
audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
textview.setText("Silent Mode Enable");
}
});
}
I'm having trouble creating the search feature for my application. I have followed various tutorials, followed the Android Docs, and other Stack Overflow answers with no success. I have the icon for the each in one activity (ContinentActivity.java) and when clicked the toolbar opens up a search window. However typing data does not register in the SearchResultsActivity. It is never created. Could this be because I am using a toolbar and a menu?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_swell_alert_logo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity"/>
<activity
android:name=".SearchResultsActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<!-- to identify this activity as "searchable" -->
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.appcompat.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name=".ui.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=".ui.locations.LocationSelectionActivity"
android:label="#string/title_activity_location_selection"
android:noHistory="true"
android:parentActivityName=".ui.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>
<activity
android:name=".ui.locations.ContinentActivity"
android:theme="#style/NoAnimationTheme"
android:label="#string/title_activity_continent"
android:parentActivityName=".ui.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>
<!-- Children of Continent Activity. Each have search capabilities-->
<activity
android:name=".ui.locations.CountryActivity"
android:theme="#style/NoAnimationTheme"
android:label="#string/title_activity_country"
android:parentActivityName=".ui.locations.ContinentActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.locations.ContinentActivity" />
</activity>
<activity
android:name=".ui.locations.StateActivity"
android:theme="#style/NoAnimationTheme"
android:label="#string/title_activity_state"
android:parentActivityName=".ui.locations.CountryActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.locations.CountryActivity" />
</activity>
<activity
android:name=".ui.locations.SurfSpotActivity"
android:theme="#style/NoAnimationTheme"
android:label="#string/title_activity_surf_spot"
android:parentActivityName=".ui.locations.StateActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.locations.StateActivity" />
</activity>
</application>
SearchResultsActivity.java
public class SearchResultsActivity extends AppCompatActivity {
public static final String TAG = "SEARCH_RESULTS_ACTIVITY";
SearchResultsAdapter mSearchResultsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate");
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.v(TAG, "onNewIntent");
handleIntent(intent);
}
private void handleIntent(Intent intent) {
Log.v(TAG, "HANDLE INTENT");
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
private void showResults(String query) {
// Query your data set and show results
// ...
Log.v(TAG, "Searching for " + query + "...");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.v(TAG, "onCreateOptionsMenu");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_location, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()) );
return true;
}
This activities menu has the search icon
ContinentActivity.java
public class ContinentActivity extends AppCompatActivity {
private ArrayList<Continent> mContinents;
#Bind(R.id.recyclerView) RecyclerView mRecyclerView;
#Bind(R.id.toolBar) Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_selection);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
LocationDataSource dataSource = new LocationDataSource(this);
dataSource.test();
mContinents = dataSource.readContinents();
ContinentAdapter adapter = new ContinentAdapter(this, mContinents);
mRecyclerView.setAdapter(adapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_location, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}
menu_location.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:orderInCategory="200"
android:title="#string/action_settings"
android:icon="#drawable/ic_search_white_24dp"
appcompat:showAsAction="always"
appcompat:actionViewClass="android.widget.SearchView"/>
</menu>
tool_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:elevation="4dp"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark"
android:titleTextColor="#color/ColorText">
</android.support.v7.widget.Toolbar>
Seems you have missing call to associate SearchView with searchable info within the onCreateOptionsMenu() method? Quote from the training link http://developer.android.com/guide/topics/search/search-dialog.html
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
But since you are not using the same activity for search result, you cannot simply use getComponentName(), but to use new ComponentName(this, SearchResultsActivity.class)
action bar not showing in main activity while the same code present for another activity and another thing action bar overflow not showing by list in right corner
image 1 : main activity with menu key pressed
image 2 : show message activity
code for main activity:
public class MainActivity extends ActionBarActivity {
EditText t;
public final static String sendMessageextra="com.example.example2.textmessage";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(EditText) findViewById(R.id.textmessage);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
public void sendMessage(View v){
Intent sendmessageintent=new Intent(this,showMessage.class);
String messagestring=t.getText().toString();
sendmessageintent.putExtra(sendMessageextra, messagestring);
startActivity(sendmessageintent);
}
}
show message activity:
public class showMessage extends Activity{
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sendmessage);
t=(TextView) findViewById(R.id.showmessage);
showmessage();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return true;
}
public void showmessage(){
Intent intent=getIntent();
String message=intent.getStringExtra(MainActivity.sendMessageextra);
t.setText(message);
Log.i("Set New Text Box", "Text BOx");
}
}
activity main actions.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search Widget -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"/>
<!-- Location Found -->
<item android:id="#+id/action_location_found"
android:icon="#drawable/ic_action_location_found"
android:title="#string/action_location_found"
android:showAsAction="never" />
<!-- Help -->
<item android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:title="#string/action_help"
android:showAsAction="never"/>
</menu>
android menifest file:
Activity Manifest File:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<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="com.example.example2.showMessage"
android:label="Show Message" android:parentActivityName="com.example.example2.MainActivity"/>
</application>
</manifest>
If you want to show the search icon in the right of the actionbar in every activity then just make a change in the actions.xml instead of android:showAsAction="ifRoom" change it to android:showAsAction="always"
ActionBar not showing
Do this in your oncreate()
ActionBar actionBar = getActionBar();
actionBar.show();
ActionBar overflow
there you can see your menu
To display menu, In your activity main actions.xml,
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:showAsAction="always"
android:title="#string/action_search"
android:icon="#android:drawable/ic_action_search"
android:actionViewClass="android.widget.SearchView"/>
</menu>
Like this you need to do changes in main actions file
I can't get my click listeners working. The ImageButton is retrieved correctly.
Listener (Extending my custom Activity which is setting the view):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("UserActivity", "Loading imagebutton...");
ImageButton iButton = (ImageButton) findViewById(R.id.user_button_ratings);
Log.d("UserActivity", "Button " + iButton);
iButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Button", "Image was clicked");
}
});
}
BaseActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout,
R.drawable.ic_drawer, 0, 0) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.menu);
}
};
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
layers = getResources().getStringArray(R.array.layers_array);
drawerList = (ListView) findViewById(R.id.left_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.adapter_navigation, R.id.navigation_text, layers));
final BaseActivity activity = this;
drawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// Do something
}
});
drawerList.setBackgroundColor(Color.LTGRAY);
}
Layout:
<ImageButton
android:id="#+id/user_button_ratings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/mmb_rating_big" />
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.panic.xenira.mmb"
android:versionCode="2"
android:versionName="0.1.1" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<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"
android:theme="#style/NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SearchActivity"
android:label="#string/title_activity_search" >
</activity>
<activity
android:name=".BaseActivity"
android:label="#string/title_activity_base" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name=".UserActivity"
android:label="#string/title_activity_user" >
</activity>
<activity
android:name=".UserViewerActivity"
android:label="#string/title_activity_user_viewer" >
</activity>
</application>
</manifest>
There is no error in the LogCat and the onClick method is not caled.
I've tryed different methods of using the listener but never got any response. I also tryed a code that worked for me befor but no response.
Would be great if you culd help me out ;)
When using a DrawerLayout, there should be only one main content View, with the drawer View - in this case, your ListView - listed after it. Using a DrawerLayout in any other way will result in incorrect, unpredictable behavior, often preventing normal interaction with other layout elements.
A tutorial with links to a sample and docs can be found on this developer page.
Here I am giving an example of Image button. If it is also giving error then clean projects of eclipse and try again.
<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" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginRight="35dp"
android:layout_marginTop="32dp"
android:contentDescription="image"
android:src="#drawable/ic_launcher" />
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
ImageButton imgButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
private void addListenerOnButton() {
imgButton = (ImageButton) findViewById(R.id.imageButton1);
imgButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT)
.show();
}
});
}
}
So I have 2 activities and in the first one I have a menu item that once clicked should open the second activity. The changing part works but it's not changing what it is supposed to, in the second activity which I activate with this :
Intent intent = new Intent(this, EditView.class);
startActivity(intent);
break;
in the first activity onOptionsItemSelected, and I click a button from the menu, in this second activity I have a diferent layout and I do this setContentView(R.layout.second); to change the layout I have a functional xml file because I tried it in another project, in the manifest file I have this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ro.merca.ionel"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".FileList"
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=".EditView"
android:label="#string/app_name" />
</application>
</manifest>
the problem is that when I click the option from the menu it loads a simple layout without all the things I put in second.xml... I don't know that the problem is...
public class EditView extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = getIntent();
TextView tv = new TextView(this);
final TextView name = (TextView) findViewById(R.id.name);
final TextView text = (TextView) findViewById(R.id.text);
setContentView(tv);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1,1,1,"Salveaza Nota").setIcon(android.R.drawable.btn_default);
menu.add(1,2,2,"Anuleaza Modificari").setIcon(android.R.drawable.btn_default);
menu.add(1,3,3,"Sterge Nota").setIcon(android.R.drawable.btn_default);
menu.add(1,4,4,"Share").setIcon(android.R.drawable.btn_default);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case 1 :
Intent intent = new Intent(this, EditView.class);
this.startActivity(intent);
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Nume" />
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text" />
<EditText
android:id = "#+id/text"
android:inputType="text|textMultiLine"
android:minLines="5"
android:gravity="top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
You are calling setContentView twice in onCreate(). Remove the second one and try again.
I am bit confused over here. If you are just moving from 1st activity to 2nd without passing any values then what's the use of using getIntent() etc?
Write simply this much, and you will land on your desired activity:
public class EditView extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv = new TextView(this);
final TextView name = (TextView) findViewById(R.id.name);
final TextView text = (TextView) findViewById(R.id.text);
}
}