I am trying to create a word quiz game. I have a list of random words which are going to appear, and the player must arrange them correctly. Now I want to add hints. For example, if word is "School" appears, I want the hint to be "photo of school"
These are the words in the code's layout
<string-array name="words">
<item>SCHOOL</item>
<item>ABETS.BEATS.BEAST</item>
<item>ANGEL.ANGLE.GLEAN</item>
<item>COATS.COAST.TACOS</item>
</string-array>
In android studios go to res/drawable and add the photos you want to use there.
Here's how to do that : How to add an image to the "drawable" folder in Android Studio?
Then you initialize the photos with :
private ImageView schoolPhoto;
schoolPhoto = (ImageView) findViewById(R.id.imageViewId);
schoolPhoto.setImageResource(R.drawable.imageFileId);
Then you can create a method with toast which is often used for hints:
public static displayHint( ImageView imageHint) {
Toast toast = new Toast(this);
toast.setView(imageHint);
toast.show();}
And run that method as displayHint(schoolPhoto) when you show the string.
Related
I am making an app similar to facebook where i have a news feed which shows all the posts. However, the number of post in this feed varies with each user, depending on the amount of friends they have and how many posts they have made. I want to create a CardView for every post item which will show the name of the user who posted it, the time it was posted and the actual post itself. The layout will be split horizontally once with the post in the bottom half and the name of the person with the time it was posted on the top half.
Currently i can only find ways to do this by altering the xml text, but i want to create these CardViews as and when i need them, in my Java code.
I know i can alter TextViews by using such functions as setText() & setTextSize(). Is there a way for me to use similar functions on a CardView which can alter the base layout.
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
CardView TVcard = new CardView(Home.this);
TextView TVposts = new TextView(Home.this);
TextView test = new TextView(Home.this);
TVcard.addView(TVposts);
TVcard.addView(test);
test.setText("test");
test.setBackgroundColor(Color.GREEN);
TVposts.setText(snapshot.getValue().toString());
TVposts.setTextColor(Color.WHITE);
TVposts.setTextSize(25);
TVposts.setBackgroundColor(Color.BLACK);
TVposts.setHeight(200);
linearLayout.addView(TVcard);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) TVcard.getLayoutParams();
params.setMargins(0, 20, 0, 0);
TVcard.setLayoutParams(params);
}
Thanks
I am trying to change photos in android studio by clicking on my button.
When I put code for changing the photo in my MainActivity.java I keep getting this type of error messages and it says :
Cannot resolve symbol "image"
image.setImageResource(R.drawable.xxx);
I am watching Udemy course for android development and I have done everything same like the professor on that video.
I have tried to restart android studio.
I have tried to make new project.
I have tried to clear invalidate caches and restart.
public void changeImage(View view)
{
ImageView bitcoin = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);
}
I hope there is actual error with android studio,because code is clone of the video that I am watching.
You are binding your layout's ImageView in Java file with bitcoin variable and you are trying to set an image on an unknown variable 'image'(maybe it's not defined in the class). So you have to set as below.
ImageView bitcoin = findViewById(R.id.bitcoin);
bitcoin.setImageResource(R.drawable.xxx);
Set Your Code Like this
ImageView image = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);
change your this line
image.setImageResource(R.drawable.xxx)
to this one:
bitcoin.setImageResource(R.drawable.xxx)
i need to know how to setup background randomly on one activity,
'android:background="#drawable/backG"' only show one image
the background come randomly with the launch of the application, and it keep on showing until the user quit the app, and when he reopen it, it will show a new background
note : i have only one activity in my project
thanks in advance
I think, you should use Java code to make this.
For example, you have RelativeLayout is the main layout in your activity
You should use
RelativeLayout rLayout = (RelativeLayout) findViewById (R.id.rLayout);
Resources res = getResources(); //resource handle
Drawable drawable = res.getDrawable(R.drawable.newImage); //new Image that was added to the res folder
rLayout.setBackground(drawable);
For random background: you create an array that contains all you images background
and use the code below for random position
Random randomGenerator = new Random();
int randomPositionInt = randomGenerator.nextInt(10); // it will generate position in 0 - 10
Hope this help
I have this line in the strings.xml file:
<string name="backup_help_body">Please watch this video if you did not understand the above steps: www.youtube.com</string>
and the above text in an AlertDialog:
AlertDialog.Builder backupHelpDialog = new AlertDialog.Builder(this);
backupHelpDialog.setTitle(getString(R.string.backup_help_title));
backupHelpDialog.setMessage(R.string.backup_help_body);
backupHelpDialog.setIcon(R.drawable.my_notes);
backupHelpDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface backupHelpDialog, int witch) {
// Do Not Do Anything.
}
});
backupHelpDialog.show();
return true;
How can I make the link clickable in the AlertDialog without using a TextView?!
I'm not sure you can do without a TextView, I would use a Custom AlertDialog, however you can create a "temporary" TextView within the code to perform what you need, here I leave the url where you have several examples
How can I get clickable hyperlinks in AlertDialog from a string resource?
Use HTML formatting :
Example :
<string name="my_link">Click me!</string>
For reference :
Android-Is it possible to add a clickable link into a string resource
Now for making the link as clickable, you need to add the LinkMovementMethod to the corresponding Textview.
To get the related Textview, you need to fetch the mAlert field from AlertDialog using reflection and then findviewbyId(android.R.id.alertTitle)
Reference :
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.0_r1/android/app/AlertDialog.java#AlertDialog.0mAlert
I think the best and fastest way (what I do) would be to add a custom layout with a textview inside, then add an onClickListener on that textview.
#SuperEasyToDo
Here, I'm trying to start an activity on different devices (Android Tablet and Android Phone). Is there a way to use if statement to check which activity should to load based on the devices?
Here's my code:
Button buttonGo = (Button) findViewById(R.id.button_goToActivity);
buttonGo.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (tablet device) {
startActivity(new Intent(MainActivity.this, TabletActivity.class));
} else {
startActivity(new Intent(MainActivity.this, AndroidPhoneActivity.class));
}
});
Thanks for your effort...
In order to achieve that you must use Fragment not an Activity. the Design Philosophy of Fragments is exactly what you want. you can take a look at Example to see how it is implemented. This is a master-detail app with two Fragments, when device in a portrait mode or it is not a tablet it just shows master, otherwise it also shows detail beside the master and when you select an item from the list you will see the details without going to other Activity.
Firstly as Android Best Practice it is adviced to use Fragments rather than Activity to achieve your requirement.
But if you are keen on using Activity, then below is the solution to find device type and load different activities.
1) Have 2 values folders: values -> strings.xml and values-sw600dp -> strings.xml
2) Create string value in both the xml
<string name="device_type">PHONE</string> in values -> strings.xml
<string name="device_type">TABLET</string> in values-sw600dp -> strings.xml
3) create a method to check if current device is Phone or not
public boolean isPhone() {
String deviceType = getResource().getString(R.string.device_type);
if (deviceType.equalsIgnoreCase("PHONE") {
return true;
} else {
return false;
}
}
4) Depending on the device load your activity as you have already done in your above code snippet
Let me know if this helps...
Most likely, you should not have two separate activities here. Instead, you should have two different layouts for the same activity. The layout for phones should be in the regular res/layout folder and the layout for tablets should be in the res/layout-large folder. Read Supporting Different Screens for more details.
If you are using same fuctionality for Tablet Activity and Phone activity then you do not need two activities for this purpose. You need to make two different Layout xml for Tablet as well as Phone.
Make a Layout folder named : layout-large and layout-xlarge.
Now copy the xml file from normal layout folder and paste in both new layout folders.
No need to change the name of xml file.
Now Adjust xml file of layout-large and layot-xlarge as you want to make design for tablet.
Now on running application Tablet and phone bot will pick xml according to there size and screen density.
Note : the functionality should be same.
And if: you want to show different screen with different functionality for phone and tablet then. use this code for check.
if ((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
}
Hope this wll help you.