Linking xml pages with layout - android

Excuse the simplicity of this request - but is there a way for me to link onClick commands for buttons through the Layout rather than code. I am trying to create a simple app and I want to be able to make buttons and have clicks go from one "page" to the other.
Is there a site that might overview how to use the UI to code for droid?
Thanks!

Make how many ever buttons that you would like to have. Make sure if its more than the screen amount you must assign a scroll view.
assume that you have on your R.layout.main.xml. To find this go to res/layout/main.xml
Copy XML, rightclick and paste it in the layout folder. Then it will say Rename. Rename it to what ever you want.
Now copy this specifically so lets say you put
btn1
btn2
btn3
btn4
btn5
To make the id tag go to the properties on each button and scroll to id and rename the ending to btn1 , 2,3, 4, and so on.
Now, if you were to place that in your .java file under src/com.whateveryounamed.app/what ever you named .java
Place this code below and it will work. Below is a example of mine with 5 buttons in xml page.
package com.nashvillekurds.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class kurdishhistoryapp extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 =(Button)findViewById(R.id.btn1);
Button btn2 =(Button)findViewById(R.id.btn2);
Button btn3 =(Button)findViewById(R.id.btn3);
Button btn4 =(Button)findViewById(R.id.btn4);
Button btn5 =(Button)findViewById(R.id.btn5);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.btn1);
}
});
btn2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.btn2);
}
});
btn3.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.btn3);
}
});
btn4.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.btn4);
}
});
btn5.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.btn5);
}
});
}}
hope this helped if not sorry but your doing something wrong ...

What james newton said involves multiple buttons. this is how it goes with one button, just for clarity:
Button btn1 =(Button)findViewById(R.id.~btn1~);
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
setContentView(R.layout.~btn1~);
}
});
Besides that nothing needs to be changed, just change the button id and xml id to work with your app. Button btn is just to label it to the system, so you should be keep that, and
btn1.setOnClickListener... can be kept as well. Just change the parts within the ~ . Make sure to change the ~ too! I probably wouldn't without a warning, and some others wouldn't either.

Related

OnClick method does not start new activity

This code isn't calling the next activity. I don't understand why, but this onClick method isn't starting the next activity, though the same code in other activities does. Tested calling other activities and it didn't work even. The button is created, but when I tap it, nothing happens.
Edit: Folks, when we make a question about code, we want to know what is wrong, why is wrong, how to fix and why and this fixing works. We want to understand both the problem and the solution, and just drop a sample of code alone as an answer doesn't quite help some people. I thank you all for spending time with my question and for helping me solve this issue, but keep this words in mind when you are answering someone's question.
package com.example.conjuradortormenta;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class ListadePersonagens extends AppCompatActivity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listade_personagens);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// Using shared preferences, we get information about the characters
SharedPreferences informacoesdepersonagem = getSharedPreferences("com.example.conjuradortormenta_informaçoes_de_personagem", MODE_PRIVATE);
int num = informacoesdepersonagem.getInt("numero_de_personagens", 0);
String nome, resumo;
Button botao[]= new Button[num+1];
if(num!=0)
{
for(int i=1; i<=num; i++)// cria os botões de cada personagem
{
nome = informaçõesdepersonagem.getString("nome_personagem"+(i+1), "Nenhum");
resumo = informaçõesdepersonagem.getString("raça_personagem"+(i+1), "Nenhum")+ " " + informaçõesdepersonagem.getString("classe_personagem"+(i+1), "Nenhum") + " " + Integer.toString(informaçõesdepersonagem.getInt("nivel_personagem"+(i+1), 0));
botao[i]=new Button(this);
botao[i].setText(nome+" "+resumo);
botao[i].setId(i);
botao[i].setOnClickListener(this);
layout.addView(botao[i]);
}
}
botao[0]=new Button(this);
botao[0].setText("Criar Novo");
botao[0].setId(1);
botao[0].setOnClickListener(this);
layout.addView(botao[0]);
}
#Override
public void onClick(View view)
{
Intent nova = new Intent (this, CriadordePersonagem.class);
startActivity(nova);
}
}
You seem to add a bunch of buttons to your LinearLayout, but you don't actually add layout to your view hierarchy. Can you try calling setContentView(layout) and clicking on the buttons then?
I think you can use this example :-
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button:
startActivity(new Intent(this, Doctor.class));
break;
}
}
}
Would be useful to see your XML layout, but my first guess is that you didn't add the layout anywhere.
Your layout seems to be R.layout.activity_listade_personagens so if you want to edit it, you should get it instead of creating a new one:
setContentView(R.layout.activity_listade_personagens);
LinearLayout layout = findViewById(R.layout.activity_listade_personagens);
layout.setOrientation(LinearLayout.VERTICAL);
// Rest of your code
In onClick method, you did not get Id of which view be clicked. So android cannot know onClick for what view. You should do that:
#Override
public void onClick(View view){
switch (view.getId()){
case R.id.button:
Intent nova = new Intent (this, CriadordePersonagem.class);
startActivity(nova);
break;
}
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(OneActivity.this, TwoActivity.class);
startActivity(intent);
}
});

