Adding TextView dynamically to an layout for a messaging app - android

I am trying to build a simple messaging app. which have one EditText and one Button at the bottom of the screen. when someone enters text and click the send button the entered text should appear as a msg on the top of the (EditText and Button). If the user have sent lots of msgs unable to fit to fit in the screen, the scrollbar for scrolling and viewing msgs should automatically appear.
I have made the layout as
<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"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#android:color/black"
android:onClick="sendMessage"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:hint="#string/edit_message"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/button"
android:layout_toStartOf="#+id/button"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
and Java code to fetch the msg is
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
}
I need someone to guide me how to proceed after this.
I want the msg to appear above each time. Any help or suggestion will be great helpful for me.

For a simpler solution, you might want to try using one textview. You just keep adding the new messages to it.
I would create my own custom layout just so that different combinations are possible (e.g timestamps, emojis). Basically a custom layout which draws all your different items on there.

You can use a custom listView with 9patch images.

Related

Phone number inside TextView become underscored but not clickable

I have complicated XML file. Here is a piece:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.design.widget.TextInputLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="#string/tel"
style="#style/AppTheme.TextInputLayout"
android:layout_marginBottom="#dimen/space_high">
<android.support.design.widget.TextInputEditText
android:id="#+id/point.j_tel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/AppTheme.TextInputEditText"/>
</android.support.design.widget.TextInputLayout>
<ImageButton
android:layout_gravity="center"
android:id="#+id/point.telEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/tab_icon_edit"/>
</LinearLayout>
I'm trying to make phone numbers inside android:id="#+id/point.j_tel" clickable. I tried android:autoLink="all", android:autolink="phone", android:linksClickable="true". But this only led phone numbers become underscored and semi tranceparent but not clickable . I also tried this inside my Java code:
cameraManager = new CameraManager(this);
etNotice = findViewById(R.id.point_c_notice);
getValidations().add(new TextEmptyValidation(this, etNotice));
etNotice.addTextChangedListener(getLastValidation());
tiAddress = findViewById(R.id.point_c_address);
tiTel = findViewById(R.id.point_j_tel);
tiTel.setAutoLinkMask(Linkify.PHONE_NUMBERS);
tiTel.setMovementMethod(new ScrollingMovementMethod());
I tried a lot of things during two days, but nothing helps and I almost gave up.
Maybe somebody has any ideas?
Why do you need to click on EditText to make the call? It should be TextView or add calling button beside the EditText

EditText not accepting an input

I have 2 EditTexts on my screen. The first one works fine, but the second doesn't. I click on the first EditText and can enter text. But click on the second EditText and any input continues to appear in the first EditText.
Looking at other questions on the subject, there is talk of using setFocus and having onClick listeners for the EditText fields. Is this correct?
I have another app which had several screens with multiple EditTexts and never had to do this.
This is my current XML:
<TextView
android:id="#+id/namLabel"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name" />
<EditText
android:id="#+id/namTxt"
android:layout_below="#id/namLabel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:minWidth="200sp" />
<TextView
android:id="#+id/numLabel"
android:layout_alignParentLeft="true"
android:layout_below="#id/namTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20sp"
android:paddingRight="10sp"
android:text="#string/units" />
<EditText
android:id="#+id/numTxt"
android:layout_toRightOf="#id/numLabel"
android:layout_alignBaseline="#id/numLabel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:inputType="numberDecimal"
android:minWidth="50sp" />
This is all the relevant code I have in the onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
namText = (EditText) findViewById(R.id.namTxt);
numText = (EditText) findViewById(R.id.numTxt);
Please don't tell me I have to add a whole load of onClickListeners for these fields?
I found the problem. I just had to add the setFocusableInTouchMode to both EditText's:
android:focusableInTouchMode="true"
I didn't do that in my last app, so no idea why I have to now.
You don't need to add any listeners or setFocus. The only "strange" thing I see is that you are not wrapping your xml in a RelativeLayout. It should work just fine, just tested it.

UI Layout Issues

