I have just started with developing apps in android, but when I am trying to create an email intent I am getting an error. The file crashes when the buttoon is clicked. The code is as follows:
MainActivity.java:-
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button myButton;
myButton = (Button)findViewById(R.id.enter);
myButton.setOnClickListener(new OnClickListener() {
#Override
private void onClick () {
submit();
}
}
);
public void submit() {
// Getting text from the EditText View
EditText editText = (EditText) findViewById(R.id.name);
String text = editText.getText().toString();
// Creating an email intent
Intent email = new Intent(Intent ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, newString[]{"youremail#gmail.com"});
email.putExtra(Intent.EXTRA_SUBJECT, text);
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
}
}
activity_main.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:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"
android:hint="Name"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/submit"
android:onClick="submit"
android:id="#+id/enter" />
</LinearLayout>
You don't need the line:
if (intent.resolveActivity(getPackageManager()) != null) {
Also, I believe that Intent.EXTRA_EMAIL takes a string array. Instead, try this:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"xyz#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, text);
startActivity(Intent.createChooser(intent, "Send Email"));
Yeah, what mattfred has answered is correct. Do as he told. And then also if that not work then remove
onClick:"Submit"
from your Button attribute.
Give your button a specfic id and initialize Button in your activity.
Button myButton;
Then activity's onCreate
myButton = (Button)findViewById(R.id.yourButtonId);
myButton.setOnClickListener(new OnClickListener(){
#Override
private void onClick(){
submit();
}
});
I think I have found a bug.
Replace your submit method as I have done
public void submit(){
// Getting text from the EditText View
EditText editText = (EditText)findViewById(R.id.name);
String text = editText.getText().toString();
// Creating an email intent
Intent email =new Intent(Intent ACTION_SEND);
email.setType("text/plain");
email.putExtra(Intent.EXTRA_EMAIL,new String[]{"youremail#gmail.com"});
email.putExtra(Intent.EXTRA_SUBJECT, text);
startActivity(Intent.createChooser(email,"Choose an Email client :"));
What you were doing wrong is you were getting text from a TextView. You have to get it from an EditText
Moreover, in your submit method you have initialized TextView with ID R.id.name. But there is no TextView in your xml and in your xml, your EditText is initialized with ID R.id.name.
More bug is found...
In your xml's Button attribute you have written
android:onClick="Submit"
But your method name is
submit() not Submit()
Remember, Java is case sensitive...
Also see, I think you can simply use
public void submit(){}
You have no reason to use
public void submit(View v){}
After all, change every code as I have told. Not only the method. You also remove
android:onClick="submit"
also from your XML's button attribute.
Hope you have understood
--Sorry for my mistyped code. Actually now I am on Mobile device
Related
friends. I started to learn Android app developinng with Android Studio and Java. Now i am trying to make one progressbar. When app is started we have two EditText fields where first is 100 by default(how will be the max value for progressbar ) and one EditText field for increment by(this is step) for progressbar. When we click Start it must show up via dialog.
I write the code, there is no more errors, but app is closing when i hit the Start Button. The progressbar is not working
This is the code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/txt_max"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Max Value" />
<EditText
android:id="#+id/maximum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100.0" />
<TextView
android:id="#+id/txt_increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Increment by"/>
<EditText
android:id="#+id/increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5.0" />
<Button
android:id="#+id/butt_Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
</LinearLayout>
This is the MainActivity.java:
public class MainActivity extends AppCompatActivity {
int increment;
ProgressDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = (Button) findViewById(R.id.butt_Start);
startButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
EditText et = (EditText) findViewById(increment);
increment = Integer.parseInt(et.getText().toString());
dialog = new ProgressDialog(MainActivity.this);
dialog.setCancelable(true);
dialog.setMessage("Loading...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
EditText max = (EditText) findViewById(R.id.maximum);
int maximum = Integer.parseInt(max.getText().toString());
dialog.setMax(maximum);
dialog.show();
Thread background = new Thread(new Runnable()
{
public void run()
{
try
{
while (dialog.getProgress() <= dialog.getMax())
{
Thread.sleep(500);
progressHandler.sendMessage(progressHandler.obtainMessage());
}
} catch (java.lang.InterruptedException e)
{
}
}
});
background.start();
}
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
dialog.incrementProgressBy(increment);
}
};
});
}
}
There are some error with that code.. I will comment each by reporting your snippet:
public class MainActivity extends AppCompatActivity {
int increment;
ProgressDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = (Button) findViewById(R.id.butt_Start);
startButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
EditText et = (EditText) findViewById(increment);
Here is the first error: not really a wrong thing, but it's better to istantiate the EditText in your onCreate (outside the onClick), because this way you will re-create the EditText every time you click the button.
The error is double: you are using findViewById(increment) where, in your code, increment is an int variable whose value is actually 0. You have to use (as you did for the button)
findViewById(R.id.increment);
Going on:
increment = Integer.parseInt(et.getText().toString());
dialog = new ProgressDialog(MainActivity.this);
dialog.setCancelable(true);
dialog.setMessage("Loading...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
EditText max = (EditText) findViewById(R.id.maximum);
int maximum = Integer.parseInt(max.getText().toString());
Here the error is xml side: since you are assuming that max.getText().toString() is an Integer, add inputType to your xml with number value.
dialog.setMax(maximum);
dialog.show();
Thread background = new Thread(new Runnable()
{
public void run()
{
try
{
while (dialog.getProgress() <= dialog.getMax())
{
Thread.sleep(500);
progressHandler.sendMessage(progressHandler.obtainMessage());
}
} catch (java.lang.InterruptedException e)
{
}
}
});
background.start();
}
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
dialog.incrementProgressBy(increment);
}
};
});
}
}
Another thing is that is better to avoid naming a variable with same name of your id, expecially because id should be representative of the control you are pointing at (for example call it "EditTextMaxValueMainActivity", this is a limit example but it is representative).
If I can suggest you, TheNewBoston channel/site is the best for tutorials, it has a couple of playlist with android basics to advanced and it is simple and really explicative. Good luck!
for other errors, we will need the logtrace
The Answer for me is here:
This line:
EditText et = (EditText) findViewById(increment);
Must be changed to:
EditText et = (EditText) findViewById**(R.id.increment)**;
And i go to design view of activity_main.xml and changed the inputType of increment EditText and maximum EditText to number.
And removed the decimal format from the numbers.
<EditText
android:id="#+id/increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
And here i had number with decimal, now i removed them.
<EditText
android:id="#+id/maximum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100" />
How can I display a Toast messages for the items I've chosen in my layout?
For example, I have 15 ImageViews that are selectable, for example I've selected ImageViews 1, 2 and 3. A toast will appear when I click a button, "You've chosen ImageViews 1, 2, 3."
By the way, I've used setTag to know when an ImageView is selected. I've setup setTag("1") for the views that is selected and setTag("0") for the rest.
Sample code that I've tried:
public void onClick(View v) {
String message = "You've chosen";
if (v.getTag().toString().equals("1")) {
message = message + " " + ivCircles[i].getId();
}
}
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
finish();
}
This line should be in onClick method of ImageView
message = message + " " + view.getId();
This line should be a global variable
public static String message = "You've chosen";
After toast displayed in onClick method of the button, the global varibale should be initiated again
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
message = "You've chosen";
set contentDescription for each image & after selecting one of the image get content discription
Toast.makeText(context,imageview.getContentDiscription(),Toast.LENGTH_SHORT).show();
You need to use the ActionMode class.
Try this way,hope this will help you to solve your problem.
activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/lnrItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
MainActivity.java
public class MainActivity extends Activity {
private LinearLayout lnrItems;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lnrItems = (LinearLayout) findViewById(R.id.lnrItems);
for (int i=1;i<=15;i++){
ImageView imageView = new ImageView(this);
imageView.setId(i);
imageView.setTag(String.valueOf(i));
imageView.setImageResource(R.drawable.ic_launcher);
imageView.setAdjustViewBounds(true);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You've chosen ImageViews "+v.getTag().toString(),Toast.LENGTH_SHORT).show();
}
});
lnrItems.addView(imageView);
}
}
}
I have a user type in information such as a UPC in the EditText box and then click a button next to it. How can I get the text from the box to pass to my other activity when they click the button? I'm assuming I use an Intent to launch the activity.
The edit text element:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:cursorVisible="true"
android:ems="10"
android:inputType="text" />
Code for search button:
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_below="#+id/textView1"
android:layout_toRightOf="#+id/textView1"
android:onClick="onClickSearch"
android:text="#string/search" />
Code in main.java:
public void onClickSearch(View view) {
String UPC = R.id.editText1.getText().toString();
Intent intent = new Intent("net.example.glutefree.Networking");
intent.putExtra("UPCA", UPC); //text is some key used to retrieve value in NextActivity
startActivity(intent);
}
If you need anymore code let me know
Ok, so you were off to a good start, the important parts are to:
1) Get a reference to the EditText object in your activity code:
private EditText mMyEditText;
which should be initialized during onCreate like this:
mMyEditText = (EditText) findViewById(R.id.editText1);
2) Handle the click:
public void onClickSearch(View view) {
//String UPC = R.id.editText1.getText().toString();
String UPC = mMyEditText.getText().toString();
//Intent intent = new Intent("net.example.glutefree.Networking");
Intent intent = new Intent(this, YourOtherClass.class);
intent.putExtra("UPCA", UPC); //text is some key used to retrieve value in NextActivity
startActivity(intent);
}
The difference is that you were calling getText on an Identifier: R.id.editText1, which is an int, where now, you will be accessing the actual instance of an EditText.
You assume correctly. This is basically how you do it depending on exactly what you have and need. But you really should read Here
String et = editText.getText().toString();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("text", et); //text is some key used to retrieve value in NextActivity
startActivity(intent);
R.id.editText1 returns an int, which is the reason for your error. Try something like this
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.name_of_your_layout);
EditText editText = (EditText) findViewById(R.id.editText1);
}
then use the code I provided originally. You have to inflate the layout using setContentView() then create a variable, editText here, you might want to declare it at the class level. Then initialize it to the id you set in your xml. The way you have it, you are trying to call getText() on the int which is what is returned by R.id.some_id. Once you have the variable and cast it to EditText with (EditText) findViewById(R.id.editText1); then you can call getText() on that variable.
Also note that you have to call setContentView() as I have shown before trying to access a View in that xml or you will get a NPE when you try to use the variable created from the xml since the View, EditText here, exists in the layout
I am trying to develop a chat room for an Android App. I have a created some area for EditText and a corresponding button to Enter the text that is typed by a user.
On clicking on Enter I want to display the typed text on the same screen i.e. whatever text is being typed, it is subsequently being displayed on the same screen. I am using Linear Layout(Horizontal) for my app.
How can I implement this ?? Can someone help me with the code. I am totally new to Android Development Framework. Thanks and Regards.
Its very simple. You create the xml file with one textView and one edittext and one button. Then you handle the event of button click in mainActivity and call onResume from it. Override the onResume so that you can update the textview.
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
TextView text = (TextView) findViewById(R.id.txtView1);
EditText editBox = (EditText)findViewById(R.id.txtBox1);
String str = text.getText().toString();
text.setText(str+" "+editBox.getText().toString());
editBox.setText("");
editBox.setHint("Type in here");
}
You can use 'Toast' to display the msg or use another 'TextView' which is set using 'setText()'
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent"android:orientation="vertical" android:layout_height="match_parent">
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout2"android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent">
<EditText...
<Button...
</LinearLayout>
</LinearLayout>
setContentView(R.Layout.main);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1); //Layout where you want to put your new dynamic TextView.
String s=editText.getText().toString(); //Fetching String from your EditText
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tv.setText(s);
ll.addView(tv); //Add TextView inside the Layout.
You can use an one editText for input and one TextView for displaying the typed message:
tvChatWindow = (TextView) findViewById(R.id.tvChatWindow);
etInputWindow = (EditText) findViewById(R.id.etInputWindow);
btnEnter = (Button) findViewById(R.id.btnEnter);
btnEnter.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// send message to other chat clients here
//add a new line break character and the typed string to Chat Window
tvChatWindow.append("\n" + etInputWindow.getText().toString());
//clear the text you have typed on the edittext
etInputWindow.setText("");
}
});
I am working on a android program. A user clicks on a button I do some math and I would like to change the values that I have on my view in some TextView objects. Can someone please tell me how to do it in my code?
I presume that this question is a continuation of this one.
What are you trying to do? Do you really want to dynamically change the text in your TextView objects when the user clicks a button? You can certainly do that, if you have a reason, but, if the text is static, it is usually set in the main.xml file, like this:
<TextView
android:id="#+id/rate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/rate"
/>
The string "#string/rate" refers to an entry in your strings.xml file that looks like this:
<string name="rate">Rate</string>
If you really want to change this text later, you can do so by using Nikolay's example - you'd get a reference to the TextView by utilizing the id defined for it within main.xml, like this:
final TextView textViewToChange = (TextView) findViewById(R.id.rate);
textViewToChange.setText(
"The new text that I'd like to display now that the user has pushed a button.");
First we need to find a Button:
Button mButton = (Button) findViewById(R.id.my_button);
After that, you must implement View.OnClickListener and there you should find the TextView and execute the method setText:
mButton.setOnClickListener(new View.OnClickListener {
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.my_text_view);
mTextView.setText("Some Text");
}
});
First, add a textView in the XML file
<TextView
android:id="#+id/rate_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/what_U_want_to_display_in_first_time"
/>
then add a button in xml file with id btn_change_textView and write this two line of code in onCreate() method of activity
Button btn= (Button) findViewById(R.id. btn_change_textView);
TextView textView=(TextView)findViewById(R.id.rate_id);
then use clickListener() on button object like this
btn.setOnClickListener(new View.OnClickListener {
public void onClick(View v) {
textView.setText("write here what u want to display after button click in string");
}
});