Dialog fragment cannot be cast to android.app.activity - java

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.

Related

Could not find method in a parent or ancestor Context

Problem
I am getting java.lang.IllegalStateException: Could not find method secondOne(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton
What I've done?
In manifest, I have activities:
<activity
android:name=".MainActivity"
android:label="#string/app_name">
</activity>
<activity
android:name=".LeadActivity"
android:label="#string/app_name">
</activity>
<activity
android:name=".MainMenu"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In game_menu.xml, I have two buttons. First has android:onClick="firstOne" property, second has android:onClick="secondOne" property.
In class MainMenu, which extends AppCompatActivity I have two methods:
public void firstOne(View view) {
Intent intent = new Intent(MainMenu.this, MainActivity.class);
startActivity(intent);
}
public void secondOne(View view) {
Intent intent = new Intent(MainMenu.this, LeadActivity.class);
startActivity(intent);
}
When I tap on the first button, my MainActivity.class runs, as it should.
But when I tap on the second button this error appears. java.lang.IllegalStateException: Could not find method secondOne(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton
Please, help. What am I doing wrong?
UPD
full MainMenu class code:
public class MainMenu extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_menu);
//Hide the status bar and the action bar
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
}
public void firstOne(View view) {
Intent intent = new Intent(MainMenu.this, MainActivity.class);
startActivity(intent);
}
public void secondOne(View view) {
Intent intent = new Intent(MainMenu.this, LeadActivity.class);
startActivity(intent);
}}
UPD2
game_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/sky"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="-16dp" />
<Button
android:id="#+id/button_start"
android:layout_width="175dp"
android:layout_height="76dp"
android:layout_marginTop="200dp"
android:background="#color/black"
android:onClick="firstOne"
android:text="#string/playButtonText"
android:textColor="#color/white"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#+id/imageView"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_lead"
android:layout_width="175dp"
android:layout_height="76dp"
android:layout_marginTop="312dp"
android:background="#color/black"
android:onClick="secondOne"
android:text="#string/leaderboardButtonText"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#+id/imageView"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
That was terrible one. I've spent a lot of time on this, but the answer was simple. Always care about onCreate method and actually setContentView(R.layout.); line. I've been copy-pasting and tried to create classes with the same layout.

Android included layout shows in preview but not on emulator [duplicate]

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

Android O Pinned Shortcuts - CREATE_SHORTCUT intent filter

In the documentation for the new "Pinned Shortcuts" feature in Android O, they mention that "You can also create a specialized activity that helps users create shortcuts, complete with custom options and a confirmation button".
I tried following the documentation, but when I tried to create a new shortcut I only saw the default dialog, and not my activity.
Here's the declaration in the Manifest:
<activity android:name=".ShortcutActivity">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
</intent-filter>
</activity>
P.S
In the documentation they also shows an example in the Gmail app - how do I get to that screen? I wanted to see the flow but I couldn't find it.
You have to create a new activity as dialog, then you can add whatever options you want on that dialog and handle as you want the addShortcut method.
I tried that code to add and remove a dynamical shortcut and it worked.
AndroidManifest.xml
<activity android:name=".DialogActivity"
android:excludeFromRecents="true"
android:theme="#style/Theme.AppCompat.Dialog"
>
...
</activity>
DialogActivity.java
public class DialogActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
this.setFinishOnTouchOutside(false);
findViewById(R.id.add_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addShortcut();
}
});
findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
...
}
activity_dialog.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:id="#+id/activity_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.metax.myapplication.DialogActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you want to add shortcut?"/>
<Button
android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_alignParentStart="true"
android:text="No thanks"/>
<Button
android:id="#+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_alignParentEnd="true"
android:text="Add me"/>
</RelativeLayout>
In you Java class insert
ShortcutManager sM = c.getSystemService(ShortcutManager.class);
Intent intent2 = new Intent(c.getApplicationContext(), ShortcutActivity.class);
intent2.setAction(Intent.ACTION_VIEW);
ShortcutInfo shortcut2 = new ShortcutInfo.Builder(c,MSG_SHORCUT_CUSTOM)
.setIntent(intent2)
.setShortLabel("ShortLabel")
.setLongLabel("LongLaber")
.setDisabledMessage("DisabledMessage")
.setIcon(Icon.createWithResource(c, R.mipmap.ic_add_outline_short))
.build();
listshortcut.add(shortcut2);
Intent pinnedShortcutCallbackIntent = mShortcutManager.createShortcutResultIntent(shortcut2);
PendingIntent successCallback = PendingIntent.getBroadcast(context, 0, pinnedShortcutCallbackIntent, 0);
mShortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender());
sM.setDynamicShortcuts(listshortcut);
To launch your shortcut activity with action as ACTION_CREATE_SHORTCUT, long tap on app icon, and press widget icon, you ll notice a 1x1 widget, on tap of which your desired activity would be launched.
Also you can explicitly fire that intent from your app as well on some desired action.
Create a new resource file: res/xml/shortcuts.xml. This is how you create shortcuts,After you have done the nessicary changes in manifest
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="compose"
android:enabled="true"
android:icon="#drawable/compose_icon"
android:shortcutShortLabel="#string/compose_shortcut_short_label1"
android:shortcutLongLabel="#string/compose_shortcut_long_label1"
android:shortcutDisabledMessage="#string/compose_disabled_message1">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="your package"
android:targetClass="com.example.myapplication.ComposeActivity" />
</shortcut>
<!-- Specify more shortcuts here. -->
</shortcuts>
In manifest add this to the activity tag,
<meta-data android:name="android.app.shortcuts"
android:resource="#xml/shortcuts" />
If you want to read more refer this doc it explains about pinned shortcuts, static and dynamic shortcuts.
Here is an example from google, in their samples repo

