Android : Buttons in the background Activity of a 'Dialog' is active? - android

I have an Activity which has an 'OK' button. And I have an 'Edit' button which will open a Dialog (theme="#android:style/Theme.Holo.Light.Dialog"). When I am in the EditDialog, I can see the OK button in the background Activity, and I can press that, and it the press is getting registered.
Is there a way to disable the background Activity actions when a Dialog is open? i.e. I want to modify things in the Dialog alone.
Edit: Adding a sample code which shows this behaviour.
Main Activity:
public class DialogTestActivity extends Activity implements OnClickListener {
private final String TAG = "DialogTest.main";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.button_open)).setOnClickListener(this);
((Button) findViewById(R.id.button_ok)).setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_ok:
Log.w(TAG, "OK Button Pressed!");
break;
case R.id.button_open:
Log.d(TAG, "Opening new Window.");
Intent intent = new Intent(this, TestDialog.class);
startActivity(intent);
default:
break;
}
}
}
TestDialog 'dialog':
public class TestDialog extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_dialog);
LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
Window window = this.getWindow();
window.setAttributes((android.view.WindowManager.LayoutParams) params);
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
}
}
Manifest:
<activity
android:name=".DialogTestActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TestDialog"
android:label="#string/dialog_label"
android:theme="#android:style/Theme.Holo.Light.Dialog" >
</activity>
With the above code, when the TestDialog was open, the Button Press on the background Activity were registered - OK Button Pressed! will be logged.

First, this is not a dialog. This is a dialog-themed activity. A dialog inherits from Dialog.
Second, your use of WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL is giving you precisely the behavior that you do not want. Delete this line of code, and things should work better.

Nothing in the underlying Activity should be clickable when a Dialog is in the foreground (this is how Dialogs in Android work). If you are able to interact with the underlying Activity when the Dialog is open, then there is probably something wrong with your implementation.
Edit:
Remove this line and all should work:
this.setCanceledOnTouchOutside(false);

If your Activity is opened with set Dialog theme use code below
setFinishOnTouchOutside(false);

Related

How to make splash screen image visible in navigation bar also?

I have an image which I want to place in splash screen in such a way that it is visible in navigation bar also at the top. Normally the image is visible in full activity except the navigation bar. But I want it to start from navigation bar also as in swiggy app splash screen.
Below is the related image:
Try this add to the manifest the theme of the first activity to exclude the action bar, and maybe even the notification bar?
Method 1
<application
android:allowBackup="true"
android:icon="#drawable/lojacidadao"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.example.basicmaponline.Intro"
android:screenOrientation="portrait"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Method 2
Another way is, do this through code in the activity. In your activity do the following before calling setContentView() function
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
the reason you have the action bar is because you have set the default theme to have it, in the application tag, so it's not an activity before yours, it's really your own splash activity.
In your splash screen activity theme in styles.xml add
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
maybe if you set activity theme none(default in manifest) and set image fill_parent, the screen is displayed like that.
Try this:
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setFullScreenView();
}
public void setFullScreenView() {
if (Build.VERSION.SDK_INT < 19) {
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
setContentView(R.layout.activity_splash); // set your view here
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(MainActivity.class);
}
}, 2500);
}
/**
* #param cls ClassName.class to start new Activity
*/
private void startActivity(Class<?> cls) {
Intent intent = new Intent(SplashActivity.this, cls);
startActivity(intent);
finish();
}
#Override
public void onBackPressed() {
}
}

App crash for no view found which is declared in landscape only

I created a Hello World app and added Landscape version of the xml.
In portrait mode I have NO TextView but in Landscape version I added a TextView. I do a findviewbyid in java for this textView
In Manifest I set the screen orientation to be landscape.
First time when I launch app: no problem. Now I press Power Off and then Power ON button and app crashes with NullPointer exception that textView is not found.
How do I make sure that app load fine in landscape mode directly without looking for resources in portrait mode?
Manifest:
<activity android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Activity:
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tv);
tv.setText("inLandscape");
}
If you don't have the TextView in portrait mode, then you'll need to change your code to:
tv = (TextView)findViewById(R.id.tv);
if(tv != null) {
tv.setText("inLandscape");
}
I'm not sure that it's possible to only ever have it load in landscape - when you power the device back on, it probably tries to come up in portrait, and then switch to landscape. I wouldn't be surprised if that behavior were device-specific and undocumented.
Add :
android:configChanges="orientation|keyboardHidden"
in your Activity.
<activity android:name=".MainActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And override the onConfigurationChanged() in your Activity
#Override
public void onConfigurationChanged(Configuration newConfig) {
// ignore orientation/keyboard change
super.onConfigurationChanged(newConfig);
}
For More Clarification see this Answer
When you change orientation, your Activiy is destroyed and recreated. You can handle this by initialising the TextView only if the phone is in ORIENTATION_LANDSCAPE:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getResources().getConfiguration().orientation == android.content.res.Configuration.ORIENTATION_LANDSCAPE) {
// Initialize your TextView here
} else {
// Handle your potrait here
}
}
You can resolve this error in many ways. You can add null checking to TextView before setting text on it as #GreyBeardedGeek said.
OR, You can add checking about device orientation using getResources().getConfiguration().orientation. If its in Landscape mode only then set Text to TextView. I have tested on my device and its fine.
Here is the working code:
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class LandscapePortraitActivity extends AppCompatActivity {
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tv);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
tv.setText("inLandscape");
}
}
}
Hope this will help~

