I'm trying to program an android App with AndroidStudio. It is like an calculator and i don't know how to print the answer on my screen. I tried it with System.out.println() but it doesn't work Log._() doesn't work too.
You can display a temporary pop-up message to the screen using a Toast as below:
Toast.makeText(context,"<Message here>", Toast.LENGTH_LONG).show();
If you want the text to persist, then use a TextView object. Add it to your .xml for that activity:
<TextView
android:id="#+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Then declare it in your Activity screen:
TextView myTextView = (TextView) findViewById(R.id.myTextView);
Then set the text using .setText(), probably in the onClick() method of whatever button you're using to calculate the result for the user:
myTextView.setText(calculatorResult);
Related
I am making an android app where the same imageview is displayed every time the user enters a value. but I want the imageview to be changed every time the user enters a value. I wrote a code to do this but the problem is after the first image is displayed I face black screen and it takes me to the first page in the app, it doesn't crash just shows black screen.
here is the imageview code in xml file:
<ImageView
android:id="#+id/ImageSuccess"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:scaleType="centerInside"
android:src="#drawable/image1" />
and here where I changed the imageview in java file:
ImageView myImage= (ImageView) findViewById(R.id.ImageSuccess);
if(userVlue.equals("SCHOOL")){
myImage.setImageResource(R.drawable.image1);
}
else if(userVlue.equals("CAR")){
myImage.setImageResource(R.drawable.image2);
}
else if(userVlue.equals("TOY")){
myImage.setImageResource(R.drawable.image3);
}
One of myImage or userVlue is not set properly and probably is null.
Debug and share logs. Your partcular activity is getting distroyed probably because of nullpointexception hence returning to main activity in backtrack.
I tried with a sample app and your logic is working perfectly.
You are handling user action on some UI element, when you do findViewById, cotext is that UI element, so this method searches ImageSuccess in children of it. Try giving page layout as rootlayout and do rootlayout.findViewById("ImageSuccess");
I found the solution finally, the black screen appeared because the images were only in the drawable file, so I needed to add the images in all drawbridge folders such as drawable-ldp, drawable-ldpi etc...., so that the app can work in different phone sizes.
I am new to the android sdk. I have this weird gui thing and I made a Button and an EditText inside of it. I think I properly set up the onClick stuff for the button, but how to I get the text inside of the EditText with the id of editText1 from what it says inside the xml file? Here is my EditText xml element inside of the xml file:
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"
android:ems="10"
android:hint="#string/test"
android:inputType="textMultiLine|none" >
<requestFocus />
Thanks! Sorry that I am so new to Android development. None of the things I have searched make an sense to me. I keep seeing stuff about the "R" class and I have no idea what that means.
Starting learning from https://developer.android.com/training/
In onCreate() of your source code say MainActivity.java
Try this!
final EditText et=(EditText)findViewById(R.id.editText1);
in onClick() listener of your button, try String ss=et.getText().toString();
Hope it helps!
To get the text inside the EditText do that:
EditText editText = (EditText) findViewById(R.id.editText1);
String yourText = editText.getText().toString();
To get the text entered inside of your EditText, you would do the following inside of your onClick:
EditText et = (EditText)findViewById(R.id.editText1);
String words = et.getText().toString();
Then do whatever you want with the text with the variable.
I have a TextView with links and other text. I need to links to be clickable & I need to capture the click and open the link in a webview in my activity. I followed this this answer. My code looks like this:
actitivity_main.xml
<TextView android:id="#+id/textlinktest"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
And this is the way I invoke it:
textLinkTest.setText(Html.fromHtml("Something else google"));
textLinkTest.setText(RichTextUtils.replaceAll(
(Spanned) textLinkTest.getText(),
URLSpan.class,
new URLSpanConverter()));
The links show up fine, but they are not clickable! The onclick in my customeclickablespan never gets called. I tried toggling android:linksClickable & also tried different values for android:autoLink. But the links remain unclickable. Any thoughts?
Add
textLinkTest.setMovementMethod(LinkMovementMethod.getInstance());
before
textLinkTest.setText(RichTextUtils.replaceAll((Spanned) textLinkTest.getText(), URLSpan.class, new URLSpanConverter()));
I want to use an EditText as an indicator to show values, which i receive from a remote device. So the text in the EditText must not be editable. How to do that ?.
Use TextView isntead. That is what TextView is for. If you read its documentation it says: It displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.
Hope this helps.
For what it sounds like you're trying to use it for, a TextView would be more appropriate, as others have suggested.
However, if you're set on using an EditText for a particular reason, you can achieve what you're looking for by disabling the EditText. This greys it out and prevents the user from editing the text. You can do this either statically (in XML) or dynamically (in Java).
XML:
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="This is disabled." />
Java:
EditText editText1 = (EditText) findViewById(R.id.editText1);
editText1.setEnabled(false);
What you are looking for is TextView. Set any text you want. It is un-editable.
TextView myTextView;
#Override
public void onCreate()
{
....
myTextView = (TextView) findViewById(R.id.textView);
myTextView.setText("Hello world");
}
For further info LINK.
I have an edittext and I would like to paste some text in it. I can copy the text from some web page but I am not able to paste the text in my edittext control.How can I enable my edittext to paste some text.Here is my main.xml for edittext ;
enter code here
<EditText
android:id="#+id/enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight= "2"
android:scrollbars="vertical"
android:textColor="#color/black"
/>
Thanks
This is on Android 4.4.2 Samsung S4;
Documentation for TextView says that:
To allow users to copy some or all of the TextView's value and paste
it somewhere else, set the XML attribute android:textIsSelectable to
"true" or call setTextIsSelectable(true). The textIsSelectable flag
allows users to make selection gestures in the TextView, which in turn
triggers the system's built-in copy/paste controls.
There is also another Textview attribure called android:cursorVisible which determines if the system should be invoked about the copy/paste callbacks.
By default I believe both of these are true and selection/copy/paste mechanics are already enabled. I could not change that behaviour by using android:textIsSelectable="false" but if I set android:cursorVisible="false" initially you can't paste anything inside the EditText. Only after you type something in, cursor and selection behaviour becomes enabled again. Maybe this should be handled inside the code rather than in the layout xmls, or it might be related to android:inputType which also did not make a difference for me.
So try setting android:cursorVisible="true" in your EditText's layout xml if paste is not enabled by default.
According to your problem if you copied some data any where in your system and you want to paste it in some specific variable, like Edit TextBox, Textview etc, then this code will surely help you.
ClipboardManager clipMan = (ClipboardManager)getSystemService(v.getContext().CLIPBOARD_SERVICE);
myEdtTxt.setText(clipMan.getText());
Note:- here the clipMan object will store the data whenever copied process take place and we will return that data from that object and will set it,
To enable the standard copy/paste for TextView, U can choose one of the following:
Change in layout file:
Either add below property to your TextView
android:textIsSelectable="true"
and In your Java class write this line to set it programmatically.
myTextView.setTextIsSelectable(true);
if fragment try with
mContext.myTextView.setTextIsSelectable(true);
And long press on the TextView you can see copy/paste action bar.
Try setting the inputType="text" for the EditText field