+ 7

[SOLVED]how can run a class with a button aside main activity in android studio??

I checked a lot of web site never find a good answer😓😓

6th Jun 2020, 3:17 PM
hamid
hamid - avatar
22 Respostas
6th Jun 2020, 3:20 PM
Nilesh
Nilesh - avatar
+ 4
hamid I don't find any difficulty upon your problem. These can be done by just basic of Android skills. However I have simple prototype for your password generator.
6th Jun 2020, 4:17 PM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 4
You just need is a text view and a button.
6th Jun 2020, 4:18 PM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 4
Just write this XML code for simple layout of your app. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginEnd="10dp" android:id="@+id/txt"/> <Button android:text="click" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginTop="72dp" android:id="@+id/btn"/> </LinearLayout>
6th Jun 2020, 4:19 PM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 4
And just paste this following code for MainActivity.java. package com.mycompany.myapp; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn=(Button)findViewById(R.id.btn); final TextView txt=(TextView)findViewById(R.id.yxt); btn.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View p1) { txt.setText(generatePwd(6).toString()); } }); } static char[] generatePwd(int len) { System.out.println("Your Password:"); String charsCaps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String chars = "abcdefghijklmnopqrstuvwxyz"; String nums = "0123456789"; String symbols = "!@#$%^&*_=+-/€.?<>)"; String math = "⅙œŸ⁶⁷⁞âč⅘Ÿ⅜⅗⅔⅖⅕⅒⅑"; String nam = "â˜…â€ â€ĄÂ«Â»â€čâ€șÂĄÂżâ€œâ„–â‚ŹÂąÂŁÂ„â‚±â€”â€“Â±â™Łâ™ â™Ș♄♊ΩΠΌ§
6th Jun 2020, 4:20 PM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 4
String passSymbols = nam + math + charsCaps + chars + nums + symbols; Random rnd = new Random(); char[] password = new char[len]; int index = 0; for (int i = 0; i < len; i++) { password[i] = passSymbols.charAt(rnd.nextInt(passSymbols.length())); } return password; } }
6th Jun 2020, 4:22 PM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 4
You can run this code or implement to an Android apk through Android studio.
6th Jun 2020, 4:23 PM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 4
Comment me if you find any difficulty or you need different than this.
6th Jun 2020, 4:24 PM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 4
You don't need onClick attribute in XML it's a different way of handling Click events. We will be using Click listeners. that we already have in our code.
6th Jun 2020, 4:28 PM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 4
Is that works? Did you run it on your computer?
6th Jun 2020, 4:29 PM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 4
You are welcome. Don't stop learning and questioning all the best for your future improvements
6th Jun 2020, 4:32 PM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 3
I don't know what is your Question pointing but can you just describe your problem more clearly that people can answer you more accurately. Or did you mean jumping through different activities with a Button clicked.
6th Jun 2020, 3:39 PM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 3
for example I want to run my password generator in very simple app with one button and text area to show the generated password in it
6th Jun 2020, 3:49 PM
hamid
hamid - avatar
+ 3
and where should I add Android: onclick in XML
6th Jun 2020, 4:26 PM
hamid
hamid - avatar
+ 3
yes thanks again
6th Jun 2020, 4:30 PM
hamid
hamid - avatar
+ 3
hamid you are trying to run Java console program through Android sdk or Android studio. You don't need Scanner to get user input instead we will use EditText So copy this XML code between the TextView and Button. <EditText android:layout_width="150dp" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:id="@+id/edt"/>
7th Jun 2020, 2:28 AM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 3
You also don't need a hole class program you can turn it into a method that returns value b instead of printing. So copy this code on MainActivity.java public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn=findViewById(R.id.btn); final EditText edt=findViewById(R.id.edt); final TextView txt=findViewById(R.id.txt); btn.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View p1) { try{ //Getting the user input int fruit=Integer.valueOf(edt.getText().toString()); txt.setText(Integer.toString(program(fruit))); }catch(Exception e){ } } }); } public int program(int fruit){ int a=fruit/2; int b=a/3; return b; } }
7th Jun 2020, 2:31 AM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 3
worked amazing thank you so much
7th Jun 2020, 5:28 AM
hamid
hamid - avatar
+ 2
also this is my password generator if want to look at it https://code.sololearn.com/cYEEBGEhEnvf/?ref=app
6th Jun 2020, 3:54 PM
hamid
hamid - avatar
+ 2
thank you so so so much ❀❀❀
6th Jun 2020, 4:29 PM
hamid
hamid - avatar