Change theme in Android app [duplicate]

This question already has answers here:
Switching application-wide theme programmatically?
(2 answers)
Closed 7 years ago.
I want dynamically change the theme of my app with buttons, so I implemented this:
sharedPreferences = getSharedPreferences("VALUES",MODE_PRIVATE);
int theme = sharedPreferences.getInt("THEME",2);
switch (theme){
case 1: setTheme(R.style.AppTheme);
break;
case 2: setTheme(R.style.AppTheme_AppBarOverlay);
break;
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutotial);
And this is the code of the buttons:
tb1 =(Button) findViewById(R.id.button2);
tb1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sharedPreferences.edit().putInt("THEME",1).apply();
Intent intent = new Intent(tutotial.this, tutotial.class);
startActivity(intent);
}
});
tb2 =(Button) findViewById(R.id.button3);
tb2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sharedPreferences.edit().putInt("THEME",2).apply();
Intent intent = new Intent(tutotial.this, tutotial.class);
startActivity(intent);
Intent intent1 = new Intent(tutotial.this, MainActivity.class);
startActivity(intent1);
}
});
The problem is that code just changes the theme of the activity associated and it does not make the change theme in all the app.
There is an open source podcast player called AntennaPod on github. It contains example code that does this.
The way they do it is to call ContextThemeWrapper.setTheme(int) at the beginning of each Activity.onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(UserPreferences.getTheme());
super.onCreate(savedInstanceState);
......
}
This could be done in each activity, or by creating a base activity that does this for you on each subclass.
On closer reading of your question, this is exactly what you are doing. So I would say you are on the right track.
It also seems this has been asked before:
Switching application-wide theme programmatically?
Android - Change app Theme on onClick
I want my users to be able to switch from a dark and light theme for my entire app
All offering the same solution.
You can apply a theme to any activity by including android:theme inside activity inside manifest file.
For example:
<activity android:theme="#android:style/Theme.Dialog">
<activity android:theme="#style/CustomTheme">
And if you want to set theme programatically then use setTheme() before calling setContentView() and super.onCreate() method inside onCreate() method.
You can do this by implementing various themes and on click of button change the themes accordingly, refer this change theme programatically for assistance.

How to ban system soft keyboard?

I code to ban system soft keyboard.It can achieve the effct,but it disappear after one flash.I don't want this one flash.This is my code.
public class MainActivity extends Activity {
private EditText mEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEditText = (EditText) findViewById(R.id.test_et);
mEditText.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
hideSoftInputMode((EditText)v);
}
});
}
private void hideSoftInputMode(EditText editText) {
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(editText.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
In your manifest file, you can set the windowSoftInputMode to stateAlwaysHidden:
<activity
...other attributes
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
There is another way - don't lay focus on your EditText. Set it on the View at the background (the Layout) or something. This can be done by using:
setFocusable(true);
requestFocus();
This should do the trick for you without working with the Manifest etc.

Problems creating a new screen

I am creating a project in which i need to take in some numbers, makes some calculations and then on a new screen create show the answers. I am using an Intent object to go to the new screen:
final Button button = (Button) findViewById(R.id.save);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent();
myIntent.setClass(HelloAndroid.this, screen2.class);
myIntent.putExtra("eFiber", Double.toString(E_fiber));
startActivity(myIntent);
}
});
but when i do this it crashes when i click the button. If i use the same xml file as i do in the first screen then it works just fine, its when i use a different xml file that i have the problems.
Have you registered your second Activity as an Activity in the android-manifest xml?
Under the <application> node, something to the effect of:
<activity android:name=".my.screen2" android:label="#string/app_name"></activity>
With your specific Activity information in place of ".my.screen2"

Categories

Resources