FINAL EDIT: Okay well a total hack but at the moment I'm like "meh" it works. All I did to fix the issue was add android:lines="10" to the TextView and it showed everything like in 2.2 and ICS/JB. Total hack because it's not abstract at all but whatever :P..Thanks to everyone who helped!
I'm having trouble displaying text in a custom dialog with Gingerbread (API 10). The only text shown is the first line as shown here. In Froyo, ICS, and JB it displays with every line of text shown. I believe it's an XML thing, but I'm not sure. Where am I going wrong?
Edit: What I have tried:
-Changing the RelativeLayout to LinearLayout
-Adding in ScrollView
-Putting my string on one line
-using requestLayout() and forceLayout()
-putting the dialog functions in a separate class
-taking out the margins in my button
-using \n instead of HTML
-AlertDialog
-inputType and singleLine XML attributes on my TextView
-I think there's one or two more that I forget..
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/dia_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:padding="5dp"/>
<View
android:id="#+id/bar"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#CCCCD0"
android:layout_below="#+id/dia_text"/>
<Button
android:id="#+id/dialogbuttongotit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/dialog_confirm"
android:textSize="20dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
android:layout_below="#+id/bar"/>
</RelativeLayout>
Here is the Code:
final Context context = this;
public void addListenerOnRectHelpButton() {
img = (ImageButton) findViewById(R.id.rect_img);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//create a new dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialogs);
dialog.setTitle("Rectangular Area");
// set the custom dialog text
TextView text = (TextView) dialog.findViewById(R.id.dia_text);
String dialog_rect_txt = "<u>Area of a Rectangular Channel</u><br />" +
"Height x Width (H x W)<br />--Example:<br />" +
"Height: 3ft, Width 5ft<br />" +
"H x W = 3ft x 5ft = 15ft<sup>2</sup><br />";
text.setText(Html.fromHtml(dialog_rect_txt));
Button dialogButton = (Button) dialog.findViewById(R.id.dialogbuttongotit);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
Having you tried calling requestLayout() on the TextView?
...
text.setText(Html.fromHtml(dialog_rect_txt));
text.requestLayout();
...
Why not just use an AlertDialog instead of building your own?
Dialog dlg = new AlertDialog.Builder(this).setTitle(
"Rectangular Area").setMessage(dialogRectText).setPositiveButton(
"Got It", clickListener).create();
dlg.show();
Related
I have created a custom layout with 3 buttons for alertdialog which is working fine. I am trying to make the layout so that width and height layout will be only wrap its content i.e. no extra width/height but I am unable to do so. Can you help me on this please?
Here is my custom layout for alertdialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#00ffffff"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/rectangle_menu"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/wowButtonId"
android:layout_marginLeft="5dp"
android:src="#drawable/love_icon"
android:background="#drawable/round_button_for_round_menu_like_button"
android:layout_gravity="center"
/>
<ImageButton
android:layout_width="40dp"
android:layout_height="35dp"
android:id="#+id/blehButtonId"
android:layout_marginLeft="5dp"
android:src="#drawable/bleh"
android:background="#drawable/round_button_for_round_menu_like_button"
android:layout_gravity="center"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dislikeButtonId"
android:layout_marginLeft="5dp"
android:src="#drawable/dislike_icon"
android:background="#drawable/round_button_for_round_menu_like_button"
android:layout_gravity="center"
/>
</LinearLayout>
</LinearLayout>
alertDialog implement in adapter code:
AlertDialog.Builder builder=new AlertDialog.Builder(context);
AlertDialog alertDialog=builder.create();
View view1=LayoutInflater.from(context).inflate(R.layout.layout_for_long_like_button_option,null);
ImageButton wow=(ImageButton) view1.findViewById(R.id.wowButtonId);
ImageButton disLike=(ImageButton) view1.findViewById(R.id.dislikeButtonId);
ImageButton bleh=(ImageButton) view1.findViewById(R.id.blehButtonId);
wow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"wow",Toast.LENGTH_SHORT).show();
}
});
disLike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"dislike",Toast.LENGTH_SHORT).show();
}
});
bleh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"bleh",Toast.LENGTH_SHORT).show();
}
});
alertDialog.setView(view1);
alertDialog.show();
try this on your class file where you inflate your alert dialog...
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alertDialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
Summary: Use RelativeLayout tag at the root in your custom layout
I had two alert dialogs in the application I was writing. One of them did not wrap content, while the other did. The one with the LinearLayout tag at its root, was expanding a tad more in height, than the space its contents really occupied.
I tried setting the width and height properties to wrap_content on the LinearLayout but to no avail.
The one with the RelativeLayout tightly wrapped its contents. The code structure to achieve this would look like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<!-- More widgets -->
</LinearLayout>
</RelativeLayout>
Although care must be taken, that the inner LinearLayout should still specify wrap_content. You can of course specify match_parent if your intention is to present an elongated dialog, the intentions for doing so, which I would leave it to the developer.
Android Studio still warns me that The LinearLayout layout or its RelativeLayout parent is useless. I do not consider this a solution to the problem, rather it's just a fix. And, I should mention that I've always had these layout problems working with Android. This, I feel, is a more saner approach as it keeps the code modifications down to the layout.
You can use below code for set layout and show dialog
public void showDialog() {
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.layout_for_long_like_button_option, null);
final TextView txtOk = (TextView) promptsView.findViewById(R.id.txtOk);
final TextView txtCancel = (TextView) promptsView.findViewById(R.id.txtCancel);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(BaseActivity.this);
alertDialogBuilder.setView(promptsView);
final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
txtOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
txtCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
You can try setting the width of the inflated view directly when the dialog is shown and therefore has a window
val dialog = AlertDialog.Builder(context)
.setView(myView)
.create()
dialog.setOnShowListener {
dialog.window?.setLayout(
myView.width,
ViewGroup.LayoutParams.WRAP_CONTENT
)
}
Please help. I have tried everything but failed every time. I am building an android app where I need to update text of a TextView in dialog box programmatically.
This dialog box is created using a custom layout. Posting code below, Please help in updating value of tv1 to "It's the new text". In XML layout file text was set to "old text".
After I run this program, the value of tv1 in 'dialogabt', is still seen as "old text".
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.b1);
//generating first dialog box
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Dialog dialog1 = new Dialog(MainActivity.this);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.setContentView(R.layout.setings);
dialog1.show();
}
});
}
}
function to generate the second dialog box and update TextView's text in it
public void abtit(View view) {
Dialog dialogabt = new Dialog(this);
dialogabt.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom2, null);
TextView tv1 = (TextView) dialogView.findViewById(R.id.firstapp);
System.out.println("Going to set new value");
tv1.setText("Its new text");
System.out.println("done setting new value");
dialogabt.setContentView(R.layout.custom2);
dialogabt.show();
}
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/set3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="abtit"
android:text="About app" />
</RelativeLayout>
custom2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="315dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/custom_alert2"
android:orientation="vertical">
<TextView
android:id="#+id/firstapp"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:onClick="openapp2"
android:text="old text" />
</RelativeLayout>
Try this:
dialogabt.setContentView(R.layout.custom2);
TextView tv1 = (TextView) dialogabt.findViewById(R.id.firstapp);
System.out.println("Going to set new value");
tv1.setText("Its new text");
System.out.println("done setting new value");
dialogabt.show();
Check your abtit() method, you are setting value in one custom view and in alert dialog you are setting another view, so it will not affect the values which you have set for dialogView object.
So just change your below line,
dialogabt.setContentView(R.layout.custom2);
By
dialogabt.setView(dialogView);
It will work
Remove following line because you are seting contentView after setting text to textView
dialogabt.setContentView(R.layout.custom2);
and add this line to below
View dialogView = inflater.inflate(R.layout.custom2, null);
When I click on the button, the font size shrinks to 12.
However, the result is :
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:background="#80ff0000"
android:text="#string/hello" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
java:
public class FontSizeTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView text = (TextView) findViewById(R.id.test);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text.setTextSize(12);
}
});
}
}
How do I shrink the height of the textView so that it only wraps the actual font?
So, After a long search I have found a solution.
Every time you set text do:
setText("I am a Text",TextView.BufferType.SPANNABLE);
Or after resizing your text just do:
setText(getText(),TextView.BufferType.SPANNABLE);
Finally, I found the reason/solution!!!
This is a known bug for Android 3.1+
Issue 17343
Issue 22493
Possible workaround are:
text.setText(text+"\n");
or
final String DOUBLE_BYTE_SPACE = "\u3000";
text.setText(text + DOUBLE_BYTE_SPACE);
Instead of using:
final String DOUBLE_BYTE_SPACE = "\u3000";
text.setText(text + DOUBLE_BYTE_SPACE);
Better use:
final String DOUBLE_BYTE_WORDJOINER = "\u2060";
text.setText(text + DOUBLE_BYTE_WORDJOINER);
Extra information: Word Joiner is 0 width space.
I found that adding the character "\u200b" does not appear as a broken character, nor does it add a whitespace/linebreak.
For more details on the "ZERO WIDTH SPACE" see: http://www.fileformat.info/info/unicode/char/200b/index.htm
Maybe setting the view to View.GONE, changing your text size, then setting to View.VISIBLE would work?
try calling invalidate() or a variant of invalidate().
From android developer docs:
public void invalidate ()
Since: API Level 1
Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().
http://developer.android.com/reference/android/view/View.html#invalidate()
In main.xml file TextView size given android:textSize="40sp" . and in side java, click on the button change the value here
public void onClick(View v) {
text.setTextSize(12);
}
When I click on the button, the font size shrinks to 12.
However, the result is :
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:background="#80ff0000"
android:text="#string/hello" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
java:
public class FontSizeTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView text = (TextView) findViewById(R.id.test);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text.setTextSize(12);
}
});
}
}
How do I shrink the height of the textView so that it only wraps the actual font?
So, After a long search I have found a solution.
Every time you set text do:
setText("I am a Text",TextView.BufferType.SPANNABLE);
Or after resizing your text just do:
setText(getText(),TextView.BufferType.SPANNABLE);
Finally, I found the reason/solution!!!
This is a known bug for Android 3.1+
Issue 17343
Issue 22493
Possible workaround are:
text.setText(text+"\n");
or
final String DOUBLE_BYTE_SPACE = "\u3000";
text.setText(text + DOUBLE_BYTE_SPACE);
Instead of using:
final String DOUBLE_BYTE_SPACE = "\u3000";
text.setText(text + DOUBLE_BYTE_SPACE);
Better use:
final String DOUBLE_BYTE_WORDJOINER = "\u2060";
text.setText(text + DOUBLE_BYTE_WORDJOINER);
Extra information: Word Joiner is 0 width space.
I found that adding the character "\u200b" does not appear as a broken character, nor does it add a whitespace/linebreak.
For more details on the "ZERO WIDTH SPACE" see: http://www.fileformat.info/info/unicode/char/200b/index.htm
Maybe setting the view to View.GONE, changing your text size, then setting to View.VISIBLE would work?
try calling invalidate() or a variant of invalidate().
From android developer docs:
public void invalidate ()
Since: API Level 1
Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().
http://developer.android.com/reference/android/view/View.html#invalidate()
In main.xml file TextView size given android:textSize="40sp" . and in side java, click on the button change the value here
public void onClick(View v) {
text.setTextSize(12);
}
I am trying to read a EditText box and send its contents to another method from within a Custom dialog box. My current code causes a Force Close. The logcat is very vague... however I know the uncaught exception takes place in this method:
public void getName(){
Dialog dialog = new Dialog(main.this);
dialog.setContentView(R.layout.customdialog);
dialog.setTitle("New Game");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
final EditText inputBox = new EditText(this);
//set up text
final TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Enter Your Name...");
//set up button
final Button button = (Button) dialog.findViewById(R.id.namebutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = inputBox.getText().toString();
setName(str);
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
Here is the custom XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/linearLayout1">
<TextView android:text="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/nameprompt"></TextView>
<EditText android:layout_width="fill_parent" android:id="#+id/editText1" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="Player">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="wrap_content" android:id="#+id/namebutton" android:text="Ok" android:layout_width="fill_parent"></Button>
</LinearLayout>
</RelativeLayout>
Any ideas???
You have the EditText in your XML for the Dialog's layout... and you're properly using findViewById() to instantiate your TextView...
You need to do the same for the EditText, also use findViewById() to instantiate it.
final EditText inputBox = (EditText) dialog.findViewById(R.id.editText1);
Something is up with the inputBox object. You create it in this method, but I don't see you actually adding to the layout anywhere. When this method completes, you'll be displaying the dialog box, but the input box won't be displayed anywhere. In fact, I think that inputBox might be garbage collected since there are not references to if around after the getName() method completes. Therefore when you call get input on it, it might be null.
I think what you meant to do was this:
final EditText inputBox = (EditText)dialog.findViewById(R.id.editText1)