open radio buttons in different class - android

I am building a login system in android and i need to accomplish the following:
save userdata on the local storage
when a user is created and i press on the button it needs to dynamically create a radiobutton with the user name and short name in it.
I have already managed to accomplish these things, but the only problem i have now is that i want to to have 2 classes, one where you register your account and the other where the radiobuttons with the user data is displayed. So my question: how can i program the calss in such a way that when i press on the register button that it creates the radiobuttons in a different class.
beneath is the code of the classes:
enter code here
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.Save:
Intent I = new Intent();
I.putExtra(
I.setClass(this, ShowUsers.class);
startActivity(I);
break;
}
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Start other Activity
switch (v.getId()) {
case R.id.create:
Intent intent = new Intent();
intent.setClass(Main.this, Register.class);
startActivity(intent);
break;
}
Bundle extras = getIntent().getExtras();
String userName;
if (extras != null) {
userName = extras.getString("editTextName,editTextPassword,editTextshortname");
// and get whatever type user account id is
}
}
}
when i click the save button it needs to create the radio button in the main class, now the question is how can i accomplish this?

If the second class is an Activity, then one solution is to send it an Intent with the data it needs to create the radio buttons. You do this by creating an Intent object, calling any of its setExtra() methods, then calling startActivity() with the Intent. Check out the Android API docs for more details.

Related

How to open a webView from assets, by switchcase function?

I have 10 cardview in a fragment, when user click on one it'll open a specific html file within the application. When I try to open this class, the apps always crash .
My MainActivity is here
2nd Activity is here
What you can do to solve your issues is, in your MainActivity's onclick method change like :
Intent i;
switch (v.getId()) {
case R.id.indonesia_card:
i = new Intent(this, Indonesia.class);
i.putExtra("isFrom", "Indonesia");
startaCtivity(i);
case R.id.mathematika_card:
i = new Intent(this, Indonesia.class);
i.putExtra("isFrom", "Mathematika");
startaCtivity(i);
case R.id.ipa_card:
i = new Intent(this, Indonesia.class);
i.putExtra("isFrom", "IPA");
startaCtivity(i);
}
Now, In your other Activity receive the intent and set data appropriately.
String from = getIntent.getStringExtra("isFrom");
if (isFrom != null) {
if (isFrom.equals("Indonesia") {
webview.loadUrl("file:///android_asset/yourfile.html");
} else if (isFrom.equals("Mathematika") {
webview.loadUrl("file:///android_asset/yourfile.html");
}else if (isFrom.equals("IPA") {
webview.loadUrl("file:///android_asset/yourfile.html");
}
}
Hope this will help you.

Cancel/Modify Intent Message

I want to send a confirmation from the second activity to the main activity. If the checkbox is marked then I want to set Intent data and set the Result returned from activity to true.
Otherwise if the checkbox is unmarked then I want to either:
Modify the intent that was set to false
Remove the intent altogether as if it wasn't set
ConfirmCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
if (checked) {
Intent data = new Intent();
data.putExtra(EXTRA_CONFIRM, true);
setResult(RESULT_OK, data);
}
else
{
// Remove or modify to false.
}
}
});
How can I do either of the actions to modify or cancel the Intent message? also, I would like to know what's the best way to handle it?
you can remove the extras using removeExtra() method and replace it using getIntent().replaceExtras()
Remove argument from intent:
getIntent().removeExtra("search");
Replace bundle with new extras:
Bundle extras = new Bundle();
extras.putString("key","Value");
getIntent().replaceExtras(extras);
You can simply use the checkbox value as value for your Intent. There's no need to have this if/else construct.
#Override
public void onClick(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
Intent data = new Intent();
data.putExtra(EXTRA_CONFIRM, true);
setResult(RESULT_OK, data);
}
In your MainActivity's onActivityResult you can simply check the Intents Extra for EXTRA_CONFIRM and decide how to react if the value is true/false.
Thats the basic approach to handle data sending between activities.
Further reading:
https://developer.android.com/training/basics/intents/result.html

How to add a registration page to log-in page it?

