I have a simple Activity that uses a android:theme="#android:style/Theme.Dialog" in the manifest.
My activity consists of an EditText, 2 Buttons, and a TextView. It is nothing more than a box for the user to enter in a name and press OK/Cancel.
I just want to focus the EditText and have the soft keyboard automatically show when the Activity is started. I've read countless posts about this but I just can't seem to get it to work. When the activity starts the blinking cursor appears in the EditText, but the keyboard won't show until I click inside it.
Here is my Activity:
public class Finalize extends Activity {
private EditText mEditName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.finalize_dialog);
mEditName = (EditText) findViewById(R.id.file_name_edit);
mEditName.setFocusable(true);
mEditName.requestFocus();
mEditName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
});
}
}
I've also tried this in onCreate:
InputMethodManager mgr = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
mgr.showSoftInput(mEditName, 0);
Edit: My manifest for reference
<activity class=".Finalize"
android:name=".Finalize"
android:label="#string/file_name_title"
android:theme="#android:style/Theme.Dialog"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysVisible">
</activity>
The following should work. Go to your manifest and update your activity line with the android:windowSoftInputMode attribute.
<activity android:name=".Finalize"
android:windowSoftInputMode="stateAlwaysVisible">
...
</activity>
See the following documentation page for more details on the different parameters that can be passed into this attribute.
I tested the above and it works fine for me. Here is my terribly simple example.
Code:
public class DialogActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<EditText android:id="#+id/edit_text_test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Manifest:
<activity android:name=".DialogActivity"
android:windowSoftInputMode="stateAlwaysVisible"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Try this its work for me
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
Related
Hello!
I'm just starting in Android Studio. I searched for a matching question but no joy, please yell out if you've seen this one already!
My main activity has a single button which opens the second activity, the button works and it opens. But the second activity shows as a blank screen instead of the text that should be there.
Apologies for any irrelevant copy/paste!
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.NoActionBar" >
<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=".fiveThreeOne"
android:label="#string/title531">
</activity>
</application>
Main activity:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:textColor="#ffdedede"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fiveThreeOne"
android:textAllCaps="false"
android:id="#+id/open531btn"
android:layout_below="#+id/mainTitle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
</RelativeLayout>
Button code in main class
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button open531button = (Button) findViewById(R.id.open531btn);
open531button.setOnClickListener(new OnClickListener(){
public void onClick(View v){
startActivity(new Intent(MainActivity.this, fiveThreeOne.class));
}
});
}
Second activity xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/title531"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:editable="false"
android:textSize="35sp"
android:id="#+id/title531"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Thanks!
It sometimes happens when instead of overloading
protected void onCreate(Bundle savedInstanceState) {
you end up overloading
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState)
instead.
Make sure the onCreate with only savedInstanceState as a argument is overloaded.
Have you set the layout for your activity in the line setContentView() like this. Here activity_second is the activity layout of my SecondActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
if not then your activity will show up as blank.
Hope this solves your problem.
Check weather you have set the correct layout for the setContentView() method of your second activity.
You can just go to dependencies in build.gradle file and add this line there.. & try.
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
In my case, I changed device in Android Virtual Divice Manager and this fix problem
Why do my android apps keep closing when using 2 intent? when I delete one of them, it's work. especially if I delete the "Reg" Button in the java files.
Thanks
XML Files:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".LoginAct">
<TextView
android:id="#+id/daftar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/daftar"
android:textAlignment="center"
android:textColor="#color/Linktext"
android:clickable="true"
android:focusable="true"
android:layout_marginTop="10dp">
</TextView>
<Button
android:id="#+id/pass"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:background="#color/colorPrimaryDark"
android:clickable="true"
android:focusable="true"
android:text="#string/lewat"
android:textColor="#color/textwhite" />
Java Files (If I deleted the "Reg" Button from this file, the app is working, but otherwise it will force close after the splash screen):
package com.soerja.ngalamhistory;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class LoginAct extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_login);
Button Pass = (Button) findViewById(R.id.pass);
Button Reg = (Button) findViewById(R.id.daftar);
Pass.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginAct.this, MainActivity.class);
startActivity(intent);
}
});
Reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginAct.this, RegisAct.class);
startActivity(intent);
}
});
}
}
AndroidManifest Files:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.soerja.ngalamhistory">
<application
android:allowBackup="true"
android:icon="#mipmap/logo"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashAct"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginAct"
android:label="#string/applogin"
android:theme="#style/LogTheme"></activity>
<activity
android:name=".MainActivity"
android:label="#string/home"
android:theme="#style/AppTheme"></activity>
<activity android:name=".RegisAct"
android:label="#string/appdaftar"
android:theme="#style/LogTheme"></activity>
</application>
</manifest>
I would really appreciate your help because this is my school project :)
You are casting a TextView into a Button. Probably the problem is a invalid cast exception.
In XML, you define the "daftar" as a TextView, but in java you are trying to use it as a Button.
Change it
Button Reg = (Button) findViewById(R.id.daftar);
To it
TextView Reg = (TextView) findViewById(R.id.daftar);
Or change your XML implamentation, defining "daftar" as a Button
here you are using your text view id
android:id="#+id/daftar"
as a reference to declaring button
Button Reg = (Button) findViewById(R.id.daftar);
so i think ,that may be the reason for crash
try this method :
add this in your text xml code:
android:onClick="daftar"
add for java class use this:
public void daftar(View v)
{
Intent intent = new Intent(this, RegisAct.class);
startActivity(intent);
}
and remove the old java code you used for this text view:
i hope this helps.
I am recreating the custom dialog layout on Android Developers. with minor tweaks. I am getting an issue telling me that the dialogfragment cannot be cast to android.app.activity. I am having trouble understandin why I am gettin this error in the logcat.
Logcat:
01-20 22:03:10.317: E/AndroidRuntime(9949): FATAL EXCEPTION: main
01-20 22:03:10.317: E/AndroidRuntime(9949): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.customdialogfragment/com.customdialogfragment.CustomDialogFragment}: java.lang.ClassCastException: com.customdialogfragment.CustomDialogFragment cannot be cast to android.app.Activity
01-20 22:03:10.317: E/AndroidRuntime(9949): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2004)
Activity
public class CustomDialogFragment extends DialogFragment {
static CustomDialogFragment newInstance() {
CustomDialogFragment newFragment = new CustomDialogFragment();
return newFragment;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(
inflater.inflate(R.layout.activity_custom_dialog_fragment, null))
// Add action buttons
.setPositiveButton("Sign In",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CustomDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
public void showMyDialog() {
CustomDialogFragment newFragment = new CustomDialogFragment();
newFragment.show(getFragmentManager(), "custom");
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.customdialogfragment"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.customdialogfragment.launcher"
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>
</manifest>
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#FFFFBB33"
android:contentDescription="#string/app_name"
android:scaleType="center"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:hint="username"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:fontFamily="sans-serif"
android:hint="password"
android:inputType="textPassword" />
</LinearLayout>
If you want to show your custom dialog fragment, you don't need to define it in the Manifest, rather you need to create an instance and call its show method in your activity. That's why you are receiving that error. You have in your manifest an activity defined: com.customdialogfragment.launcher, you need to create that activity and then create the fragment inside the activiy.
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class launcher extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
showMyDialog();
}
public void showMyDialog() {
CustomDialogFragment newFragment = new CustomDialogFragment();
newFragment.show(getSupportFragmentManager(), "custom");
}
}
*Note: you should probably rename the activity MainActivity or something equivalent
DialogFragment is a child of Fragment, so it cannot to be cast to Activity.
3 ways to create a dialog.
use AlertDialog, which extends Dialog. Actually it is a Window.
use DialogFragment, which extends Fragment.
use a dialog style Activity, as the document says.
Showing an activity as a dialog on large screens
Instead of showing a dialog as a fullscreen UI when on small screens, you can accomplish the same result by showing an Activity as a dialog when on large screens. Which approach you choose depends on your app design, but showing an activity as a dialog is often useful when your app is already designed for small screens and you'd like to improve the experience on tablets by showing a short-lived activity as a dialog.
To show an activity as a dialog only when on large screens, apply the Theme.Holo.DialogWhenLarge theme to the manifest element:
<activity android:theme="#android:style/Theme.Holo.DialogWhenLarge" >
This one need definition in manifest.
this is my codes LoginActivity.java file
package com.example.crims;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
public class LoginActivity extends Activity {
TextView screen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
screen = (TextView) findViewById(R.id.link_to_register);
// Listening to register new account link
screen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
}
}
this is my code for 'activity_login.xml' file
<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=".LoginActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>`
And Finally this is my menifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.crims"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.crims.LoginActivity"
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=".RegisterActivity"
android:label="Register New Account">
</activity>
</application>
</manifest>
All of these files are here and i want to find my error as I m new to android app development...
You have NullPointerException(NPE) here:
10-01 21:02:07.580: E/AndroidRuntime(1299):
at com.example.crims.LoginActivity.onCreate(LoginActivity.java:17)
So, check line 17 of LoginActivity.java (you can just double click on this message in the logcat view and you will be navigated to this line).
It seems, line 17 is this:
registerScreen.setOnClickListener(new View.OnClickListener() {
As this is NPE, registerScreen is null. So, you should inspect why it is null. This is because Activity can't find it in line above and it returns null instead:
TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
This can be one of two possibilities: either you do not have widget with id 'link_to_register' in activity_login.xml, or something other is wrong :)
Check this please and show your activity_login.xml file, please.
Listen brother take this code. Change it according to you.
public class MainActivity extends Activity {
TextView screen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen = (TextView) findViewById(R.id.screen);
// Listening to register new account link
screen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), Register.class);
startActivity(i);
}
});
}
}
You need to register your activity in manifest.xml file too in case if you don't know about it.
I just started working on this application I feel like I've done everything right (This isn't my first application I've been working on) but when I try to run it It crashes?? It gives me no errors.
Here's the code from the main activity, then from manufest and then from the xml layout.
`
package com.theory.game;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GameTheoryActivity extends Activity implements View.OnClickListener{
Button play, settings;
int i;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play.setOnClickListener(this);
settings.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bPlay:
Random irand = new Random();
i = irand.nextInt(4);
switch(i){
case 0:
Intent GoMillionair = new Intent(GameTheoryActivity.this, millionair.class);
startActivity(GoMillionair);
return;
case 1:
return;
case 2:
return;
case 3:
return;
}
return;
case R.id.bSettings:
return;
}
}
}
Manufest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.theory.game"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application >
<activity
android:name=".GameTheoryActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".millionair" >
<intent-filter>
<action android:name="com.theory.game.MILLIONAIR" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml
<?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:background="#drawable/backgroundimage"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="390dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/bPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.38"
android:background="#drawable/play" />
<Button
android:id="#+id/bSettings"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_weight="0.36"
android:background="#drawable/settings" />
</LinearLayout>
</LinearLayout>
`
And I have another class called millionair which is just a normal class nothing much, nothing wrong there I guess. Please check whats up with this and let me know why my application wont start saying it "has stopped working".. Thanks
My guess is that you are getting a NullPointerException because both play and settings objects are null.
change your onCreate() to look like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (Button)findViewById(R.id.bPlay);
settings = (Button)findViewById(R.id.bSettings);
play.setOnClickListener(this);
settings.setOnClickListener(this);
}
You'll probably get a NullPointerException because you forgot to assign the buttons to your fields (Button play, settings;).
Here's an example on how to assign them:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (Button) findViewById(R.id.bPlay);
settings = (Button) findViewById(R.id.bSettings);
}
Looks like play and settings will both be null when you try to use then during the onCreate() method:
play.setOnClickListener(this);
settings.setOnClickListener(this);
make sure to initiate them like so:
play = (Button)findViewById(R.id.bPlay);
settings = (Button)findViewById(R.id.bSettings)
play.setOnClickListener(this);
settings.setOnClickListener(this);