Polygon Onclick Openwindow - android

I want make application on Android with multiple polygon, and when i click one polygon it show something like open window and show detail of the polygon. Is there any solution to make function like that?

Yes you can use ImageButtons for polygon images and handle their click events by opening new activity or dialog for showing information about the polygons.
You will handle the click events like this:
ImageButton ib = findViewById(R.id.polygon1);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//show dialog or new activity with details
Intent i=new Intent();
int polygon_id = id_of_polygon;
i.putExtra("id", polygon_id);
i.startActivity(this,DetailsActivity.class);
finish();
}
});
You can get this value in details activity like this:
int id;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
id= 0;
} else {
id= extras.getInt("id");
}
}
Then in this activity you can show details about the polygon.
All this code will be added to the onCreate() methods of the activities.

Related

Why are all the indexes in my array being set to null after being set to another activity?

I have a game app where you can go into your inventory, which is another activity, and select items that you have picked up while playing the game then when you go back to the main game activity the items are used. To keep track of which of the 6 items the user wants to use I created a boolean array and set the corresponding index to true when an item is clicked then send it back to the main game activity. But when I go back to the main activity the app crashes when I try to check if an index is true. I used the debugger and found that all the indexes in my boolean array were set to null but they were all false in the inventory activity.
Game Activity
The app crashes once it gets to the if statement.
#Override
public void onResume(){
super.onResume();
//this code checks if a item was used and calls the items method
Intent Item= getIntent();
boolean[] usedItem = Item.getBooleanArrayExtra(Inventory.tools);
if (usedItem[0] == true) {
breadItem();
} else if (usedItem[1] == true) {
potionItem();
} else if (usedItem[2] == true) {
swordItem();
}
}
Inventory Activity
I have temporarily commented out working code that I believe has nothing to do with the problem
Intent Item= null;
public static final String tools= "an item was used";
boolean[] itemUsed = new boolean[6];
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory);
/*bread= (ImageButton) findViewById(R.id.ibBread);
potion =(ImageButton) findViewById(R.id.ibPotion);
sword =(ImageButton) findViewById(R.id.ibSword);
wings =(ImageButton) findViewById(R.id.ibWings);
bubble =(ImageButton) findViewById(R.id.ibBubble);
teleport =(ImageButton) findViewById(R.id.ibTeleport);*/
Item = new Intent(Inventory.this, MainGame.class);//data sent to MainGame activity
/*ImageButton[] items = {bread, potion, sword, wings, bubble, teleport};
Intent bag = getIntent();
boolean[] boolItems = bag.getBooleanArrayExtra(MainGame.booInventory);
for(int x =0; x <6; x +=1) {
if (boolItems[x] == false)
{
items[x].setImageDrawable(getDrawable(R.drawable.question_mark));
}
}*/
Item.putExtra(tools, itemUsed);
}
One of the six methods that set an index to true when the item is pressed. Also in Inventory activity
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void breadAction(View v)//lowers hunger by 2
{
bread= (ImageButton) findViewById(R.id.ibBread);
if(bread.getDrawable() != getDrawable(R.drawable.question_mark))
{
//set image back to question mark
bread.setImageDrawable(getDrawable(R.drawable.question_mark));
itemUsed[0] = true;
Item.putExtra(tools, itemUsed);
}
}

Multiple Image Buttons

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.

change application background through a button

i want to set a different background color to all application activities through a click on a button, but at now i ve done it only for one activity and i can't do it for all activities. this is the rude code:
optionlayout = (RelativeLayout) findViewById(R.id.optionlayout);
backgroundbutton = (Button) findViewById(R.id.backgroundbutton);
int counter = 0;
backgroundbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (counter ==0) {
optionlayout.setBackgroundColor(0xFF5774B3);
counter++;
}else if (counter == 1){
optionlayout.setBackgroundColor(0xFF0CC258);
counter++;
} else if (counter==2){
optionlayout.setBackgroundColor(0xCC000000);
counter++;
}else if (counter== 3){
optionlayout.setBackgroundColor(0xFFFFFFFF);
counter = 0;
}
}
});
i want to change the
optionlayout.setBackgroundColor(0xFF0CC258);
with an action that changes the background in all activities permanently. thanks
You can use shared preferences and store your counter there, and in onCreate of activities first look in your shared preferences to determine what color to set your background.
You can't. There's no way to change a property of a whole application through an activity. You can create a custom class that extends Activity and implements OnClickListener.
Something like that:
class MyCustomActivity extends Activity implements View.OnClickListener{
#Override
public void onClick(View v) {
//your code inside onClick()
}
}
And then you can extend your activities to this one, for example:
class MainActivity extends MyCustomActivity { ... }
You could try to use the extras method.
Pass the changes on from activity to activity maybe it helps.
Its a longer process to set up your page each time but i don't see any other way. I'm still open to suggestions
// you can have multiple extras.
String bgColor = "yourColor";
Intent newIntent = new Intent(thisActivity.this, theNextActivity.class);
// Start new activity
dispatchIntent.putExtra("bgColor", bgColor);
// Send color to new activity
startActivity(newIntent);
finish();
// Get the background color in the new page
Bundle extras = getIntent().getExtras();
String bgColor = extras.getString("bgColor");
// The trick is to get the string into your background color format such as
0xFF5774B3

