Android layout loaded twice - android

On button click event I'm using setContentView(R.layout.activity_main); it works correctly.
When if I try to start new activity with Intent and startactivity commands it loads layout twice it looks like layout loading correctly then 1 second same layout loaded again.
Before start activity its loaded single time.
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String selected = spinner0.getSelectedItem().toString();
if(selected.equals("Item 2"))
{
Intent intent = new Intent(second_layout.this,MainActivity.class);
setContentView(R.layout.activity_main);
startActivity(intent);
}
}
});

I'm guessing this line is the problem.-
setContentView(R.layout.activity_main);
setContentView will just change the layout for the current activity, so you're changing the current layout to activity_main, and then you open the Intent for MainActivity class.
Just remove that line.

When you are starting a new activity, there is no need of setContentView while starting the intent.
The intent which is getting started will have the code for loading the layout. So please remove this line.
I hope, in your MainActivity.class, you will already be writing setContentView(R.layout.activity_main) and this is enough for showing the required layout. So remove this extra line which you have included while starting the intent.

You can not set your second activity layout in your first activity before starting your second activity. It will set automatically in your second activity's onCreate() method. So you should write setContentView(R.layout.activity_main); in your MainActivity's onCreate() method. Just remove it from the onClick listener.
So write in your onClick as below:
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String selected = spinner0.getSelectedItem().toString();
if(selected.equals("Item 2"))
{
Intent intent = new Intent(second_layout.this,MainActivity.class);
startActivity(intent);
}
}
});
And in your MainActivity you have to set your layout as below:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Related

Android Studio onClick and OnclickListener not working once changed to another Activity

I've added my acivity class in this link
Click MeI am fairly new to Android and am having difficulty trying to add a button on my second activity. I am able to place a button in my main activity and then I use it to navigate to my secondary activity (using setContentView(R.layout.)) and then I use the same 'onClick' method or even 'OnClickListener' method but the button on my second activity just wont work on another activity. Maybe i am missing something
]3
just try to do this:
public class FirstActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
findViewById(R.id.about_us).setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
}
});
}
}
and in second activity again find your button in second activity xml by id and write onClickListener for it
You need to implement two separate methods for two different buttons. I would suggest do these things in the Java code instead of XML.
You can do some thing like this:
Button button = findViewById(R.something.something);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//perform your operation(s) here.
}
});
As I understand you try to use one layout.xml for both activities.
You need to declare method click1 in both activities, not only in first.
It means that your first activity has to have method public void click1() and the second activity has to duplicate method public void click1()
I know it's old however,
#Meikiem idea is great. When you use setContentView(View View) you are just setting the activity's
content to another view (xml), and thus not really using the other .java file which has another
onClick method defined for the second button.
Activity's setContentView(view)
You need to create an Intent and pass it along the startActivity method.
Intent Definition

Show checked items from dialog box into the previous activity

I have a listview of shopping lists in a fragment and when I click one I get a new activity that shows a listview with all the products in the clicked shopping list. What I am trying to do is to add products this list by selecting some checkboxes in the newly created activity and pass them to the previous activity.
public class DisplayShoppingListDetailsActivity extends AppCompatActivity{
private ShoppingList list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//defines the activity layout
setContentView(R.layout.shopping_list_details);
ListView listView = (ListView) findViewById(R.id.shopping_list_details);
Intent intent = getIntent();
list = (ShoppingList) intent.getSerializableExtra("ShoppingList");
ProductsOnListAdapter ad = new ProductsOnListAdapter(this, -1, Service.getService().getProductsOnList(list));
listView.setAdapter(ad);
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.floatingButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(new Intent(DisplayShoppingListDetailsActivity.this, ListOfProductsActivity.class));
//This method here is not working...it says it cannot resolve the method
}
});
}
You really should not have one activity try to modify another activity's instance. That will lead to bugs in your code.
Instead, in the first activity, you can use startActivityForResult() to launch the second activity, then the second activity can use setResult() to return data to the first activity. This is all documented if you read the javadoc for Activity under the heading "Starting Activities and Getting Results".

