I'm trying to figure out how to embed things, other than Drawables, inside an EditText widget. Specifically the example I'm thinking of is from the Google Buzz widget:
screenshot
(no inline image, sorry, I'm a newb)
It appears to the casual observer that there's an entire layout object pinned to the bottom of the EditText, containing an ImageView, a TextView, and a Button.
Anyone have any idea how to pull this off? Or do we think this is a custom subclass of EditText?
The EditText + Button + ... it's a FrameLayout with the EditText with fill_parent and the Buttons with layout_gravitiy:"bottom". Something like this:
<?xml version="1.0" encoding="utf-8"?> <!-- Main Layout (may be relative or whatever --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Layout for edittext and button -->
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="5dip"
android:text="Overflow"/>
</FrameLayout>
<!-- More layouts ..... --> </RelativeLayout>
you can use frame layout for embed Button in EditText,here i give sample code for embed TextView in EditText,just change the TextView as Button
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="40px"
android:layout_y="35px"
>
<EditText android:id="#+id/twt_post_content" android:layout_gravity="center_vertical"
android:layout_width="320dp" android:layout_height="140dp"
android:paddingTop="5dp" android:imeOptions="actionDone"
android:gravity="left" android:focusableInTouchMode="true"
android:maxLength="140" android:ellipsize="end" />
<TextView
android:text="123"
android:paddingLeft="270dp"
android:paddingTop="100dp"
android:layout_alignParentRight="true"
android:id="#+id/twt_content_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/red"
android:layout_gravity="center"
android:background="#color/transparent"/>
</FrameLayout>
I think that what they have done here is create a background for their layout that looks like an EditText. Then they added an EditText with the background turned off and come Buttons.
Related
I want to make a chat screen which has an EditText and a Button at the bottom (wrapped in a LinearLayout), and a ScrollView for the messages at the top. The problem is that when the user taps the EditText and the keyboard opens, the ScrollView will go off screen as a result of moving up. I would like to move the Layout (with the EditBox and the Button) above the keyboard and the ScrollView to be resized to fit the screen. The problem is shown on the pictures below:
As you can see, the line that writes "Chat" (left image) has disappeared in the right image.
I use this XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15sp"
android:orientation="vertical"
android:background="#ccdcf7"
tools:context="com.orionz.sockettest.ChatActivity">
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="15"
android:isScrollContainer="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/chatBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Chat\n"
android:textColor="#000000"
android:textIsSelectable="true"
android:textSize="22dp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="horizontal">
<EditText
android:id="#+id/msgInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:ems="10"
android:hint="Message..."
android:inputType="text" />
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:background="#drawable/mybutton"
android:padding="5sp"
android:text="Send"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
I have seen many answers such as setting the android:windowSoftInputMode but none has worked. I use Android Studio. Any help would be appreciated.
Try to replace the ScrollView with a ListView like this :
<ListView
android:id="#+id/list_view"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
store all the messages in an ArrayList, and set an adapter to the ListView you made :
ListView listView=(ListView) findViewById(R.id.list_view);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ArrayList);
listView.setAdapter(adapter);
Finally, add a listenner on your Button writing these lines :
EditText editText= (EditText) findViewById(R.id.msgInput);
ArrayList.add(editText.getText().toString());
adapter.notifyDataSetChanged();
Finally, I found what was wrong in my case.
The theme was full screen. In the style of the activity I had this line that caused the problem:
<item name="android:windowFullscreen">true</item>
In the java code, I gave a command to open the keyboard on the start of the activity:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
which was also part of the problem
After adding the adjustResize property to the activity in the manifest, everything worked fine:
android:windowSoftInputMode="adjustResize"
Wrap Scrollview to the whole layout.
i.e place editText and button inside Scrollview and set scrollView Height to android:layout_height="match_parent"
Please check this screenshot
How to add buttons like this in android. Is there a library for it or what is it called?
Please note that when keyboard is hidden, this button takes up the space for navigation buttons.
Another question: How to change the size of the blue app bar when keyboard appears or hides?
**UPDATE : ** I can implement a similar layout using borderless buttons in frame layout and setting the gravity to bottom. Then I used android:windowSoftInputMode="adjustResize"
in the manifest file to make it work with keyboard hide/unhide. Still these buttons are not taking the space of navigation buttons. Also still need guidance for change app bar size on keyboard appear.
You can use
1- AppIntro
2- AppTour
Well, I would say that the button is not related in any way with they keyboard. I bet it's just a button inside a FrameLayout with the android:layout_gravity="bottom" property set.
It is a borderless button placed in a container(probably LinearLayout)
container itself is called buttonBar
Borderless button
One design that can be useful is a "borderless" button. Borderless
buttons resemble basic buttons except that they have no borders or
background but still change appearance during different states, such
as when clicked.
To create a borderless button, apply the borderlessButtonStyle style
to the button. For example:
<Button
android:id="#+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage"
style="?android:attr/borderlessButtonStyle" />
sample for button bar
<LinearLayout android:id="#+id/footer" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_alignParentBottom="true" style="#android:style/ButtonBar">
<Button android:id="#+id/saveButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="#string/menu_done" />
<Button android:id="#+id/cancelButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="#string/menu_cancel" />
</LinearLayout>
Effect of everything else collapsing and this button bar being at top op keyboard can be achieved by keeping your entire view except buttonBar in a scroll view.
Use this as a reference. Insert your layout inside ScrollView.
<LinearLayout
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:orientation="vertical"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
/>
</LinearLayout>
</LinearLayout>
I've got a need to display and TextView (label), EditText(Input) and TextView(suffix) in a line.
The issue I have is - if the label TextView is too long it The EditText and Suffix TextView and not drawn.
Here's my code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="0"
android:padding="5dp"
android:text="Label"
android:id="#+id/label_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_weight="1"
android:id="#+id/input_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorHint="#color/dark_grey"
android:textColor="#color/black"
android:hint="text.." />
<TextView
android:padding="5dp"
android:layout_weight="0"
android:text="Suffix.."
android:background="#color/lite_grey"
android:id="#+id/suffix_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
So what I want is - even if the label is over 2-3 lines, for an EditText to appear followed by another TextView.
Any pointers would be appreciated.
Thanks!
Edit:
I've added some 'diagrams' so you can understand what I want to do a bit better.
The label to the EditText can be arbitrary length(2/3 lines), so whenever the label finishes I want an EditText to start (which is followed by another TextView showing a suffix..)
..
![This shows a 2 line label and TextView..]
Edit 2: Thanks for the responses guys!! I think #questioner has really understood what I want to do i.e. I want your 3 views to move to other lines so that they behave like they were one view - and as he has suggested i'll have to find a library for this!
How about this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:padding="5dp"
android:text="Label"
android:id="#+id/label_text_view"
android:layout_width="100dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"/>
<EditText
android:layout_weight="1"
android:id="#+id/input_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColorHint="#color/dark_grey"
android:textColor="#color/black"
android:hint="text.."/>
<TextView
android:padding="5dp"
android:text="Suffix.."
android:layout_gravity="center_vertical"
android:background="#color/lite_grey"
android:id="#+id/suffix_text_view"
android:layout_width="100dp"
android:layout_height="wrap_content"/>
</LinearLayout>
If you want your 3 views to move to other lines so that they behave like they were one view - you should use external library for that.
Why not use a RelativeLayout instead of LinearLayout and then use android:layout_below="#+id/..." or android:layout_above="#+id/..." also android:layout_toRightOf="#+id/..." or android:layout_toLeftOf="#+id/..."
Set
android:layout_weight="1"
for each view so that they all occupy the same amount of horizontal space.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1">
Try this or please expand a little more.
I'm trying to add a footer containing an imagebutton, an textview and another imagebutton to my activity (I want to make an app like whatsapp and it should nearly look the same). I have tried many ways, but all didnt work.
I tried to make a footer like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/chatBottom"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:background="#D8D8D8"
android:layout_alignParentBottom="true" >
<ImageButton
android:id="#+id/emojiButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:src="#drawable/emoji_button"
android:background="#D8D8D8"
android:padding="10dp"
android:contentDescription="#string/empty"/>
<EditText
android:id="#+id/enterMessage"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:layout_toLeftOf="#+id/sendMessage"
android:layout_toRightOf="#id/emojiButton"
android:background="#drawable/textfield_multiline_activated_holo_dark"
android:hint="" />
<ImageButton
android:id="#+id/sendMessage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#D8D8D8"
android:contentDescription="#string/empty"
android:padding="10dp"
android:src="#drawable/action_send_message" />
and add it using include in xml or via addView() in java, but nothing worked properly. Most important is that the footer has its on background colour (always when I tried to set a colour to the layout where the footer is in, the whole activity got this colour) and that the textview resizes automatically.
Best regards, Oliver
Say I have an ImageButton. I want a textview on top of the imagebutton, and centered on it. How would I do this?
you could do it by wrapping both widgets in a FrameLayout
for example:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text"
android:layout_gravity="center" />
</FrameLayout>
Use a regular Button, which extends TextView anyway. Then set the background to your image and use setText() to set your text. You can even add icons above, below and/or left & right of the text with the drawableLeft,drawableTop,etc attributes.
You Are overcomplicating your layout. Just add text to the button.
myImageButton.setText("text");
If you really do want to layer these views, you should use a RelativeLayout:
<RealtiveLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/img"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:layout_centerInParent="true"/>
</RelativeLayout>
Make a special note of the attribute layout_centerInParent . this is how the TextView is correctly centered.