I'm trying to call an EditText from one activity to another but when i do my app crashes.
I've tried using the addPreferencesFromResource method using 'import android.preference.PreferenceActivity' and import android.preference.PreferenceFragment' but the 'addPreferencesFromResource' stays red.
How can I make it callable ?
settings_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:id="#+id/linearLayout">
<TextView
android:layout_width="127dp"
android:layout_height="wrap_content"
android:text="website A"
android:id="#+id/textView3"
android:layout_weight="0.07"
android:layout_marginTop="115dp"
android:layout_below="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"
android:onClick="showZaire"
android:id="#+id/button5"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/webA"
android:layout_alignBottom="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3" />
<TextView
android:layout_width="127dp"
android:layout_height="wrap_content"
android:text="website B"
android:id="#+id/textView2"
android:layout_weight="0.07"
android:layout_below="#+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="34dp" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/webB"
android:layout_alignBottom="#+id/textView2"
android:layout_toRightOf="#+id/textView2"
android:layout_toEndOf="#+id/textView2" />
</LinearLayout
Second Activity
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class secondActivity extends Activity {
EditText webs = (EditText)findViewById(R.id.webA);// this is what im trying to call from settings
String web = webs.getText().toString();
TextView titleText;
String title = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seccond_layout);
Button button = (Button) findViewById(R.id.refresh);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
restartActivity(seccondActivity.this);
}
});
//initialize variables
titleText = (TextView) findViewById(R.id.titleText);
;
//run on new thread because we cannot do network operation on main thread
new Thread(new Runnable() {
#Override
public void run() {
try {
//get the Document object from the site. Enter the link of site you want to fetch
Document document = Jsoup.connect(web).get(); // this is the website string
//Get the title of blog using title tag
title = document.select("h6").text().toString();
//set the title of text view
//Run this on ui thread because another thread cannot touch the views of main thread
runOnUiThread(new Runnable() {
#Override
public void run() {
//set both the text views
titleText.setText(title);
titleText.setMovementMethod(new ScrollingMovementMethod());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
public void showZaire(View view) {
String button_test;
button_test = ((Button) view).getText().toString();
if (button_test.equals("Home")) {
Intent intent1 = new Intent(this, MainActivity.class);
startActivity(intent1);
} else if (button_test.equals("Directions")) {
Intent intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
}
}
}
settingsActivity
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.text.Editable;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by Shane on 10/01/2016.
*/
public class SettingsActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_layout);}
public void showZaire(View view) {
String button_test;
button_test = ((Button) view).getText().toString();
if (button_test.equals("Back")) {
Intent intent1 = new Intent(this, MainActivity.class);
startActivity(intent1);
} else if (button_test.equals("Help")) {
Intent intent2 = new Intent(this, Help.class);
startActivity(intent2);
}
}
}
Related
I tried to display User Input from my main activity to my second activity, but when I typed in and click the send button on the main activity the whole app just crashes. Please help!
crash solved! Thanks a lot!
However, I can't get the message to display on the second activity.
MainActivity.java
package com.example.dell_inspiron.sendmessage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import static android.R.id.message;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String message = getResources().getString(R.string.UserInput);
final Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent sendMessage = new Intent(MainActivity.this, DisplayMessageActivity.class);
sendMessage.putExtra("UserInput", message);
startActivity(sendMessage);
}
});
}
}
activity_main.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_main"
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="com.example.dell_inspiron.sendmessage.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="#string/edit"
android:ems="10"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp"
android:id="#+id/UserInput" />
<Button
android:text="#string/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/UserInput"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="14dp"
android:id="#+id/send"
android:onClick="sendMessage" />
<TextView
android:text="#string/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
DisplayMessageActivity.java
package com.example.dell_inspiron.sendmessage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent Extra = getIntent();
String textView = Extra.getStringExtra("UserInput");
TextView UserInput = (TextView) findViewById(R.id.UserOutput);
UserInput.setText(textView);
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
launchActivity();
}
});
}
private void launchActivity() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
activity_display_message.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_display_message"
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="com.example.dell_inspiron.sendmessage.DisplayMessageActivity">
<Button
android:text="#string/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp"
android:id="#+id/button2" />
<TextView
android:text="#string/UserOutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp"
android:id="#+id/UserOutput" />
<TextView
android:text="#string/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/enter"
android:textSize="18sp"
android:textColor="#android:color/holo_purple" />
</RelativeLayout>
You are passing int, but receive it in String, so your app crashed.
MainActivity
int message = R.string.UserInput;
Intent sendMessage = new Intent(MainActivity.this, DisplayMessageActivity.class);
sendMessage.putExtra("UserInput", message);
DisplayMessageActivity
Bundle Extra = getIntent().getExtras();
String textView = Extra.getString("UserInput");
You should also add
intent.putExtra("UserInput", message);
into launchActivity method.
Solution 1
Change String textView = Extra.getString("UserInput"); to int textView= Extra.getIntExtra("UserInput", 0);
Solution 2
To send the message to your second Activity, you need to use intent inside your MainActivity button click. Modify your code as below.
package com.example.dell_inspiron.sendmessage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import static android.R.id.message;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String message = getResources().getString(R.string.UserInput);
final Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent sendMessage = new Intent(MainActivity.this,DisplayMessageActivity.class);
sendMessage.putExtra("UserInput", message);
startActivity(sendMessage);
}
});
}
}
Solution 3
This line look wrong to me
final String message = getResources().getString(R.string.UserInput);
If you want to get the editText string, this is the way to go
EditText input = (EditText)findViewById(R.id.UserInput); // declare your editText
final String message = input.getText().toString(); // get your input type
You mentioned that you are getting the user input from the MainActivity but i don't see anything like that in your code but you can do the below to pass some value from one activity to another...
Change your second activity like this
Bundle Extra = getIntent().getExtras();
String textView = Extra.getString("UserInput");
to
Intent Extra = getIntent();
String textView = Extra.getStringExtra("UserInput");
and change your MainActivity like this
int message = R.string.UserInput;
to
String message= getResources().getString(R.string.UserInput);
and
final Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
launchActivity();
}
});
to
Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
startActivity(sendMessage);
}
});
There is issue with your second Activity.You are passing int value in Intents but trying to get String value.
Change it as below:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Bundle Extra = getIntent().getExtras();
int textView = Extra.getInt("UserInput");
TextView UserInput = (TextView) findViewById(R.id.UserInput2);
UserInput.setText(textView);
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
launchActivity();
}
});
}
private void launchActivity() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
I made a client program on my android and when I receive long messages it doesn't show the whole msg. I have tried to add android:orientation="horizontal" to the XML file but that didn't fix anything.
package com.example.marcus.chatclient1;
import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import static android.R.layout.list_content;
public class chat extends Activity {
Handler hanGET;
String string = "test";
String name;
EditText msgBox;
Button sButton;
ListView lv;
TextView errorT;
ObjectOutputStream o;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
final Button join = (Button) findViewById(R.id.joinButton);
final EditText nameT = (EditText) findViewById(R.id.editTextName);
msgBox = (EditText)findViewById(R.id.msgField);
sButton = (Button)findViewById(R.id.sendButton);
errorT = (TextView) findViewById(R.id.errorText);
lv = (ListView)findViewById(R.id.ChatList);
msgBox.setVisibility(View.INVISIBLE);
sButton.setVisibility(View.INVISIBLE);
lv.setVisibility(View.INVISIBLE);
join.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
name = nameT.getText().toString();
join.setVisibility(View.INVISIBLE);
nameT.setVisibility(View.INVISIBLE);
lv.setVisibility(View.VISIBLE);
msgBox.setVisibility(View.VISIBLE);
sButton.setVisibility(View.VISIBLE);
new Thread(new ClientThread()).start();
final ArrayList<String> ar = new ArrayList<String>();
final ArrayAdapter ad = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_gallery_item, ar);
lv.setBackgroundColor(Color.BLACK);
lv.setAdapter(ad);
hanGET = new Handler(){
public void handleMessage(Message message) {
ar.add(string);
ad.notifyDataSetChanged();
}
};
}
});
}
class ClientThread implements Runnable {
public void run() {
Message message;
Socket s;
try {
s = new Socket("192.168.0.15", 55555);
o = new ObjectOutputStream(s.getOutputStream());
o.writeObject(name);
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
sButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
String m = msgBox.getText().toString();
o.writeObject(m);
} catch (IOException e) {
e.printStackTrace();
}
}
});
while(true) {
try {
string = in.readObject().toString();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
message = Message.obtain();
hanGET.sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
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="com.example.marcus.chatclient1.chat">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Join"
android:id="#+id/joinButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="#+id/editTextName"
android:layout_marginBottom="31dp"
android:layout_above="#+id/joinButton"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/errorText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ChatList"
android:layout_alignParentTop="true"
android:clickable="false"
android:layout_alignBottom="#+id/joinButton"
android:choiceMode="none"
tools:listitem="#android:layout/simple_gallery_item"
tools:listfooter="#layout/activity_chat"
android:visibility="visible"
android:layout_centerHorizontal="true"
android:headerDividersEnabled="false" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/msgField"
android:layout_below="#+id/ChatList"
android:layout_toRightOf="#+id/errorText"
android:layout_toEndOf="#+id/errorText"
android:visibility="visible" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/sendButton"
android:layout_alignBottom="#+id/msgField"
android:layout_toRightOf="#+id/joinButton"
android:layout_toEndOf="#+id/joinButton"
android:visibility="visible" />
</RelativeLayout>
A ListView must have the layout_height attribute not setted to wrap_content. A ListView content height is dynamic, since you don't always have the same number of cells. Thus, you need to match the parent size or have a fixed height.
It's a simple login activity but when it must switch activity, the app crash.
I tried use also the normal form pubic void onClick() {...} but it doesn't work.
Login.java
package com.example.corrado_mattia_danny.face_offbrains;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity {
private Player player;
private Button login;
private EditText Username;
private EditText Password;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login=(Button)findViewById(R.id.button_sign_in);
Username=(EditText)findViewById(R.id.nickname);
Password=(EditText)findViewById(R.id.password);
String a = Username.getText().toString();
String b = Password.getText().toString();
player.inserisciCredenziali(a,b); //insert into a player class
//username and password
signIn();
}
public void signIn() {
Intent intent = new Intent(this, Home.class);
startActivity(intent);
}
}
activity_login.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.corrado_mattia_danny.face_offbrains.Login"
android:background="#drawable/sfondo_custom" >
<EditText
android:layout_width="wrap_content"
android:layout_height="35dp"
android:inputType="textPersonName"
android:hint="Nickname"
android:ems="10"
android:id="#+id/nickname"
android:linksClickable="false"
android:textColor="#ff000000"
android:background="#ffd4d4d4"
android:textStyle="normal|bold|italic"
android:layout_marginTop="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="35dp"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/password"
android:layout_below="#+id/nickname"
android:layout_alignStart="#+id/nickname"
android:layout_marginTop="30dp"
android:hint="password"
android:textColor="#ff000000"
android:background="#ffd4d4d4"
android:textStyle="normal|bold|italic" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/button_sign_in"
android:background="#ffffeb00"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/nickname"
android:layout_marginLeft="10dp"
android:onClick="signIn"/>
</RelativeLayout>
Home.java
the buttons in Home doesn't work yet.
package com.example.corrado_mattia_danny.face_offbrains;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class Home extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Intent intent = getIntent();
Button bottone_sfida_amico = (Button)findViewById(R.id.button_sfida_amico);
Button bottone_avversario_casuale = (Button)findViewById(R.id.button_avversario_casuale);
}
}
}
When you set a method through the android:onClick attribute, your method signIn() must have a parameter View, which is the source of the event. So, declare the method as follows:
public void signIn(View view) {
...
}
can be 2 things:
1did you declare it on the manifest?
<activity android:name=".SampleActivity" android:screenOrientation="landscape"
android:label="SampleActivity"
/>
and the other reason must be the declaration of your function, when you call it for the layout it should be like this (View v) as param
public void signIn(View v) {
Intent intent = new Intent(this, Home.class);
startActivity(intent);
}
I have two classes and a XML. The first one creates an ListApter full of cocktail names like bloody mary,margarita, ect. The second class is set up so I can change the TextView and an Image displayed in the XML. I'm having difficulty trying to pass the item that I'v pressed in the ListAdapter into the CocktailDetail class to change the values of the TextView and the Image. Can anybody help? It runs but opens Bloody mary for every item selected in the list.
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);
;
Intent ourIntent = new Intent(Menu.this, CocktailDetail.class);
ourIntent.putExtra("cocktail_name","bloodymary");
startActivity(ourIntent);
};
}
CocktailDetail class
package com.drunktxtapp;
import android.net.Uri;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.widget.TextView;
import android.view.View;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class CocktailDetail extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cocktaildetail);
ImageView imageView1 = (ImageView)findViewById(R.id.imageCocktail);
imageView1.setImageDrawable(getResources().getDrawable(R.drawable.bloodymary));
Button b1 = (Button) findViewById(R.id.buttonYoutube);
TextView t1 = (TextView)findViewById(R.id.textCocktailName);
String cocktailName = "Bloody Mary";
t1.setText(cocktailName);
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")));
}
});
}
}
XML
<?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/cocktailDetail" >
<TextView
android:id="#+id/textCocktailName"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Cocktail Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageCocktail"
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/textIngredients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Insert txt here"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
android:textStyle="bold"
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/textPrepration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Insert txt here"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<Button
android:id="#+id/buttonYoutube"
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="YouTube Clip"
android:textSize="20dp" />
</LinearLayout>
In Menu class, you are passing hard coded string :
ourIntent.putExtra("cocktail_name",**"bloodymary"**);
Change this to the value retrieved from the ListView :
String selectedFromList =(String) (l.getItemAtPosition(position));
ourIntent.putExtra("cocktail_name",selectedFromList);
Also in Cocktail Class, OnCreate, again you are setting text to hardcoded string. Using Bundle, retrieve the value and then setText;
Bundle bundle = getIntent().getExtras();
//Extract the data…
String name = bundle.getString(“cocktail_name”);
TextView t1 = (TextView)findViewById(R.id.textCocktailName);
t1.setText(name);
Try this in Menu activity:
// React to user clicks on item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
// here you get the Item selected using position
Intent i = new Intent(MenuActivity.this, CocktailDetails.class);
i.putExtra("title", your_title_cocktail);
startActivity(i);
}
});
in the DetailActivity
protected void onCreate(Bundle savedInstanceState) {
Intent i = getIntent();
---
}
give a look here and here.
I have two activities and the first is generating properly however the second is not. only the first element in the xml file will show up. here is my xml file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/alternateActivity"
android:shadowColor="#color/shadowColor"></TextView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/oldActivityButton"
android:text="#string/oldActivityButton"></Button>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/changeText"
android:text="#string/changeTextButton"></Button>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/originalText"
android:id="#+id/textToChange"></TextView>
</LinearLayout>
and here is my activity that corresponds:
package fsg.dev.activitytest;
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.TextView;
public class newActivity extends Activity {
private Button oldActivityButton;
private Button changeText;
private TextView textToChange;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alternate);
oldActivityButton = (Button) findViewById(R.id.oldActivityButton);
changeText = (Button) findViewById(R.id.changeText);
textToChange = (TextView) findViewById(R.id.textToChange);
oldActivityButton.setOnClickListener(new OnClickListener(){
public void onClick(View view){
changeActivity();
}
});
changeText.setOnClickListener(new OnClickListener(){
public void onClick(View view){
changeText();
}
});
}
private void changeActivity(){
Intent i = new Intent(this, activityTest.class);
startActivity(i);
}
private void changeText(){
if(textToChange.equals(R.string.originalText)){
textToChange.setText(R.string.newText);
} else {
textToChange.setText(R.string.originalText);
}
}
}
has anyone else seen this problem? or know of a way to fix it?
Try to add android:orientation="vertical" to your LinearLayout
replace
Intent i = new Intent(this, activityTest.class);
by
Intent i = new Intent().setClass(this, activityTest.class);