changing from a window to another - Java for Android App Development

Hi Im just starting to learn android code and I dont understand why this doesnt work. Just for as a test I wanted to create 2 pages (or 1 page and 1 popup page but i thought 2 pages would be easier to make), and a way to get from one to the other, which I thought could be solved easly with a button on each page which takes me to the other page.
So I started with this:
HTML:
final ImageButton button = (ImageButton) findViewById(R.id.imageButton);
button.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
setContentView(R.layout.layout2);
} });
Basicually I start in a layout called activity_my which has a button and when I press it I get to the next page called layout2. I thought I could just make a similiar function to get back to my original page:
HTML:
final Button button2 = (Button) findViewById(R.id.button);
button2.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
setContentView(R.layout.activity_my);
}
});
But now I get error for whatever reason

Hi,
Basically in android, a 'page' is an Activity (class where you attach a XML layout).
So you must create 2 activity with 2 layout.
To switch activity use this foloowing code :
HTML:
Intent intent = new Intent(this, YourSecondActivity.class);
startActivity(intent);

Related

[Q] How do I make a call through a long press context menu

I orginally had it setup to make a call based on a button click, but now I want the context menu through a long press to do it.
The menu works fine as it finds it way to my call class but I can't figure out how to change it around for it to just start dialing as any android phone does. Can someone suggest what I should change the code below to.
HTML:
public class Dial extends Activity {
Button Btn2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note_edit);
Btn2 = (Button) findViewById(R.id.dial);
Btn2.setOnClickListener(new OnClickListener () {
public void onClick(View v) {
EditText num=(EditText)findViewById(R.id.phone);
String number = "tel:" + num.getText().toString().trim();
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number));
startActivity(callIntent);
}
});
}
}
take a look at these methods from View
http://developer.android.com/reference/android/view/View.html#setLongClickable(boolean)
http://developer.android.com/refere...ndroid.view.View.OnCreateContextMenuListener)
http://developer.android.com/refere...stener(android.view.View.OnLongClickListener)
should handle the many ways to make an item long clickable

Switching to and from layouts using buttons

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button display = (Button) findViewById(R.id.button);
TextView tv = (TextView) findViewById(R.id.textView);
display.setOnClickListener( new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main2);
// TODO: Implement this method
}
});
Button ba = (Button) findViewById(R.id.buttonBac);
ba.setOnClickListener(new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main);
// TODO: Implement this method
}
});
}
}
Anyway, the first button works and when clicked, displays main2 makin 2 whoum d have a textview and a button that switches back 2 main but when the onclick listener whTever is aplyed to the code it wont even disllag the first layout and simply crashes... hitting the wall again
Focusedrelaxaation87 said:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button display = (Button) findViewById(R.id.button);
TextView tv = (TextView) findViewById(R.id.textView);
display.setOnClickListener( new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main2);
// TODO: Implement this method
}
});
Button ba = (Button) findViewById(R.id.buttonBac);
ba.setOnClickListener(new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main);
// TODO: Implement this method
}
});
}
}
Anyway, the first button works and when clicked, displays main2 makin 2 whoum d have a textview and a button that switches back 2 main but when the onclick listener whTever is aplyed to the code it wont even disllag the first layout and simply crashes... hitting the wall again
Click to expand...
Click to collapse
You can't do a second setContentView call in an activity. What you can do is define two Layouts within a LinearLayout in the XML, set the first one to "android:visibility="gone"", the second one to "visible" and switch between them onClick
Code:
[user=439709]@override[/user]
public void onClick(View v) {
switch (v.getId()){
case R.id.btnOne:
mLayoutFirst.setVisibility(View.GONE);
mLayoutSecond.setVisibility(View.VISIBLE);
break;
case (R.id.btnTwo):
mLayoutFirst.setVisibility(View.VISIBLE);
mLayoutSecond.setVisibility(View.GONE);
break;
}
}
Zatta said:
You can't do a second setContentView call in an activity. What you can do is define two Layouts within a LinearLayout in the XML, set the first one to "android:visibility="gone"", the second one to "visible" and switch between them onClick
Click to expand...
Click to collapse
Alternatively you could use a ViewSwitcher and call it's .next() and .previous() methods. This would probably be necessary when you have more than 2 views.
octobclrnts said:
Alternatively you could use a ViewSwitcher and call it's .next() and .previous() methods. This would probably be necessary when you have more than 2 views.
Click to expand...
Click to collapse
Thanks, never heard of before. I use that method for hiding/unhiding complete ViewGroups, Buttons and what not but I'll look into that, might be more easy.
Try naming the activity other than the "main" attribute, that is only for the first or "parent" activity. You need it to be a child activity.
Sent from my HUAWEI-M835 using xda app-developers app
Cool, learned something new

