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();
Related
I am new to Android Programming and I am making a simple browser in which I want to open my web activity by clicking the button but I am amazed to see that setOnClicklistener is not available in Android Studio 3.5 as I have just updated
You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener().
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filters);
Button button = findViewById(R.id.google);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO()
}
}
}
Access/init Your button inside onCreate() method.
private Button btn;
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.home_search_layout);
btn = findViewById(R.id.someId);
setClickListener();
}
private void setClickListener() {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
All the functional elements' code is to be initialized in the activity's onCreate() method. Since you want to make a button clickable, you need to add the setOnClickListener() in the onCreate() method, like so:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectionactivity);
Button button = findViewById(R.id.google); //this id should be of a button not a view
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO do something
}
}
}
I highlighted some text because from the image, you can see that the 9th that you get from the code completion menu is the setOnClickListener but the parent is from the group android.view.View but not from android.widget.Button. Make sure, that R.id.google is a Button and also put the code inside onCreate()
I had created a simple message passing application in android studio to include this code into my Project work.But I was not able to switch the activity by passing a string into another activity.please help me out to find the problem.
When the button is triggered the application got ended itself instead of switching the activity.I had tried many ways to figure it out.
First Activity(MainActivity)
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn;
EditText text;
btn = (Button)findViewById(R.id.button);
text = (EditText)findViewById(R.id.editText);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String mess = text.getText().toString();
Intent i= new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("EXTRA",mess);
startActivity(i);
}
});
}
}
Second Activity(Main2Activity)
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
EditText editText = (EditText)findViewById(R.id.editText2);
editText.setText(getIntent().getStringExtra("EXTRA"));
}
}
The expected output was to exchange the data from one activity to another,
but now the application gets stopped.
I made one of the simplest programs that creates a login page, however, I cannot add an OnClickListener to my button, and I do not know why. I'm really new to Android Studio and have no idea what to do. When I hover over the error it says "In View cannot be applied".
I've tried these bits of code found on the internet, but the error doesn't leave, and the machine says that the #Override does not override what's above.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_in_j);
regist1 = (Button)findViewById(R.id.btnregister1);
regist1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent teachtoregist = new Intent(TeacherInJ.this, TeacherRegisterInJ.class);
startActivity(teachtoregist);
}
});
etUsername = (EditText) findViewById(R.id.editText2);
etPassword = (EditText) findViewById(R.id.editText3);
bLogin = (Button) findViewById(R.id.btnlogin);
bLogin.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlogin:
//Start activity one
break;
When I hover over the error it says "In View cannot be applied".
I've tried these bits of code found on the internet, but the error doesn't leave, and the machine says that the #override does not override what's above.
Both the above problems are because you didn't add the implements keyword to your Activity. You need to add it so the Activity can be regarded as OnClickListener interface by the button.
You need to do something like this (See the comments inside the code):
// see below the implements View.OnClickListener line that
// need to be added so the Activitiy can be regarded as the listener.
public class TeacherInJ extends AppCompatActivity implements View.OnClickListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_in_j);
...
// now you can use this as the listener. It's because you have
// set the current Activity class as the View.OnClickListener
// this is refer to current Activity object.
bLogin.setOnClickListener(this);
}
// Now you can add the #Override to the onClick method from
// the View.OnClickListener.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlogin:
//Start activity one
break;
}
}
}
I created a project that use RecyclerView and CardView (for PointOfInterest). These 5 activities are relate to each other :
PointOfInterest.java
PlacesAdapter.java
Places.java
layout_poi.xml
activity_point_of_interest.xml
Meanwhile in activity_main.xml I design the Main Menu together with some buttons. One of the button named Rapid Penang (id: rapid_btn). I call an activity of Rapid Penang (from MainActivity.java) like below:
public class MainActivity extends AppCompatActivity {
private Button button_for_rapid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Rapid Penang class
button_for_rapid = (Button) findViewById(R.id.rapid_btn);
button_for_rapid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openRapid();
}
});
}
public void openRapid()
{
Intent intent_rapid = new Intent(this, RapidPenang.class);
startActivity(intent_rapid);
}
}
RapidPenang consist of only one activity and it is success. But when I try to do exactly the same to PointOfInterest activites (as mention above), suddenly the app were crashed.
This is how I try to open PointOfInterest activites from a button in MainMenu called Point Of Interest:
private Button button_for_poi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Point Of Interest class
button_for_poi = (Button) findViewById(R.id.poi_btn);
button_for_poi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openPOI();
}
});
}
public void openPOI()
{
Intent intent_poi = new Intent(this, PointOfInterest.class);
Intent intent_poi2 = new Intent(this, PlacesAdapter.class);
Intent intent_poi3 = new Intent(this, Places.class);
startActivity(intent_poi);
}
Firstly check your activity is define in Android manifest file and then call
StartActivity(new Intent(getApplicationcontext(),RapidPenang.class));
That's it
I am trying to create an Android app, and i want to create a on click listener, here is what I have so far.
public void amazonListener() {
amazonButton = (Button) findViewById(R.id.amazonButton);
}
As you see, i am in the very early stages, but where I first referenced amazonButton (before the = sign) button, it turns into red text and it says Cannot resolve symbol 'amazonButton'. Also, I have referenced this method in the onCreate method
This is how you'd go about creating a button and setting a click listener to it.
public class MainActivity extends YouTubeFailureRecoveryActivity {
Button amazonButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
amazonButton = (Button) findViewById(R.id.amazonButton);
amazonButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Define what should happen when the button is clicked.
}
});
}
}
You can also put the initializations in a single method and call that method, like you've tried :
public class MainActivity extends YouTubeFailureRecoveryActivity {
Button amazonButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
}
private void initializeViews() {
//Make sure you've declared the buttons before you initialize them.
amazonButton = (Button) findViewById(R.id.amazonButton);
amazonButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Define what should happen when the button is clicked.
}
});
// Add more Views initialization here ...
....
}
}
You need to give the type of the variable when you declare it.
Button amazonButton = (Button) findViewById(R.id.amazonButton);
The alternative is to declare it (but not initialize it) outside of any method and then initialize it later.
Button amazonButton;
/* ... */
private void amazonListener() {
amazonButton = (Button) findViewById(R.id.amazonButton);
}