Hi I'd like to create 2 sets of images where one set is displayed if the user selects something from a radio button and the other is displayed if the user selects the other radio button,
In truth I'm an amateur at this and it may not even be radio buttons that I want but its one set of images for a girl and another for a boy, any suggestions welcome
this is what I did... its inside an edittext but it shouldnt make any difference :
<EditText
android:id="#+id/exerciseInputFilter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="search..."
android:textColor="#android:color/holo_blue_light" />
<ImageView
android:id="#+id/drillListSearchImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/exerciseInputFilter"
android:layout_alignParentRight="true"
android:src="#drawable/search_white" />
in the xml for example... frame is also possible...
and in code...
SearchImage = (ImageView) findViewById(R.id.drillListSearchImage);
SearchImage.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!filterText.getText().equals(null)) {
filterText.setText("");
SearchImage.setImageResource(R.drawable.search_white);
}
return false;
}
});
so just create 1 image view and then on touch do something...
if you want to use a radio or checkbox its the same principle... on item click... set imageview to whatever you want...
rd2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
rd1.setChecked(false);
SearchImage.setImageResource(R.drawable.search_white);
}
});
for example... does this answer your question?
Edit
after reading your comment (which wasnt understandable from your original question)... what you want is just to pass information between activities to do that use this:
Activity A:
create a boolean and a 2 check or radios and
CheckBox maleBox = (CheckBox) findViewById(R.id.Male);
maleBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
isMale = true;
isFemale = false;
femaleBox.setChecked(false);
}
}
});
you can do the same for female if you want several radios or checkboxs... then...you pass the information to activity two... like so:
intent = new Intent(whateverclass.this, whoeverclass.class);
intent.putExtra("Gender", isMale);
startActivity(intent);
now you can read it in activity two with:
if (getIntent().getBooleanExtra("Gender", false)) {
// set Images or buttons or what ever...(like i showed you above)
SetImage1.setImageResource(R.drawable.boy);
.
.
.
}
you can also use bundle to get your variables...
Intent intent = new Intent();
intent = getIntent();
Bundle bundle = intent.getExtras();
then just get the boolean...
Related
We have tried two ways to display a Custom Snackbar (1) as a masquerading Dialog which will not move to the bottom of the screen It does however not dismiss the current Activity view just makes it opaque. I know why it is in the center of the screen but I am not able to move it to the bottom. (2) next is a view that takes over the entire screen because it is a new content view that I am guessing dismisses the current Activity view BUT it is at the bottom of the screen.
So my question is how to use design number 1 and move the Dialog to the bottom of screen?
Second question how to stop the new view in design number 2 from dismissing the view of the current Activity? After careful reading and little thought and extreme testing I do not think this is possible! I have posted the code for my two methods below. The XML file uses a Relative Layout as the base container.
public void seeSB(){
setContentView(R.layout.custom_snackbar);
// Line of Code above shows XML file
// Line of code tested but no control over the "viewMyLayout"
//LayoutInflater inflater = LayoutInflater.from(ListActivity.this);
//final View viewMyLayout = inflater.inflate(R.layout.custom_snackbar, null);
//viewMyLayout.setEnabled(true);
Button btnAB = (Button) findViewById(R.id.btnAB);
btnAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// viewMyLayout.setEnabled(false);
// Line above does not function
// CODE BELOW WORKS BUT FAR FROM elegant
setContentView(R.layout.activity_list);
//Intent intent = new Intent(ListActivity.this, ListActivity.class );
//startActivity(intent);
Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show();
}
});
}
public void displaySB(){
final Dialog openSnack = new Dialog(context);
openSnack.setContentView(R.layout.custom_snackbar);
Button btnAB = (Button)openSnack.findViewById(R.id.btnAB);
TextView tvSB =(TextView)openSnack.findViewById(R.id.tvSB);
//Dialog dialog = new Dialog(ListActivity.this);
//dialog.setContentView(Bottom);
// if YES delete Master Password from TABLE_MPW
btnAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openSnack.dismiss();
Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show();
}
});
openSnack.show();
}
This is far from functional in my book because the method design has just one Custom Snackbar to look at so you need to work on how to have multiple fixed Custom Snackbars. One suggestion might be to have multiple sub views in your parent view and call the sub view you want. I will post just the sub view I added to the parent XML file and the not so real dynamic method to implement which is implemented in this case with a button click. For this to work in a real application the code would need be called from some method or event.
You might consider a switch statement for multiple views ? ? ?
TAKE NOTE THE RELATIVE LAYOUT has its visibility set to GONE at the start
<RelativeLayout
android:id="#+id/hold_snackbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_Black"
android:visibility="gone"
android:orientation="vertical">
<TextView
android:id="#+id/tvSB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:text="#string/snackbar_text"
android:textColor="#color/color_Yellow"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="350dp"
android:layout_marginTop="5dp"
android:background="#color/color_Transparent"
android:focusable="false"
android:text="#string/snackbar_action"
android:textColor="#color/color_Red"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
Notice the View subViewGroup is declared when the Activity starts
View subViewGroup;
public void makeSB(View view) {
subViewGroup = findViewById(R.id.hold_snackbar);
subViewGroup.setVisibility(View.VISIBLE);
seeSB();
}
public void seeSB(){
Button btnAB = (Button) findViewById(R.id.btnAB);
btnAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
subViewGroup.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show();
}
});
}
Countdown Timer to close a Snackbar with no Action Button
public void makeCDT(View view) {
cdt = new CountDownTimer(5000, 100) {
// 5 sec 5000,100
// 10 sec 10000,100
#Override
public void onTick(long secsUntilFinished) {
etCPW.setText(String.valueOf(secsUntilFinished / 1000));
//etCPW.setText("seconds remaining: " + secsUntilFinished / 1000);
subViewGroup = findViewById(R.id.SB_NO_ACTION);
subViewGroup.setVisibility(View.VISIBLE);
}
#Override
public void onFinish() {
etCPW.setText("Counter Done");
subViewGroup.setVisibility(View.GONE);
if(cdt!=null){
cdt.cancel();
}
}
};
cdt.start();
}
I am making a music player app in which there is an image of the play button and once clicked it switch to pause.png and song start playing. Clicking again on the button will change the image to play.png and pause the sound. This pattern continues.
This question has been previously answered first click change to new image and second click change to old image, android
But the checked answer doesn't work because boolean variable used to switch need to be declared final. And once declared final I cannot change the value of the variable.
boolean showingFirst = true; //Declare globally
image1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(showingFirst){
image1.setImageResource(R.drawable.img1);
showingFirst = false;
}else{
image1.setImageResource(R.drawable.img2);
showingFirst = true;
}
}
});
You can do this in this way using tag attribute of ImageView:
iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(iv.getTag().equals("playing")){
iv.setImageResource(R.mipmap.play);
iv.setTag("paused");
} else {
iv.setImageResource(R.mipmap.pause);
iv.setTag("playing");
}
}
});
And your ImageView declaration in xml file will be like below:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#mipmap/pause"
android:tag="playing"
android:id="#+id/iv"/>
I created an activity on android studio and I have put there something like 20 ImageButtons. I want to use it as on each click on an image it will move to a new activity. All of the Image Buttons are working on the same principle, my app is a game, and each image represents a level. I want to build one function that will be used on all buttons and will move the user to a new activity according to the data(the properties of the image button) and use that data on the new activity. Every level has its own activity and the main activity is the menu of the game.
Below is my code:
public ImageButton beatsCall; public void Beats(){ beatsCall=(ImageButton)findViewById(R.id.beats); beatsCall.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) { Intent toy = new Intent(Levels.this,Beats.class); startActivity(toy); } }); }
You need to provide more information and code. However, you may want to try set a distinct onClickListener and then set all the imageButtons to that listener that will perform an action depending on the button clicked. For example, say you have 4 imageButtons and you want to perform a different action (in your case, start a new activity) for each different button click.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
//Start activity 1 here, for example
Intent intent = new Intent(this, YourNewActivity1.class);
String message = v.getId().toString;
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
break;
case R.id.textView2:
//Start activity 2 here
break;
case R.id.textView3:
//Start activity 3 here
break;
case R.id.textView4:
//Start activity 4 here
}
}
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
This is assuming you have the imageButtons set up in your layout file and you have them initialized in your activity.
In your new activity, you can get the message as such:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
if (some condition with message){
do something
}
You may also check out this documentation for further information regarding intents.
Something like this? In your xml make your images clickable and give them ID's like this...
<ImageView
android:id="#+id/level_1_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
/>
Then call a function like this in your Activity's onCreate
private void setupButtons() {
findViewById(R.id.level_1_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplication(), LevelOne.class));
}
});
findViewById(R.id.level_2_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplication(), LevelTwo.class));
}
});
}
You could assign a tag via android:tag to each of your views and then use your single listener to switch on the view's tag to branch the behavior you want.
I have a text view in which the user can enter data at run time using the custom buttons that I have created.
My delete button is able to delete one character at a time but if i hold the button then it stops.
I want the text field to get cleared when i hold the button.
Is there any solution to this....??
Please help me out.
This is my xml,
<RelativeLayout
android:id="#+id/btnClear"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
>
<ImageView
android:id="#+id/imgClear"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/img_clear" />
</RelativeLayout>
This is my code,
imgClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getNumber;
if(isFirstNum){
getNumber = txtFirstNumber.getText().toString();
if(getNumber.length() > 0)
txtFirstNumber.setText(getNumber.substring(0, getNumber.length()-1));
} else if(!isFirstNum){
getNumber = txtSecondNumber.getText().toString();
if(getNumber.length() > 0)
txtSecondNumber.setText(getNumber.substring(0, getNumber.length()-1));
}
}
});
You can use the onLongClickListener to check if the delete button is pressed for a long time
imgClear.setOnLongClickListener(new OnLongClickListener()
{
#Override
public boolean onLongClick(View v)
{
txtFirstNumber.setText("");
return true;
}
});
This will set your txtFirstNumber to blank but the onClickListener will not be called.
Your question is not clear. But as per my understanding you want to clear the text of the text view then in your button click listener just set the text for textview to empty.
eg:
public void onClick(View v){
textview.setText("");
}
I'm trying to code a checkbox into a help screen which is essentially a pop up view (an activity that's started by the main program) containing a ScrollView that has the help text, and OK button, and a checkbox that asks if you want the help screen to appear automatically at program start. Everything displays properly on the screen, and the checkbox toggles, when touched. However, when OK is pressed, and I test the state of the checkbox with .isChecked() I'm always getting a false. I'm sure it's something simple I've missed. XML file follows follows:
helpdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#ffffff">
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content" android:layout_below="#+id/ImageView01"
android:layout_height="300px">
<TextView android:text="#+id/helpView" android:id="#+id/helpView"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#000000"/>
</ScrollView>
<Button android:id="#+id/Button01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:text=" OK "
android:layout_below="#id/ScrollView01"/>
<CheckBox android:id="#+id/chkNoHelp" android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Don't Display at Startup"
android:layout_below="#id/Button01" />
</RelativeLayout>
HelpBox.java:
public class HelpBox extends Activity {
CheckBox checkBox;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set up dialog
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.helpdialog);
dialog.setTitle("Help");
dialog.setCancelable(true);
//set up text
TextView text = (TextView) dialog.findViewById(R.id.helpView);
text.setText(getString(R.string.help_text));
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Boolean mDisplayHelp;
setContentView(R.layout.helpdialog);
checkBox = (CheckBox) findViewById(R.id.chkNoHelp);
if (checkBox.isChecked()) {
mDisplayHelp = true;
} else {
mDisplayHelp = false;
}
finish();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
}
Setting breakpoints on both "mDisplayHelp" lines always breaks at the 'false' branch regardless of whether the check box displays a green check or not when the OK button is pressed.
Thanks in advance.
Edit (10/10):
Its clear what I want to do is pick up information after the user exits the dialog, so I can sense the state of the checkbox and store it as a preference. For this I assume I have to test it in onDestory. So, I did that:
#Override
public void onDestroy() {
Boolean mDisplayHelp;
setContentView(R.layout.helpdialog);
checkBox = (CheckBox) findViewById(R.id.chkNoHelp);
if (checkBox.isChecked()) {
mDisplayHelp = true;
} else {
mDisplayHelp = false;
}
}
However, I'm still always coming up with FALSE as a result, regardless of whether the checkbox is display checked or off. In this instance, if I don;t include the setContentView, I get a NullPointerException on the isChecked.
Why are you calling setContentView a second time? After the second time you call it, your checkbox is being reset to unchecked (the default state) and then you are immediately checking its state to set a flag.
Its not clear what your intention is here by inflating helpdialog.xml twice, once in a Dialog and once in your main Activity. It sounds like you want to use a dialog-themed activity here and only call setContentView() once in onCreate. It should behave as expected after that.
I finally figured this out, and boy was I going about things the wrong way. Chalk it up to learning curve.
What I needed to do was save the checkbox state. I needed to use SharedPreferences for that. I needed to detect when the checkbox was touched by using a listener on it and then saving the preference at that time. Upon opening the help box, I load the preference state and set the check box to that state so it shows properly based on the user's previous setting.
Finally, load the preference in onCreate at the main activity and call the showHelpBox() if the preference was so set.
In onCreate of main activity:
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Boolean mDisplayHelp = prefs.getBoolean("checkboxPref", false);
if (mDisplayHelp == false)
showHelpBox();
In help box activity pre-initialize the check box based on previous setting:
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
mDisplayHelp = prefs.getBoolean("checkboxPref", false);
final CheckBox ckBox = (CheckBox) dialog.findViewById(R.id.chkNoHelp);
ckBox.setChecked(mDisplayHelp);
When checkbox is touched:
ckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (ckBox.isChecked()) {
mDisplayHelp = true;
} else {
mDisplayHelp = false;
}
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("checkboxPref", mDisplayHelp);
// Don't forget to commit your edits!!!
editor.commit();
}
});
Hopefully others can learn from my foolish mistakes and misunderstanding.