what is better way to organize onclicklistener? [duplicate]

This question already has answers here:
Multiple Buttons' OnClickListener() android
(11 answers)
Closed 7 years ago.
When you have many buttons in a view and all the button have listener. Your main activity gets dirty.
Anyone know how to organize listeners ?
Currently I used this way and implement onClickListener.
spotify =(Button)findViewById(R.id.spotifyBtn);
superDuoBtn = (Button) findViewById(R.id.superDuoBtn);
libraryBtn = (Button) findViewById(R.id.libraryBtn);
buildBiggerBtn = (Button) findViewById(R.id.buildItBiggerBtn);
capstoneBtn= (Button) findViewById(R.id.capstoneApp);
spotify.setOnClickListener(this);
superDuoBtn.setOnClickListener(this);
libraryBtn.setOnClickListener(this);
buildBiggerBtn.setOnClickListener(this);
capstoneBtn.setOnClickListener(this);
You could set the property:
android:onClick="buttonClicked"
in the xml file for each of those buttons, and use this in the java code:
public void buttonClicked(View view) {
if (view.getId() == R.id.button1) {
// button1 action
} else if (view.getId() == R.id.button2) {
//button2 action
} else if (view.getId() == R.id.button3){
//button3 action
}
}
You can implement onclicklistner for multiple buttons using swith case
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.firstButton:
// do your code
break;
case R.id.secButton:
// do your code
break;
case R.id.thirdButton:
// do your code
break;
......
default:
break;
}
}
Ya...It s the best way to use multiple onClickListener.
spotify =(Button)findViewById(R.id.spotifyBtn);
superDuoBtn = (Button) findViewById(R.id.superDuoBtn);
libraryBtn = (Button) findViewById(R.id.libraryBtn);
buildBiggerBtn = (Button) findViewById(R.id.buildItBiggerBtn);
capstoneBtn= (Button) findViewById(R.id.capstoneApp);
spotify.setOnClickListener(this);
superDuoBtn.setOnClickListener(this);
libraryBtn.setOnClickListener(this);
buildBiggerBtn.setOnClickListener(this);
capstoneBtn.setOnClickListener(this);
#Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.spotifyBtn:
intent = new Intent(this, SimpleSingleExample.class);
break;
case R.id.superDuoBtn:
intent = new Intent(this, CustomExample.class);
break;
case R.id.libraryBtn:
intent = new Intent(this, SequenceExample.class);
break;
case R.id.buildItBiggerBtn:
Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
break;
}
if(intent!=null){
startActivity(intent);
}
}
If you want better way than you have to use Android Annotations, its simple and useful, you can find here
Add those View object references to some type of list, iterate through it usin a for-each loop, then call the setOnClickListener on each element which will reduce those lines to just 2 lines for you.
ArrayList <View> list = new ArrayList <>(spotify,superDuoBtn,libraryBtn, buildBiggerBtn, capstoneBtn);
for (View view : list) {
view.setOnClickListener(this);
}
The most obvious example of alternative approaches to solving a single problem seems to be the various ways you can handle button clicks. As far as I know, there are four different ways to add listeners for handling button clicks. If you know of other ways, please post a comment and share them with us.
Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="Inner Class (btn1)" android:id="#+id/Button01"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</Button>
<Button android:text="Anonymous Inner Class (btn2)"
android:id="#+id/Button02" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="Implementing an Interface (btn3)"
android:id="#+id/Button03" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="Calling From XML Layout (btn4)"
android:id="#+id/Button04" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="btn4Listener">
</Button>
</LinearLayout>
in MainActivity
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Main extends Activity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//method 1 - uses an inner class named btn1Listener...
Button btn1 = (Button)findViewById(R.id.Button01);
btn1.setOnClickListener(btn1Listener);
//method 2 - use an anonymous inner class as a listener...
Button btn2 = (Button)findViewById(R.id.Button02);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showToastMessage("You clicked btn2 - uses an anonymouse inner class");
}
});
//method 3 - note that this class implements
//the View.OnClickListener interface
//which means that we must implement the onClick()
//method (which you'll find below)..
Button btn3 = (Button)findViewById(R.id.Button03);
btn3.setOnClickListener(this);
//method 4 - look at the method btn4Listener() below
}
//here's the inner class used as a listener for btn1...
private View.OnClickListener btn1Listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
showToastMessage("You clicked btn1 - uses an inner class named btn1Listener");
}
};
//here's a method that you must have when your activity implements the
//View.OnClickListener interface...
#Override
public void onClick(View v) {
showToastMessage("you clicked on a btn3, which uses this Activity as the listener");
}
//here's the handler for btn4 (declared in the xml layout file)...
//note: this method only works with android 2.1 (api level 7), it must be public and
//must take a single parameter which is a View
public void btn4Listener(View v) {
showToastMessage("You clicked btn4 - listener was set up in the XML layout");
}
private void showToastMessage(String msg){
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toast.show();
}
}

