Not able to update TextView's text in custom dialog box - android

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);

Related

Android TextView Visibility in AlertDialog does not change

I am using an AlertDialog created using android.suport.v7.app.AlertDialog and I am having a custom layout for the Dialog with a few EditTexts and a few TextViews.
The TextViews that are used are initially having their visibility set to GONE.
I am using the TextViews mainly to prompt the user for incorrect inputs in the EditText fields.
So when I try to toggle the visibility of the TextView to VISIBLE for showing an error message, the visibility does not change.
My XML File for layoutS is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/contactName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="#string/sampark_naam"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="5dp">
<requestFocus />
</EditText>
<TextView
android:id="#+id/noName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/no_name_error"
android:textColor="#android:color/holo_red_dark"
android:layout_marginEnd="5dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="5dp"
android:visibility="gone"/>
</LinearLayout>
The AlertDialog used is:
final Context context = view.getContext();
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View aDrishya = layoutInflater.inflate(R.layout.layoutS, null, false);
vNaam = (EditText) aDrishya.findViewById(R.id.contactName);
nameError = (TextView) aDrishya.findViewById(R.id.noName);
sSanddok = new AlertDialog.Builder(context)
.setView(aDrishya)
.setTitle("Add A New Contact")
.setPositiveButton("Add +", new PositiveButton(context))
.setNegativeButton("Pick", new null);
sSanddok.show();
In the positive button I check for the contactName entered as:
if(vNaam.getText().toString().equals("")) {
Toast.makeText(this, "No Name", Toast.LENGTH_SHORT).show();
nameError.setVisibility(View.VISIBLE);
} else {
nameError.setVisibility(View.GONE);
}
It is showing the Toast but it's not showing the TextView.
Any idea what's wrong here and how to fix it?
Hi can you try to change and run this code, See if it works..
Alert dialog changes :
sSanddok = new AlertDialog.Builder(context)
.setView(R.layout.layoutS)
.setTitle("Add A New Contact")
.setPositiveButton("Add +", null)
.setNegativeButton("Pick",null);
sSanddok.show();
sSanddok.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//if edit text value is empty do this
TextView tv = (TextView)sSanddok.findViewById(R.id.noName);
tv.setVisibility(View.VISIBLE);
}
});
TextView visibility in xml is set to gone..
Hi this is working for me...Try this code pls

Click on TextView inside inflated Linear Layout

I have an inflated Linear Layout that contains 2 TextViews inside it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_m"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ll_borders"
android:tag="m"
android:text="m" />
<TextView
android:id="#+id/tv_q"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ll_borders"
android:tag="q"
android:text="q" />
</LinearLayout>
All i want is that when this Linear Layout is inflated then i want to get the only TEXTVIEW on which i click. For example if i click on "tv_m" then it shall only return me the text of tv_m.
May b its simple but i am not getting a way to it. So i need help.
Thanks
After inflating the layout get the textview objects as below
LinearLayout layout = inflater.inflate(<your layout name>, null);
TextView textView1 = layout.findViewById(R.id.tv_m));
TextView textView2 = layout.findViewById(R.id.tv_q));
String selectedText;
textView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
selectedText = textView1.getText().toString();
}
});
Similarly you can put listener for textView2 also. The selectedText will be the final string which you want.
You need to set up on click listeners for the text views. Then when one is clicked, it will call a function in your code passing it the view that was touched. Then you can call getText on it.
here is the code just check this out :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout lt = (LinearLayout) findViewById( R.id.linearLayout );
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(your xml to be inflate, null, false);
lt.addView(view);
TextView tv_m = (TextView)view.findViewById( R.id.tv_m);
TextView tv_l = (TextView)view.findViewById( R.id.tv_l);
tv_m.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv_m.getText(); // to get the value written on text view
} });
}

Set height and width in an Dialog Box?

I am using a XML-layout which I am prompting as the dialog box.
Designing of XML-layout is well formatted with enough required height and width..
But when I open it as the dialog box its width is getting disturbed so how to set height and width of dialog box through coding.
I even had referred this previous STACK OVERFLOW QUESTION
Here is the code:
// Layout Inflater Code..
editDialog = new Dialog(this);
layoutEdit = LayoutInflater.from(this).inflate(R.layout.createlayout, null);
//layoutEdit.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
editDialog.setContentView(layoutEdit);
// Called the Dialogbox to inflate
updateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
editDialog.show();
}
});
// XML File Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bd"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="false"
android:text="Enter Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/whtie"
android:typeface="monospace" />
<EditText
android:id="#+id/txtname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
</EditText>
</LinearLayout>
</ScrollView>
Try this...
1.Dialog snippet:
private void CustomDialog(String msg) {
final Dialog dialog = new Dialog(YourActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LinearLayout.LayoutParams dialogParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 300);//set height(300) and width(match_parent) here, ie (width,height)
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dislogView = inflater
.inflate(R.layout.my_custom_popup, null);
dialog.setContentView(dislogView, dialogParams);
TextView popupMsg = (TextView) dialog.findViewById(R.id.popupMsg);
Button popupOk = (Button) dialog.findViewById(R.id.popupOk);
popupMsg.setText(msg);
popupOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
2.Then call CustomDialog(Str) where you want to prompt in your activity.
CustomDialog("This is customized popup dialog!");
You better use an activity that looks like a dialog (I feel it will be better in your case). Here is an example code:
public class DialogActivity extends Activity {
/**
* Initialization of the Activity after it is first created. Must at least
* call {#link android.app.Activity#setContentView setContentView()} to
* describe what is to be displayed in the screen.
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
// See assets/res/any/layout/dialog_activity.xml for this
// view layout definition, which is being set here as
// the content of our screen.
setContentView(R.layout.dialog_activity);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
android.R.drawable.ic_dialog_alert);
}
}
This code is from api demos
View layout = inflater.inflate(R.layout.view, NULL);
layout.setMinimumWidth(200);
layout.setMinimumHeight(200);
dialog.setContentView(layout);
Try
dialog.getWindow().setLayout(height, width);

Text cuts off in Dialog

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();

NullPointerException on setText() method

I have a strange problem I don't understand...
I want to do a PopupWindow from an XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#404040"
android:padding="15px">
<TextView android:id="#+id/popup_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="16dip"
android:gravity="center_horizontal" />
</LinearLayout>
and I just want to do a so simple thing: set the text in my TextView... Here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//UI
ui = new RelativeLayout(this);
ui.setBackgroundColor(Color.LTGRAY);
setContentView(ui);
//PopUp
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popUp = new PopupWindow(this);
popUp.setContentView(inflater.inflate(R.layout.popup_piece, ui, false));
//Boutton
bouton = new Button(this);
bouton.setText("POWPEUP");
ui.addView(bouton);
bouton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
popUp.showAtLocation(ui, Gravity.CENTER, 0, 0);
popUp.update(0, 0, 350, 400);
titrePopUp = (TextView)findViewById(R.id.popup_title); // THIS RETURNS NULL
Log.d("TextView", ""+titrePopUp);
//titrePopUp.setText("blop", TextView.BufferType.NORMAL); SO THIS DONT WORK
}
});
}
Seriously, I don't understand why it returns a NULL value.. Can anyone help me, please?
You are searching for the TextView in your main layout, not in the PopupWindow. You need to do this instead:
titrePopUp = (TextView) popUp.getContentView().findViewById(R.id.popup_titre);
In layout id is spelled as popup_title, and in code it's spelled as popup_titre. Maybe, this is the problem.

Categories

Resources