Why to inflate layout into another layout in Android? - android

I'm new in Android development and I wanted to make application that has header, body and footer and by clicking on one of the buttons in footer some layout will be loaded into body. I used some kind of "MasterPage" as described here.
When the button is pressed neither new_exercise layout nor exercises layout is loaded. Why? Maybe instead of all of this I should use any kind of tabs? Or maybe I can't inflate layout and should create new activity?
Here the code of the BaseActivity and NewExercise activity:
public class BaseActivity extends Activity{
LinearLayout linBaseBody;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.base_layout);
linBaseBody = (LinearLayout)findViewById(R.id.base_body);
initButtons();
}
#Override
public void setContentView(int id) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(id, linBaseBody);
}
private void initButtons()
{
Button btn1 = (Button) findViewById(R.id.newEx);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setContentView(R.layout.new_exercise);
}
});
Button btn2 = (Button) findViewById(R.id.showAllEx);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setContentView(R.layout.exercises);
}
});
}
public class NewExercise extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.new_exercise);
}
}
public class Exercises extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exercises);
}
}

How your code is written, it would make more sense to use a new Activity. However, If you wanted to keep all of the view in one Activity, you could walk through all of your layouts calling mLayout.setVisible(View.VISIBLE); or you could use ViewStubs.
As to answer your question, why, what you are doing is adding the view (and their layouts) directly to your already created and inflated content view (the one you created in onCreate). You will need to clear the Activities contentView first to see the changes you are making with the button.

Related

How to move a View from one ViewGroup to another

I have LinearLayout1, LinearLayout2 and a Button in MainActivity. When I click the Button, I want it to jump from LinearLayout1 to LinearLayout2. How can I do that?
You can do like this :
LinearLayout mLinearLayout1 = (LinearLayout)findViewById(R.id.linearlayout_1);
LinearLayout mLinearLayout2 = (LinearLayout)findViewById(R.id.linearlayout_1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mLinearLayout1.setVisibility(View.GONE);
mLinearLayout2.setVisibility(View.VISIBLE);
}
});
Thanks to all of you specially Mike M
i get my answer with:
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
final LinearLayout mLinearLayout1 = (LinearLayout)findViewById(R.id.liner1);
final LinearLayout mLinearLayout2 = (LinearLayout)findViewById(R.id.liner2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mLinearLayout1.removeView(button);
mLinearLayout2.addView(button);
}
});
}
}
If you want one of the many different views (not LinearLayouts) to be displayed right on the main activity (may depend on a condition or a state engine or time based interval), you probably can use ViewFlipper.
You can hide and show one or the other, for example:
LinearLayout mLinearLayout1 = (LinearLayout) findViewById(R.id.linearlayout_1);
LinearLayout mLinearLayout2 = (LinearLayout) findViewById(R.id.linearlayout_1);
mLinearLayout1.setVisibility(View.GONE);
mLinearLayout2.setVisibility(View.VISIBLE);
But If you want to show an hide some views with diifferent behavior I recommend you using Fragments.

Unable to start the Android application - NullPointerException on Click Listener

I'm having some problems trying to navigate between screens im my Android Project. I'm not creating other Activities classes yet, I'm just trying to open other XML files by the SetContentView(R.layout.XXX). Here is my main Activity:
package com.android.eduardo.navegacao;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class NavegacaoActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
chamaTelaPrincipal();
Button btCadastro = (Button) findViewById(R.id.btCadastro);
btCadastro.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamaCadastro();
}
});
Button btConsulta = (Button) findViewById(R.id.btConsulta);
btConsulta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamaConsulta();
}
});
Button btVoltar1 = (Button) findViewById(R.id.btVoltar);
btVoltar1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamaTelaPrincipal();
}
});
}
public void chamaCadastro(){
setContentView(R.layout.activity_cadastro);
}
public void chamaConsulta(){
setContentView(R.layout.activity_consulta);
}
public void chamaTelaPrincipal(){
setContentView(R.layout.activity_navegacao);
}
}
As you can see, the "R.layout.activity_navegacao" is my main layout. When I try to execute this code the application closes and I receive a NullPointerException error, indicating some problems on SetContentView.
When I cut the code of the last setOnClickListener (the button "btVoltar") it works and I can open the two other screens. The button "btVoltar" is being used by the other XML to return to the main screen (activity_navegacao).
I already checked the id of the XML on the R class, and it's ok. I also don't receive any error notifications until I execute the project. Sorry for the bad english, if you guys can help me, I appreciate.
You are getting the NullPointerException because you are referencing a button that is on an XML layout which has not been displayed. (ie, the button is not found on activity_navegacao.xml).
For this reason, you should not call setContentView multiple times to change the view, like you are doing in this code. Instead, you should consider either making each screen a different activity (and calling setContentView only once per activity), or look into Fragments and FragmentTransactions. Fragments will allow you to replace the view, like you are attempting to do here.
You Can Initialize views only view is present in screen.In Your You clearly saying that the Button "btVoltar" not present in your current screen(activity_navegacao). So Initialize the view after its screen come to present.So Change your code like below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
chamaTelaPrincipal();
Button btCadastro = (Button) findViewById(R.id.btCadastro);
btCadastro.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamaCadastro();
}
});
Button btConsulta = (Button) findViewById(R.id.btConsulta);
btConsulta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamaConsulta();
}
});
}
public void chamaCadastro(){
setContentView(R.layout.activity_cadastro);
}
public void chamaConsulta(){
setContentView(R.layout.activity_consulta);
}
public void chamaTelaPrincipal(){
setContentView(R.layout.activity_navegacao);
Button btVoltar1 = (Button) findViewById(R.id.btVoltar);
btVoltar1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamaTelaPrincipal();
}
});
}
}