First let me attempt to layout what I am trying to accomplish here.
EditText
EditText SearchButton
ListView (search result. there can be only one, ListView with adapter and height of wrap_content seems to work for this, there is a button to add the result to the ListView below. Once the add button is clicked this ListView collapses, which is exactly what I am after)
TextView (label for objects added)
ListView (list of objects added, again I'm using an adapter for the list row layout)
SaveButton
I was going to paste the code that I have but there is just too much to go through. The issues I am having are with the ListViews. Basically, the ListView that contains the objects added will end up pushing the SaveButton off of the screen. I have tried a ton of solutions laid out on this and many other sites but they just don't seem to work right.
Basically, I want the SaveButton to always be at the bottom and I don't want it to get pushed off the screen when the ListView gets too big. The only solution I have found to "work" was to explicitly set the height of the ListViews. However, this causes problems when going from tablet to phone (Nexus7 & Galaxy S3). I thought that using dip for sizes would prevent this from happening but apparently not.
If anyone has a good strategy for creating this type of layout that would be great. Or even a good resource for learning how to use Android's clunky UI system (it really leaves a bit to be desired).
Edit: here is my attempt at using a RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/main_background"
android:orientation="vertical" >
<EditText
android:id="#+id/plan_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/plan_name_hint"
android:textColor="#color/text_color" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/object_search_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/plan_name"
android:ems="10"
android:hint="#string/search_objects_text"
android:textColor="#color/text_color" >
</EditText>
<Button
android:id="#+id/objects_search_button"
style="#style/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/object_search_text"
android:layout_below="#id/plan_name"
android:layout_alignParentRight="true"
android:background="#drawable/black_button"
android:text="#string/search_objects_button_label" />
<ListView
android:id="#+id/search_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/object_search_text"
android:background="#color/main_background"
android:textColor="#color/text_color" >
</ListView>
<TextView
android:id="#+id/objects_list_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/search_result"
android:paddingBottom="8dip"
android:paddingLeft="8dip"
android:text="#string/plan_objects_list_label"
android:textColor="#color/text_color"
android:textSize="22sp"
android:textStyle="bold" />
<ListView
android:id="#+id/plan_objects"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/objects_list_label"
android:background="#color/main_background"
android:textColor="#color/text_color" >
</ListView>
<Button
android:id="#+id/save_plan_button"
style="#style/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/black_button"
android:paddingLeft="8dip"
android:text="#string/save_button_label" />
If you think the Android UI system is clunky, you obviously haven't tried to understand it. For most things its extremely well designed.
If you want a certain view (or views) to always be at the bottom, then you want to make your screen a RelativeLayout and put android:layout_alignParentBottom="true" on those element(s). Then add android:layout_above="id" on whatever you want to be above them, where id is the id of the element you want at the bottom.
Make the SaveButton and ListView at the same hierarchy level. e.g if your parent layout is RelativeLayout in your SaveButton add this property android:layout_alignParentBottom="true"
It looks like the only real solution here is to use explicit sizes for the list views and plan accordingly for different screen sizes (i.e. create different layouts for different screens and outlined here.). I was hoping for something a little more generic. Oh well.

Clickable Button (or any View) inside EditText in android

I want to have a Button or a clickable View in my EditText so that I can perform some action on click of it. I was able to put a drawable inside my EditText thanks to Marcosbeirigo for this answer. But, now I want to make it clickable so that I can perform some action on it. I know this is possible as HTC uses buttons inside EditText in their stock sms app as shown in the following image-
In my case, the button will be positioned anywhere, can be in the center also. But the main thing is how can I put a button in EditText?
Use RelativeLayout. The Send and Attach buttons are simple Android Buttons and the 32/160 is a TextView. Put the buttons and the textview on the EditText object, play with the layout arrangments and set some right padding to the EditText object so that the text inside it won't go under the buttons.
You don't need any drawable and listening to the click event of the android buttons is not a question anymore.
Its absolutely correct
Use this , It worked for me -
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText android:id="#+id/id_search_EditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:paddingRight="40dp"
android:hint="Enter your feelings and connect" />
<ImageButton android:id="#+id/id_search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/id_search_EditText"
android:layout_alignBottom="#+id/id_search_EditText"
android:layout_alignRight="#+id/id_search_EditText"
android:background="#drawable/ic_launcher"/>
</RelativeLayout>
I think you try to search set click event for compound drawable. You can find some solutions here
handling-click-events-on-a-drawable-within-an-edittext
there is no button inside editText It's just an editText with white background so you don't see it's margins and a simple layout arrangement.
The only possible solution is to use a RelativeLayout that allow you to put also Views in overlay to others. Other Layout wont allow you to do such things.
Also I found this tutorial: http://developer.android.com/resources/articles/layout-tricks-merge.html maybe it can helps you :)
Use this this code may work.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/REFReLayTellFriend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<EditText
android:id="#+id/txtSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/editext_rounded"
android:drawableLeft="#android:drawable/ic_menu_search"
android:gravity="start"
android:hint="Search"
android:singleLine="true"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtSearch"
android:background="#drawable/ic_action_content_filter_list"/>
</RelativeLayout>
</LinearLayout>

Android EditText like Google Search EditText

Is there somewhere the code of Google Search in Android? The xml-layout would be enough for me. Or maybe there is a possibility to add a button to the right of the EditText just like adding an icon in the corner? Thanks.
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bar"//set back ground for total look
android:orientation="horizontal" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Your hint"
android:drawableRight="#drawable/search"//give your search icon
android:id="#+id/editBox"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageSpeak"
android:background="#drawable/iconSpeak"//icon for speak
/>
</LinearLayout>
You have a LinearLayout with horizontal orientation where you can have an EditText, an ImageView with the search image and an ImageView for the microphone image. All of them should have the required background images cut in such way in order to seem as one. This is the general notion.
Regarding the source code, I can't help you!
Hope this helps!

Categories

Resources