I'm trying to execute more then one layout screens from single class using onClick() method
Here goes my code
Button bt1,bt2;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.button);
bt2 = (Button)findViewById(R.id.button1);
onClick(); //onClick(View) in MainActivity cannot be applied to ()
}
public void onClick(View v){
if(v.getId()==R.id.button){
setContentView(R.layout.next1);
}
else if(v.getId()==R.id.button){
setContentView(R.layout.next1);
}
}
Kindly help me out, Thank you
Fragment is what you have to use, in this case. Load either of the fragment based on button click.
If you go for setContentView in this scenario, then your code will be clumsy and it will be very tedious for you to keep track on view.
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 have a problem and I can't seem to set 2 onClickListener for 2 separate buttons located on 2 different layout, when running the program, it cause an exception to occur.
btnClickToSecondPage button is located in activity_main.xml layout and btnObjClickToGoToFirstPage button is located at second_activity.xml layout.
The java code for my program is located below here
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(getWindow().FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button btnObjClickToGoToSecondPage = (Button) findViewById(R.id.btnClickToSecondPage);
Button btnObjClickToGoToFirstPage = (Button) findViewById(R.id.btnChangetoFirstPage);
btnObjClickToGoToFirstPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.second_activity);
}
}
);
btnObjClickToGoToSecondPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.activity_main);
}
}
);
} }
Please help me rectified the problem thanks.
Please implement the View.Onclick listener not Button.onclick listener
btnObjClickToGoToFirstPage.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.second_activity);
}
}
);
It is not a proper way switching pages in Android. Use two activities for switching pages with intents.
Intent newPage = new Intent (this, YourActivityNameForNewPage.class);
startActivity(newPage);
Put above code in your button's onClick().
If you want to show a new page you either start a new activity or start a new fragment.
Changing the contentView is not the correct way to approach this and should not be done.
You refer to the documentation on activities here.
Assuming you have another Activity called SecondActivity here is how you would start it:
btnObjClickToGoToSecondPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
);
And then you define the layout in the XML of the new activity, that is second_activity.xml
If they all have similar layouts using a fragment is also a good option.
Basically you start a new activity or fragment to show anything new or change the data dynamically say on your button's onClick().
This question may further clear your doubts:
What is setContentView(R.layout.main)?
I am trying to display a button on my android app but everytime i run the app it crashes. i realise this is because i use setContentView multiple times? I dont understand how it works, and dont understand how i can fix this problem so my button will display. my code is below.
public class MainActivity extends Activity {
Draw draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new Draw(this);
draw.setBackgroundColor(Color.BLUE);
setContentView(draw);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
setContentView(l);
l.addView(new Draw(this));
//setContentView(R.layout.activity_main);
setUpBlockBtn();
}
private void setUpBlockBtn(){
setContentView(R.layout.activity_main);
Button addBlockButton = (Button)findViewById(R.id.btnBlock);
addBlockButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("DemoButtonApp", "you clicked the button");
//finish();
}
});
}
You try to access Button from android xml layout but you do not set this layout in Activity.
Put you button activity_main.xml and use this button in your activity.
Thanks
You can create one more layout and add Draw and Linear layout to that layout.
Something like this.
LinearLayout l1=new LinearLayout(this);
l1.setOrientation(LinearLayout.VERTICAL);
l1.addView(draw);
l1.addView(l2) // your linearLayout.
setContentView(l1)
Remember you can't use setContentView more than one time.
There should be top layout which includes subview and other layouts and then you can add that layout to your activity.
I'm new to Java and I encountered the following code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = new Button(this);
button.setText("Touch That!");
button.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.this.onButtonClick(v);
}
});
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rootlayout);
relativeLayout.addView(button);
}
public void onButtonClick(View view){
//do something when button is clicked.
}
}
I didn't understand the syntax, View.OnClickListener() c'tor is called and it is followed by {} and overidding method.
what does this syntax stand for?
to which object this refers?
My guess is the button. but if I'm right why to use MainActivity.this instead of this? (the object that invoked the method)
This is an anonymous class declaration. It means that you will override some methods inside the class dynamically.
Take a look at this arcticle:
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
About your second question, MainActivity.this refers to the instance of the Activity you are currently in. If you call only this, it would refer to the actual object. When you call MainActivity.this, you will get the instance of MainActivity you are in, even if there is more activities created. Take a look at Android's activity lifecycle.
What's the difference between this and Activity.this
Hope it helps.
By calling
new View.OnClickListener(){}
you are creating an object implementing interface OnClickListerner that requires you to implement the click method.
Someone can correct if I am wrong.
I'm trying to learn android development with Android Studio but I can't seem to figure out why my clicks are not registering. Is there something I'm missing here?
I don't like the way Stack Overflow FORCES me to format things they way THEY want. Here is pastebin! :_
http://pastebin.com/hdzZpsud
call this changeText() method inside onCreate() method of your activity
your activity should look like this:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
changeText();
}
public void changeText(){
final Button button = (Button) findViewById(R.id.button1);
final TextView text = (TextView)findViewById(R.id.largetext);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setText("Button has been pressed");
text.setText("The large text has been changed");
}
});
}
}
In fact, you didn't set the listener. So you should add changeText() after setContentView(R.layout.activity_my);.