I am a new android developer trying to create a log-in and register page. I have created both Log-in and Registration pages but i don't know how to add the registration page with the main log-in interface. I have tried to add it but I don't know how to do it. Can anyone let me clear on how to add a page X with another page Y.
Your answers to guide me are welcome.
Create a button in login page suppose btn_login.Then
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i=new Intent(Loginpage.this,Registration.class);
startActivity(i);
}
});
In this way you can connect two pages using Intent.
For this you can make use of Intents ,Assuming that you have two activities and A button named buttonReg
1.Login Page (Login.java)
2.Registration Page(Register.java)
Create an Intent
For the register page like like
Intent in=new Intent(getApplicationContext(),Register.class);
and start activity in the button click like
buttonReg.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
startActivity(in);
}
});
And do not forget to add the Activity to your Manifest.xml
For more information about intents goto this tutorial

Make phone call without getting phone number (click icon to call) in android?

I'm currently working on my 2nd program and stuck with phone calling, sending SMS, emailing, and showing location using Google Map. I'm using ImageButtons for all of these. User doesn't need to type any numbers or addresses (not using EditText to get user input). However, it doesn't work.
For example, when user click on Phone icon, it will make a call. However, I still need a user input (the actual message, not the destination number or email address) for sending SMS and email. I can do it if I use EditText. However, without using EditText, I cannot do it. How do I do this? I've added users permission in manifest file.
PhoneActivity .java
public class PhoneActivity extends Activity {
ImageButton btnphonecall;
String phoneNumber = "091111111";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_phone);
btnphonecall=(ImageButton)findViewById(R.id.imageButton1);
btnphonecall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse("091111111"));
PhoneActivity.this.startActivity(i);
//I've tried startActivity(i) along but still doesn't work
}
});
}
Use this:
Uri number = Uri.parse("tel:"+091111111 );
Intent dial = new Intent(Intent.ACTION_CALL,
number);
dial.setPackage("com.android.phone");
PhoneActivity.this.startActivity(dial);
Try this:
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phoneNumber));
startActivity(callIntent);

Android: Error transfer data after click of buttons

Hi there I am new to Android Programming
I am trying to create an application in which, the user clicks button on the first page
the text color in the buttons change color and the change is reflected in another activity page.
To do this I have
1) one fragment class(BookLockerFragment) which reference to an xml file containing the buttons
2) The parent activity file (TabActivity.java)
3) The activity file to reflect the change ( complainResponse.java)
Here is the code:
LodgeComplaintFragment.java
ArrayList<String>userSelectedOptions = new ArrayList<String>();
if(btnSis.getCurrentTextColor()==Color.BLUE){
userSelectedOptions.add("SIS");
}
Button but = (Button) root.findViewById(R.id.searchButton);
.....
but.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
buttonListener.onMakeBookingButtonPressed(userSelectedOptions);
}
});
TabMainActivity.java
public void onMakeBookingButtonPressed(ArrayList<String> list) {
// TODO Auto-generated method stub
Intent intent = new Intent(TabMainActivity.this,
complainResponse.class);
intent.putStringArrayListExtra("userSelectOptions",list);
startActivity(intent);
}
complainResponse.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
setContentView(R.layout.complainresponse);
userInput = intent.getStringArrayListExtra("userSelectOptions");
// Creates the window used for the UI
if (userInput != null) {
if (userInput.get(0) != null) {
textview1 = (TextView) findViewById(R.id.textView1);
textview1.setText(userInput.get(0));
}
}
}
Error occurs at this line:
if (userInput != null) {
//of complainResponse.java
Logcat:
java.lang.IndexOutOfBoundsException
Please help
There's nothing in the ArrayList that you pass to your activity.
I suspect this bit of code isn't being executed -
if(btnSis.getCurrentTextColor()==Color.BLUE){
userSelectedOptions.add("SIS"); <------------ never gets here
}
To verify this, run the application in debug mode, and place a breakpoint at the if statement
userInput.get(0) != null
this is the cause of error in my opinion, list can be initialized but empty.
instead you should use,
if (!userInput.isEmpty())

Categories

Resources