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();
}
});
}
}
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 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.
I'm a newbie working on an android app, following is the first screen of the app. I want my application to perform search using that search dialog box.
For this, I followed http://developer.android.com/guide/topics/search/search-dialog.html
But I was getting following error:
Logcat:
FATAL EXCEPTION: main
Process: com.example.shalini.circlelayout, PID: 19330
java.lang.NullPointerException
at com.example.shalini.circlelayout.MainActivity.onCreateOptionsMenu(MainActivity.java:69)
where line:69 in MainActivity.java is:
ComponentName cn = new ComponentName(this, SearchResultsActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
I referred to this
Cannot get searchview in actionbar to work
and made changes but the same problem still persists. My question is why is it showing error in searchView.setSearchableInfo(searchManager.getSearchableInfo(cn)); line. I couldn't figure that out, I still think that error is arising due to some other part of the code, maybe searchresultsactivity.xml or SearchResultsActivity.java. Any help is much appreciated. Thank you.
Code below:
MainActivity.java
package com.example.shalini.circlelayout;
import android.app.SearchManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
EditText e1;
ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.search);
addListenerOnButton();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
private void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), SymptomsList.class);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
ComponentName cn = new ComponentName(this, SearchResultsActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
//call to getSearchableInfo()
//SearchableInfo object
//SearchView starts an activity with the ACTION_SEARCH intent when a user submits a query
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
SearchResultsActivity.java
package com.example.shalini.circlelayout;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class SearchResultsActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleIntent(getIntent());
}
#Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
//use the query to search your data somehow
}
}
private void doMySearch(String query) {
Log.d("Event", query);
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="wrap_content" android:padding="#dimen/activity_horizontal_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity"
android:background="#drawable/blue_bluecrop1">
<EditText
android:layout_width="160dp"
android:layout_height="60dp"
android:id="#+id/search"
android:inputType="text"
android:hint="#string/sym"
android:drawableStart="#drawable/search_s2"
android:drawableLeft="#drawable/search_s2"
android:padding="20dp"
android:gravity="center_vertical|center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:background="#drawable/roundededittext"/>
<ImageButton
android:layout_width="40dp"
android:layout_height="45dp"
android:contentDescription="#string/symbutton"
android:id="#+id/imageButton"
android:background="#00000000"
android:layout_marginLeft="160dp"
android:layout_marginStart="160dp"
android:layout_marginTop="270dp"
android:clickable="true"
/>
searchresultsactivity.xml
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
#xml/searchable
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_searchable">
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shalini.circlelayout" >
<application
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/doraemon" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
<activity android:name=".SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
</application>
Your searchView seems to be null. How did you make it?
I am doing a Android app where I want to show the travels contact numbers. Here is the code:
App2Activity.java
package com.example.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
AppActivity.java
package com.example.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class AppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
Intent intent = new Intent(Intent.ACTION_CALL);
startActivity(intent);
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Travels and Holidays" />
</LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/travels"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Travels and Holidays Details</string>
<string name="travels">\n
Nirmala travels - 0824-2497051 \n
RR Tours and Travels - 0824-4280999 \n
Surabhii Travels - 0824-2212111 \n
</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".App2Activity" >
</activity>
</application>
</manifest>
When clicked on "Travels and Holidays" button, it shows the 3 travels names with their contact number.
I want to call that travels through the app. So if I click on particular travels, it should redirect to the call of that number.
Where am I going wrong? Please help. Thanks in advance.
AppActivity.java
public class AppActivity extends Activity {
Button button;
Context context = this;
private final CharSequence[] TRAVELS ="Nirmala travels","RR Tours and Travels","Surabhii Travels"};
String numbertodial;
String phonenumberNT ="08242497051";
String phonenumberRR ="08244280999";
String phonenumberST ="08242212111";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Dialog dialog = null;
AlertDialog.Builder builder = null;
builder = new AlertDialog.Builder(context);
String travels = getString(R.string.app_name);
builder.setTitle(travels);
builder.setSingleChoiceItems(TRAVELS, 3,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
CharSequence s = (TRAVELS[item]);
//THIS CAN BE IMPROVED. I DONT HAVE THE TIME BUT IT SHOULD STILL WORK.
if (s.equals("Nirmala travels")){numbertodial =phonenumberNT; }
if (s.equals("RR Tours and Travels")){numbertodial=phonenumberRR; }
if (s.equals("Surabhii Travels")){numbertodial=phonenumberST ;}
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:"+numbertodial ));
startActivity(callIntent);
dialog.dismiss();
}});
dialog = builder.create();
dialog.show();
return;
}
});
//REMOVE addListenerOnButton(); and it's method
}
I started off by trying to make a simple app on Eclipse.
the target for me was to create a button which simply calls a number.
however, after many tutorials and such, I still couldn't figure out where to write the code and what code was the best.
here is my Manifest . I inserted uses permission CALL_PHONE since it is required (As I learned from some tutorials)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.projectrandomfox.randomfox"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.projectrandomfox.randomfox.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
here is my activity_main
<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:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select VAS"
android:textSize="40dp"
android:layout_gravity="center" />
<Button
android:id="#+id/balance"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Check Balance"
android:layout_gravity="center"
/> </LinearLayout>
here is my activity java
package com.projectrandomfox.randomfox;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
THATS ALL.
please help me figure out where to write code in activity, and what code to write
it would mean a lot to me :)
Add this inside your onCreate method:
Button button = (Button) findViewById(R.id.balance);
myButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + telephoneNumber));
startActivity(intent);
}
}
All you need to do is set the telephoneNumber variable
First you have to get a reference your Button.
Button myButton = findViewById(R.id.balance);
Then add the onClickListener.
myButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
// Do whatever
}
}