Unable to set data from a Dialog on a TextView - android

I read data from a Dialog but I'm unable to set that data on a TextView.
What am I doing wrong?

// We can't imagine what you are doing wrong, without seeing your source code.
TextView facePalm = (TextView)findViewById(R.id.youtextview);
facePalm.setText("Like Octavian Damiean said you should improve your question.");

When you customize a dialog, you should do it like this:
LayoutInflater inflater = LayoutInflater.from(this);
final View layoutview = inflater.inflate(R.layout.rootview, null);
final TextView title=(TextView) layoutview.findViewById(R.id.text);

Related

How to getText from another Layout from editText Android

I am making an activity in which an alertDialog will appear. The dialog will have a view which contains:
Linear layout with two editText (Numeric texts) and then setHeight(wrapContent). You can see below.
Hope you are understanding now what i am doing. Here the view is a xml file which i created and it is not the activity's xml file. Now I want that when a user put the pin in the editText then i can get it and confirm it or whatever i want. But the problem is that i am not getting anything when i call getText(). Here is my code.
LayoutInflater factory = getLayoutInflater();
View view = factory.inflate(R.layout.zalert_pass_enable, null); // zalert_pass.. is the xml file
EditText passInput1 = view.findViewById(R.id.inputPass);
EditText passInput2 = view.findViewById(R.id.inputPassConfirm);
String value = passInput1.getText().toString().trim();
I edited my code to so that you can understand, this is not the actual code but it can easily tell my problem. When i toast the value String then i get nothing. Like there are 0 charaters. But i am putting 4 charaters at least each time. Now i know that the problem is in the process of linking the xml file to inflator.
infalInflater.inflate(R.layout.list_item, parent, false);
See the above line of code. This a well upvoted answer of stackOverflow. which tells us that we should not null the inflator. We should give it parent. But what will be the parent in my case.
You can see the picture and that is my problem and i want a solution for that, please help.
Add this line Dialog dialog = new Dialog(context); after you are inflating view and use it to get edittext object like this
LayoutInflater factory = getLayoutInflater();
View view = factory.inflate(R.layout.zalert_pass_enable, null);
Dialog dialog = new Dialog(context); // add this line
EditText passInput1 = dialog.findViewById(R.id.inputPass);
EditText passInput2 = dialog.findViewById(R.id.inputPassConfirm);
String value = passInput1.getText().toString().trim();

Unable to add dynamic textView in android

I want to add textview dynamically for which I have added the below code.But I am do not see any textview on my activity. I cant add a listview as I am already using recycleview . Any one any error ?
{
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout review_layout = (LinearLayout) rootMovieView.findViewById(R.id.review_comment);
for (ReviewsResult comment : review.getReviewsResults()) {
View view = layoutInflater.inflate(R.layout.layout_movie_comments, review_layout, false);
TextView tvCommentBy = (TextView) view.findViewById(R.id.tv_comment_by);
TextView tvCommentContent = (TextView) view.findViewById(R.id.tv_comment_content);
tvCommentContent.setText(comment.getContent());
tvCommentBy.setText(comment.getAuthor());
review_layout.addView(view);
}
}
You can also try something like this
View myLay; // view that contains you textView
LinearLayout rootlay; //root view in which you want to add
myLay = LayoutInflater.from(this).inflate(R.layout.layout_movie_comments,
null);
TextView tvCommentBy = (TextView) view.findViewById(R.id.tv_comment_by);
TextView tvCommentContent = (TextView) myLay.findViewById(R.id.tv_comment_content);
tvCommentContent.setText(comment.getContent());
tvCommentBy.setText(comment.getAuthor());
rootlay.addView(myLay);
Hope this helps :)
Do below two changes and check weather it is working or not, as nothing else seems to wrong in code.
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.layout_movie_comments, null);
rootMoviewView is not the rootView it was a recylceView in a rootView .This way the textview is generated in rootView not in recylceView.

settext to a dynamic added textview in another activity

