Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Button to change view
    primarykey
    data
    text
    <p>I have a problem I cant get my head around, I am fairly new to android and I am struggling with a basic concept of using a button to change the layout view. I have my activity Main as so </p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/bg" android:orientation="vertical" tools:context=".MainActivity" &gt; &lt;DigitalClock android:id="@+id/DigitalClock" android:layout_width="fill_parent" android:layout_height="100dp" android:textColor="#ffffff" android:gravity="center" android:textSize="70sp" /&gt; &lt;View android:layout_width="fill_parent" android:layout_height="3dp" android:background="@color/line" /&gt; &lt;Button android:id="@+id/AddAlarm" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/add_button" /&gt; &lt;Button android:id="@+id/AddAlarm2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/add_button" /&gt; </code></pre> <p></p> <p>I can make the AddAlarm button open the add_alarm layout. But when I have opened add_alarm There is a button on that view that I need to open add_alarm2, but it wont work Here is my MainActivity.java :</p> <pre><code>package com.example.emma; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.DigitalClock; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DigitalClock dc = (DigitalClock) findViewById(R.id.DigitalClock); final Button button = (Button) findViewById(R.id.AddAlarm); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { setContentView(R.layout.add_alarm); } }); final Button button1 = (Button) findViewById(R.id.AddAlarm2); button1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { setContentView(R.layout.add_alarm2); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } </code></pre> <p>I kind of understand how to use a button click to change the view but I am obviously doing something wrong. Here is my add_alarm2 layout code. But when you click the imagebutton android:id="@+id/AddAlarm2" It does nothing instead of opening the view add_alarm2.xml Any help would be greadly appreciated </p> <p>here is my add_alarm2.xml file : </p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@color/bg" tools:context=".MainActivity" &gt; &lt;ImageButton android:id="@+id/AddAlarm2" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@null" android:src="@drawable/normal" android:scaleType="fitCenter" /&gt; &lt;View android:layout_width="fill_parent" android:layout_height="1dp" android:background="@color/line" /&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload