Android: findViewById in a not main.xml file
-
04-12-2019 - |
Frage
I have two layouts: main.xml and journal.xml When I click on a button in the main.xml layout, the journal.xml layout will be opened.
The problem is that i would like to create a back button which will re-open the main.xml layout. When I try to do:
ImageButton buttonHome = (ImageButton)findViewById(R.id.image);
The image is not found because she is not in my main.xml
Could you please help me.
Thanks
Here is the code:
public class MDPI_1Activity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.journal);
ImageButton buttonJournal = (ImageButton)findViewById(R.id.imageButton1); //Journal
ImageButton buttonHome = (ImageButton)findViewById(R.id.image); //Journal
buttonJournal.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.journal);
}
});
buttonHome.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.main);
}
});
}
}
The "imageButton1" is in my main.xml file and works good but the "image" is in my second xml file named "journal" and does not exist in the R file.
Lösung
EDIT: I don't know if this will work.. but you can set the onClick parameter on your layout (XML) like this:
//Activity
public class MDPI_1Activity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.journal);
}
public void goJournal(View v)
{
setContentView(R.layout.journal);
}
public void goHome(View v)
{
setContentView(R.layout.main);
}
}
//BUTTON IN HOME LAYOUT
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Journal"
android:onClick="goJournal" />
//BUTTON IN JOURNAL LAYOUT
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:onClick="goHome" />
Andere Tipps
I have compiled a code for you. This code is working on my side. You can switch between two layouts using Button in the following way.
public class TestActivity extends Activity implements OnClickListener {
Button btnGoToJournal = null;
Button btnGoToMain = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnGoToJournal = (Button) findViewById(R.id.btn_go_to_journal);
btnGoToJournal.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_go_to_journal:
setContentView(R.layout.journal);
btnGoToMain = (Button) findViewById(R.id.btn_go_to_main);
btnGoToMain.setOnClickListener(this);
break;
case R.id.btn_go_to_main:
setContentView(R.layout.main);
btnGoToJournal = (Button) findViewById(R.id.btn_go_to_journal);
btnGoToJournal.setOnClickListener(this);
break;
}
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_go_to_journal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to journal" />
</LinearLayout>
Journal.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_go_to_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Main" />
</LinearLayout>
You need to use separate activities for your two layouts. You can call and display them like so:
Intent yourIntent = new Intent(currentActivity.this, nextActivity.class);
startActivity(yourIntent);
You can do this on your button click. Don't forget to declare the activities in your AndroidManifest.xml. Let me know if you need any help.