+ 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đđ
22 RĂ©ponses
+ 5
This may help you!
https://developer.android.com/guide/topics/ui/controls/button
+ 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.
+ 4
You just need is a text view and a button.
+ 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>
+ 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 = "â
â âĄÂ«Â»âčâșÂĄÂżâœââŹÂąÂŁÂ„â±ââ±âŁâ âȘâ„âŠÎ©Î Ό§
+ 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;
}
}
+ 4
You can run this code or implement to an Android apk through Android studio.
+ 4
Comment me if you find any difficulty or you need different than this.
+ 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.
+ 4
Is that works? Did you run it on your computer?
+ 4
You are welcome.
Don't stop learning and questioning all the best for your future improvements
+ 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.
+ 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
+ 3
and where should I add Android: onclick in XML
+ 3
yes thanks again
+ 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"/>
+ 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;
}
}
+ 3
worked amazing thank you so much
+ 2
also this is my password generator if want to look at it
https://code.sololearn.com/cYEEBGEhEnvf/?ref=app
+ 2
thank you so so so much â€ïžâ€ïžâ€ïž