Android switching to multiple layouts - android

This is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get_started1);
mNextBtn1 = findViewById(R.id.getStartedBtn);
mNextBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.get_started2);
mNextBtn2 = findViewById(R.id.button2);
}
});
mNextBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.get_started3);
}
});
}
I am getting this error:
Process: com.example.storefrontapp, PID: 32160
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.storefrontapp/com.example.storefrontapp.Get_Started}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
What I am trying to achieve is to change layouts when button click. I want to show 1 field at a time as the user clicks next? For example, first user sees only Name TextField and click next to see Address TextField etc..Kinda like a registration page but with 1 field at a time.
I am creating 1 activity that points to multiple layouts. I can have Layout1, Layout2 Layout3 etc. Next button on Layout1 takes user to Layout2 and so on.?

Move your mNextBtn2.setOnClickListener() call into the click listener for mNextBtn1:
mNextBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.get_started2);
mNextBtn2 = findViewById(R.id.button2);
mNextBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.get_started3);
}
});
}
});
The way you have it now, your app will still try to set the click listener for mNextBtn2 immediately. Since the button is null until the user taps mNextBtn1, this will throw an exception.
If you're concerned about this nesting poorly, consider something like this instead. In your layout files, add the following attribute to each button you'd normally call setOnClickListener() for:
android:onClick="switchLayout"
Then, in your activity, create the switchLayout method:
public void switchLayout(View view) {
switch (view.getId()) {
case R.id.getStartedBtn:
setContentView(R.layout.get_started2)
break;
case R.id.button2:
setContentView(R.layout.get_started3)
break;
...
}
}
Now you don't need to call setOnClickListener() at all; your buttons will just automatically call through to this switchLayout() method when you click them. And you can use the button's id to figure out which layout to swap in.

You are getting that error because you did not initialize your second button. You can't attach a listener before initializing.
You have two options. Either initialize your second button outside of the listener like this:
mNextBtn1 = findViewById(R.id.getStartedBtn);
mNextBtn2 = findViewById(R.id.button2);
or put your second button inside of the listener of the first button:
mNextBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.get_started2);
mNextBtn2 = findViewById(R.id.button2);
mNextBtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.get_started3);
}
});
}
});

Related

Android can not remove button

I am trying to remove a button when the button itself is tapped, I am trying the following:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
tagsView.removeView(button);
}
};
}
This code is working, but when I add the following line of code:
editText.setText(button.getText());
The code stops working and the button does not get removed. I add it like so:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
tagsView.removeView(button);
}
};
}
What is the problem here?
use this in your OnClick method
button.setVisibility(view.GONE);
Your code will look like this
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
button.setVisibility(view.GONE);
}
};
}
Or Try this
Button mybtn = (Button)findViewById(R.id.mybtn_id);
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mybtn.setVisibility(view.GONE); // or (view.INVISIBLE)
}
});
Depending on what you're trying to achieve, something like deejay proposed would work just fine. If want the button to hide, call button.setVisibility(View.INVISIBLE). However, if you are trying to dismiss it completely from the view hierarchy, call button.setVisibility(View.GONE).
just set button visibility to false
Obviously button.setVisibility(View.GONE) comes to mind but if it doesn't work you should look one level above for the source of the bug. Maybe you don't set OnClickListener you created to the button and hence nothing happens?

imagebutton pass reference

I am trying to pass an imagebutton as a reference through an indexed array.
I thought I could set the ID and then pass that ID as such:
MyButton=(ImageButton) findViewById(R.id.imageButton1);
MyButton.setId(0);
Then in my onClick I want to pass index "0":
MyButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (MyButton.isSelected()){
Switch_Ctrl(0, OFF );
}
});
index is passed to method:
boolean Switch_Ctrl(char button_num, byte state){
button_num.setImageResource(R.drawable.switch_off);
button_num.setSelected(false);
}
I get error can't resolve method setImageResource.
So I can't use the id "button_num". Not sure how I can reference the ImageButton?
You can just store a reference to the button and change it onClick. Just make it an instance variable in your Activity or Fragment. You can declare a button seperately and then initialize it onCreate or onActivityCreated (once findViewById has become available). For example
private Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState){
myButton = (Button) findViewById(R.id.button);
myButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
changeButtonBackground(v.getId());
});
}
}
//if doing this for multiple butons, check which one
//was pressed and change the background
private void changeButtonBackground(int id){
if(id == R.id.myButton){
myButton.setBackground(R.drawable.whatever);
}
}
Also - I wouldn't set the id of my button to anything else like 0. Your button already has an id, it's the one you defined in the XML as android:id="#+id/whateveritis". Unless you are creating view's programatically that do not exist in the XML (e.g. if you were adding TextView's to some parent layout on click of this button) you don't need to define a new id.

android view id is not correct

I create a whole layout setup in XML then attempt to attach listeners to the buttons using findViewById(). The problem I am having now is that the View parameter I receive in the method does not contain the ID of the view I clicked: 830009633920 vs 2131099657.
Button btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
#Override
public void onClick(View view) {
if (view.getId() == R.id.btnNext) {
...
}
}
How about this:
Button btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
#Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.btnNext:
...
break;
case R.id.foo:
...
break;
}
}
That should be working, so my only guess without seeing the rest of the code or layout is that you have set click handlers on two items that overlap.
If you plan on making an Android Library project switch like this will not work due to a recent change in the tool chain.
http://tools.android.com/tips/non-constant-fields
In general I find it is much simpler to use an anonymous class the handle clicks rather than a large single handler.
btnNext.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
...
}
});
You might try this approach just to debug that the item you expect to get the click is what you are pressing, to make sure you don't have layout issues.
Try skipping the IDs, and just compare the actually view object. so something like this:
#Override
public void onClick(View view) {
if (view.equals(btnNext)) {
...
}
}

Facing problem in implementing OnClickListener on android

I want to implement a click listener for a button on my main view. My code is something like below
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.btnFinish);
// Register the onClick listener with the implementation above
button.setOnClickListener(mFinishListener);
...
}
private OnClickListener mFinishListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
But shows me error as follows
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener) MobileTrackerActivity.java /MobileTracker/src/com/example/mobiletracker line 37 Java Problem
I have no idea what to do. Please help.
You are not using the correct interface to instantiate the mFinishLinstener variable...
It is possible you have an import specifying DialogInterface and that is confusing the view.
Try specifying View.OnClickListener explicitly.
private View.OnClickListener mFinishListener = new View.OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
As per my opinion Best way to implement On click event for the Button.
Instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
android:onClick="selfDestruct" />
Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:
public void selfDestruct(View view) {
// Kabloey
}
Note: The above code is given in Android SDK - Button.
try this code :::
final Button button = (Button) findViewById(R.id.btnFinish);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Simply try this one as:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do something when the button is clicked
}
};
you can also use like below code..
Button button = (Button)findViewById(R.id.btnFinish);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
//Write Your code here
}
});
You can also declare the onclick in the xml.
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="buttonClick" />
And in your code you would define the function as:
public void buttonClick(View view)
{
// handle click
}

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