multiple buttons to send predefined SMS

Hi guys i am new to programming. I am trying to have multiple button to send different predefined SMS to predefined number. I am not sure how to have multiple setOnClickListener(new OnClickListener() as the 2nd setOnClickListener(new OnClickListener() gave me error.
public class SendSMSActivity extends Activity {
Button buttonSend;
Button buttonSend2;
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSend = (Button) findViewById(R.id.buttonSend);
buttonSend2 = (Button) findViewById(R.id.buttonSend2);
buttonSend.setOnClickListener(new OnClickListener() {
buttonSend2.setOnClickListener(new OnClickListener() {
@override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonSend:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "abc");
sendIntent.putExtra("address", "9909990");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
break;
case R.id.buttonSend2:
Intent sendIntent1 = new Intent(Intent.ACTION_VIEW);
sendIntent1.putExtra("sms_body", "def");
sendIntent1.putExtra("address", "012345678");
sendIntent1.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent1);
break;
}
}
});
});
}
}
@stewypost
You cant write statements anywhere inside an anonymous inner class anyways ignoring the poor syntax
To do this first declare your
OnClickListner listner = (View v) ->
{
// your code
};
then call
button1.setOnClickListener(listner);
button2.setOnClickListener(listner);
Sent from my GT-S5302 using Tapatalk 2

CountDownTimer problem

Hello! I'm new to this forum. I'm developing an Android app, I have some problems with the functionality of a timer and I don't figure out why. I would need some ideas.
In my application I have 2 activities: one is with the levels of a game, where you can chose between them and the second one is with the game itself. In the second activity I have a CountDownTimer which tells me when the game must finish. I have a progressBar assigned to that timer.
CountDownTimer mCountDownTimer; -global variable
In onCreate I have:
mProgressBar=(ProgressBar)findViewById(R.id.progressBar);
mProgressBar.setProgress(0);
mCountDownTimer=new CountDownTimer(90000,1000) {
int i=0;
@override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
i++;
mProgressBar.setProgress(i); }
@override
public void onFinish() {
i++;
mProgressBar.setProgress(i);
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
}
};
mCountDownTimer.start();
I have also overriden the native back button from Android to go to the first activity, the one with the levels and there I try to stop the counter, but it doesn't seem to work at all. The counter doesn't stop, and the other functions don't work as well.
Here is the code:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
mCountDownTimer.cancel();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
return;
How could I solve this? Thank you so much!
Here is your problem:
CountDownTimer mCountDownTimer; -global variable
There are no global variables in Java, only instance variables. Since you didn't post your full code I'm going to make a few assumptions here...
public class myAwesomeTimerClass {
CountDownTimer mCountDownTimer;
// bla bla
}
Then in your second activity/class, you should refer to the class:
instead of:
mCountDownTimer.cancel();
try:
myAwesomeTimerClass.mCountDownTimer.cancel();
Good luck.

PreferenceFragment and Tabs

Hi,
currently I am developing a App.
I use the NavigationDrawer to set up a Menu (with the "Burger-Button") on the left side.
Wenn I choose a menu item, for example a PreferenceFragment i displayed.
My problem: I want to display a Tab-Layout with a PreferenceFragment in each Tab.
But I don´t know how...
In my MainActitivty-Class (AppCompatActivity) I change the content of my App (after the user clicked on a menu item) like this:
Code:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
FragmentTransaction fragmentTransaction = this.getFragmentManager().beginTransaction();
int id = item.getItemId();
// Globale Eistellungen
if (id == R.id.nav_main_settings) {
fragmentTransaction.replace(R.id.containerView, new MainPreferenceMenu());
fragmentTransaction.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Whereby "MainPreferenceMenu" is a PreferenceFragment.
I tried it with this tutorial: https://guides.codepath.com/android/google-play-style-tabs-using-tablayout
First Problem: I can´t set my PreferenceFragment here:
Code:
@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
}
Because "PreferenceFragment" is the wring Class.
Also I don´t know how to set the "ViewPager" like in the Tutorial (MainActivity).
Can anybody help me?
I hope my Problem is clear.
Using view pager for tabs is the best option as it behaves as per material guide lines.

Categories

Resources