How does setContentView work in android? - java

I am trying to display a button on my android app but everytime i run the app it crashes. i realise this is because i use setContentView multiple times? I dont understand how it works, and dont understand how i can fix this problem so my button will display. my code is below.
public class MainActivity extends Activity {
Draw draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new Draw(this);
draw.setBackgroundColor(Color.BLUE);
setContentView(draw);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
setContentView(l);
l.addView(new Draw(this));
//setContentView(R.layout.activity_main);
setUpBlockBtn();
}
private void setUpBlockBtn(){
setContentView(R.layout.activity_main);
Button addBlockButton = (Button)findViewById(R.id.btnBlock);
addBlockButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("DemoButtonApp", "you clicked the button");
//finish();
}
});
}

You try to access Button from android xml layout but you do not set this layout in Activity.
Put you button activity_main.xml and use this button in your activity.
Thanks

You can create one more layout and add Draw and Linear layout to that layout.
Something like this.
LinearLayout l1=new LinearLayout(this);
l1.setOrientation(LinearLayout.VERTICAL);
l1.addView(draw);
l1.addView(l2) // your linearLayout.
setContentView(l1)
Remember you can't use setContentView more than one time.
There should be top layout which includes subview and other layouts and then you can add that layout to your activity.

Related

Adding a button to activity without XML or Layout

Android Studio
We have a project (game) that we need to do android game, a space racing game and we're almost done. We just need to add a retry button and exit button on the game view. The code below is on my GamePlayScene.java and it doesn't have a XML file on the layout folder. They said that it is connected through the game itself. I'm super new to coding a game and on Java language. When I press the start button on the emulator it goes to MainActivty2.java and that MainActivity2 has a XML or on the layout folder but it doesn't have any designs on it, it doesn't have spaceship or anything that we can see if we run the game or start the game on the emulator. So I tried to add a button on that XML and when I tried to press the start button, the message appears "Unfortunately, SpaceRacing has stopped". I hope someone can understand me with this.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Button button= new Button (this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
params.topMargin = 0;
params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
button.setText("Retry");
addContentView(button, params);
// setContentView(tv);
button.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(this, MainActivity2.class);
}
});
}
The bold letter or has " ** ** " on the code has a red font on the studio.
You need to start with a setContentView so your activity will have an initial layout.
To that method you will pass either a view or a layout id.
So if for example you have a layout file named activity2_layout.xml your onCreate method should look something like this
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_layout)
And then you call your button with
findViewById(R.id.buttun_id)
And if you want to add the button there without XML, you first find your layout and then call
myLayout.addView(button)
This is really Android basics, you probably want to take a look at this before proceeding

How to add a button to a layout in another .xml

This is the code I have in mainActivity. I want to add a button in the layout but in another activity. How can I do this?
layout = (LinearLayout)findViewById(R.id.linear);
Button btnTag = new Button(this);
btnTag.setText(name);
btnTag.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
btnTag.setId(tel);
layout.addView(btnTag);
As #WoogieNoogie said, certainly there is much better ways to do what you are looking for, but if it is a must then save add button to the layout and keep it invisible, then set a Boolean variable and save it into preferences, then on activity oncreate read preference Boolean state and set visibility as visible or gone.
I hope you know how to work with preferences.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphTemperature GT = new GraphTemperature(getApplicationContext());
layout = (LinearLayout) findViewById(R.id.statsviewlayout);
Button buyButton = new Button(this);
buyButton.setText(R.string.button_back);
buyButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(GT); // line 27
layout.addView(buyButton);
setContentView(layout);
}

Causing an exception when setting 2 onClickListener() on buttons located on 2 different layout

I have a problem and I can't seem to set 2 onClickListener for 2 separate buttons located on 2 different layout, when running the program, it cause an exception to occur.
btnClickToSecondPage button is located in activity_main.xml layout and btnObjClickToGoToFirstPage button is located at second_activity.xml layout.
The java code for my program is located below here
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(getWindow().FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button btnObjClickToGoToSecondPage = (Button) findViewById(R.id.btnClickToSecondPage);
Button btnObjClickToGoToFirstPage = (Button) findViewById(R.id.btnChangetoFirstPage);
btnObjClickToGoToFirstPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.second_activity);
}
}
);
btnObjClickToGoToSecondPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.activity_main);
}
}
);
} }
Please help me rectified the problem thanks.
Please implement the View.Onclick listener not Button.onclick listener
btnObjClickToGoToFirstPage.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.second_activity);
}
}
);
It is not a proper way switching pages in Android. Use two activities for switching pages with intents.
Intent newPage = new Intent (this, YourActivityNameForNewPage.class);
startActivity(newPage);
Put above code in your button's onClick().
If you want to show a new page you either start a new activity or start a new fragment.
Changing the contentView is not the correct way to approach this and should not be done.
You refer to the documentation on activities here.
Assuming you have another Activity called SecondActivity here is how you would start it:
btnObjClickToGoToSecondPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
);
And then you define the layout in the XML of the new activity, that is second_activity.xml
If they all have similar layouts using a fragment is also a good option.
Basically you start a new activity or fragment to show anything new or change the data dynamically say on your button's onClick().
This question may further clear your doubts:
What is setContentView(R.layout.main)?

Trying to execute multiple layout from single class

I'm trying to execute more then one layout screens from single class using onClick() method
Here goes my code
Button bt1,bt2;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.button);
bt2 = (Button)findViewById(R.id.button1);
onClick(); //onClick(View) in MainActivity cannot be applied to ()
}
public void onClick(View v){
if(v.getId()==R.id.button){
setContentView(R.layout.next1);
}
else if(v.getId()==R.id.button){
setContentView(R.layout.next1);
}
}
Kindly help me out, Thank you
Fragment is what you have to use, in this case. Load either of the fragment based on button click.
If you go for setContentView in this scenario, then your code will be clumsy and it will be very tedious for you to keep track on view.

Setting a buttons on click listener before it is on the screen?

I have a button I am trying to to listen to but the button is not on the screen yet. There are many different Fragments that I am using and the button that I want to listen to doesn't appear on the screen at first, so when I start the app it crashes right away. I am thinking that because it isn't on the screen yet and it is trying to listen to something that isn't there it begins to crash. How do I use it so that the button can begin listening once the fragment that the button is in appears on the screen?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// This button is not in the main fragment
Button akbutton = (Button) findViewById(R.id.akbutton);
akbutton.setOnClickListener(new View.OnClickListener() {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = null;
public void onClick(View v) {
fragment = new ak47();
fragmentManager.beginTransaction()
.replace(R.id.container, ak47.newInstance(0))
.commit();
}
});
Ok What the MAIN problem is that your finding the View of your Button not inside where your setting it on Listener.So Please Declare it where it should be used with your Listener.Second solution is to add a single line to where your finding the view of button mainly this word final or static or both final static in general your code must look like this.final Button akbutton = (Button) findViewById(R.id.akbutton); where you can change final to my above written varialiables.

Categories