I have a fragment defined in my project plus an activity with a button. What I want is to launch the fragment with that button onclick listener, enter a value in the edit text defined in the fragment's xml layout file and press another button2 present in the fragment. Now what I want is that the button2 return's the text value and open's the activity again. I can then handle what happens to that value.
Java
package com.dreamgoogle.gihf;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Kahinsebhii extends Fragment {
Button ok;
EditText number;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.kahin, container, false);
ok = (Button) view.findViewById(R.id.button1);
number = (EditText) view.findViewById(R.id.editText1);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
number.getText();
}
});
return view;
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/kahinbhiedit"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok" />
</FrameLayout>
Java file to launch the fragment is some other java file.
It sound like what you need is a Dialog. Have a look at the development site: http://developer.android.com/guide/topics/ui/dialogs.html
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
Related
I am unable to find the way to create a popup like info window in map . I have to use it to show the three button inside
Yes
later
No
these three options are clickable.
The pop up will show above three button in vertical manner in recycler view . I have created recycler view and pop up view but how to show it like a Tooltip with clickabe
you can use PopupWindow, its starting with API +23
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFBBFFBB" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Hello My Window" android:textSize="20sp" /> <Button android:id="#+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Button" android:textSize="20sp" /> </LinearLayout>
class
package com.example.hellopopupwindow;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends Activity {
private Context mContext = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showPopupWindow(view);
}
});
}
private void showPopupWindow(View view) {
// A custom layout, as the display content
View contentView = LayoutInflater.from(mContext).inflate(
R.layout.pop_window, null);
// Set the button click event
Button button = (Button) contentView.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "button is pressed",
Toast.LENGTH_SHORT).show();
}
});
final PopupWindow popupWindow = new PopupWindow(contentView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("mengdd", "onTouch : ");
return false;
// It returns true if the words, the touch event will be blocked
// PopupWindow onTouchEvent interception is not called, so click on the external area cannot be dismiss
}
});
// If you do not set the PopupWindow background, both the external region click or Back keys are not dismiss box
// I think there is a bug API
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.drawable.selectmenu_bg_downward));
// After setting the parameter to show
popupWindow.showAsDropDown(view);
}
}
I need to use a .java file to perform actions for a .xml layout. I have used this layout for a dialog box. I have linked the .java file to that layout but it doesn't seem to work at all.
This is the MainActivity
package com.example.ayush.projectfive;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
AlertDialog.Builder alrt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
alrt = new AlertDialog.Builder(MainActivity.this);
alrt.setIcon(R.mipmap.ic_launcher);
alrt.setTitle("Login");
alrt.setCancelable(false);
alrt.setView(R.layout.mylayout);
alrt.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alrt.show();
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
}
This is the activity_main
<?xml version="1.0" encoding="utf-8"?>
<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"
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="com.example.ayush.projectfive.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
This is the dialog box layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="LOGIN"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_marginTop="20dp"
android:id="#+id/editText" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:layout_marginTop="15dp"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="20dp"
android:id="#+id/button3" />
This is the .java file I'd like to link with.
package com.example.ayush.projectfive;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Second extends AppCompatActivity{
EditText et, et2;
Button btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
et = (EditText) findViewById(R.id.editText);
et2 = (EditText) findViewById(R.id.editText2);
btn2 = (Button) findViewById(R.id.button3);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s1 = et.getText().toString();
String s2 = et.getText().toString();
if(s1.equals(s2)){
Toast.makeText(Second.this, "Login Successful", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Second.this,Third.class);
startActivity(i);
}
else{
}
}
});
}
}
This is the .java file I'd like to link with [...]
public class Second extends AppCompatActivity{
Okay... That's what this code does
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
If you are trying to use the views that are contained within the dialog box and respond to the buttons there to login, then you need to remove the button from the activity, add alrt.show() in the onCreate instead of onClick and move the Activity start code into the dialog's onClick handlers.
It's also worth mentioning that the object is a builder, so you can do this
View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.mylayout, null);
final EditText editUsername = dialogView.findViewById(R.id.editText);
// TODO: Get other views from mylayout.xml
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.mipmap.ic_launcher)
.setTitle("Login")
.setCancelable(false)
.setView(dialogView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String username = editUsername.getText().toString();
// TODO: Get more variables
// TODO: Verify credentials
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
})
.setNegativeButton("CANCEL", null)
.show();
And, as stated earlier, remove btn.setOnClickListener
just add alrt.show(); in your MainActivity.java e.g. here:
public class MainActivity extends AppCompatActivity {
Button btn;
AlertDialog.Builder alrt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
alrt = new AlertDialog.Builder(MainActivity.this);
alrt.setIcon(R.mipmap.ic_launcher);
alrt.setTitle("Login");
alrt.setCancelable(false);
alrt.setView(R.layout.mylayout);
alrt.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.show();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
}
i'm experiencing this problem on DialogFragment, with an input edit text box, when the dialog popup and you touch the text box for input the keyboard shows up and immediately hide, pushing up and down the pop up dialog. hence i cannt put in anything.
here is the dialogfragment code
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import com.test.abc.testingapppay.R;
public class AlertCaptureItemQuantity extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.activity_quantity_prompt,null);
final EditText quantity = (EditText) view.findViewById(R.id.text_quantity);
quantity.requestFocus();
return new AlertDialog.Builder(getActivity())
.setView(view)
.setTitle("Quantity")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
// val_quantity =Double.parseDouble(quantity.getText().toString());
Intent i = new Intent().putExtra("quantityEntered", quantity.getText().toString());
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);
if (quantity.getText().length() > 0) {
dismiss();
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
dismiss();
}
}).create();
}
}
and the main call from main actvity
AlertCaptureItemQuantity AlertDialogItems= new AlertCaptureItemQuantity();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();//getFragmentManager();
AlertDialogItems.show(fragmentManager.beginTransaction(),"items");
activity xml file for the dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/text_quantity"
/>
</LinearLayout>
image id dialog
i've been trying to make an app with a buttonbar with add/exit buttons at the bottom, and a EditText at the top.
when i hit the add button i want the app to add checkboxes with the text from the EditText.
but when i try to launch the app it force closes.
the logcat show absolutely nothing from my app,
the console says:
[2014-06-24 18:41:06 - CheckList] Starting activity com.pzayx.checklist.CheckList on device 7a0ec297
[2014-06-24 18:41:06 - CheckList] ActivityManager: WARNING: linker: app_process has text relocations. This is wasting memory and is a security risk. Please fix.
[2014-06-24 18:41:06 - CheckList] ActivityManager: WARNING: linker: app_process has text relocations. This is wasting memory and is a security risk. Please fix.
[2014-06-24 18:41:07 - CheckList] ActivityManager: Starting: Intent {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.pzayx.checklist/.CheckList }
[2014-06-24 18:41:07 - CheckList] Attempting to connect debugger to 'com.pzayx.checklist' on port 8960
i cant seem to find much information about "app_process has text relocations. This is wasting memory and is a security risk. Please fix" and suspect the demonspawn lies there.
here is the source of the CheckList.java
package com.pzayx.checklist;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
public class CheckList extends Activity {
LinearLayout lil = (LinearLayout) findViewById(R.id.LinearMain);
EditText mTextInput;
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_check_list);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
setupWidgets();
}
private void setupWidgets() {
mTextInput = (EditText) findViewById(R.id.editText1);
Button addBtn = (Button) findViewById(R.id.button_add);
addBtn.setOnClickListener(addListener);
Button exitBtn = (Button) findViewById(R.id.button_exit);
exitBtn.setOnClickListener(exitListener);
}
private OnClickListener addListener = new OnClickListener() {
public void onClick(View v) {
doSaveButtonPress();
}
};
private OnClickListener exitListener = new OnClickListener() {
public void onClick(View v) {
doExitButtonPress();
}
};
public void doSaveButtonPress() {
String text = mTextInput.getText().toString();
CheckBox cb = new CheckBox(getApplicationContext());
cb.setId(i);
cb.setText(text);
lil.addView(cb);
mTextInput.setText("");
i++;
}
public void doExitButtonPress() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Exit Application?");
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
And here is my layout source
<LinearLayout
android:layout_below="#+id/LinearMain"
android:id="#+id/LinearLayoutButtons"
style="?android:buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:divider="?android:attr/dividerVertical"
android:orientation="horizontal"
android:showDividers="middle" >
<!-- <requestFocus /> -->
<Button
android:id="#+id/button_add"
style="?android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/add"
android:layout_gravity="bottom" />
<View
android:id="#+id/ViewColorPickerHelper"
android:layout_width="1dip"
android:layout_height="40dip"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="4dip"
android:layout_marginTop="4dip"
android:background="?android:attr/dividerVertical"
android:layout_gravity="bottom"/>
<Button
android:id="#+id/button_exit"
style="?android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/exit"
android:layout_gravity="bottom" />
<!-- Checkbox Area -->
</LinearLayout>
<LinearLayout
android:id="#+id/LinearMain"
android:layout_width="match_parent"
android:layout_height="420dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/editText"
android:ems="10" />
</LinearLayout>
</RelativeLayout>
would appreciate it if anyone could help me make some sense out of it!
Thank you!
I am looking for a way to close a custom dialog with a button that is inside the xml used in the dialog, alternatively closing it by pressing anywhere on the dialog. What I have is this; a layout with a Image Button that brings up the custom dialog with the content. I have setCanceledOnTouchOutside(true); and that works, but I need the dialog to fill up most of the screen and it can be hard for the user to click in the small space that is available. So how do I do this?
My java code:
import android.app.Activity;
import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class Rose extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.rose);
ImageButton b = (ImageButton) findViewById(R.id.imageButton1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Dialog d1 = new Dialog(Rose.this);
d1.setContentView(R.layout.tariquet);
d1.setCanceledOnTouchOutside(true);
d1.show();
}
});
}
}
And my XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:isScrollContainer="true"
android:minHeight="1100dp"
android:minWidth="650dp">
<ImageView
android:src="#drawable/rose_tariquet"
android:id="#+id/imageView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"></ImageView>
<Button android:text="X"
android:layout_height="wrap_content"
android:id="#+id/button1"
android:layout_width="55dp"
android:layout_gravity="right"></Button>
</FrameLayout>
public class CustomizeDialog extends Dialog implements OnClickListener {
Button close;
TextView tv;
public CustomizeDialog(Context context,String Stringcontent) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_diolog_main);
tv=(TextView) findViewById(R.id.content);
tv.setText(Stringcontent);
close = (Button) findViewById(R.id.close);
close.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == close)
dismiss();
}
}
called:
CustomizeDialog customizeDialog = new CustomizeDialog(CustomDialog.this,"clickme");
customizeDialog.show();