I am writing my first android application, and am trying to write an app where you simply click a button to display a message, and can do so as many times as you want. So far I have:
public class MyProject extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv = new TextView(this);
Button startButton = (Button) findViewById(R.id.startbutton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
tv.setText("Hello World!");
setContentView(tv);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {setContentView(R.layout.main);}}, 2000);
}
});
}
}
However doing it this way, when I get back to my main screen and click on the button again...nothing happens. How can I get repeated button clicks to repeat the behaviour?
Your example is very simple and, from what I understand, no multithreading is needed.
You just have to initialize your layout one time, after that you can update the content of the single views.
So... this will set "hello world" string on every button click, you will not notice any difference because the string is always the same :D
public class MyProject extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// this is needed one time only
setContentView(R.layout.main);
// add your textview in xml
final TextView tv = (TextView) findViewById(R.id.textView);
final Button startButton = (Button) findViewById(R.id.startbutton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
tv.setText("Hello World!");
}
}
}
}
To do something more fun you can set a counter to update on every click, this way the textview change will be noticeable are more fun!!
int i = 0;
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
tv.setText("you have reached " + i);
i++;
}
}
Here, you are calling setContentView to replace the entire current view with a TextView containing "Hello World!" Then after two seconds you setContentView again to set it back to the main layout. Since OnCreate is only called once when the activity is created, the OnClickListener is never set again.
This code will do what you are looking for.
public class MyActivity extends Activity
implements View.OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
setContentView(R.layout.main);
Button startButton = (Button) findViewById(R.id.startbutton);
startButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
TextView tv = new TextView(this);
tv.setText("Hello World!");
setContentView(tv);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
init();
}
}, 2000);
}
}
You may notice that OnClick is a separate method in my example. This is because I implemented the interface View.OnClickListener. I was able to write the implemented method as part of my class and then pass this as an argument to setOnClickListener. I'm explaining this because you mentioned you are new to Java.
TextView txt;
// our handler
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
//display each item in a single line
txt.setText(txt.getText()+"Item "+System.getProperty("line.separator"));
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt=(TextView)findViewById(R.id.txt);
}
#Override
protected void onStart() {
super.onStart();
// create a new thread
Thread background=new Thread(new Runnable() {
#Override
public void run() {
for(int i=0;i<10;i++)
{
try {
Thread.sleep(1000);
b.putString("My Key", "My Value: "+String.valueOf(i));
// send message to the handler with the current message handler
handler.sendMessage(handler.obtainMessage());
} catch (Exception e) {
Log.v("Error", e.toString());
}
}
}
});
background.start();
}
}
Note:Handler one type of thread not udpate any UI here
Take an example from here
Related
This is my code:
AlphaAnimation anim_fadeIn;
Button button, button2;
TextView t, e;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
t = findViewById(R.id.text_Splash_t);
e = findViewById(R.id.text_Splash_e);
button = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate2();
}
});
anim_fadeIn = new AlphaAnimation(0.0f, 1.0f);
anim_fadeIn.setDuration(1000);
anim_fadeIn.setFillAfter(true);
private void mAnimate() {
t.startAnimation(anim_fadeIn);
}
private void mAnimate2() {
e.startAnimation(anim_fadeIn);
}
Scenario:
press button1 and text1 will animate (even if you do it some times). Then pressing button2 will add the view somewhere so no matter if you press the button1 or 2, both Texts will animate
Scenario 2:
press button2 and text2 will animate (even if you do it some times). Then pressing button1 will add the view somewhere so no matter if you press the button1 or 2, both Texts will animate.
How can I avoid this problem
Remove new Thread wrap, just call mAnimate() or mAnimate2() in main thread like this:
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate2();
}
});
...
Also mAnimate, mAnimate2 can be optimized like this:
private void mAnimate() {
t.startAnimation(anim_fadeIn);
}
private void mAnimate2() {
e.startAnimation(anim_fadeIn);
}
What you actually do is you register the same animation to two Views. If you want to only animate one view at the same time you have to clear the Animation for the other View at first otherwise both will start. E.g.
private void mAnimate() {
e.clearAnimation();
t.startAnimation(anim_fadeIn);
}
private void mAnimate2() {
t.clearAnimation();
e.startAnimation(anim_fadeIn);
}
I implemented a custom button and added a task to it with delay so it shows the animation.
When I double click it, it crashes. I want to make so it's only clickable once.
i have tried setEnabled(false);
i have tried setClickable(false);
i tried a variable that check if a button has been clicked and disables it.
public class Login extends AppCompatActivity {
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final SubmitButton LoginBtn = findViewById(R.id.login);
handler = new Handler();
LoginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginBtn.setEnabled(false);
handler.postDelayed(new Runnable() {
#Override
public void run() {
LoginBtn.setEnabled(true);
Intent startActivity = new Intent(Login.this, Main_page.class);
startActivity(startActivity);
finish();
}
}, 3200);
}
});
}
}
As I wrote, I want that if the button has been clicked that it becomes unclickable.
Try this:
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final SubmitButton LoginBtn = findViewById(R.id.login);
handler = new Handler();
LoginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginBtn.setEnabled(false);
handler.postDelayed(new Runnable() {
#Override
public void run() {
LoginBtn.setEnabled(true);
Intent startActivity = new Intent(Login.this, Main_page.class);
startActivity(startActivity);
finish();
}
}, 3200);
}
});
...
Explanation:
Your enable/disable block is outside of the onClick listener and it's in the onCreate method: in this way you call setEnable() method only when the activity was created.
But some time setEnable() can not work in case of very rapid click like explained in the second response here. In that case you can use a timer to check the elapsed time.
By the way, I think that your app crash because you don't handle in the right way the Handler. I suggest you also to add:
#Override
protected void onStop(){
handler.removeCallbacksAndMessages(null);
}
Try...
view.setOnClickListener(null);
...after your click event.
i have checked this links: 1- How to show a button after 5 seconds in android studio?
2- How make a button invisible for 1 or 2 second on another button click
But i couldnt understand where should i this handler in my code. I tried but didnt worked. "While u r going to home heard a scream " after this text i want to make visible my buttons. İ hope i could express myself.
This is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final TypeWriter tw = (TypeWriter) findViewById(R.id.tv);
tw.setText("");
tw.SetCharacterDelay(120);
tw.animatedText("While u r going to home heard a scream ")
Button btn2 = (Button) findViewById(R.id.button3);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent c = new Intent(Main2Activity.this,Main3Activity.class);
startActivity(c);
}
});
}
}
Try this .
Button btn2 = (Button) findViewById(R.id.button3);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 3 secs
}
}, 3000);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Button b = (Button) findViewById(R.id.btn);
invisibleButton(b);
}
private void invisibleButton(final View view){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
view.setVisibility(View.GONE);
}
}, 1000 * 3);
}
}
But Remember your import Handler should be-:
import android.os.Handler;
EDIT
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Button b = (Button) findViewById(R.id.button5);
b.setVisibility(View.GONE);
visibleButton(b);
}
private void visibleButton(final View view){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
view.setVisibility(View.VISIBLE);
}
}, 1000 * 3);
}
}
I am new to development field. I am developing android application where in my activity_main layout I have many different items. On clicking one of the button the top layout is replaced with a new layout.
Now instead of defining the new layouts buttons, textview etc in my main class , I want to have a separate class which can initialize my buttons etc, and also in that class I can declare onClickListners.
In my main Activity I want:-
public void onCreate(){
button bb = (Button)findViewById(R.id.mybutton);
View CurrentContentView= getLayoutInflater().inflate(R.layout.activity_main, null, false);
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView);
}
});
}
In my MyNewViewInit() class :-
public class MyNewViewInit{
ImageButton backbutton;
ChipsMultiAutoCompleteTextview search;
ImageButton searchclear;
ImageButton modeTime;
ImageButton modeTag;
TextView modeTimeTxt;
TextView modeTagTxt;
ScrollView mainScroll;
ScrollView selectedScroll;
public MyNewViewInit(View v){
backbutton = (ImageButton)v.findViewById(R.id.backbutton);
search = (ChipsMultiAutoCompleteTextview)v.findViewById(R.id.search);
searchclear = (ImageButton)v.findViewById(R.id.searchclear);
modeTime = (ImageButton)v.findViewById(R.id.modeTime);
modeTag = (ImageButton)v.findViewById(R.id.modeTag);
modeTimeTxt = (TextView)v.findViewById(R.id.modeTimeTxt);
modeTagTxt = (TextView)v.findViewById(R.id.modeTagTxt);
mainScroll = (ScrollView)v.findViewById(R.id.HAT1);
selectedScroll = (ScrollView)v.findViewById(R.id.HAT2);
tag = new OtherHelper().arrayread();
mainHashTag.setVisibility(View.GONE);
selectedHashTag.setVisibility(View.GONE);
clickListners();
}
public void clickListners(){
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
searchclear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTimeTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTagTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
So when I try using this code the onclicklistners are not working.
What is the best way to do this.
Thanks
Write an Interface in your class,
class MyNewViewInit{
public interface ClickListenerInterface{
void onCLickSomething(Something something);
}
ClickListenerInterface mClickListener;
...
public MyNewViewInit(View v,ClickListenerInterface clickListener){
mClickListener = clickListener;
...
}
public void clickListners(){
...
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mClickListener!=null){
mClickListener.onCLickSomething(v);
}else{
//throw an error that activity should implement ClickListenerInterface
}
}
});
}
}
In your Activity,
class MyActivity extends Activity implements ClickListenerInterface{
public void onCreate(){
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView,this);
//if using Android Studio, then use the context menu to implement the interface
}
});
}
void onCLickSomething(Something something){
//do something with something
}
}
You can try do a abstract class extends Activity, like :
public abstract class MyInitActivity extends Activity
{
EditText editText;
TextView textView;
#Override
public void setContentView(int layoutResID)
{
super.setContentView(layoutResID);
editText = (EditText)findViewById(R.id.editText);
textView = (TextView)findViewById(R.id.textView);
}
}
and in your main activity you can extend your abstract class, like :
public class MainActivity extends MyInitActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*********** Listener ************/
public void onButtonClick(View v)
{
textView.setText(editText.getText().toString());
}
}
you can also implement onClick() in the abstract class
i don't know if its a good practice , i prefer do the methods with view arguments and set this methods in the layout.xml file .
I had use this in one of code
STEP 1
Declare button in layout(XML file)
STEP 2
Declare all ur button like this--
Button btn1 = (Button)findViewById(R.id.btn1);
Button btn2 = (Button)findViewById(R.id.btn2);
Button btn3 = (Button)findViewById(R.id.btn3);
............so on
STEP 3
implement View.OnClickListener
Now after step 2 do this
btn1.setOnClickListener(this);
.
.
.
same way for all
STEP 4:
Use switch case inside Onclick
public void OnClick(View v)
{
case:R.id.btn1{
//ur command similarly use multiple case stmt for many button
}
}
In the android sdk, I have programmed 2 buttons to bring me to 2 different layouts. However, only the first one I have programmed will work, while the second one will do completely nothing. I will provide the code if you can catch any things I missed, please tell me what you think. This is one of my first applications to run on android, so try to explain your suggestion as simple as you could.
Code:
public class MainActivity extends Activity {
private final String T = "Settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
private final String T2 = "ManualAdd";
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feederButton();
}
private void settingsButton() {
Button messageButton = (Button) findViewById(R.id.Settings);
View.OnClickListener myListener = new View.OnClickListener() {#
Override
public void onClick(View v) {
Log.i(T, "You clicked the settings button!");
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
};
messageButton.setOnClickListener(myListener);
}
private void feederButton() {
Button feedButton = (Button) findViewById(R.id.AddFeeder);
View.OnClickListener my2ndListener = new View.OnClickListener() {
public void onClick(View v) {
Log.i(T2, "You clicked the add button!");
startActivity(new Intent(MainActivity.this, ManualAdd.class));
}
};
feedButton.setOnClickListener(my2ndListener);
}
}
You cannot make a different onCreate() method for every button you have. The reason why only one of your Buttons work is because only the onCreate() method is only called. The system has no idea about onCreate1(). To get both of your buttons working change
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
feederButton();
}
You can completely delete onCreate1() from your source code, it is now obsolete.
It would also be a good idea to follow the official Android "First App" tutorial instead.
Try this code
public class MainActivity extends Activity {
Button Your_Button_Name;
Button Your_Button_Name;
Activity activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_Name);
//In (R.id.Your_Btn_Name) that's the name that you gave your button in the XML
activity = this;
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name.setOnClickListener(listener);
Your_Button_Name.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
}
}
};
}