How do i get a button to open a new activity in android studio?

I'd seen this asked in many, many places and have tried to follow the instructions given to no avail. I dont know if the questions are old, i'm doing things incorrectly or if my android studio program isnt working. What i want to do is to only open a new activity when a button is clicked. I'm very new to developing android applications.
I've recently tried to follow the answer provided here: How do I get a button to open another activity in Android Studio?
OnClick on the button is called "goTutorials"
My original activity is called home (Not mainActivity)
The new one is called tutorials.
This is what i added from trying to follow the link above:
In home's java file:
btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(home.this, tutorials.class));
}
});
In manifest file:
<activity
android:name="tutorials"
android:label="#string/app_name">
</activity>
Method 1
Use the onclick attribute in XML (so that whenever you click the button, defined method will trigger)
Step 1. - go to the XML where you button is(activity_home) & add
<Button
............
android:onClick="gotoTutorial"/>
Step 2. - then go to the home.Java & add following
public void gotoTutorial(View v){
Intent tutorialPage = new Intent (this, tutorials.class);
startActivity(tutorialPage);
}
Method 2
Use the setOnClickListener
Step 1. -
//create the link to the button in the interface
btn_tutorial = (Button)findViewById(R.id.tutorial_button);
btn_tutorial.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tutorialPage = new Intent (this, tutorials.class);
startActivity(tutorialPage);
}
}
I have created a sample app please see the code below.
You need to specify information of all activities in Manifest file.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nextech.startnewactivity">
<application
android:allowBackup="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" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TutorialsActivity"
android:label="#string/title_activity_tutorials"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
My Launcher activity is MainActivity.java
package com.nextech.startnewactivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startTutorials = (Button)findViewById(R.id.startTutorials);
startTutorials.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tutorialsActivityIntent = new Intent(MainActivity.this,TutorialsActivity.class);
MainActivity.this.startActivity(tutorialsActivityIntent);
}
});
}
#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);
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);
}
}
activity_main.xml
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.nextech.startnewactivity.MainActivity"
tools:showIn="#layout/activity_main">
<TextView android:id="#+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_centerHorizontal="true"
android:text="Hello World! This is main activity!" />
<Button android:id="#+id/startTutorials"
android:layout_below="#+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Start Tutorials"/>
</RelativeLayout>
TutorialsActivity.java
package com.nextech.startnewactivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class TutorialsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorials);
}
}
activity_tutorials.xml
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.nextech.startnewactivity.TutorialsActivity"
tools:showIn="#layout/activity_tutorials">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is tutorial activity"
android:layout_centerInParent="true"/>
</RelativeLayout>
I hope it helps.
Your manifest file is not displaying, but make sure you add the activity in your manifest AndroidManifest.xml in the application field. Here's an example from Google: Link
<application ... >
...
<activity
android:name="com.mycompany.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.mycompany.myfirstapp.MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MyActivity" />
</activity>
Otherwise, the method you're using should be correct. You create an intent to the class of the Activity you want to start.
Intent intent = new Intent(this, tutorials.class);
startActivity(intent);
Make sure you have the necessary methods such as onCreate(Bundle savedInstance) Overrided.
Note that in the sample above android:name="" is your activity's full path name to the class (without .class/.java) and label is going to be the action toolbar string you'll see. In the <meta-data/> section you can alter the android:value="" to the starting activity's name so that will need to change based on what you call it.
Finally, make sure it has an XML view to go with it, as it gets inflated in the onCreate(Bundle savedInstance) call.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout_here);
}
inside your activity file write this
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(home.this, tutorials.class));
}
});
XML
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
.Java class
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, YourActivityYouWantToOpen.class);
startActivity(intent);
}

Display soft keyboard automatically on a dialog activity

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);

Categories