Multiple List on a single listview through different buttons - java

I'm trying to use multiple data on a single listview through different buttons, as from activity1 there are two buttons, if button1 is clicked on activity2 it shows different data and if button2 is clicked then diff data respectively
//main
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, NEXT.class) );
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, NEXT.class) );
}
});
}
}
//for second activity
public class NEXT extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
if (R.id.button1 == clicked) //did onClick="clicked"
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.acceptors)));
}
else
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.donars)));
}
}
}

You should use putExtra() method of the Intent class. Put boolean extra in your MainActivity with key acceptors:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, NEXT.class).putExtra("acceptors", true);
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, NEXT.class).putExtra("acceptors", false));
}
});
}
}
Then in your NEXT activity, get intent, and check whether you get acceptors or donors:
public class NEXT extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
boolean isAcceptors = getIntent().getBooleanExtra("acceptors", false);
if (isAcceptors) //did onClick="clicked"
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.acceptors)));
}
else
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.donars)));
}
}
}

Related

OnLongClickListiner is not working

The code throws compile time error:
Class 'Anonymous class derived from OnLongClickLister' is not abstract and does not override abstract method onLongClick(View) in OnLongClickListener
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button amitsbutton = (Button) findViewById(R.id.amitsbutton);
amitsbutton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView amitstext = (TextView) findViewById(R.id.amitstext);
amitstext.setText("Small click is working");
}
}
);
amitsbutton.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean OnLongClick(View v) {
TextView amitstext = (TextView) findViewById(R.id.amitstext);
amitstext.setText("long click is also working ");
return true;
}
}
);
You're not overriding the right method. It's onLongClick, not OnLongClick. See below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button amitsbutton = (Button) findViewById(R.id.amitsbutton);
amitsbutton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView amitstext = (TextView) findViewById(R.id.amitstext);
amitstext.setText("Small click is working");
}
}
);
amitsbutton.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean onLongClick(View v) {
TextView amitstext = (TextView) findViewById(R.id.amitstext);
amitstext.setText("long click is also working ");
return true;
}
}
);

How to Use button with Page indicator activity in android

I use the one Fragment and use three images for page slider. When i use this code(Button visible on 3rd screen) then the indicator not shifted or run only images are sliced. So, how i use the button and indicator together.
public class MainActivity extends FragmentActivity {
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
Button button = (Button) findViewById(R.id.nextbutton1);
CirclePageIndicator indicator = (CirclePageIndicator)findViewById(R.id.indicator);
mIndicator = indicator;
indicator.setViewPager(mPager);
float density = getResources().getDisplayMetrics().density;
indicator.setBackgroundColor(0xFFFFFFFF);
indicator.setRadius(7 * density);
indicator.setPageColor(0xFFFFFFFF);
indicator.setFillColor(0xFFE63F36);
indicator.setStrokeColor(0xFF000000);
indicator.setStrokeWidth((float) (0.5* density));
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
Button button = (Button) findViewById(R.id.nextbutton1);
if(position==2) {
button.setVisibility(View.VISIBLE);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
Intent i=new Intent(MainActivity.this,Login.class);
startActivity(i);
}
});
} else {
button.setVisibility(View.GONE);
}
/* FragmentChangeListener listener = (FragmentChangeListener) //cast fragment at this position to FragmentChangeListener
listener.onCentered();*/
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
}

android - java.lang.RuntimeException

Why am I getting an java.lang.runtimeexception: Unable to instantiate activityComponentInfo {com.example.carprofile/com.example.carprofile.CarRegistraion}: java.lang.NullPointerExceprion
This is my code:
public class CarRegistration extends Activity{
//ParseXML parseClass;
Button saveCar;
EditText carMake = (EditText) findViewById(R.id.etCarMake);
EditText carModel = (EditText) findViewById(R.id.etCarModel);
String newCar;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.car_registration);
saveCar = (Button) findViewById(R.id.bSaveCar);
saveCar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
newCar = carMake.getText() +" "+ carModel.getText();
//parseClass = new ParseXML(newCar);
//parseClass.editXML();
}
});
}
if I clear the onCreate method, from saveCar = (Button)..., everything works fine.
Can someone tell me what am I doing wrong?
Try this,
public class CarRegistration extends Activity{
Button saveCar;
String newCar;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.car_registration);
EditText carMake = (EditText) findViewById(R.id.etCarMake); //these lines should be after setContentView()
EditText carModel = (EditText) findViewById(R.id.etCarModel);
saveCar = (Button) findViewById(R.id.bSaveCar);
saveCar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
newCar = carMake.getText() +" "+ carModel.getText();
//parseClass = new ParseXML(newCar);
//parseClass.editXML();
}
});
}

button click event doesnt work

I'm trying to switch the views, but when I'm in the second view, the back event click doesnt work.. I don't know what's wrong.
Pls, see my code and help me!
Part1
Part2
public class t extends Activity implements OnClickListener {
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
volta = (Button) findViewById(R.id.button2);
volta.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == volta) {
startActivity(new Intent(t.this, MainActivity.class));
}
}
}
You have to override onBackPressed. Change your MainActivity as below
public class MainActivity extends Activity {
private boolean goBack = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
goBack = true;
setContentView(R.layout.janela2);
}
});
}
#Override
public void onBackPressed() {
//If you have switched to R.layout.janela2 then go back
if (goBack){
setContentView(R.layout.activity_main);
goBack = false;
return;
}
//else do default action
super.onBackPressed();
}
}
Just do the following code, I hope it might help you
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, t.class);
startActivity(intent);
}
});
}
}
In t.java
public class t extends Activity{
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
}
#Override
public void onStop() {
super.onStop();
finish();
}
}
If you want two layouts then use viewflipper. If you want two activities (java classes) AND two layouts separately then use:
Intent i = new Intent (this, myClass.class);
startActivity(i);
To start the Activity and NOT setcontentview
So here:
public void onClick(View v) {
startActivity(new Intent (MainActivity.this, t.class));
OR IN THE CASE OF T.CLASS:
startActivity(new Intent (t.this, MainActivity.class));
}
You have to override onBackPressed() method if you want to back button functionality in your application. i.e.
public void onBackPressed() {
Intent start = new Intent(CurrentClass.this,Next_Activity.class);
startActivity(start);
finishActivity(0);
}

How to make separate button functions in Java/Android?

I have a class called Menu and inside that class I want to place a menu of buttons such as buttonA, buttonB, buttonC, and so on. However when I run the app on my phone I cant tap buttonB before I tap buttonA. If I tap buttonA first, I can choose buttonA or buttonB all I want. The question is how do you separate the buttons in the Menu class to be able to tap any button at any time?
package com.emods.app1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
}
});
}
}
You need to take your btnB and place it outside your onClick event for btnA. Currently you have your declaration for btnB inside your onClick event for btnA.
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
}
});
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
}
});
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
You defined the listener for the second button inside the listener for the first one.
Nesting issue.
Change:
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
}
});
to:
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
}
});
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
That should fix it.
Only the brackets are wrong:
package com.emods.app1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnA = (Button) findViewById(R.id.button1);
btnA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.emods.app1.BUTTONA"));
}
});
Button btnB = (Button) findViewById(R.id.button2);
btnB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent ("com.emods.app1.BUTTONB"));
}
});
}
}
You should activate the automatic formatting in your IDE.

Categories

Resources