how to get out of editText after typing the text? - android

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

Related

Problems with creating a new/next Android activity

I just wanted to click on a button in the MainActivity. After I clicked on this button a new activity appears. In this activity is also a button and when I click on this button a text appears. My Problem now is, that the text doesn't appear, when I click on the button. My code for clicking on a button so that a text appears is actually working. But not if I use it in a new Activity/Layout/ page. So there must be s.th. wrong with the Connection between the first and the second activity?!
MainActivity.java:
package com.example.xxx;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;}
{}
public void page2 (View view){
setContentView(R.layout.pagetwo);
}}
activity_main.xml:
<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=".MainActivity" >
<Button
android:id="#+id/BtnKlick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="178dp"
android:text="Button"
android:onClick="page2"/>
</RelativeLayout>
Pagetwo.java:
package com.example.xxx;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Pagetwo extends Activity implements OnClickListener {
public Button btn1;
public TextView tw1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pagetwo);
btn1 = (Button) findViewById(R.id.BtnKlick);
tw1 = (TextView) findViewById(R.id.Text);
btn1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tw1.setText("Hallo");
}}
pagetwo.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/BtnKlick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Klick" />
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="87dp" />
</RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxx"
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.xxx.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>
</application>
</manifest>
Update
All you seem to be doing is swapping your layouts within PageOne activity.
Change
public void page2 (View view){
setContentView(R.layout.pagetwo);
}
to
public void page2 (View view){
Intent i = new Intent(this, Pagetwo.class);
startActivity(i);
}
This will actually launch your pagetwo activity rather than just swapping layouts within pageone activity
Make that change and all should be good. You will most likely have to add some imports, at least for the intent class
Also Activities must be registered in the manifest. To add the activity to your manifest check the code below
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.xxx.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="Pagetwo"
android:label="#string/app_name" >
</activity>
</application>
Change the label to something more appropriate than app name once you have it working
End update
Try renaming your xml view id's to meet coding standards by using all lower case letters and separating words with underscores. The Android platform may well be getting confused as Text is a reserved word.
so
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:textSize="70sp" />
becomes
<TextView
android:id="#+id/tv_hallo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:textSize="70sp" />
and your code becomes
...
public TextView tw;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.eclipse);
btn = (Button) findViewById(R.id.BtnKlick);
tw = (TextView) findViewById(R.id.tv_hallo);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tw.setText("Hallo");
}
...
Not sure if this is going to have much of an effect but it is just possible android is getting the word Text confused
Also check your logcat output to see if there are any errors.

My app stops working

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>

Simplify android classes

I'm quite new to android programming and I made my first project yesterday, a cocktail bible app. It runs perfectly but the problem is I'v loaded loads of classes and xml's to store the cocktails data. I was just wondering is there any simpler methods I could use to do away with all my unsuitable classes of the cocktails. Any help would be greatly appreciated.
Menu class
package com.drunktxtapp;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity{
String classes[] = {"Bloody_Mary", "Capirinha", "Cosmopolitan", "Cuba_Libre", "Daiquiri", "Mai_Tai", "Manhattan", "Margarita", "Martini", "Mint_Julep", "Mojito", "Old_Fashoned", "Pina_Colada", "Screwdriver", "Singapore_Sling", "Tom_Collins", "Whiskey_Sour", "White_Russian"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cocktailType = classes[position];
try{
Class<?> ourClass = Class.forName("com.drunktxtapp." + cocktailType);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
Example of one of my cocktail classes. I have many of these.
package com.drunktxtapp;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Bloody_Mary extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bloody_mary);
Button b1 = (Button) findViewById(R.id.bYoutube);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=Alt-ehDc3fc")));
}
});
}
}
One of my xml's for which I have many for each cocktail.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/beer"
android:id="#+id/bloody_mary" >
<TextView
android:textStyle="bold"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Bloody Mary"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/bloodymary" />
<TextView
android:textStyle="bold"
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Ingredients"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="40ml vodka \n\ 120ml tomato juice \n\ 5ml lemon or lime juice \n\ 5ml worcestershire sauce \n\ 2 dashes tabasco \n\ salt"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"/>
<TextView
android:textStyle="bold"
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:text="Preparation"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Squeeze the liquid out of the horseradish, then shake ingredients well with cracked ice in a chilled cocktail shaker, then strain into a Collins glass with 2 or 3 ice cubes in it; add a pinch of salt and a grind or two of fresh pepper, to taste. Garnish, if necessary, with a stalk of celery."
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"/>
<Button
android:id="#+id/bYoutube"
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="YouTube Clip"
android:textSize="20dp" />
</LinearLayout>
My manifest with some parts removed
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.drunktxtapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
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=".CocktailMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.drunktxtapp.CocktailMenu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.drunktxtapp.Menu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Bloody_Mary"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.drunktxtapp.Bloody_Mary" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You will need just one Activity and one xml Layout for all types of cocktails. (Assume that all cocktails has same layout, just the text and image are different.)
Instead of getting the class like your current approach, you will pass a parameter in the start intent.
Intent ourIntent = new Intent(Menu.this, CocktailDetailActivity.class);
ourIntent.putExtra("whichcocktail", "bloodymary");
startActivity(ourIntent);
In the destination activity's onCreate method ( Let's say CocktailDetailActivity):
// Get the param back
String cocktail = getIntent().getStringExtra("whichcocktail");
textView1.setText(cocktail);
Now you have "bloodymary" display on your textview.
In real life, you would want to pass an index to your database / array.
Then in CocktailDetailActivity, you will get the info by this index and display the correct cocktail.
All you need is two activities (ListActivity and Activity to display Cocktail detail) & two xml layouts (one for each activity).
To work with this :
1. Put all your cocktail images in drawable folder(set the name of images same as the cocktail name)
2. Start the cocktail activity when user selects an cocktail from list using Intent and pass the name of the cocktail as extra (or any other detail).
Intent intent = new Intent(Menu.this, CocktailDetailActivity.class);
ourIntent.putExtra("cocktail_name","bloodymary");
startActivity(intent);
3. In CocktailActivity's onCreate() method get the extras from intent object
String cocktailName = getIntent().getStringExtra("cocktail_name");
textView1.setText(cocktailName);
4.To set the image use the cocktailName value (if stored with same name)
ImageView imgView1 = (ImageView)findViewById(R.id.imageView1);
imageView1.setImageDrawable(getResources().getDrawable(R.drawable.cocktailName));