How to make a button open a new layout xml

I'm trying to make a button for my app, which will bring the screen to another page. However, I'm not successful in doing so.
I've tried many things, without a reliefing answer.
My project doesn't accept "Intent" in my program.
My button I need to open a new layout is called "OptionButton"
Here's what I've got:
in MainActivity.java
In the beginning I got this
public class MainActivity extends Activity {
private Button startButton;
private Button pauseButton;
private Button resetButton;
public Button OptionButton;
/** further I got this**/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
/** (I'm just mentioning this because I use savedInstanceState here too)**/
/**MainActivity.java And my code for my button is this **/
OptionButton = (Button) findViewById(R.id.Button1);
OptionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myClick(v); /* my method to call new intent or activity */
}
public void myClick(View v) {
Intent intent = new Intent(this, Background2.class);
startActivity(intent);// for calling the activity
}
});
}
}
}
I added this in AndroidManifest:
<activity android:name=".Background2"></activity>
and this in the 2nd class (java file in src map)
(package & imports, then this:
public class Background2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
_
I got 2 classes in src map:
-Background2.java
-MainActivity.java
Also 2 layout xml's:
activity_main.xml
activity main2.xml
In Activity_main, I got this for the button:
<Button
android:id="#+id/Button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/OptionButtonLabel"/>
Still it's not working. What am I missing?
Ty all so much!
I've tried changing this:
button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(this);
But it didn't work.
Inside of the listener you are calling this (which refers to the listener itself), while what you want to refer to is the MainActivity.
Simply change to
Intent intent = new Intent(MainActivity.this, Background2.class);
You Main Activity has to implement OnClickListner
public class MainActivity extends Activity implements OnClickListener{
From Eclipse IDE press Ctrl+Shift+O it will automatically implement and import necessary functions
Thank you for the answers! :)
I've tried installing it on my real device.
Now, it opens, but it's the 2nd xml that opens, and when I click the button, it re-opens the same xml.
:-/
I don't get any error messages though when I changed your suggested solutions (from both of you)

Android: I have created a new activity, I called it but the window doesn't have the GUI

(I am running this on a Nexus 7) I created a new activity and I have called on it properly. The new window appears correctly, except none of the GUI elements that I declared int eh XML file appear in this new window. How do I fix that?
In my main activity:
final Intent mapIntent = new Intent(this, MapActivity.class);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
Button b = (Button)findViewById(R.id.MapButton);//Finds the button w/id "MapButton"
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(mapIntent);
}
});
And the called activity (MapActivity) is empty except for the super.onCreate(SavedInstanceState) line in onCreate().
Let me guess: you didn't call setContentView(R.id.xml_file_layout) in your Activity onCreate() - just after the call to super.onCreate(savedInstanceState)!
I think you need to set the layout in MapActivity.
setContentView(R.layout.your_layout);
If you are calling super() then always place it properly, just 1st line of the method. Hope this will help you, if you have correctly get what I'm saying.

setcontent view not working

button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setContentView(R.layout.activity_chart);
}
});
Hi i have the above code wherein upon clicking a button i am trying to display them activity activity_chart. In that activity i want to display a graph. Here i am calling a method createIntent(). But my problem is that the graph is not getting plotted. Please help i am new to android.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createIntent();
}
public Intent createIntent()
{
...
}
Am i calling the method right.
A new Activity is called with:
startActivity(new Intent(currentActivity.this, nextActivity.class));
Then in your new Activities onCreate(Bundle savedInstance) method you can call setContentView(Layout layout); to set the new Layout.
So if you want to change the Activity when clicking on a Button you have to do the following:
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(currentActivity.this, nextActivity.class));
}
});
You are currently only changing the layout of the current Activity when clicking the button and not changing to another Activity.
I hope I understood you correctly. If not then provide me with some more code so I can try to understand what you want to do.

Categories

Resources