Android OnClickListener not firing for button on separate Layout

I have two different layouts. One is which loads while start of the Activity and the other which loads after running some checks and creates a custom dialog. The Dialog has a button in it to trigger, at this point in time, onclick has a Toast message so I can confirm that the button has been clicked. Unfortunately I can't able to get any response when the button is clicked. I've been all over the web and I can't quite find what I'm missing.
public class myactivity extends Activity{
Dialog accesspopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null);
final Button cabtn = (Button)inflatedView.findViewById(R.id.cb);
cabtn.setOnClickListener(cListener);
}
private OnClickListener cListener = new OnClickListener() {
public void onClick(View v) {
//Log.d("HiThereActivity", "THIS IS DEBUG OUTPUT TO LOGCAT");
Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
}
};
public void showPopup(){
accesspopup = new Dialog(myactivity.this);
accesspopup.setContentView(R.layout.pop_window);
accesspopup.setCancelable(false);
accesspopup.setTitle("Window Title");
accesspopup.show();
}
I did some more searching around and found that I need to create the OnClickListener inside the method which I am using to build and display the Dialog and not in the OnCreate.
use this way...
public class myactivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null);
final Button cabtn = (Button)inflatedView.findViewById(R.id.cb);
cabtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
}
});
}
May be still your R.layout.activity_myactivity is the controllable Contentview in your activity.
So you have to define your new layout as setContentView.
or you mentioned it is a Dialog box.
So you can add a content view for a dialog like the following,
Dialog d = new Dialog (this);
d.setContentView(your inflated view);

Android: Can i show multiple Dialogs one over another? Is there something like Dialog Z-Level?

Is it possible to show multiple Dialogs one over another? Is there something like Dialog Z-Level?
I am using DialogFragment where user chooses elements, when he comfirms his choice, it is saved to database and sent on server. if the save action fails I would like to inform user with ... another dialog is it possible? And will it not clear off my first dialog?
Thanks in advance.
Indeed, it's possible to show multiple dialog Fragments one inside another one. The z-order depends on the order they are created.
In the code below there is an example of a FragmentActivity with the behavior that you require.
public class MyActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
//...
}
public void onSave(View view) {
Intent intent = getIntent();
this.setResult(RESULT_OK, intent);
finish();
}
public void onCancel(View view) {
finish();
}
public void SelectWeekDay(View view) {
DialogFragment selectWeekDayFragment = new SelectWeekDayFragment();
selectWeekDayFragment.show(getSupportFragmentManager(), "WeekDayDialog");
}
public class SelectWeekDayFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.week_day_dialog, container, true);
Button saveButton = (Button) view.findViewById(R.id.button_save);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CheckBox checkboxMonday = (CheckBox) getDialog().findViewById(R.id.checkBox_monday);
if (!checkboxMonday.isChecked()) {
DialogFragment saveErrorFragment = new SaveErrorFragment();
saveErrorFragment.show(getSupportFragmentManager(), "SaveErrorFragment");
}
else {
SaveToDb(); //Perform actions to store on db or what you wish
dismiss();
}
}
});
Button cancelButton = (Button) view.findViewById(R.id.button_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
return view;
}
}
public class SaveErrorFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setMessage("You must select Monday").setPositiveButton("Ok", null).create();
}
}
}
My advice is to use a custom layout with a ViewFlipper inside your dialog so you can easily switch between a progress-bar or whatever different layouts you want to show. If you want to show multiple Dialogs my guess is that the z-order depends on the order they were created the latest beeing shown on top.
You usually can, however, just be a little careful. Use the dialog's lifecycle to your advantage to avoid side-effects. For example: you can do a check on a function like onStop() to see if the child dialog is open, and if so, close it.
Ideally, cutting down on the amount of layers of dialogs you have is ideal, as long as it's sane (for example: doing it ends up being hundreds of lines of code more)

NullPointerException at Home.onCreate

I am trying to run a test program that allows a user to click a button and move to a different screen. I have the Home(First Activity) and Away(Second Activity) classes and a xml file specifying the layout for each. My source code is as follows:
public class Home extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button create = (Button)findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(Home.this, Away.class);
startActivity(intent);
}
});
setContentView(R.layout.main);
}
}
And Away.java
public class Away extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.away);
}
}
I get a NullPointerException in the DDMS trace
at Home.onCreate(line 17)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2627)
Anyone see anything in my code that may be causing this?
findViewById() traverses the view hierarchy set up when setContentView() inflates your layout. You cannot retrieve a reference to a view in your XML before you have called setContentView(). Change your onCreate() method in Home to look like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Call me first
setContentView(R.layout.main);
Button create = (Button)findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(Home.this, Away.class);
startActivity(intent);
}
});
}
Otherwise, findViewById() will return null because there are no views in the tree...thus, a view with your requested id value doesn't exist.
Hope that helps!
If the findViewById call can't find the actual thing you are looking for, it could return a null object and cause the call to setOnClickListener to throw an NPE. Are you sure you've got the right ID (R.id.create) specified?

Categories

Resources