Launch new contentView onClick (to obtain new window from another XML layout)

I'm trying to execute a new contentView this way. What am I missing? I get a force close onClick.
final Button btnStatus = (Button) findViewById(R.id.Status);
btnStatus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
startActivity(intent);
setContentView(R.layout.newlayout);
}
});
This is just wrong. You can do one of two things here.
Not recommended but could work for what you are doing
Simply using setContentView(R.layout.newlayout) will set the new layout assuming newlayout.xml is a layout in your layout folder
public void onClick(View v)
{
setContentView(R.layout.newlayout);
}
Recommended
Create a new Activity and set the content in it to this new layout and call that Activity in your onClick()
public void onClick(View v)
{
Intent intent = new Intent(CurrentActivityName.this, NextActivityName.class);
startActivity(intent);
}
});
if you want to change layout then try to,
{setContentView(R.layout.newlayout);}
if you want to change activity, then try to intent to forward another activity, and check also to entry of this activity in manifest file in android.

Android Buttons in Eclipse Helios (ADT 10.0.1) do not link properly

This is my first post on the site, and I'll try to be just as specific as the tips requested. I'm using eclipse Helios, with the ADT 10.0.1
I've been working on an Android application which is supposed to be a general guide of Starcraft II. Pretty basic stuff, it's for a programming class. It's supposed to have an intro background with a continue button, linking to the main menu.
The main menu consists a few buttons that should all link to different layouts created. When I start my application in an emulator (I tried level 12, 9 etc.) the first button links to the menu, but the buttons on the menu fail to link. I have no syntax errors in my code, however it does show the whole yellow underline for all the buttons except the first one. I've fiddled with the basic syntax a bit on and off to get it to not display any red or yellow underlines, and it didn't get me anywhere either.
When I removed the last button from the code, which is a back button from the layouts, linking back to the front menu, the buttons on the menu instead started linking to the last button's link, by which I mean the last one in the code. So I thought it might be skipping all of the other listeners and just using the one for the last button in the code, or something like that. Bear in mind I'm not very good at programming yet.
Here's the basic look of the code.
package lol.lol;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ofk extends Activity {
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle button1) {
super.onCreate(button1);
setContentView(R.layout.intro);
Button button = (Button) findViewById(R.id.continuebutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.frontmenu);
}
});
final Button button2 = (Button) findViewById(R.id.aboutapp);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.aboutsc2g);
}
});
final Button button3 = (Button) findViewById(R.id.aboutsc);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.aboutsc2);
// Perform action on click
}
});
final Button button4 = (Button) findViewById(R.id.micro);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.micro);
// Perform action on click
}
});
final Button button5 = (Button) findViewById(R.id.macro);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.macro);
// Perform action on click
}
});
final Button button6 = (Button) findViewById(R.id.mechanics);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.mechanics);
// Perform action on click
}
});
final Button button7 = (Button) findViewById(R.id.zergbasics);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.zergbasics);
// Perform action on click
}
});
final Button button8 = (Button) findViewById(R.id.terranbasics);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.terranbasics);
// Perform action on click
}
});
final Button button9 = (Button) findViewById(R.id.protossbasics);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.protossbasics);
// Perform action on click
}
});
final Button button10 = (Button) findViewById(R.id.backbutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.frontmenu);
// Perform action on click
}
});
So my question being, how do I get the buttons on the layout frontmenu to link where they're supposed to? Are there any syntax errors or things I need to add to this code snippet to make it function properly?
Thanks in advance!
well, let me first say: this is not the way it was meant by the android sdk. you gotta have an activity for every change of a screenful of content, which is realized by switching of contentview here. maybe look here, if you haven´t done so: http://developer.android.com/guide/topics/fundamentals.html
the right way (with activities), it´s possible for the users to go back with the system-backbutton, so there´s basically no need to have a backbutton, and users will expect the system-backbutton to work like that. now, pressing the system-backbutton causes the app to 'close' and to show what was opened before.
however, it is possible to do it like you tried it to. here´s a short snippet:
public class Start extends Activity {
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
LayoutInflater mLayoutInflater = getLayoutInflater();
final LinearLayout mLinearLayoutIntro = (LinearLayout) mLayoutInflater.inflate(R.layout.intro, null);
setContentView(mLinearLayoutIntro);
final LinearLayout mLinearLayoutFrontMenu = (LinearLayout) mLayoutInflater.inflate(R.layout.frontmenu, null);
Button continueButton = (Button) findViewById(R.id.continuebutton);
continueButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(mLinearLayoutFrontMenu);
}
});
final LinearLayout mLinearLayouAboutSc2g = (LinearLayout) mLayoutInflater.inflate(R.layout.aboutsc2g, null);
final Button aboutAppButton = (Button) mLinearLayoutFrontMenu.findViewById(R.id.aboutapp);
aboutAppButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(mLinearLayouAboutSc2g);
}
});
final Button backButton = (Button) mLinearLayoutFrontMenu.findViewById(R.id.backbutton);
backButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(mLinearLayoutIntro);
// Perform action on click
}
});
super.onCreate(savedInstanceState);
}
}
you see, there´s quite a bit difference to your code. first error in your code was to bind the click listeners only to the first button (possibly copy/pasted and then forgot).
second, if you want to solve it like this, you have to manually inflate your layouts. otherwise you will get null pointers everywhere, because these layouts and its childs (buttons etc) are instantiated lazily (only when they are needed e.g. called in setcontentview).
and third: well...really, do your app with one activity for every screenful of content :)

Button in slidingdrawer!

I'm having a button in a sliding drawer in a Android Application. The problem is it does not seem to react to any clicks as normal buttons do.
I'm guessing the problem is that it's a different view than buttons on the normal view.
If I implement a button the normal way like this
myAgenda = (Button)findViewById(R.id.BtnMyAgenda);
myAgenda.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.BtnMyAgenda:
test.setAnimation(leftLeft);
test.startAnimation(leftLeft);
break;
}
I'm guessing there is something wrong with the above code since the button is in a SlidingDrawer and not in the "normal" view.
Any ideas how to fix the problem?
Here is the code
Register with event listner like below code
button.setOnClickListener(clickButtonListener);
and create this listner for button
private OnClickListener clickButtonListener= new OnClickListener()
{
#Override
public void onClick(View v)
{
if(v == button)
{
}
}
}
I actually found the solution to the problem, I simply created a new view.onclicklistener specific to that button.
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});

Categories

Resources