i wanted to start a new activity after pressing the three buttons ...please let me know how can i achieve this?

Hi i am creating a simple educational game in which user has to press three images in order to proceed to the next level and in next level user has to press 5 images.
i have gone through the on click but i using onIntent intent = new Intent(this.getApplicationContext(), Activity.class);
this.startActivity(intent, 0); i am only able to start new activity on single button pressed but i wanted to start new activity when user has finished pressing three image Buttons.
Thanks in advanced.
you can use global int variable and increase it every button click and if its more than your button number open new activity
public int btn = 0;
MyButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if(btn >= 2) {
//open your activity
}
else{
btn++;
}
}
});
Based on my understanding of your problem,I have provided a possible simple implementation.
Use a simple data structure like queue or stack. When a image is tapped, add information about the image to the data structure. After adding the information to data structure see if number of items in data structure is equal to 3 ? if yes check data structure has info about the required three images and not just info about the same image (happens if user taps on same image more than once). If condition is met then call startActivity(). Generalize this so that you can reuse the logic in different activities, irrespective of the number of images.
Hi i finally achieved my desired activity. now with this after checking two check box app will start new activity.
`private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay;
OnClickListener checkBoxListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
checkBoxListener =new OnClickListener() {
public void onClick(View v) {
if(chkIos.isChecked()&&chkAndroid.isChecked()) {
Intent i1=new Intent(getApplicationContext(), Main1.class);
startActivity(i1);
if(chkAndroid.isChecked()) {
Intent i2=new Intent(getApplicationContext(), Main1.class);
startActivity(i1);
}
}
}
};
chkIos.setOnClickListener(checkBoxListener);
chkAndroid.setOnClickListener(checkBoxListener);
}
}
`
thanks for all your answers.

Android programming help

I'm working on an app where i have textview's in one layout and a button that sends you to a second layout with Edittext's. Every edittext is for an textview. How can i replace text in a textview with the text in edittext with a button in the second layout?
you mean like this ??
in the method onCreate() :
btn.setOnClickListener(this);
txtView = (TextView)findViewById(R.id.mytxtView);
editTxt = (EditText) findViewById(R.id.myeditText);
and then , ovverride the onClick method like this :
#Override
public void onClick(View v ) {
txtView.setText(editText.getText());
}
textview textview = (textview)findViewById(R.layout.nameoftextview);
edittext edittext = (edittext)findViewById(R.layout.nameofedittext);
textview.settext(edittext.text());
First of all, you will have to pass the edittext value to the first activity through intent.
Eg:
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("edittext_value", edittext.getText().toString());
startActivity(i);
Then inside your first activity, you will have to fetch this data as:
String value;
Bundle extras = this.getIntent().getExtras();
if (extras != null) {
value = extras.getString("edittext_value");
textview.setText(value);
}
Hope this may help you.
From what I understand is that you want your second activity (let's call it Activity2) to pass text back to the first one (Activity1). To do that, you have to (some code comes from :
Change the way you open Activity2 to
Intent EditIntent = new Intent(this, Activity2.class);
//Other stuff you may want to do with intent
startActivityForResult(EditIntent , 0);
Add override to you Activity1
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
if (data.hasExtra("myText")) {
//get your data with data.getExtras().getString("myText")
}
}
}
Change what button on your Activity2 does
{
Intent returnData= new Intent();
returnData.putExtra("myText", /*Text from your EditText*/);
if (getParent() == null) { //This part was taken from StackOverflow by Ilya Taranov
setResult(Activity.RESULT_OK, returnData);
} else {
getParent().setResult(Activity.RESULT_OK, returnData);
}
finish();
}
This should return text from EditText from Activity2 to Activity1. Code was not tested
create a variable for the textview to access it like
Textview txt = (Textview) finviewByid........;
implement the following code on button click listener
txt.setText(edittext.getText().toString());

Categories

Resources