Android: Custom AlertDialog has different text color - android

I created a custom layout for an AlertDialog, and it works fine,
But i have a problem with the text style of my layout.
This is the layout for my alertDialog:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/namedialoglable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/profilename" />
<EditText
android:id="#+id/namedialoginput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/namedialoglable"
android:layout_marginTop="24dp"
android:ems="10"
android:selectAllOnFocus="true"
android:text="#string/defaultname" >
<requestFocus />
</EditText>
</RelativeLayout>
The problem is that the text into the default AlertDialog is white, and in my custom dialog is black. I just want to use the same color used into other AlertDialog.
How to do that?
EDIT
To be more clear:
I want to keep the father text color, not force my custom dialog color to be white. I think that AlertDialog use system color, so my alert need to keep the same color

Why wont you just apply:
android:textColor="#android:color/white"
to the Views you want with while text color.
Also look into styles and themes to achieve what you wnat:
http://developer.android.com/guide/topics/ui/themes.html#PlatformStyles

Ok in my case, i found another solution.
Since my need is to have only a message and an input text into the dialog box, the best solution is to create a layout with only an EditText, like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/namedialoginput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="24dp"
android:ems="10"
android:selectAllOnFocus="true"
android:text="#string/defaultname" >
<requestFocus />
</EditText>
</RelativeLayout>
And for the message text, using the builder method: setMessage:
builder.setView(content)
.setTitle("Profile Name")
.setMessage(R.string.profilename);
In that way, i am sure that i keep the other dialog boxes format.

Related

Use default title text view style for customize title text for AlertDialogs

I was needed to create a custom title view to be able show message and title, when the list appears. Because the list appears in the dialog's content area. So the dialog cannot show both a message and a list.
My custom view is a LinearLayout :
<?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:orientation="vertical"
android:padding="24dp"
android:paddingBottom="20dp"
style="#style/Base.Theme.AppCompat.Dialog.Alert"
>
<TextView
android:id="#+id/alert_dialog_custom_title_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/alert_dialog_custom_message_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"/>
</LinearLayout>
Padding values and text size was given in accordance with the information given material design guidelines
However, This appearance is different from the default one.
my question:
How can I create a textView that has a same style with default one ?
Maybe there is a way like setting a style to my TextView
Edit : I just want to set style attribute that makes its visuality same as default AlertDialog.
My textview is already like this:
<TextView
android:id="#+id/alert_dialog_custom_title_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="messsage"
android:layout_marginBottom="20dp"
android:textSize="20sp"
android:textStyle="bold"
/>
I want to do that just like this :
<TextView
android:id="#+id/alert_dialog_custom_title_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="messsage"
style="?android:attr/titleTextStyle"
/>
In your style you can use this
<style name="TextAppearance.AlertDialogPro" parent="android:TextAppearance" />
for you textview you can add style
<TextView
android:id="#+id/alert_dialog_custom_title_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="messsage"
style="#style/TextAppearance.AlertDialogPro"
android:layout_marginBottom="20dp"
android:textSize="20sp"
android:textStyle="bold"
/>

How to retain the hint text in android EditText field?

In my android app, I have a hint text in my EditText view. Once the user starts typing, I want the hint to be displayed on top of the edittext field. The original hint text disappears when user starts typing which is fine. How do I place a text on top of the edittext field ? Is there a way to make the EditText field multiline and use the first line for hint? But the user input should always start from the second line.
Thanks in advance for your help!
Just found that they are called floating inline labels in android.
http://www.google.com/design/spec/components/text-fields.html#text-fields-floating-labels
But looks like API 21 doesnt support this yet.
There new widget provide by android called TextInputLayout it is the standard way. Below is an example of it:
<android.support.design.widget.TextInputLayout
android:id="#+id/first_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/first_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hello"/>
</android.support.design.widget.TextInputLayout>
You can use FloatLabelLayout. It's a custom component that works as described in the google documentation.
Once you create the custom component you can use it in this way:
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<your.package.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="#style/TextAppearance.YourApp.FloatLabel">
<EditText
android:id="#+id/edit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/account_username_hint"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:imeOptions="actionNext"
android:nextFocusDown="#+id/edit_password" />
</your.package.FloatLabelLayout>
<your.package.FloatLabelLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:floatLabelTextAppearance="#style/TextAppearance.YourApp.FloatLabel">
<EditText
android:id="#+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/account_password_hint"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:imeOptions="actionDone" />
</your.package.FloatLabelLayout>
</LinearLayout>

Android: EditText displayed with black background

I have a row with two EditTexts and two ImageButtons. In the preview of android studio everything looks fine and the EditTexts have the default style. But if I display it on my tablet the background is black.
How it should look like:
How it looks like on the tablet:
Other EditText-Boxes are displayed correctly.
Does anyone know why the these look different and how I can fix it?
Here the XML-Layout of this line:
<?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="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
>
<EditText
android:id="#+id/first_input"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<EditText
android:id="#+id/second_input"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.4"
/>
<ImageButton
android:id="#+id/first_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:src="#drawable/move"
android:background="#drawable/first_button_background"
android:onClick="onFirstButtonClicked"
/>
<ImageButton
android:id="#+id/second_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:src="#drawable/trash"
android:background="#drawable/second_button_background"
android:onClick="onSecondButtonClicked"
/>
</LinearLayout>
EDIT:
I tried changing the background color to white but then the EditTexts lose their whole style so also the underlining will not be displayed anymore.
this's because the editext default background change based on device theme go for an edittext with custom background and in a simple way include android:background="#ffffff" for your editexts
The problem might be occurring due to the imageButtons but You can change the background color of the edittext like this:
android:background="#FFFFFF"
May be your tab does not support the selected theme, try to change background color of your edittext using:
android:background="#FFFFFF"
if problem does not solve, then you can change background of edittext programmatically like this:
youeEditText.setBackgroundColor(Color.parseColor("#ffffff"));

cannot see button next to search box android

I'm having trouble when showing a button next to the search box. I'm using an auto complete text view and i don't know if this is due to the auto complete text view. How can i fix this. Here's what i got so far:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
/>
<Button
android:id="#+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go"
/>
</LinearLayout>
To fix this, just add the tag android:layout_weight=1 to your AutoCompleteTextView.
This happens because android noticed you set the layout_width to fill_parent on your AutoCompleteTextView, and then it has no space for the Button left. Setting the weight to 1 means the textview will be "generous" and give as much space to any other components as they request, which in this case is limited to the width of the Button after is wrapped.
You are filling the parent width with your AutoCompleteTextView (thus pushing the Button out of screen width). Try to change this
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
/>
into this
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
/>
And your Button should become visible. Here is a nice resource conserning layouts: Android Developer | Declaring Layouts

How to embed a View (with Buttons, etc.) inside an EditText?

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.

Categories

Resources