I'm having trouble making my textview editable - while I can make it editable, I can't pass the text to another activity via an intent.
In the xml of my View activity, for my textViewComment textbox, I have:
<TextView
android:id="#+id/textViewComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/textboxes_for_contact"
android:textIsSelectable="true"
The text in the textbox is indeed selectable, I am able to copy it with a long press.
However, I have an Edit activity - when an edit button is clicked in View activity, the text in textViewComment should be passed to this Edit activity with an intent. Instead, the Edittext in my EditView is empty.
When I remove android:textIsSelectable="true" the text is passed via the intent successfully. But... I can't edit it in the textview.
I also tried it programmatically with commentname.setTextIsSelectable(true); but this gives the same problem.
In ViewContact class I have
i.putExtra("comment", commentname.getText());
In EditContact class:
comment = i.getStringExtra("comment");
The xml in my EditContact class for textViewComment is:
<EditText
android:id="#+id/textViewComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:background="#drawable/textboxes_for_contact"
android:maxLength="360"
/>
Any idea how I can make the text in ViewContact selectable while also passing the intent successfully? Is this a known issue?
Replace:
i.putExtra("comment", commentname.getText());
with:
i.putExtra("comment", commentname.getText().toString());
and see if you have better luck.
Related
i am trying to achieve a behavior where user click an arrow that can reveal more content such as more description abort something. It can a recycler view as well where more things can be added dynamically and the list will expand.Right now i do not have any idea how it can be achieved. I tried searching on the internet for solutions and saw a widget called spinner but i do not think it can help me achieve my desired behavior. YouTube does apply similar behavior as well
Below are the pictures which will make my question clear. Any help would be appreciated Thank You
Before clicking the arrow pic 1
After clicking the arrow pic 2
In your layout.xml include a nested layout that includes a Textview that holds the additional information and set android:visibility="gone". Use an OnClickListener to the button that is meant to expand the view. In the onClick method check if the view is visible or not. If it's not you make it visible, otherwise you set it to gone again.
layout:
...
<ImageView
android:id="#+id/chevron"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/chevron"
/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="your additional info here"
android:visibility="gone"/>
...
In your Activity:
ImageView yourView = findViewById(R.id.chevron);
..
yourView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getVisibility() == View.Gone) {
view.setVisibility(View.Visible);
} else {
view.setVisibility(View.Gone);
}
}
});
I used for this purpose ExpandbleLayout from this github library
ExpandableLayout. In readMe of the github repo you can find example of using it, you can get similar experience with as in your example, without need to manually create View for arrow and handling the animation.
You can use it like this :
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ael_expanded="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text goes here"
android:textSize="28sp" />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
And in your java/kotlin code : do additional logic to expand/collapse call : expandableLayout.toggle();.
All the credit goes to the author of the library.
https://github.com/AAkira/ExpandableLayout
Android beginner experiments:
I have created a layout with only an editText without an id.
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="Enter Text"/>
Now , how should I prepare my Java code so that on orientation, the text in editText remain saved ?
Goto your android manifest, find the activity you are using and be sure it is like this:
<activity
android:name=".YourActivity"
android:configChanges="orientation|screenSize|keyboard|keyboardHidden" />
I am a beginner trying to make a calculator app in Android Studio that takes input from buttons. This is proving to be much more difficult than I thought it would be compared to just using EditText, but it has been a great learning experience so far.
I am working on the display portion of the app for which I am using a textView. I have it set up so that when a button is clicked, the button's value is appended to the textView. It's a calculator, so I want the textView to be displayed on a single line and shift horizontally for new input when the width of the textView is full.
I found a way to do this from another person's post that works by setting the following in the XML:
android:inputType="text"
android:maxLines="1"
When I did this it does exactly what I want, but I get an IDE warning that says:
Warning: Attribute android:inputType should not be used with <TextView>: Change element type to <EditText> ?
Without the android:inputType="text" piece of code, the textView doesn't seem to scroll properly and I don't want to use an EditText. Is this something to worry about? Is there a better way to do this?
Thanks for the help.
Do not use inputType in TextView.
From this link - https://youtu.be/HYt1Ntu89X4
What I understood is you want the text to scroll dynamically i.e. on Button press.
You can do it like this :
XML
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hzw"
android:fillViewport="true"
android:layout_gravity="end">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv1"
android:maxLines="1"
android:textSize="20dp"
android:textStyle="bold"
android:gravity="end"/>
</HorizontalScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/b"
android:text="Add Text"/>
Java
tv1 = (TextView)findViewById(R.id.tv1);
tv1.setText("123+123");
hzw = (HorizontalScrollView)findViewById(R.id.hzw);
b = (Button)findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tv1.append("d+1+cff");
hzw.post(new Runnable() {
#Override
public void run() {
hzw.fullScroll(View.FOCUS_RIGHT);
}
});
}
});
I started learning java a few days ago, so I'm quite lost with it.
I want to show a text received from an intent and make it look like this:
"You've written : ."
It isn't working at all and I'm only able to show the text from the intent.
Here's my code:
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
System.out.print("You've written: ");
textView.setText(message);
System.out.print(".");
// Set the text view as the activity layout
setContentView(textView);
}
Besides, I'm trying to display the text written above in the first line of the page (or as many lines it takes) and, below, a label to insert a text together with a Button. The problem is that I can only see the text from the intent.
Here's my code:
<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=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage"/>
</RelativeLayout>
Thankyou very much, I wish I'll be answered soon.
Instead of System.out.println.
Do like this.
<TextView
android:id="#+id/topTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
and Get the view in activity.
public void onCreate(Bundle bundle) {
.........
setContentView(layout_xml);
TextView textView = (TextView)findViewById(R.id.topTextView);
textView.setText("You've written: " + message + " .");
.........
}
If you are trying to reference the TextView inside your layout it needs to referenced in the Activity. The above solution shows you how to handle that. setContentView(R.layout.file_name) is what should be used to reference a layout created inside an xml file stored in res/layout/file_name.xml or created manually inside your code. If you are going to call setContentView for a layout contructed inside your (Activity) it will take some more code. If the message is all is need to be displayed you can always call Toast.maketText(Context, message, Toast.LENGTH_SHORT).show(); for example. The other suggestion is Java developers are used to using System.out.println() to debug to the console. In Android they have a neat feature we use called LogCat that can display messages going on inside your code by saying (example for debugging purposes)
Log.d(TAG, message); Where TAG is usually a constant defined for the Activity,Fragment, etc. you are in so you can see where the message is coming from and display whatever value you normally would have used in System.out.println().
can any one suggest how can i have a list of icons like browser icon,email icon and contacts icon upon clicking on those it should lead to android browser,email and contacts apps respectively...right now i have done it, upon clicking buttons. Now i want icons(with image and text) instead of buttons...
Check this out:
http://developer.android.com/resources/tutorials/views/hello-formstuff.html#CustomButton
This will show you had to put the images you want in res/drawable/ and then load them up as buttons in your app.
There is no widget icon.
You can do it yourself using a LinearLayout an ImageView and a TextView.
The xml should be something like this:
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_image"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/your_icon_text"/>
</LinearLayout>
You can use Android Image Button and set its property "Clickable=true" and do the required work on its click event listener
I thought you want to have ImageButton , clicking on that you want to open particular application/browser/contact list.
ImageButton - Displays a button with an image (instead of text) that can be pressed or clicked by the user
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon/>
If you wants ImageButton with Background button then set android:background attribute, as below:
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:background="#drawable/background_image"/>
If you want an image and text in your button, the way I do it is create a 9 patch, so I can define where the text will go using the padding and stretchable areas. Then follow the same instructions as for the Custom Button already mentioned, but use a standard button, not an Image Button.