Creatig a pop up menu when an image clicked Android - android

I'm trying to show a menu when I clicked on an image.
This is my full_screen_image.xml.I want to show when I clicked on #+id/photo_menu.
<ImageView
android:contentDescription="verter"
android:id="#+id/photo_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_more_vert_black_30dp"
android:tint="#android:color/white"
android:layout_marginTop="6dp"
android:layout_marginEnd="5dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
This is menu_photo.xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/savePhoto"
android:title="Kaydet" />
<item android:id="#+id/sharePhoto"
android:title="Paylaş" />
</menu>
This is fullScreenImageActivity.java:
package com.example.ahmetbesli.circles;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class FullScreenImageActivity extends AppCompatActivity {
private ImageView arrowBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen_image);
arrowBack = (ImageView) findViewById(R.id.arrow_back);
arrowBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
ImageView fullScreenImageView = (ImageView) findViewById(R.id.fullScreenImageView);
Intent callingActivityIntent = getIntent();
if (callingActivityIntent != null) {
Uri imageUri = callingActivityIntent.getData();
if (imageUri != null && fullScreenImageView != null) {
Glide.with(this)
.load(imageUri)
.into(fullScreenImageView);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_photo,menu);
return true;
}
}
I couldn't understand how to connect that image and the menu. Thanks for your helps...

Set a click listener to the image and in onclick method for that image view create any of the below 4 entities based on your requirement.
You have many options in your use case two of them would be.
1.Toast(only shows message no click events)
2.SnackBar (dismisses itself after some time, prefer this if you want user to respond quickly with some click action)
3.Using AlertDialog
4.Dialog Fragment
and in the both of the above methods(3 & 4) you can reference your menu items for click events or describe your own layout for dialog box and define buttons in those layouts for click events.
creation steps documentation: Android Dialogs
Dialog offers more functionalities and stays there till user closes it unlike toast which dismisses after some time if no action is taken.

Related

Adding more Buttons when a Button in clicked

Please look at the following algorithm and tell me if I can achieve it:
create a main_activity
inside the main_activity, create a simple Button
the Button is labeled as "Add Button"
once the users clicks the Button, an additional Button is created and placed in the Activity.
In other words:
once the user clicks on the add Button, it should create another Button and
place it under the "Add Button" Button.
I apologize in advance as this may be confusing, so please feel free to comment and ask for clarification.
I originally thought about creating a separate method, in which I would create a ButtonView, but I am not sure how I can physically create a Button.
Would I need to apply code to .xml file also?
I am really confused.
Here is my code:
MainActivity.java
package inc.fimp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addArm = (Button) findViewById(R.id.btnAddArm);
addArm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addButton();
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
int res_id = item.getItemId();
if(res_id==R.id.action_contact)
{
Toast.makeText(getApplicationContext(), "You selected Contacted us option", Toast.LENGTH_SHORT).show();
}
if(res_id==R.id.action_settings){
Toast.makeText(getApplicationContext(), "You selected Settings Option", Toast.LENGTH_SHORT).show();
}
return true;
}
public void addButton(){
// create an aditional button
}
}
xml file code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="inc.fimp.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_arm"
android:textStyle="italic"
android:id="#+id/btnAddArm"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/addarm"
android:singleLine="false" />
</RelativeLayout>
Start by adding an ID to the parent layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/buttonContainer"
Then, get that with findViewById. ViewGroup simply used because that is all you need to get the addView method.
public class MainActivity extends AppCompatActivity {
private ViewGroup rootView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootView = (ViewGroup) findViewById(R.id.buttonContainer);
Then, in the addButton,
Button button = new Button(MainActivity.this); // Need to provide the context, the Activity
// button.setText("Added!"); // for example
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.BELOW, R.id.btnAddArm);
// params.addRule ... (there's a bunch you can add)
rootView.addView(button, params);
Since you have a RelativeLayout, you can also programmatically put LayoutParams to do layout above/below, etc. other views.

button hovering not working in android