I created two activites, the first one is containing the dynamic added textview and the second one will do some operation then settext to the dynamic added textview in activity one. please help to suggest a sample code for my reference. Thank you!
To add textViews can dynamically using the following method.
private void addChild(boolean right) {
LayoutInflater inflater = LayoutInflater.from(this);
int id = R.layout.unLayout_;
RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(id, null, false);
TextView textView = (TextView) relativeLayout.findViewById(R.id.textViewDate);
textView.setText(String.valueOf(System.currentTimeMillis()));
layout.addView(relativeLayout);
}

Accessing the Spinner's Layout and set its TextView

I am trying to set the TextView of the Spinner, but I cannot reach it. There are many solutions about this issue but all of them solve from Spinner Adapter. In my issue I must not change the text from Adapter. I want to change it from the spinner object itself.
So I tried several ways to solve it.
1)
LayoutInflater layoutInflater = LayoutInflater.from(spnSektorInsert.getContext());
View convertView = layoutInflater.inflate(R.layout.spinner_item, null);
TextView txtSpinner = (TextView) convertView.findViewById(R.id.txtSpinner);
txtSpinner.setText("Something");
txtSpinner.setTextColor(Color.RED);
2)
View convertView = spnSektorInsert.getAdapter().getView(0, null, null);
TextView txtSpinner = (TextView) convertView.findViewById(R.id.txtSpinner);
txtSpinner.setText("Something");
txtSpinner.setTextColor(Color.RED);
3)
TextView txtSpinner = (TextView) spnSektorInsert.getChildAt(0);
txtSpinner.setText("Something");
txtSpinner.setTextColor(Color.RED);
In the first two ways, nothing happened, in the last way, the application crashed with NullPointerException. Any ideas will be appreciated.

Android: findviewbyid: finding view by id when view is not on the same layout invoked by setContentView

I have an activity MyActivity that extends from MapActivity. In the .xml file containing the layout I can only include the MapView
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/trail_map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="key"
/>
However I do need to find another view that is located in another .xml file.
Unfortunately, findViewById returns null.
How can I get the view I am looking for?
Thanks a lot!
Thanks for commenting, I understand what you mean but I didn't want to check old values. I just wanted to get a pointer to that view.
Looking at someone else's code I have just found a workaround, you can access the root of a layout using LayoutInflater.
The code is the following, where this is an Activity:
final LayoutInflater factory = getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.landmark_new_dialog, null);
landmarkEditNameView = (EditText) textEntryView.findViewById(R.id.landmark_name_dialog_edit);
You need to get the inflater for this context, access the root view through the inflate method and finally call findViewById on the root view of the layout.
Hope this is useful for someone! Bye
I have changed in my activity but effected.
Here is my code:
View layout = getLayoutInflater().inflate(R.layout.list_group,null);
try {
LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.ldrawernav);
linearLayout.setBackgroundColor(Color.parseColor("#ffffff"));
}
catch (Exception e) {
}
}
try:
Activity parentActivity = this.getParent();
if (parentActivity != null)
{
View landmarkEditNameView = (EditText) parentActivity.findViewById(R.id. landmark_name_dialog_edit);
}
Another way to do this is:
// inflate the layout
View myLayout = LayoutInflater.from(this).inflate(R.layout.MY_LAYOUT,null);
// load the text view
TextView myView = (TextView) myLayout.findViewById(R.id.MY_VIEW);
It's impossible. You can only find and access views that are currently running. If you want to check the value of ex. TextView used in previus activity you must save the value is SharedPreferences, database, file or pass by Intent.
I used
View.inflate(getContext(), R.layout.whatever, null)
The using of View.inflate prevents the warning of using null at getLayoutInflater().inflate().
In main Activity just create Static Variable Like below.
//Create Static variable
public static View mainView;
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.activity_home, null);
mainView = view;
//Now just need to Call MainActivity.mainView to Access your home view or Something else.

Categories

Resources