I have to following code for selecting layout on button click.
View.OnClickListener handler = new View.OnClickListener(){
public void onClick(View v) {
switch (v.getId()) {
case R.id.DownloadView:
// doStuff
setContentView(R.layout.main);
break;
case R.id.AppView:
// doStuff
setContentView(R.layout.app);
break;
}
}
};
findViewById(R.id.DownloadView).setOnClickListener(handler);
findViewById(R.id.AppView).setOnClickListener(handler);
When I click the "AppView" button, the layout changes, but when I click the "DownloadView "button, nothing happens.
This link says that I have to start a new activity.
But I don't know how to use the code there of intent to start new activity, will a new file be added?
EDIT: I have my code on the new activity:
package com.example.engagiasync;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class AppView extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.app);
TextView tv = (TextView) this.findViewById(R.id.thetext);
tv.setText("App View yo!?\n");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
but it does not work, it force closes, the logcat says:
To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.
Button btnDownload = (Button) findViewById(R.id.DownloadView);
Button btnApp = (Button) findViewById(R.id.AppView);
btnDownload.setOnClickListener(handler);
btnApp.setOnClickListener(handler);
View.OnClickListener handler = new View.OnClickListener(){
public void onClick(View v) {
if(v==btnDownload){
// doStuff
Intent intentMain = new Intent(CurrentActivity.this ,
SecondActivity.class);
CurrentActivity.this.startActivity(intentMain);
Log.i("Content "," Main layout ");
}
if(v==btnApp){
// doStuff
Intent intentApp = new Intent(CurrentActivity.this,
ThirdActivity.class);
CurrentActivity.this.startActivity(intentApp);
Log.i("Content "," App layout ");
}
}
};
Note : and then you should declare all your activities in the manifest .xml file like this :
<activity android:name=".SecondActivity" ></activity>
<activity android:name=".ThirdActivity" ></activity>
EDIT : update this part of Code :) :
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);// Add THIS LINE
setContentView(R.layout.app);
TextView tv = (TextView) this.findViewById(R.id.thetext);
tv.setText("App View yo!?\n");
}
NB : check this (Broken link) Tutorial About How To Switch Between Activities.
I would add an android:onClick
to the layout and then change the layout in the activity.
So in the layout
<ImageView
(Other things like source etc.)
android:onClick="changelayout"
/>
Then in the activity add the following:
public void changelayout(View view){
setContentView(R.layout.second_layout);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With