I am trying to open an activity which will cover half screen on Home Scree.
It will be called by BroadcastReceiver which will be triggered when call ends.
Problem I am facing is it opens the MAIN activity in the background and I am not able to get Home Screen.
Here is the code:
public class IncomingCallInterceptor extends BroadcastReceiver { // 1
#Override
public void onReceive(Context context, Intent intent) { // 2
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); // 3
if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) { // 4
Intent intent_new=new Intent(context, HalfScreen.class);
intent_new.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent_new.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent_new);
}
}
}
Activity Class:
public class AddAppointmentHomeScreenActivity extends Activity {
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.add_appointment_home_screen_activity);
}
}
add_appointment_home_screen_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:windowAnimationStyle="#android:style/Animation.Translucent"
android:windowBackground="#android:color/transparent"
android:windowIsTranslucent="true"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="24dp"
android:layout_marginLeft="72dp"
android:text="Button" />
</RelativeLayout>
Manifest File:
<activity android:name=".AddAppointmentHomeScreenActivity" ></activity>
Please let me know if more details are required..
public class AddAppointmentHomeScreenActivity extends Activity {
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.add_appointment_home_screen_activity);
}
}
Related
I know this question has been asked many times but hear me out. I have MainActivity which is a LoginActivity & it uses facebook login and AccountKit From Facebook to login and start next activity which is a ContactsActivity. When i am in ContactsActivity and press back button it takes me to MainActivity but i want to close the application as user has successfully logined in and if the user again opens the app it should open ContactsActivity directly as user has signed in successfully previously.
Problem is when i press Back button in Contacts Activity,it takes me to LoginActivity,i already tried finish() after startActivity(intent) which closes my app and not takes me to ContactsActivity after successfull signin.If i open my app again then it takes me directly to ContactsActivity.
I even tried onBackPressed in which i wrote finish but it doesnt work.
In Android Manifest also i wrote android:noHistory="true" in MainActivity tag but it doesn't work.
Please help
activity_main
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="pritish.sawant.com.simplychat.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/loginwithphonenumber"
android:text="#string/loginwithphoneno"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:textColor="#android:color/white"
android:background="#drawable/login_button_selector"
android:textSize="16sp"
android:layout_centerInParent="true"
android:textAlignment="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/email_login_button"
android:text="#string/loginwithemail"
android:layout_below="#+id/loginwithphonenumber"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:textColor="#android:color/white"
android:background="#drawable/login_button_selector"
android:textSize="16sp"
android:textAlignment="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
/>
<com.facebook.login.widget.LoginButton
android:id="#+id/facebook_login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/email_login_button"
android:textSize="16sp"
android:textAllCaps="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
/>
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
public static int APP_REQUEST_CODE=1;
private LoginButton facebookloginButton;
private Button emailloginbutton,phonenologinbutton;
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
facebookloginButton=(LoginButton)findViewById(R.id.facebook_login_button);
emailloginbutton=(Button)findViewById(R.id.email_login_button);
phonenologinbutton=(Button)findViewById(R.id.loginwithphonenumber);
callbackManager=CallbackManager.Factory.create();
facebookloginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
launchContactsActivity();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
Toast.makeText(getApplicationContext(), R.string.loginfailed,Toast.LENGTH_LONG).show();
}
});
AccessToken accessToken= AccountKit.getCurrentAccessToken();
com.facebook.AccessToken loginToken= com.facebook.AccessToken.getCurrentAccessToken();
if(accessToken!=null || loginToken!=null){
launchContactsActivity();
}
emailloginbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onLogin(LoginType.EMAIL);
}
});
phonenologinbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onLogin(LoginType.PHONE);
}
});
}
private void launchContactsActivity(){
Intent intent=new Intent(getApplicationContext(),ContactsActivity.class);
startActivity(intent);
finish();
}
private void onLogin(final LoginType loginType) {
// create intent for the Account Kit activity
final Intent intent = new Intent(this, AccountKitActivity.class);
// configure login type and response type
AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
new AccountKitConfiguration.AccountKitConfigurationBuilder(
loginType,
AccountKitActivity.ResponseType.TOKEN
);
final AccountKitConfiguration configuration = configurationBuilder.build();
// launch the Account Kit activity
intent.putExtra(AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION, configuration);
startActivityForResult(intent, APP_REQUEST_CODE);
finish();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,resultCode,data);
if(requestCode==APP_REQUEST_CODE){
AccountKitLoginResult accountKitLoginResult=data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);
if(accountKitLoginResult.getError()!=null){
String toastMessage = accountKitLoginResult.getError().getErrorType().getMessage();
Toast.makeText(this, toastMessage, Toast.LENGTH_LONG).show();
} else if (accountKitLoginResult.getAccessToken() != null) {
launchContactsActivity();
}
}
}
}
In normal way you shouldn't close app - user will moves it to back by pressing home or closing your activities by pressing back.
But you can use this tips:
1) Close old activities after starting the new ones if they not needed in back stack:
startActivity(newActivityIntent);
finish();
The override onBackPressed and the app will close
2) You can move task to back (instead of close) if you need http://developer.android.com/reference/android/app/Activity.html#moveTaskToBack(boolean)
Sorry for my English.
I'm trying to use a second activity in Android, but it doesn't load. It's like the code jump it. Below, you can see the code. Thanks.
1- First Activity
public class MainActivity extends Activity
{
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.btenviardados);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helooo");
Intent intent = new Intent(MainActivity.this, com.example.seven.reader.activity_janela1.class);
startActivity(intent);
System.out.println("ebadasdadas");
}
});
}
2- Second Activity
public class activity_janela1 extends Activity
{
public void OnCreate(Bundle saveInstaceState)
{
super.onCreate(saveInstaceState);
setContentView(R.layout.activity_janela1);
}}
3- First layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm screen 1 (main.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btenviardados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me to another screen" />
4- Second layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutFormulario"
android:orientation="vertical">
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Im screen 2 (main2.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
5- AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.seven.reader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".MainActivity" >
<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="com.example.seven.reader.activity_janela1" >
</activity>
</application>
</manifest>
You have created a method within your oncreate. Remove that and leave it in onCreate.
///public void addListenerOnButton() {
button = (Button) findViewById(R.id.btenviardados);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helooo");
Intent intent = new Intent(MainActivity.this, com.example.seven.reader.activity_janela1.class);
startActivity(intent);
System.out.println("ebadasdadas");
}
});
You are misunderstanding how to use methods and scope. I recommend you do a read up on these things.
The problem is with your method addListenerOnButton().
You could define a method which will be executed when button is clicked via xml just add onClick attribute where you have defined your button.
As:-
<Button
android:id="#+id/btenviardados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me to another screen"
android:onClick="click" />
And define click method in MainActivity.java
as:-
public void click(View view)
{
System.out.println("helooo");
Intent intent = new Intent(MainActivity.this, com.example.seven.reader.activity_janela1.class);
startActivity(intent);
System.out.println("ebadasdadas");
}
Or you can add OnClickListener in you onCreate method of MainActivity.
Try this, may help you:
public class MainActivity extends Activity
{
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btenviardados);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helooo");
Intent intent = new Intent(getApplicationContext, activity_janela1.class);
startActivity(intent);
finish();
System.out.println("ebadasdadas");
}
});
}
}
This is my first time to use PreferenceActivity.
I have made a simple activity for a settings screen, and when I set up a button in the layout and set the onClickListener for the button, there are no actions nor are there any errors.
All I want to do is to send the user back to the main screen by pressing the button. Is there some error or some setup that I have missed?
The mysterious part is that neither the xml onClick nor the direct method call work. No actions, no errors, it does not even show a Toast (when altered for debugging). Is there anyone who actually used PreferenceActivity and used some widgets like buttons ?
Here is my sample activity for the Settings Screen
public class Settings extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
//This button doesn't work
Button b = (Button)findViewById(R.id.setupid);
b.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(Settings.this,HomeActivity.class);
startActivity(i);
}
});
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
Here is the xml layout for the Settings.java
<?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"
>
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3498db"
android:gravity="center"
>
<Button
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="Set"
android:id="#+id/setupid" />
</LinearLayout>
</LinearLayout>
You didn't have to use the Fragment in the first place. All you have to do is to use addPreferencesFromResource(R.xml.preferences); under setContents View.
public class Settings extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
//It is depricated but it works
addPreferencesFromResource(R.xml.preferences);
Button b = (Button)findViewById(R.id.setupid);
b.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(Settings.this,HomeActivity.class);
startActivity(i);
}
});
//getFragmentManager().beginTransaction().replace(android.R.id.content, //new MyPreferenceFragment()).commit();
}
}
I placed a Button in a layout
here is my xml code
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="#ff0000"/>
and wrote the following code in activity:
public class Basic extends Activity {
Button btn;
public void oncreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.first);
btn = (Button) findViewById(R.id.button1);
}
public void clkBtn(View v) {
Toast.makeText(this, "hai.........", Toast.LENGTH_SHORT).show();
}
}
When I run this code, I am getting white blank screen (without any button). Can any one tell me what is wrong with my code?
Update your first.xml under res->layout folder like this
<?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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clkBtn"
android:text="Click Me" />
</LinearLayout>
To get the Button event you need to first register your Button for that. The code which you have written is right but to invoke that method you have to add the property android:onClick="clkBtn" in your layout file.
OR
If you don't want to use this way then you can also explicitly invoke the event by registering your Button in your class as below:
public class Basic extends Activity {
Button btn;
public void oncreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.first);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(this, "hai.........", Toast.LENGTH_SHORT).show();
}
}
And also to launch your activity make sure have added your activity as launcher in your manifest file as below .
<activity android:label="#string/app_name"
android:name="Basic" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is My Answer. In XML file Button should be like this:
<Button
android:id="#+id/ID of you button "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name To Display Button name" />
In the MainActivity OR In any activity (in which you have called XML):
public class MainActivity extends Activity {
public void oncreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout."Your XML file in which Button is.");
Button btn = (Button)findViewById(R.id."Your button Id");
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Do your onclick program here
}
});
}
}
I made this app, it gives notification to user. Notification is just a string. Sometimes a string can be long so I tried to show that string in a pop-up layout.
Like this, my xml:
<activity
android:name="com.dot.Popup"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Dialog">
</activity>
My layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aeb25e" >
<TextView
android:id="#+id/notif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="asd"
android:padding="5dp"
android:textColor="#1D331C"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="serif" />
</RelativeLayout>
And the code:
public class Popup extends Activity {
String notif;
Intent sender;
TextView popup_notif;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.popup);
sender=getIntent();
notif=sender.getExtras().getString("notif_ana");
popup_notif = (TextView) findViewById(R.id.notif);
popup_notif.setText(notif);
}
}
So here is my problem, I can display first item of string array (notifications), when second notification comes and I click, I display the previous screen. How can I refresh the string array, or kill the activity to call next string array, etc?
Thanks I hope I explanied good enough.
To close Activity you need to call finish(). And instead of activity i'd use rather Toast (with perhaps custom layout) or one of numerous libraries providing toast alterantives. Using activity here may be slightly overkill
try getting the intent and extras in the onResume() function and update the TextView in onResume() function of the activity.
public class Popup extends Activity {
String notif;
Intent sender;
TextView popup_notif;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.popup);
popup_notif = (TextView) findViewById(R.id.notif);
}
#Override
protected void onResume() {
super.onResume();
sender=getIntent();
notif=sender.getExtras().getString("notif_ana");
popup_notif.setText(notif);
}
}