I have 2 textviews on my android app with ids : textView1 , textView2 . Among others on my main layout OnClickListener I am trying to make these 2 textViews INVISIBLE with this :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View activity = findViewById(R.id.mainlayout);
activity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView1.setVisibility(View.INVISIBLE);
textView2.setVisibility(View.INVISIBLE);
}});
}
I get ... cannot be resolved messages .
Thank you guyz all in advance for your immediate responses.
You need to initialize textviews
TextView textView1,textView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
View activity = (View)findViewById(R.id.mainlayout);
Try this:
findViewById(R.id.textView1).setVisibility(View.INVISIBLE);
findViewById(R.id.textView2).setVisibility(View.INVISIBLE);
R.id.textView1 & R.id.textView2 should correspond to the android:id fields of the TextViews you want to make invisible in R.layout.activity_main
If you're still not getting the expected behavior, make sure you're not confusing View.GONE and View.INVISIBLE
Related
lately, I was working on a very small training project.
But there is one error in my code:
public class MainActivity extends AppCompatActivity {
TextView textView = (TextView) findViewById(R.id.textview);
#override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView.setText("Welcome");
}
}
but when I tried to cut the textview global variable and paste it inside the onCreate method, the error has gone. Why is this error occur although I already have inflated the textView in a global variable ?!
You need to call findViewById(...) after calling setContentView(...).
Fetching the view before inflating the layout isn’t possible since there is no view.
findViewById() should only be used after setContentView() in onCreate.
That's where your entire layout get inflated. Only then you can access views using findViewById().
This is what you want:
public class MainActivity extends AppCompatActivity {
TextView textView;
#override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
textView.setText("Welcome");
}
}
I want to create this textview, and then display it after this button is clicked, but no textview is displayed.
I dont't want to use findViewById(), if possible, because I want to create a new textview every time the button is pressed. I've tried making a linear layout first, but I've seen a lot of websites say that you don't need to, and I would prefer not to. Any advice would be helpful. Thank you.
EditText name=layout.findViewById(R.id.enterName);
final String Name=name.getText().toString();
Button create=layout.findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView ProgrammaticallyTextView = new TextView(MainActivity.this);
ProgrammaticallyTextView.setText(Name);
ProgrammaticallyTextView.setTextSize(22);
popup.dismiss();
}
});
There are no error messages and the logcat doesn't say that anything is wrong.
Try like this :
private LinearLayout lLayout;
private EditText lEditText;
private Button lButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lLayout = (LinearLayout) findViewById(R.id.linearLayout);
lButton = (Button) findViewById(R.id.button);
lEditText = (EditText) findViewById(R.id.editText);
lButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("Text New");
}
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
lLayout.addView(createTextView(lEditText.getText().toString()));
}
};
}
private TextView createTextView(String text) {
final LayoutParams loutParams = new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(loutParams );
textView.setText("Text is " + text);
return textView;
}
Use the activity's findViewById() method to reference your layout views. For example, you can replace
EditText name=layout.findViewById(R.id.enterName);
Button create=layout.findViewById(R.id.create);
with
EditText name=getActivity().findViewById(R.id.enterName);
Button create=getActivity().layout.findViewById(R.id.create);
Note: if you are not using fragments then there is no need to use getActivity since findViewById() is a method of the superclass AppCompactActvity( or Activity).
I guess your code is not working because the Button View and Editext Views have not been reference when activity starts for the oncreate() method
This question already has answers here:
How do I declare a TextView variable so that I can use it anywhere?
(2 answers)
Closed 6 years ago.
I am still a Java beginner and I have the problem that I have a onClick function and of course the onCreate in which i define the TextView to a value. But when I want to edit the TextView from the onClick i can't do that. Now I would like to know how I can make the TextView value "global".
final TextView result = (TextView) findViewById(R.id.result);
You have to instantiate the TextView after the setcontentView() method :
public class MainActivity extends Activity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textview);
}
}
You can define your TextView above method onCreate.
TextView result;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
setContentView(R.layout.yourLayout);
TextView result = (TextView) findViewById(R.id.result);
}
In your class you create an attribute:
private Textview tv;
then in onCreate method you initialise this variable like this :
tv = (TextView) findViewById(R.id.result);
Then you can use it wherever you want in your class by calling "tv".
Can someone tell me where I am going wrong?
I'm trying to change my textview called MyText to something else, but when I try I get the error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2071)
public class MainScreen extends Activity {
TextView texting = (TextView) findViewById(R.id.MyText);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
texting.setText("Test");
};
}
This is how your code should look like:
public class MainScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
// If you're not using this view anywhere, you don't have to declare it globally. Use only a local variables
TextView texting = (TextView) findViewById(R.id.MyText);
texting.setText("Test");
};
}
You should ALWAYS initialize your views after you have your root layout. In this case, right after you did setContentView(R.layout.activity_main_screen); This scenario applies to Activites.
In case you use a fragment you should do this right after you inflated your root layout in the onCreateView method. Something like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = (LinearLayout) inflater.inflate(R.layout.my_fragment, container, false);
// THIS is where you start initializing your views
return view;
}
The TextView is not available until after setContentView is called. So, findViewById will return null when MainScreen is instantiated. The solution is to move the findViewById call to the onCreate method as follows:
public class MainScreen extends Activity {
TextView texting;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
texting = (TextView) findViewById(R.id.MyText);
texting.setText("Test");
}
}
Do the following:
public class MainScreen extends Activity {
TextView texting;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
texting = = (TextView) findViewById(R.id.MyText);
texting.setText("Test");
};
}
You have to initialize your TextView object inside your onCreate method.
i have dynamically added fragment(which is inflated from secfrag.xml) in main activity. there are two buttons in my main activity. one for adding fragment and the other is for changing text of my fragment. i have successfully added fragment to my activity but i can not change the textview text of my fragment. how can i do that.
public class MyActivity extends ActionBarActivity {
Button addFragment;
Button changeFragText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
addFragment = (Button) findViewById(R.id.mybtn);
changeFragText = (Button) findViewById(R.id.mybtn2);
addFragment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Secfragjava secfragjava = new Secfragjava();
getFragmentManager().beginTransaction().add(R.id.mainholder, secfragjava).commit();
}
});
changeFragText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//how can i have the fragment and change its textview
TextView frv=(TextView) getFragmentManager().findFragmentById(R.id.container).getView().findViewById(R.id.mysecText);
frv.setText("raton");
}
});
}
You should create a method inside of your fragment for changing the text.
e.g.
public void changeText(){
//this textview should be bound in the fragment onCreate as a member variable
TextView frv=(TextView) getView().findViewById(R.id.mysecText);
frv.setText("raton");
}
Then in your onClickListener you can just call that method:
((Secfragjava)getFragmentManager().findFragmentById(R.id.container)).changeText();