i have create simple dialog that showing when i click on the button
every thing is working fine
but i want the onKeydown is not allowed to go back
this is my activity class
public class MainActivity extends Activity implements OnClickListener {
private Button showdialog;
private Dialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showdialog=(Button)findViewById(R.id.showdialog);
showdialog.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
dialog = new Dialog(MainActivity.this,
android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.dialog);
dialog.show();
}
}
and this is the Activity xml
<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:background="#000424"
tools:context=".MainActivity" >
<Button
android:id="#+id/showdialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:text="showdialog" />
</RelativeLayout>
and this is the dialog xml
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp"
android:layout_marginTop="200dp"
android:text="this is the dialog"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="100sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
Take a look at onKeyListener:
public class MainActivity extends Activity implements OnClickListener {
private Button showdialog;
private Dialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showdialog=(Button)findViewById(R.id.showdialog);
showdialog.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
dialog = new Dialog(MainActivity.this,
android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(R.layout.dialog);
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//DO NOTHING
}
return true;
}
});
dialog.show();
}
}
Related
I am learning android. I am facing problem while displaying toast message upon button click. I am basically trying to display the password field's text upon a button click.
Java file
public class MainActivityToast extends AppCompatActivity {
private EditText passwd;
private Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_toast);
}
public void addListenerOnButton(){
passwd = (EditText)findViewById(R.id.editTextPass);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivityToast.this,passwd.getText(),Toast.LENGTH_SHORT).show();
}
});
}
}
This is my XML file.Do I have to add onClick method ? While running the app no toast message appears
XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.acer.toastapplication.MainActivityToast"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<EditText
android:id="#+id/editTextPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editTextPass"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:text="Show Password"/>
</RelativeLayout>
In your onCreate() call addListenerOnButton();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_toast);
addListenerOnButton();
}
Now the toast will show up.
There is an EditText and a Button inside an Activity, After entering some text inside an EditText and clicking button, a method is called and passes content of EditText to a method like below:
public class MainActivity extends AppCompatActivity {
TextView txt_title;
Button btn_send;
EditText edt_notification;
public final static String NOTIFICATION_DATA="NOTIFICATION_DATA";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_title= (TextView) findViewById(R.id.txt_title);
edt_notification= (EditText) findViewById(R.id.edt_notification);
btn_send= (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SendNotification(Calendar.getInstance().getTimeInMillis(), edt_notification.getText().toString());
}
});
}
private void SendNotification(long timeInMillis, String s) {
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
}
}
<?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:id="#+id/activity_main"
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="ir.wikichera.badamchi.sendnotification.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_title"
android:text="Notification Text:"
android:textSize="18sp"
android:textStyle="normal|bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="#+id/txt_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:id="#+id/edt_notification"
android:hint="put your text here"
android:singleLine="false" />
<Button
android:text="Send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edt_notification"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="17dp"
android:id="#+id/btn_send" />
</RelativeLayout>
The problem is variable "s" is all the time giving me "Cannot Find Local Variable 's'". why? This is a simple issue but I can't understand. please explain?
May be you are using AndroidStudio that has instant run enabled. Try after turning it off. Because when it is enabled our code changes doesn't take place sometimes. So try after turning it off :
File → Settings → Build, Execution, Deployment → Instant Run and
uncheck Enable Instant Run.
My Working Code :
public class TestAcitivity extends Activity {
TextView txt_title;
Button btn_send;
EditText edt_notification;
public final static String NOTIFICATION_DATA="NOTIFICATION_DATA";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_acitivity);
txt_title= (TextView) findViewById(R.id.txt_title);
edt_notification= (EditText) findViewById(R.id.edt_notification);
btn_send= (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SendNotification(System.currentTimeMillis(), edt_notification.getText().toString());
}
});
}
private void SendNotification(long timeInMillis, String s) {
txt_title.setText(s);
Toast.makeText(TestAcitivity.this, s, Toast.LENGTH_SHORT).show();
}
}
this worked for me.
i have some problems with my layout, this is the capture of my layout inflater
the layout is to big and the button are in wrong place, i don't use a title but there is a black title background in there
i just want to make it smaller like piano tiles layout
can anyone help me to fix this?
this is my layout.xml data that will show inflater layout in menu.java
<?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#drawable/backgroundblankwhite"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="#+id/exitimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/textinflateexit"
android:singleLine="true" >
<requestFocus />
</TextView>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" >
<Button
android:id="#+id/buttonexityes"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#drawable/buttonquityes" />
<Button
android:id="#+id/buttonexitno"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#drawable/buttonquitno" />
</LinearLayout>
</LinearLayout>
and this is my menu.java that have a button to display my inflate layout
public class menu extends Activity {
private Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
Button exit = (Button) findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
final Dialog openDialog = new Dialog(context);
openDialog.setContentView(R.layout.inflatequitapp);
TextView dialogTextContent = (TextView)openDialog.findViewById(R.id.exitimage);
Button dialogExitButton = (Button)openDialog.findViewById(R.id.buttonexityes);
dialogExitButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// if this button is clicked, close
// current activity
menu.this.finish();
}
});
Button dialogCloseButton = (Button)openDialog.findViewById(R.id.buttonexitno);
dialogCloseButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openDialog.dismiss();
}
});
openDialog.show();
}
});
}
#Override
public void onBackPressed() {
// do nothing.
}
#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);
}
}
}
I have change a bit variable to check at my end..please do change variables,class to match at your end...
MainActivity.java
///---////
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button exit = (Button) findViewById(R.id.but);
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
final Dialog openDialog = new Dialog(MainActivity.this);
openDialog.setContentView(R.layout.main);
openDialog.setTitle("Confirm Exit");
TextView dialogTextContent = (TextView)openDialog.findViewById(R.id.exitimage);
Button dialogExitButton = (Button)openDialog.findViewById(R.id.buttonexityes);
dialogExitButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
finish();
}
});
Button dialogCloseButton = (Button)openDialog.findViewById(R.id.buttonexitno);
dialogCloseButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
openDialog.dismiss();
}
});
openDialog.show();
}
});
}
Dialog xml file..
<?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#ffffff"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="#+id/exitimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:singleLine="true"
android:text="Are You Sure!!" >
<requestFocus />
</TextView>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" >
<Button
android:id="#+id/buttonexityes"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="Yes" />
<Button
android:id="#+id/buttonexitno"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="No" />
</LinearLayout>
</LinearLayout>
output:
you have to used below code for dialog
private void forgotPasswordDialog() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
final View dialogView = layoutInflater.inflate(R.layout.dialog_forgot_password, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.PauseDialog);
final AlertDialog dialog = builder.create();
dialog.setView(dialogView);
dialog.show();
final EditText edtUserNameDialog = (EditText) dialogView.findViewById(R.id.edtUserNameDialog);
edtUserNameDialog.setText(edtUserName.getText());
final Button btnSubmit = (Button) dialogView.findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!FieldsValidator.validateUsername(edtUserNameDialog)) {
//forgotPasswordDialog();
} else if (!checkDMSID()) {
KeyboardUtils.hideKeyboard(edtUserNameDialog);
dialog.dismiss();
} else {
KeyboardUtils.hideKeyboard(edtUserNameDialog);
forgotPassword(edtUserNameDialog.getText().toString());
dialog.dismiss();
}
}
});
final Button btnCancel = (Button) dialogView.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
KeyboardUtils.hideKeyboard(edtUserNameDialog);
dialog.dismiss();
}
});
}
I'm trying to click on row in list view but my code only works when i pressed between two rows
you can see this :
http://www.screencast.com/t/2vei8yOQh
note in rep.xml i using table layout and inside it rowTable
i uses this code :
public class LastDaysActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last_days);
final Button newFourm = (Button) findViewById(R.id.button1);
LastDaysAdapter adapter = new LastDaysAdapter(this, getDates(),
getApplicationContext(), getBusID());
setListAdapter(adapter);
newFourm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(LastDaysActivity.this,
TabHostActivity.class);
startActivity(i);
}
});
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
TextView myView = (TextView) v.findViewById(R.id.textView2);
String text = myView.getText().toString();
Toast.makeText(this, text + " selected", Toast.LENGTH_LONG).show();
}
edit xml :
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="#+id/TableRow05"
android:layout_width="fill_parent"
android:layout_height="27dp"
android:layout_marginTop="4dp"
android:background="#drawable/trans01" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="رقم اللوحة" />
<TextView
android:id="#+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:text="التاريخ"
android:textSize="12sp" />
</TableRow>
</TableLayout>
Hi I am new to Android and Java and have run into a problem. My problem is that i am trying to add a PreferenceScreen to my linear layout so I am able to set an setOnSeekBarChangeListener() on the seekbar in res/layout/activity_main.xml. At the moment the PreferenceScreen and the LinearLayout get added to the view but they are on top of each other which is not what I want. Instead I want the PreferenceScreen to go in place of the ListView inside my layout file.
res/xml/settings.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Category"
android:key="category_preference">
<SwitchPreference
android:key="switch_preference"
android:title="Title"
android:summary="Summary"
android:defaultValue="true" />
<CheckBoxPreference
android:key="checkbox_preference"
android:title="Title"
android:summary="Summary"
android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>
res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_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=".MainActivity" >
<SeekBar
android:id="#+id/main_seek_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
setContentView(R.layout.activity_main);
SeekBar seekBar = (SeekBar) findViewById(R.id.main_seek_bar);
seekBar.setOnTouchListener(new SeekBar.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
});
}
public static class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
public static final String KEY_PREF_SYNC_CONN = "pref_syncConnectionType";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
onSharedPreferenceChanged(null, "");
}
#Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("switch_preference")) {
Toast.makeText(getActivity(), "Switch", Toast.LENGTH_LONG).show();
}
}
}
}
in your res/layout/activity_main.xml file change
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
to
<fragment android:id="#+id/fragment_settings"
android:layout_width="fill_parent"
android:name="yourpackagename.MainActivity$SettingsFragment"
android:layout_height="fill_parent" />
and then in your MainActivity.java file remove the following line
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();