Application stops unexpectedly - Android

I am new to android development. After setting up an android project, I tried to get text input and passe it to another activity (screen).
When I run the project I don't get an error but when I click the application it shows the first screen then when click the button in the screen it gives the error 'application stopped unexpectedly'.
When I try the code without passing data from first screen to second screen , the application works properly.
This is MainActivity.java file:
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 MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et= (EditText) findViewById(R.id.editText1);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//intent class is used for activating another or component or an activity
Intent intent =new Intent(MainActivity.this, Second.class);
intent.putExtra("textval", et.getText().toString());
startActivity(intent);
}
});}}
Here is the code for Second.java file:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("textval"));
}
}
Here is the activitymain.xml code:
<?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">
<EditText android:id="#+id/editText1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" android:layout_alignParentTop="true"
android:ems="10"
android:inputType="text"
>
<requestFocus />
</EditText>
<Button android:id="#+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1" android:layout_marginTop="28dp"
android:text="#string/button" />
</RelativeLayout>
Here is the second xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="#string/textview" />
</LinearLayout>
Please help me to find the error. Since I don't get an error notification I am not able to proceed.
Thanks in advance...
This is what you missed on your Second.java:
setContentView(R.layout.second);
put it above of your textview declaration.
In your Second java file make changes as follows
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("textval"));
}
}
Declare Second Activity in android manifest file .
<activity android:name=".Second"/>
Add setContentView(R.layout.second); in second activity.
Add entry for second activity in your Manifest file.
Put first activity as a Launcher and other as a Default.
Here I have two activity MainActivity and Player.
First I Launches MainActivity and then call other activity Player
Note- Activity name should be same as class name, so Keep same in Manifest file as well.
Also you need to specify the layout file for both activity by setContentView in onCreate function.
Here is sample code-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vt.soc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Player"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="com.vt.soc.PALYER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
- In your Second Activity you forgot to add the setContentView().
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("textval"));
}
- Please also do see that you have added this Second Activity in your Manifest.xml file.

android button click not working in program

Hello everybody i have a problem that when i click a button it remains as it is instead of starting new activity. I searched he problem in this site and found some solutions but none of them worked for me so i am writing my problem here.
the xml layout is
<?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"
android:orientation="vertical" >
<TextView
android:id="#+id/profile_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/profile" />
<Button
android:id="#+id/create_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/create_profile" />
<Button
android:id="#+id/edit_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/create_profile"
android:text="#string/edit_profile" />
<Button
android:id="#+id/delete_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/edit_profile"
android:text="#string/delete_profile" />
<Button
android:id="#+id/activate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/delete_profile"
android:text="#string/activate" />
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/create_profile"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#id/profile_title" />
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/create_profile"
android:layout_below="#id/profile_title"
android:gravity="center_vertical|center_horizontal"
android:text="#string/no_profiles" />
</RelativeLayout>
and the activity is
package com.android.SmartSwitch;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Profile_Manager extends Activity {
private Button createButton;
private Button editButton;
private Button deleteButton;
private Button activateButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
setUpViews();
}
private void setUpViews() {
createButton = (Button)findViewById(R.id.create_profile);
editButton = (Button)findViewById(R.id.edit_profile);
deleteButton = (Button)findViewById(R.id.delete_profile);
activateButton = (Button)findViewById(R.id.activate);
createButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(Profile_Manager.this, Add_Profile.class);
startActivity(intent);
}
});
editButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
activateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
}
}
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.SmartSwitch"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".SmartSwitchActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Location_Manager" android:label="#string/app_name"/>
<activity android:name=".Profile_Manager" android:label="#string/app_name"/>
<activity android:name=".Schedule_Manager" android:label="#string/app_name"/>
<activity android:name=".Location_In_Map" android:label="#string/app_name"/>
<activity android:name=".Add_Profile" android:label="#string/app_name"/>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
When i click createButton, it doesn't respond
As your Main Activity i.e., Profile_Manager consists of the following Code:
<activity
android:label="#string/app_name"
android:name=".SmartSwitchActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You have to note if the Profile Manager class is the class which will be launched i.e., if Profile_Manager Class is the First Screen then, you should create the above code in AndroidManifest.xml with the only change as android:name=".Profile_Manager" instead of "SmartSwitchActivity" with the code additionally added for Add_Profile class as follows
<activity
android:label="#string/app_name"
android:name=".Add_Profile" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I suppose by doing this changes,things will work fine.
If you are not able to understand the above working then,Here is the Link with Perfect Working Example and with available Source Code-Correct Working of Button Click

Categories

Resources