This question already has answers here:
Moving from one activity to another Activity in Android
(14 answers)
Floating Action Button does not responds when it clicked/touched
(3 answers)
Closed 5 years ago.
I have two activities, trying to navigate from the first one to the second one by pressing a button. First one is like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_new_message);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(myIntent);
}
});
}
And the second one is like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// other stuff
}
When the Floating Action Button is pressed then, absolutely nothing happens. What is wrong with my code?
Update: This is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="...">
<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=".firstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="secondActivity"></activity>
</application>
</manifest>
And this is the code for Floating Action Button:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//Button button = (Button) findViewById(R.id.button);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(getApplicationContext(), "Pressed!", Toast.LENGTH_LONG);
toast.show();
}
});
In XML:
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:id="#+id/fab"
android:tint="#android:color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
app:fabSize="mini"
android:layout_alignParentRight="true" />
The whole layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:id="#+id/fab"
android:tint="#android:color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
app:fabSize="mini"
android:layout_alignParentRight="true" />
<ListView
android:id="#+id/list_of_messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:divider="#android:color/transparent"
android:dividerHeight="16dp"
android:focusable="auto"
android:focusableInTouchMode="true" />
</RelativeLayout>
Use Below code to resolve your problem, working for me :
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();
}
});
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
Try changing to this inside your Manifest.xml file:
<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=".FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
EDIT-
To bring FAB to front use:
fab.bringToFront(); before setting onClickListener to it.
Thanks for pointing out Mike M
Related
I'm making an android app on Android Studio. And I come out with a problem . When I click my ImageButton the app crashes instead of going to a new activity (Slide1)...
I dont know where I got it wrong .
Here is my MainActivity.java
package android.example.ptreinas;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
private ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
openSlide1();
}
});
}
public void openSlide1(){
Intent intent = new Intent(this, Slide1.class);
startActivity(intent);
}
}
and my activity_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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity"
android:background="#color/red">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="400px"
android:layout_gravity="center"
android:src="#drawable/gym" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Programa de treinos "
android:layout_gravity="center"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="80px"
android:layout_marginTop="50px"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Escolha o programa de treino mais adequado aos seus objetivos."
android:layout_gravity="center"
android:textAlignment="center"
android:layout_marginTop="50px"
android:textSize="50px"
android:textColor="#ffffff"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:src="#drawable/ic_right"
android:background="#drawable/roundcorner"
android:padding="50px"
android:layout_gravity="center"
android:layout_marginTop="150px"
/>
</LinearLayout>
</RelativeLayout>
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.example.ptreinas">
<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>
</application>
</manifest>
I dont know if you need the second activity (slide1.xml , Slide1.java) let me know
I need your help, thanks in advance .
replace this :
Intent intent = new Intent(this, Slide1.class);
by this:
Intent intent = new Intent(MainActivity.this, Slide1.class);
add your second activity in manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.example.ptreinas">
<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>
//add this line
<activity android:name=".Slide1"/>
</application>
I just want to switch activity with a simple button
Button test = findViewById(R.id.please);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("MainActivity","I pass there ");
Intent intent = new Intent(MainActivity.this, TestActivity.class);
startActivity(intent);
Log.d("MainActivity","I pass there too");
}
});
I don't have an error, I just arrive on a white screen and not on my new activity. Logs appear in my console. I also have a log when I create my second activity but this one doesn't appear.
for more code,
This is my 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">
<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=".HomeActivity">
</activity>
<activity android:name=".ScheduleActivity"></activity>
<activity android:name=".MapActivity"></activity>
<activity android:name=".TestActivity"
android:theme="#style/YourTheme">
</activity>
</application>
TestActivity :
public class TestActivity extends AppCompatActivity {
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_test);
Log.d("TestActivity","I arrived in Test Activity !!");
}
}
the button in activity_main :
</android.support.design.widget.AppBarLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="wtf"
android:background="#color/colorAccent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/please"
/>
Activity_test :
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_map_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello there! :D"/>
You are Overriding wrong onCreate method in TestActivity. You have to override this method:
protected void onCreate(#Nullable Bundle savedInstanceState) {}
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 just set an onClick method which is written in the activity class to be called whenever the button is clicked. Even before it enters the method, the app crashes.
Error message that I am getting when the floating action button is clicked;
java.lang.IllegalArgumentException: Expected receiver of type com.example.bob.vexteamqueuing.AdminControl,
but got android.support.v7.view.ContextThemeWrapper java.lang.IllegalArgumentException:
Expected receiver of type com.example.bob.vexteamqueuing.AdminControl, but got android.support.v7.view.ContextThemeWrapper
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
XML Code for the activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/NoActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_admin_control" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_input_add"
android:onClick="createNewTournament"
android:clickable="true" />
AdminControl activity Java code:
public class AdminControl extends AppCompatActivity {
Firebase ref;
public static List <Tournament> tournaments;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_control);
Toolbar b = (Toolbar) findViewById(R.id.toolbar);
b.setTitle("Tournaments");
setSupportActionBar(b);
ref = AdminLogin.firebase.child("users").child(AdminLogin.firebase.getAuth().getUid());
if (tournaments == null){
tournaments = new ArrayList<>();
}
}
public void createNewTournament(View v) {
Intent newIntent = new Intent(this, TournamentCreator.class);
startActivity(newIntent);
}
}
On Create - Tournament Creator
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tournament_creator);
/*b = (FloatingActionButton) findViewById(R.id.fab);
b.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
beginTournament(v);
}
});*/
}
Manifest file - may not be necessary but just provided
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:name=".VEXQueuing"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Initial">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AdminLogin"
android:label="#string/title_activity_admin_login"
android:parentActivityName=".Initial">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.bob.vexteamqueuing.Initial" />
</activity>
<activity android:name=".TournamentCreator" />
<activity
android:name=".AdminControl"
android:label="#string/title_activity_admin_control"
android:parentActivityName=".Initial"
android:theme="#style/NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.bob.vexteamqueuing.Initial" />
</activity>
</application>
Your problem should be solved removing your android:onClick="createNewTournament" event from your layout
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_input_add"
android:clickable="true" />
And adding a listener to R.id.fab in your onCreate, like this.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_control);
Toolbar b = (Toolbar) findViewById(R.id.toolbar);
b.setTitle("Tournaments");
setSupportActionBar(b);
ref = AdminLogin.firebase.child("users").child(AdminLogin.firebase.getAuth().getUid());
if (tournaments == null){
tournaments = new ArrayList<>();
}
FloatingActionButton myFab = (FloatingActionButton)findViewById(R.id.fab);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
createNewTournament(v);
}
});
}
The same problem happen in this question EditText OnClick Exception and was fixed using listener.
Hope this helps!!
The previous solutions (by #GueorguiObregon and #MuhammadFaisalHyder) work but were not the thing that I was wishing. I found out the problem came from setting the android:theme attribute to the view (in my case), and also is related to the AppCompat library (see this).
So I simply removed the android: namespace from this line (from the view's style):
<item name="android:theme">#style/someTheme</item>
and made it likes:
<item name="theme">#style/someTheme</item>
and it works fine.
The amazing thing is the problem is only on the high-level APIs (23 I tested) and on low-level APIs (16 and 19 I tested) both ways (with or without android: namespace) work.
Also, see #MateiSuica comment below if you want to insert theme directly to the element (without using a style).
why not doing it the typical way? Try normally like by Declaring
public class AdminControl extends AppCompatActivity {
//....
FloatingActionButton mFAB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_control);
Toolbar b = (Toolbar) findViewById(R.id.toolbar);
//....
mFAB = (FloatingActionButton) findViewById(R.id.fab);
mFAB.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent newIntent = new Intent(this, TournamentCreator.class);
startActivity(newIntent);
}
});
}
And use XML for floating button like this:
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_input_add"
/>
This is the common way and clean way to do it, hope i helped.
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
}
}