<?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="fill_parent" >
<!-- <TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="24dp"
android:text="#string/name" /> -->
<EditText
android:id="#+id/etname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:ems="10" android:inputType="text"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etname"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:text="Login" />
</RelativeLayout>
I have a button, I want when I "hover" over the button it shows a hello message with a toast.
I have a button in the layout. I tried to fetch it by findViewById in the FirstActivity. Then I tried using button.setOnHoverListener but it isn't working.
package com.example.datapass;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
final EditText name ;
Button loginButton ;
//final Context context = this;
/** Called when the activity is first created. */
//name= (EditText) findViewById(R.id.etname) ;
final TextView tv = (TextView) (findViewById(R.id.textView1) );
loginButton = (Button) findViewById(R.id.button1);
loginButton.setOnHoverListener(new View.OnHoverListener() {
#Override
public boolean onHover(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("hover", "Bring yor cursor over the button");
if(event.getAction()==MotionEvent.ACTION_HOVER_ENTER)
{
//tv.setText("hi");
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Can anyone explain what's wrong?
It would be worth changing your onHover method to be like below and break once it carries out an individual action:
#Override
public boolean onHover(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
I would also advise changing this line as this is not standard format/syntax:
final TextView tv = (TextView) (findViewById(R.id.textView1) );
To be this instead:
final TextView tv = (TextView) findViewById(R.id.textView1);
However I think your actual issue is more relating to the support on mobile devices of hovering as none of your code actually seems like it would break this code section. The majority of devices can only recognise hovers whilst using a stylus and not actually just with a finger. Only some of the higher end devices can actually recognise a hover action with just a finger. This might be something to note and may be advisable to use a swipe or a click instead of a hover to display the Toast.

How to make two diffrent Activities to use the same layout Android

Below code works fine, but it opens a new layout and shows the toast message. but i wanted it to show toast & popup menus on the current layout. I'm working on popup menu project, i want to show popup menu on same layout. Thanks in Advance.
MainMactivity.java
package com.example.twoact;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openpopactivity = new Intent(MainActivity.this,
MainActivity2.class);
startActivity(openpopactivity);
}
});
}
}
MainActivity2.java
package com.example.twoact;
import android.widget.Toast;
import android.os.Bundle;
import android.os.Handler;
public class MainActivity2 extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Handler().postDelayed(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "ya am in",
Toast.LENGTH_SHORT).show();
}
}, 1000);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.twoact.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="142dp"
android:text="Button" />
</RelativeLayout>
You can use Popup menu and Popup window. in this you can use your custom layout so you can get your view as window in same activity.
Popup window will be good option because it is since API LEVEL 1
please refer this docs and example 1,example 2
And Popup Menu is since API LEVEL 11. so you have to manage for lower version
docs for Popup Menu and example
popup window

Unfortunately (project name) has stopped working on button click

An area which will present a predefined question
A button which will cause the answer to the question to be shown while leaving the question on the screen
An area which will present the answer
A button which will cause a transition to a screen that is formatted the same as this one, with the next question shown on it
A button which will cause the app to end (a transition to (3))
Only trying to get the transition from screen 1 to screen 2 to work with a different question/answer pair in its place with a button click. If there is anyway to do this other than switching the screens and activities which is the error, please let me know.
package com.example.androidassignment2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class AndroidAssignment2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_assignment2_1);
Button next = (Button) findViewById(R.id.QButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_assignment2_1, menu);
return true;
}
}
layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="#+id/Questions"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:text="#string/Q2" />
<Button android:id="#+id/QButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_question" />
<Button android:id="#+id/AButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
<TextView android:id="#+id/Answers"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:hint="#string/A2" />
<Button android:id="#+id/QuitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_quit" />
</LinearLayout>
activity 1 file (incase needed)
package com.example.androidassignment2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.QButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class);
startActivityForResult(myIntent, 0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class);
startActivityForResult(myIntent, 0);
You are starting a new activity, the most likely cause is that this is has not been declared in the Manifest.
In your AndroidManifest.xml`, you will need to add something like this:
<activity
android:name="your.package.name.AndroidAssignment2_1" />
This will reside inside of the <application> tag.
You are using startActivityForResult() but you don't override the onActivityForResult() method in your first Activity. You could change that to just use startActivity() and remove setResult() from your second Activity.
Button next = (Button) findViewById(R.id.QButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class);
startActivity(myIntent);
}
});
If this isn't your problem then please post logcat (always when you have a crash) and make sure the Activity is decalred.
As far as your other inquiry
If there is anyway to do this other than switching the screens and activities which is the error, please let me know.
If you are simply changing the text then you can stay in the same Activity and change the text of your TextViews. You could store questions and answers in something like Arrays or in a DB if you wish and keep a counter to where you are and change the text with each click.
Another option, which may go along with the first if you want, is to use a ViewPager

How to implement click the certain area to do to action?

This is an example of facebook sliding menu.
When sliding, there is 20% space user can see which is similar with facebook. Facebook implements it with clicking anywhere of this 20% and the menu is slide back.
How to implement this?
One way of doing that is as following with OnTouchListener on your activity. You can detect exactly where is touched on the screen.
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.Toast;
public class AndroidTestActivity extends Activity implements OnTouchListener {
LinearLayout main;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
main = (LinearLayout) findViewById(R.id.main_layout);
main.setOnTouchListener(this); // you need to set the touch listener for your view. And every element around the detection area.
}
public boolean onTouch(View v, MotionEvent e) {
if(e.getX() <= main.getWidth() / 5) {
Toast.makeText(this, "In the %20..", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
You also need to id your main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

Categories

Resources