In an activity there are 2 buttons. 1st button should open one activity and the 2nd button should open another activity. The following code is what I wrote. Please help me.
Button voice;
Button msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type_of_msg);
voice=(Button)findViewById(R.id.voice);
msg=(Button)findViewById(R.id.text);
}
#Override
public void onClick(View view) {
if(view.getId()==R.id.voice)
{
Intent intent=new Intent(getBaseContext(), TypeOfMsgActivity.class);
startActivity(intent);
setContentView(R.layout.activity_voice_msg);
}
else if(view.getId()==R.id.text)
{
Intent intent=new Intent(getApplicationContext(), TypeOfMsgActivity.class);
startActivity(intent);
setContentView(R.layout.activity_text_msg);
}
}
Replace this;
`Button voice;
Button msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type_of_msg);
voice=(Button)findViewById(R.id.voice);
voice.setOnClickListener(this);
msg=(Button)findViewById(R.id.text);
msg.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if(view.getId()==R.id.voice)
{
Intent intent=new Intent(getBaseContext(), TypeOfMsgActivity.class);
startActivity(intent);
setContentView(R.layout.activity_voice_msg);
}
else if(view.getId()==R.id.text)
{
Intent intent=new Intent(getApplicationContext(), TypeOfMsgActivity.class);
startActivity(intent);
setContentView(R.layout.activity_text_msg);
}
}`
By this:
Button voice;
Button msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type_of_msg);
voice=(Button)findViewById(R.id.voice);
msg=(Button)findViewById(R.id.text);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.voice:
Intent intent=new Intent(getBaseContext(), TypeOfMsgActivity.class);
startActivity(intent);
break;
case R.id.text:
Intent intent=new Intent(getApplicationContext(), TypeOfMsgActivity.class);
startActivity(intent);
break;
default:
break;
}
}
Also implements OnClickListener in your class
please try below code:
button1 = (Button)findViewById(R.id.Button1);
button1.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
intent = new Intent(CurrentActivity.this, SecondActivity.class);
startActivity(intent);
finish();
}
});
button2 = (Button)findViewById(R.id.Button2);
button2.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
intent = new Intent(CurrentActivity.this, ThirdActivity.class);
startActivity(intent);
finish();
}
});
Related
My problem is that the beacon manager just finds a beacon.
My code is below;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//location permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1234);
}
button = (Button) findViewById(R.id.b1);
//button onClick
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBeaconManager = BeaconManager.getInstanceForApplication(getApplicationContext());
mBeaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
mBeaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
mBeaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT));
mBeaconManager.bind(MainActivity.this);
}
});
}
#Override
public void onBeaconServiceConnect() {
ArrayList<Identifier> identifiers = new ArrayList<>();
identifiers.add(null);
Region region = new Region("all-beacons-region", identifiers);
try {
mBeaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
mBeaconManager.addRangeNotifier(this);
}
//display beacon size
#Override
public void didRangeBeaconsInRegion(Collection beacons, Region region) { Log.i(TAG,"" + beacons.size()); }
}
The result beacon.size 1. But there are three beacons.
What should I do to find more than one beacon? I'll be happy if you can help me.
I created a simple app of login form but it did not go to second activity after login. There is no error in the code. Will you please help me, here is the code:
public class MainActivity extends AppCompatActivity {
private EditText Name;
private EditText Password;
private TextView Info;
private Button Login;
private int counter=5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = (EditText)findViewById(R.id.editText);
Password = (EditText) findViewById(R.id.editText2);
Info = (TextView)findViewById(R.id.textView);
Login = (Button) findViewById(R.id.btn);
Login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validate(Name.getText().toString(), Password.getText().toString());
}
});
}
private void validate(String userName, String userPasswor) {
if ((userName == "admin") && (userPasswor == "1234")) {
Intent intent= new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
} else {
counter--;
Info.setText("No of Attempts Remaining: " + String.valueOf(counter));
if (counter == 0) {
Login.setEnabled(false);
}
}
}
}
You write your code
if((userName =="admin") && (userPasswor=="1234"))
{
Intent intent= new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
change this code as
if((userName.equals("admin")) && (userPasswor.equals("1234")))
{
Intent intent= new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
also enter Second activity in Android.mainfeast file.
i created Activity5 for manage values on other activities...
in Activity3 i have a spinner...
Spinner spinner1;
ArrayList<String> SAlist;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3);
getActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
ArrayList<String> SAList = intent.getStringArrayListExtra("StringArrayList");
spinner1=(Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,SAList);
spinner1.setAdapter(adp);
}
i think if i build new spinner (for showing it to user) in activity5 and edit it , then passing array list of this spinner to activity3, this is possible...
Activity5:
Spinner sp;
EditText et;
ArrayList<String> li;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity5);
getActionBar().setDisplayHomeAsUpEnabled(true);
li=new ArrayList<String>();
li.add("first item");
sp=(Spinner) findViewById(R.id.spinner1);
Button butt=(Button) findViewById(R.id.button1);
Button butt1=(Button) findViewById(R.id.button2);
et=(EditText)findViewById(R.id.editText1);
add();
butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
li.add(et.getText().toString());
et.setText(null);
add();
}
});
butt1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity5.this, MainActivity3.class);
intent.putStringArrayListExtra("StringArrayList", li);
startActivity(intent);
}
});
}
private void add() {
ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,li);
sp.setAdapter(adp);
}
but when i launch app and open activity3 it shows "unfortunately app has stopped" !!!
help me plz!
when you open activity3 from main menu, on that event your getting SAList null,
try doing this,when SAList is null it will show empty spinner.
Intent intent = getIntent();
if(intent.getStringArrayListExtra("StringArrayList")!=null)
{
ArrayList<String> SAList = intent.getStringArrayListExtra("StringArrayList");
}else
{
ArrayList<String> SAList=new ArrayList<String>();
}
how can I start any activity from my sherlock ActionBar Item Navigation Menu?
This is my code (i've tried with toast for now and function).
public class MainActivity extends SherlockActivity implements OnNavigationListener {
private String[] pasti;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pasti = getResources().getStringArray(R.array.Pasti);
Context context = getSupportActionBar().getThemedContext();
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.Pasti, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setSubtitle("The Subtitle");
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setDisplayShowCustomEnabled(false);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Toast toast=Toast.makeText(this,"Selected: " + pasti[itemPosition],Toast.LENGTH_LONG);
toast.show();
return true;
}
}
`
please help. thanks
I've solved with this code:
switch (itemPosition) {
case 1:
Intent primi = new Intent();
primi.setClass(getApplicationContext(), PrimiPiatti.class);
startActivity(primi);
break;
case 2:
break;
case 3:
break;
}
// return super.onOptionsItemSelected(itemPosition);
return true;
}
};
getSupportActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
// TODO Auto-generated method stub
return false;
}
I am devolping an application which turns the screen off and on based proximity sensor. The code i wrote sucessful turns the screen off and on.
This is the acivity from which the proximity sensors are registed and unregistered
MainActivity.java
public class MainActivity extends Activity {
public String TAG = "MainActivity";
BackgroundService mService;
boolean mBound = false;
boolean firstTime = true;
Intent BackgroundIntent ;
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
Log.w(TAG, "Service Disconeted");
mBound = false;
mService = null;
}
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
LocalBinder binder = (LocalBinder)service;
mService = binder.getService();
mBound=true;
if(firstTime){
MainActivity.this.onStart();
}
firstTime = false;
Log.w(TAG, "Service Connectedd");
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.w(TAG, "onCreate()");
setContentView(R.layout.activity_main);
Button buttonStart = (Button)findViewById(R.id.button1);
Button buttonStop = (Button)findViewById(R.id.button2);
buttonStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mService.enableSensor();
Log.w(TAG, "ButtonStart Clicked");
}
});
buttonStop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mBound){
mService.disableSensor();
}
Log.w(TAG, "ButtonSTOP Clicked");
}
});
startService(new Intent(this,BackgroundService.class));
}
#Override
public void onStart(){
super.onStart();
Log.w(TAG, "onStart Method");
Intent intent = new Intent(getApplicationContext(),BackgroundService.class);
getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStop(){
super.onStop();
if(mBound){
getApplicationContext().unbindService(mConnection);
Log.w(TAG, "Unbinding the service");
}
Log.w(TAG, "onStop Method");
}
}
This is the service which has a SensorEventListner which which listens to the changes in the proximity sensor
BackgroundService.java
public class BackgroundService extends Service{
public String TAG = "BackgroundService";
private final IBinder mBinder = new LocalBinder();
private SensorManager mSensorManager ;
private Sensor mProximitySensor ;
private SensorEventListener mSenosorEventListener;
private PowerManager.WakeLock proximityWakeLock;
public float maxSensorValue;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.e(TAG, "Binding with backgroundService");
return mBinder;
}
public class LocalBinder extends Binder{
BackgroundService getService(){
return BackgroundService.this;
}
}
#Override
public void onCreate(){
Log.w(TAG, "BackgroundService Created");
proximityWakeLock = ((PowerManager)getSystemService("power")).newWakeLock(32, "");
proximityWakeLock.setReferenceCounted(false);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
maxSensorValue = mProximitySensor.getMaximumRange();
mSenosorEventListener = new SensorEventListener() {
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType() == Sensor.TYPE_PROXIMITY ){
if(event.values[0] == maxSensorValue)
{
Log.w(TAG, "Object moved ****AWAY*****");
screanTurnOn();
}
else{
Log.w(TAG, "Object moved ****NEAR******");
screanTurnOFF();
}
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Sensor Accuracy Changed", Toast.LENGTH_SHORT).show();
}
};
}
#Override
public void onDestroy(){
mSensorManager.unregisterListener(mSenosorEventListener);
Log.w(TAG, "BackgroundService is Destroyed");
super.onDestroy();
}
public void screanTurnOFF(){
if(proximityWakeLock.isHeld()){
this.proximityWakeLock.release();
Log.e(TAG,"WakeLock is released");
}
}
public void screanTurnOn(){
if(!proximityWakeLock.isHeld())
proximityWakeLock.acquire();
while(proximityWakeLock.isHeld()){
Log.e(TAG, "WakeLock is acurqerd");
return;
}
}
public void enableSensor(){
if(mProximitySensor == null){
Log.w(TAG, "No Proximity detector found");
}else{
mSensorManager.registerListener(mSenosorEventListener, mProximitySensor,SensorManager.SENSOR_DELAY_NORMAL);
Log.w(TAG, "Proximity detector Enabled : "+ mProximitySensor.getName());
}
}
public void disableSensor(){
mSensorManager.unregisterListener(mSenosorEventListener,mProximitySensor);
Log.w(TAG, "Proximity Detector Disabled : " + EnableProximitySensor);
}
}
I have two buttons, one that registers the SensorEventListner to the sensor manager, and one to unregister it. The first button works perfectly. When i click on the second button, the ProximityEventListen remain unregistered.The sensoEventLister still works but the app will not show any Log messages from the BackGroundService.
If i comment out the screenTurnON() and screenTurnOFF() functions, the code works perfectly.
Please Help me out