Display Text Being Typed - java

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("");
}
});

Related

Android Intent is not working

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

Create dynamically text fields

I'm trying to get my button to create a text field where the user can input information. This way they can only create as many lines as they would like. Also, is there a way to create multiple fields at once?
So, it'll end up being something like this:
"Add Event" (rest of the screen is blank until they click on that button)
Text field 1/ Text field 2/ Text field 3
(once they press that button and of course without the underlines, just an example)
So they can put in information that they want there. If they want another row, they click on the add button again.
Am I supposed to be using an onClickListener? I'm confused as to how I would go about making the button create that field for the user.
public class BudgetScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_budget_screen);
Button addBillExpense = (Button) findViewById(R.id.addBillExpense);
addBillExpense.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
TextView inputField = new TextView(BudgetScreen.this);
}
});
}
}
That is what I have so far. I've been stuck on this for a hot minute. I am aware that I haven't used "inputField" yet.
Suppose you have the following layout xml:
<LinearLayout ...>
<Button .../>
<LinearLayout ...
android:id="#+id/holder"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
in button onClickListener you can have something like:
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
EditText et = new EditText(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layout.addView(et,lp);
You can change the LayoutParams to get the layout you like.
If you want multiple EditText in a single row, you can do the following:
final int NUM_EDITTEXT_PER_ROW = 3;
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth()/NUM_EDITTEXT_PER_ROW;
LinearLayout tempLayout = new LinearLayout(this);
tempLayout.setOrientation(LinearLayout.HORIZONTAL);
for(int i=0;i<NUM_EDITTEXT_PER_ROW;i++){
EditText et = new EditText(this);
LayoutParams lp = new LayoutParams(width,LayoutParams.WRAP_CONTENT);
tempLayout.addView(et,lp);
}
layout.addView(tempLayout);

How to make phone calls programmatically in Android?

I have the following layout defined in useful_numbers_item_fragment.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/call_linear_layout">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/useful_nums_item_name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/useful_nums_item_value"/>
</LinearLayout>
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="#drawable/call"
android:id="#+id/call_btn"
android:onClick="callNumber"/>
</LinearLayout>
I dynamically populate the two text views in a class called UNItemListFragment.java
in the onCreate method:
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
if (getArguments().containsKey(Constants.UNItem.GROUP_ID)) {
simpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.useful_numbers_item_fragment, null,
new String[]{Constants.UNItem.NAME, Constants.UNItem.VALUE},
new int[]{R.id.useful_nums_item_name, R.id.useful_nums_item_value}, 0);
setListAdapter(simpleCursorAdapter);
getLoaderManager().initLoader(0, getArguments(), this);
}
}
For each number if i click on the button i want to make a phone call by
calling the callNumber method when the user clicks the button:
public void callNumber(View view) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
TextView unItemVal = (TextView) findViewById(R.id.useful_nums_item_value);
String phoneNumber = unItemVal.getText().toString();
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
}
It is ok when I click the first button in the list, but when I click on the other buttons
it continues calling the number defined in the first row...
Any idea how to resolve this?
The problem is that this line:
TextView unItemVal = (TextView) findViewById(R.id.useful_nums_item_value);
is executed on the activity, so the findViewById will always return the first item with that id, which is likely the first item in the list.
The best way to fix this would be to override the adapter and add a tag containing the phone number to the view. A quick way to fix this would be to tag along in the view hierarchy, like so:
public void callNumber(View view) {
if( view != null ) { // view is the button tapped
View parent = view.getParent(); // this should be the LinearLayout
if( parent instanceof LinearLayout ) {
TextView unItemVal = (TextView) ((LinearLayout)parent).findViewById(R.id.useful_nums_item_value);
if( unItemVal != null ) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String phoneNumber = unItemVal.getText().toString();
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
}
}
}
}
This would find the parent for the button that was clicked, and then find the text-view containing the number within that ViewGroup.
Using findViewById() will return the first view in the activity or fragment with the specified id. If this is a ListView, it will correspond to the first row.
There are many ways to work around this problem. The quickest one (but certainly not the prettiest one, since it depends on the layout) would be to use findViewById() relative to the LinearLayout that contains the list item. Assuming that view is the ImageButton, it would be somthing like:
((View)view.getParent()).findViewById(R.id.useful_nums_item_value)
A more elegant solution would be to set a tag in the adapter's getView(), containing the data you need (in this case, the phone number to call).

Creating methods and updating strings

I am relatively new to Android development but I do have a pretty good understanding of Java and xml etc. so excuse me if this is an easy question and I just can't put two and two together.
Anyway, I am trying to have a user input a few characters into an EditText field. When they press a Button, it will call a method that will output a String. I then want this String to be displayed on the same activity as the EditText field and Button.
How do I go about taking the String variable that is the result of the method and putting into a String in the strings.xml file?
See this question. I don't think that is possible, what you probably want to do is store what the user enters in SharedPreferences.
EDIT:
To take the string variable and display it on the screen you would want to add a TextView to your layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=""/>
Then in code you will add a listener to your button to listen for click events, and have a reference to the TextView in your class:
Fields in class:
TextView tv;
Button myButton;
EditText et;
onCreate():
tv = (TextView)findViewById(R.id.textview);
myButton = (Button)findViewById(R.id.button);
et = (EditText)findViewById(R.id.edittext);
//Now set the onclick listener:
myButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(args....)
{
String txt = et.getText().toString();
tv.setText(txt);
}
});
Also check out this tutorial.

How do I change TextView Value inside Java Code?

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

Categories

Resources