On pressing the send button in my code, it must move to the new activity which i created. But this is not taking place. I have the code below.
Please help. I am just a beginner in android studio.
.xml file(layout)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.example.myfirstapp.MainActivity"
android:id="#+id/relativeLayout"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<EditText
android:id="#+id/editText3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="0dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<Button
android:id="#+id/button"
android:layout_width="85dp"
android:layout_height="42dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:onClick="sendMessage"
android:text="#string/button_send"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="#string/button_send" />
main activity
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText3);
String message=editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</application>
Please help in resolving the issue. I am new to Android Studio
You have correct code, only thing you are missing is to call the startActivity call.
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText3);
String message=editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
You forgot to call this...
startActivity(intent);
Forget to call
startActivity(intent);
full code should be:
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText3);
String message=editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
here is the Official Documentation
It was a mistake on my part.
I didnt write the statement to invoke the intent.
startActivity(intent);
I know this was just silly on my part. But these mistakes can happen to anyone, I hope this helps other people.
Full Code
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText3);
String message=editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
Related
Eg. "http://www.john.com/" + "textview content from activity"
I've tried a lot of different coding, but I can't make it work. The closest I come finding the solution is a code for Google Search Query, and I've tried to modificate the code for my needs, but it doesn't work.
I have a textview named "article" which value is fetched from a database. Let's say that the output value becomes "4545", so when I click a button I want the browser to open and start at this URL "http://www.john.com/4545".
I would really appriciate the help! Thanks in advance!
MainActivity.java
package com.example.lindstreamerss.androidsqlitesearch;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textViewInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInput = (TextView) findViewById(R.id.article);
public void onSearchClick(View v) {
try {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
String term = textViewInput.getText().toString();
intent.putExtra(SearchManager.QUERY, term);
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
}
}
}
layout_item.xml
<TextView
android:id="#+id/article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="28dp"
android:fontFamily="#font/regular"
android:gravity="center_vertical|start"
android:text="4545"
android:textAllCaps="false"
android:textSize="19sp">
<requestFocus />
</TextView>
<Button
android:id="#+id/imageButton"
android:background="#color/white"
android:fontFamily="#font/regular"
android:drawableLeft="#drawable/ic_web"
android:paddingLeft="25dp"
android:textSize="15sp"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onSearchClick"
android:paddingRight="40dp"
android:text="Visa artikel" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lindstreamerss.androidsqlitesearch">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name="com.example.lindstreamerss.androidsqlitesearch.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
This simply works, on button click perform below code, and make necessary change to make it compatible with you.
String term = textViewInput.getText().toString();
String myUrl = "http://www.john.com/4545"+term;
String url = "https://developer.android.com/"+"jetpack";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
Solution if you want to import from other XML:
View inflatedView = getLayoutInflater().inflate(R.layout.YOURLAYOUT, null);
TextView text = (TextView) inflatedView.findViewById(R.id.YOURID); String
escapedQuery = text.getText().toString(); Uri uri =
Uri.parse("https://www.YOURWEBSITE.com/" + escapedQuery); Intent intent = new
Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
Solution if you're using a ViewHolder (in my case RecyclerView:
String example = ((TextView) recyclerView.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.YOURID)).getText().toString();
Uri uri = Uri.parse("https://www.YOURWEBSITE.com/" + example);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
Happy coding ;)
This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 4 years ago.
I was trying to make small application of two activities but it gives me a code error...............................................................................................................................................................................................................................................
the error
Main activity Java code:
package com.example.amr.startnewactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity
{
private Button op_btn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onClickButtonListener();
}
public void onClickButtonListener()
{
op_btn= (Button)findViewById(R.id.button);
op_btn.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(".secondActivity");
startActivity(intent);
}
}
);
}
}
Second Activity Java code:
package com.example.amr.startnewactivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class secondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
Android manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amr.startnewactivity">
<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"></activity>
<intent-filter>
<action android:name=".secondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</application>
</manifest>
Main activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="152dp"
android:layout_height="wrap_content"
android:layout_marginStart="116dp"
android:layout_marginLeft="116dp"
android:layout_marginTop="248dp"
android:text="open"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Second activity .xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".secondActivity">
</android.support.constraint.ConstraintLayout>
did you try this?
Intent intent = new Intent(MainActivity.this,secondActivity.class);
startActivity(intent);
add this code to your button click
Intent intent = new Intent(MainActivity.this,secondActivity.class);
startActivity(intent);
OR
startActivity(new Intent(MainActivity.this,secondActivity.class))
And try to follow the naming conventions in Java. Look at here
What you're looking for is Intent(Context context, Class<?> class):
startActivity(new Intent(MainActivity.this, SecondActivity.class);
Or:
Intent secondActivityIntent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(secondActivityIntent);
For more information, check out the documentation ("Start another activity").
A second note: Consider removing the <intent-filter> from your AndroidManifest.xml file for secondActivity. This declares a filter for other apps to access programmatically.
In this case, you don't actually need it if you're using an Activity. This is typically used for Intents.
I am writing a basic application which contains two activities. Both contain a TextView showing the title and the first one contains an EditText in which the user types a message and clicks on a button on its side, the second activity is launched which shows the message the user types.
It has the following problem:
1.When I click on the button, the app stops saying "Unfortunately Write n Display and stopped.", rather than launching the second activity at all.
The Logcat can be found here: enter link description here since adding it to the question exceeded the limit.
CODE OF FIRST ACTIVITY: -
package com.practice.myfirstapp1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
//import android.view.Menu;
public class MainActivity extends Activity {
public static final String key_name="com.practice.firstApp.key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
Intent intent= new Intent(this, SecondActivity.class);
EditText editText=(EditText) findViewById(R.id.EditText1_MainActivity);
String key_value= editText.getText().toString();
intent.putExtra(key_name, key_value);
startActivity(intent);
}
}
LAYOUT OF FIRST ACTIVITY: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/TextView1_MainActivity"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/title_MainActivity"
android:textStyle="bold"/>
<EditText
android:id="#+id/EditText1_MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1_MainActivity"
android:hint="#string/EditText_MainActivity"
android:textStyle="italic" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/TextView1_MainActivity"
android:layout_toRightOf="#id/EditText1_MainActivity"
android:text="#string/Button_MainActivity"
android:onClick="sendMessage"/>
</RelativeLayout>
CODE OF SECOND ACTIVITY: -
package com.practice.myfirstapp1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent= getIntent();
String intent_value= intent.getStringExtra(MainActivity.key_name);
TextView textView= new TextView(this);
textView= (TextView) findViewById(R.id.TextView2_SecondActivity);
textView.setText(intent_value);
}
}
LAYOUT OF SECOND ACTIVITY: -
<?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:orientation="horizontal"
tools:context=".SecondActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/title_SecondActivity"
android:textStyle="bold"/>
<TextView
android:id="#+id/TextView2_SecondActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
STRINGS RESOURCE FILE:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Write n Display</string>
<string name="action_settings">Settings</string>
<string name="title_MainActivity">WRITE</string>
<string name="EditText_MainActivity">Your Message here</string>
<string name="Button_MainActivity">Send</string>
<string name="title_SecondActivity">DISPLAY</string>
</resources>
ANDROID MANIFEST FILE: -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.practice.myfirstapp1"
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"
android:debuggable="true" >
<activity
android:name="com.practice.myfirstapp1.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="com.practive.myfirstapp1.SecondActivity"
android:label="#string/app_name">
</activity>
</application>
</manifest>
To solve your first issue: put below code to your textview of both xml files.
Reason: You had not put the android:layout_centerHorizontal="true" in your TextView
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/title_MainActivity"
To solve your second issue:Change LAYOUT OF FIRST ACTIVITY:
Reason: android:layout_width is "0dp". it should be wrap_content or fill_parent or match_parent
<EditText
android:id="#+id/EditText1_MainActivity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1_MainActivity"
android:hint="#string/EditText_MainActivity"
android:textStyle="italic" />
To:
<EditText
android:id="#+id/EditText1_MainActivity"
android:layout_width="wrap_content" //you can put, fill_parent or match_parent
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1_MainActivity"
android:hint="#string/EditText_MainActivity"
android:textStyle="italic" />
To solve your third issue: change your CODE OF FIRST ACTIVITY: -
Reason: You had put editText=(EditText) findViewById(R.id.EditText1_MainActivity); in sendMessage(View view). send message contains a View so whenever you initiate any component in this method, it will search that component on this particular view which is incorrect. you can initiate your components in any method which does not contain any view parameter.if you want to initiate any component in particular view then you have to initiate it like editText=(EditText)view.findViewById(R.id.EditText1_MainActivity);. But in your case edittext is in main view so you need to remove it from sendMessage(View view).
public class MainActivity extends Activity {
public static final String key_name="com.practice.firstApp.key";
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.EditText1_MainActivity);
}
public void sendMessage(View view){
Intent intent= new Intent(this, SecondActivity.class);
String key_value= editText.getText().toString();
intent.putExtra(key_name, key_value);
startActivity(intent);
}
}
Change your second Activity
Reason: TextView textView= new TextView(this); is not correct way to initialize any component of your xml view.
TextView textView= new TextView(this);
textView= (TextView) findViewById(R.id.TextView2_SecondActivity);
textView.setText(intent_value);
To:
TextView textView;
textView= (TextView) findViewById(R.id.TextView2_SecondActivity);
textView.setText(intent_value);
you can solve your problem like this
1.replace
android:gravity="center_horizontal" by android:layout_gravity="center_horizontal".
2.Give layout_width as wrap_content for edittext of first activity xml
3.Make your sendMessage method public not private
To get the logcat In Eclipse, Goto Window-> Show View -> Other -> Android-> Logcat.
OR
Write LogCat in Quick Access edit box in your eclipse window (top right corner, just before Open Prospective button). And just select LogCat it will open-up the LogCat window in your current prospec
change private void sendMessage(View view) to public void sendMessage(View view) (private to public)
LAYOUT OF FIRST ACTIVITY:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<TextView
android:id="#+id/TextView1_MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+string/title_MainActivity"
android:textStyle="bold"/>
<EditText
android:id="#+id/EditText1_MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/EditText_MainActivity"
android:textStyle="italic" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button_MainActivity"
android:onClick="sendMessage"/>
LAYOUT OF SECOND 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:orientation="horizontal"
android:gravity="center_vertical"
tools:context=".SecondActivity">
<TextView
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+string/title_SecondActivity"
android:textStyle="bold"/>
<TextView
android:id="#+id/TextView2_SecondActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Logcat shows the following:
Caused by: android.content.ActivityNotFoundException: Unable to find
explicit activity class
{com.practice.myfirstapp1/com.practice.myfirstapp1.SecondActivity};
have you declared this activity in your AndroidManifest.xml?
You did define SecondActivity in manifest, but the package name is wrong ( android:name="com.practive.myfirstapp1.SecondActivity" should be ndroid:name="com.practice.myfirstapp1.SecondActivity"
).
<activity
android:name="com.practice.myfirstapp1.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="com.practive.myfirstapp1.SecondActivity"
android:label="#string/app_name">
</activity>
It should be
<activity
android:name="com.practice.myfirstapp1.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="com.practice.myfirstapp1.SecondActivity"
android:label="#string/app_name">
</activity>
i have prepared a entry screen for my program and where i have put an edittext and also ImageViews.Now the problem is that m able to type the text inside the edittext through the virtual keyboard but the problem is that after i m done with my text then i m not able to come out of the edittext,the cursor remains at the edittext box only.
i have a imageView below to be clicked to proceed with the form but it is nt clickable at that momen..please help!!!
my code is
my xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/begin_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170dp"
android:layout_marginTop="220dp"
android:src="#drawable/begin" />
<ImageView
android:id="#+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="30dp"
android:src="#drawable/enter_name" />
<ImageView
android:id="#+id/roof"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/roof" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="180dp"
android:layout_marginTop="180dp"
android:ems="8"
android:inputType="textMultiLine"
android:imeOptions="actionDone"/>
</FrameLayout>
my main java class
package game.pack;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.*;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Begin extends Activity implements View.OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.entry);
ImageView begin = (ImageView) findViewById(R.id.begin_);
begin.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.begin_:
setContentView(new SushiMain(this));
break;
default:
break;
}
}
}
my android manifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="game.pack"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".FrontScreen"
android:screenOrientation="landscape"
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=".Begin"
android:screenOrientation="landscape"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="begingame" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Sushitap"
android:screenOrientation="landscape"
android:label="#string/app_name" >
<intent-filter>
<action android:name="gameover" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
use View.OnKeyListener for EditText to remove Focus from EditText when ENTER KEY PRESSED after typing in EditText :
ditText.setOnKeyListener(onSoftKeyboardDonePress);
private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
// code to hide the soft keyboard
EditTexst.clearFocus();
EditTexst.requestFocus(EditText.FOCUS_DOWN);
}
return false;
}
};
Try this.
editTextId.clearFocus();
editTextId.requestFocus(EditText.FOCUS_DOWN); // assuming that you want to move the focus to the next View Element. If you want it to go to the previous View, use FOCUS_UP
Can you try putting this in layout file for edittext?
android:focusableInTouchMode="true"
If your app requires an edit text and an image view next to it..then here's the code u can use.
In layout.xml u can add this code for edittext with image
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white2" >
<EditText
android:id="#+id/et_search"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:drawableRight="#drawable/search_20"
android:ems="10"
android:hint="#string/search_brdNm" />
</LinearLayout>
and in your java class you can have this code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_asset);
mEt_search = (EditText) findViewById(R.id.et_search);
mEt_search.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String str_searchTxt = mEt_search.getText().toString().trim();
String str_channelNm = AppConfigurationUtils.getChannelName();
if (str_searchTxt.length() == 0) {
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.ENTER_MANDATORY),
Toast.LENGTH_LONG).show();
return;
}
}
});
}
HEY guys my problem is solved!!!
actually i was moving from some other view to this view and in the first view i was calling the present class activity by its layout Id so in this case the imageview was not behaving as a normal button but as just a view
so in the first view i used an intent to call the class of the activity which i hav posted above and this successfully completed my task!!!
Thank you everyone for your time and efforts...much appreciated!!!!
THANK YOU!!!
You need to link your edittext in your xml to your code. In your oncreate method add
EditText edittext= (EditText ) findViewById(R.id.editText2);
So I know it has to do with my "startActivity( myIntent )" line of code. Here is my first Activity file
public class Commander2 extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "AccessCodeEntry";
private static final String LEVEL_ONE_ACCESS_CODE = "derp";
#Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.accesscodeentry );
}
protected void onDestroy() {
super.onDestroy();
}
public void accessCodeEntered( View v ) {
EditText loginPassword = (EditText) findViewById( R.id.editText1 );
if( loginPassword.toString().equals( LEVEL_ONE_ACCESS_CODE ) ) {
Intent myIntent = new Intent( Commander2.this, LevelOne.class );
startActivity( myIntent );
} else {
Intent myIntent = new Intent( Commander2.this, LevelZero.class );
startActivity( myIntent );
}
}
}
Here is the XML file as well.
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="accessCodeEntered"
android:text="OK" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="19dp"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText1"
android:layout_alignLeft="#+id/editText1"
android:text="Enter Access Code, or leave blank for Level 0"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
My code just exits as soon as I enter a password and press the OK button, no matter what the output. I have commented out the startActivity( myIntent ) lines and the code finishes without crashing. Does anyone know what I'm doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.eti"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<activity android:name=".Commander2"
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=".LevelOne"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LevelZero"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
EDIT : i am updating my answer
so finally i got where you were going wrong
check your if condition
in accessCodeEntered() method
of Commander2.java Activity
if(loginPassword.toString().equals(LEVEL_ONE_ACCESS_CODE)){}
here you are not getting the value of password and try to use it
so i added "getText()" method and its working fine like this
if(loginPassword.getText().toString().equals(LEVEL_ONE_ACCESS_CODE)){}
Commander2.java
package my.eti;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Commander2 extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final String TAG = "AccessCodeEntry";
private static final String LEVEL_ONE_ACCESS_CODE = "derp";
private Button btnOK;
private EditText passTxt;
private String strPass;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accesscodeentry);
passTxt = (EditText) findViewById(R.id.editText1);
btnOK =(Button) findViewById(R.id.button1);
btnOK.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==btnOK){
/*... here is the problem we havent added getText() to get value from EditText...*/
if(loginPassword.getText().toString().equals(LEVEL_ONE_ACCESS_CODE)){
Intent myIntent = new Intent( Commander2.this, LevelOne.class );
Toast.makeText(getBaseContext(), "if condition is true..", Toast.LENGTH_SHORT).show();
startActivity( myIntent );
}
else{
Intent myIntent = new Intent( Commander2.this, LevelZero.class );
Toast.makeText(getBaseContext(), "else condition is true..", Toast.LENGTH_SHORT).show();
startActivity(myIntent);
}
}
}
}
try to implement this code and where you going wrong.
still you get any trouble share with me Thanks.
Please post your logcat errors, but my guess is to make sure that LevelOne and LevelZero are in your manifest file. Also, it might help to change this:
loginPassword.toString().equals( LEVEL_ONE_ACCESS_CODE )
to:
loginPassword.getText().toString().equals( LEVEL_ONE_ACCESS_CODE )
have you to register LevelOne and LevelZero in manifest file
Here is a link to a similar SO question that shows how to add the activities to your manifest.
Add a new activity to the AndroidManifest?