drawableRight of EditText - android

Below is my layout:
<EditText
android:id="#+id/account_et"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawableRight="#drawable/icon_backall"
android:ems="10"
android:hint="#string/str_md_email"
android:inputType="textEmailAddress"
android:padding="10dp" >
</EditText>
I want to show the drawableRight when EditText be focused.
And hide while without focus.
Another one is that I want to set OnClickListener of drawableRight.
How can I do?

i would suggest you to add imageView separated from the EditText and align him to be on top of him with align top and align right and that you have full control so you can invisible him and setOnClickListner
<ImageView android:id="#+id/account_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/account_et"
android:layout_alignTop="#+id/account_et"
android:background="#drawable/icon_backall">
</ImageView>

Use View.OnFocusChangeListener. Putting drawable to edittext when you catch the focus, then replacing drawable with null would solve the problem.

I hope my sample will help you
activity_main.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" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:inputType="textEmailAddress" >
</EditText>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:layout_marginTop="43dp"
android:src="#drawable/ic_launcher"
android:visibility="invisible" >
</ImageView>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/editText1"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
this is the class file in which drawable options are performed
MainActivity.java
package com.example.doubtedittext;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
private EditText etext;
private ImageView imageView1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etext = (EditText) findViewById(R.id.editText1);
imageView1 = (ImageView) findViewById(R.id.imageView1);
etext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
imageView1.setVisibility(View.VISIBLE);
} else {
imageView1.setVisibility(View.INVISIBLE);
}
}
});
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
the operation you required successfully performed above.....

Use like belwo
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus)
{}
else {}
}
});

Related

Custom popup dialog with input field [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How do I create popup input field for Android?
I need Xml and Java Code.
try to use this custom popup dialog code
main.xml
<?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:orientation="vertical" >
<Button
android:id="#+id/buttonPrompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Prompt Dialog" />
<EditText
android:id="#+id/editTextResult"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
Custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type Your Message : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editTextDialogUserInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
</LinearLayout>
main.java
package cm.kikani.kalpesh;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
final Context context = this;
private Button button;
private EditText result;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// components from main.xml
button = (Button) findViewById(R.id.buttonPrompt);
result = (EditText) findViewById(R.id.editTextResult);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.custom, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// get user input and set it to result
// edit text
result.setText(userInput.getText());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
You can create a custom dialog class for your case.
your_custom_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="120dp"
android:background="#android:color/black"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Do you realy want to exit ?"
android:textColor="#android:color/white"
android:textSize="16sp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:color/blue"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_yes"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="#android:color/white"
android:clickable="true"
android:text="Yes"
android:textColor="#android:color/white"
android:textStyle="bold" />
<Button
android:id="#+id/btn_no"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:background="#android:color/white"
android:clickable="true"
android:text="No"
android:textColor="#android:color/blue"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
and you CustomDialog class must extend Dialog
public class CustomDialogClass extends Dialog {
public Activity activity;
public Dialog dialog;
public Button yes, no;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.activity = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
activity.finish();
}
});
no.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
dismiss();
}
});
}
}
and you can call it like this
CustomDialogClass customDialog =new CustomDialogClass(activity);
customDialog .show();

i need to make a button that opens a custom dialogbox

I'm creating an android app for my school project.
I created the interface normaly.
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/mappic"
android:layout_above="#+id/zoomControls"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ZoomControls
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/zoomControls"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/imageView"
android:layout_alignEnd="#+id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Obtenir le PCC"
android:id="#+id/button"
android:layout_alignBottom="#+id/zoomControls"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
now I want to make it when I click on the button : Obtenir le PCC this dialog box opens up :
<?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"
android:backgroundTintMode="multiply"
android:backgroundTint="#ff7518ff">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/Suivantbtn"
android:layout_alignLeft="#+id/Suivantbtn"
android:layout_alignStart="#+id/Suivantbtn" />
<TextView
android:id="#+id/textdp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Taper le poit de départ:"
android:textColor="#ff111124"
android:layout_alignBottom="#+id/Suivantbtn"
android:theme="#style/AppTheme"
android:textSize="30dp"
android:layout_below="#+id/image"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="28dp"
android:layout_marginLeft="28dp" />/>
<Button
android:id="#+id/Suivantbtn"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Suivant "
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_above="#+id/Suivantbtn"
android:layout_marginBottom="56dp"
android:layout_alignLeft="#+id/textdp"
android:layout_alignStart="#+id/textdp"
android:layout_alignRight="#+id/textdp"
android:layout_alignEnd="#+id/textdp"
android:textAlignment="center"
android:backgroundTint="#ff9eadff" />
</RelativeLayout>
and when I click on Suivant ,, the dialog box goes and another dialogbox appears :
<?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"
android:backgroundTintMode="multiply"
android:backgroundTint="#ff7518ff">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/Suivantbtn2"
android:layout_alignLeft="#+id/Suivantbtn2"
android:layout_alignStart="#+id/Suivantbtn2" />
<TextView
android:id="#+id/textar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Taper le poit d'arrivée:"
android:textColor="#ff111124"
android:layout_alignBottom="#+id/Suivantbtn2"
android:theme="#style/AppTheme"
android:textSize="30dp"
android:layout_below="#+id/image"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="28dp"
android:layout_marginLeft="28dp" />/>
<Button
android:id="#+id/Suivantbtn2"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Suivant "
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_above="#+id/Suivantbtn2"
android:layout_marginBottom="56dp"
android:layout_alignLeft="#+id/textar"
android:layout_alignStart="#+id/textar"
android:layout_alignRight="#+id/textar"
android:layout_alignEnd="#+id/textar"
android:textAlignment="center"
android:backgroundTint="#ff9eadff" />
</RelativeLayout>
the java code :
package artofdev.org.admaps;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Context context = getApplicationContext();
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);
Button button2 = (Button) dialog.findViewById(R.id.Suivantbtn);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog2 = new Dialog(context);
dialog2.setContentView(R.layout.dialog2);
Button button3 = (Button)
dialog2.findViewById(R.id.Suivantbtn2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog2.dismiss();
}
});
dialog.dismiss();
dialog2.show();
}
});
dialog.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.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And when this appears and I type in and click on next. This dialog box dismisses and another msg box I'll create later shows a msg.
how to do it ?
Here is a fully working code for you:
Context context = getAplicationContext();
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.your_dialog_layout);
Button button2 = (Button) dialog.findViewById(R.id.Suivantbtn);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog2 = new Dialog(context);
dialog2.setContentView(R.layout.your_second_dialog_layout);
Button button3 = (Button) dialog2.findViewById(R.id.Suivantbtn2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog2.dismiss();
}
});
dialog.dismiss();
dialog2.show();
}
});
dialog.show();
}
});
Hope my answer helped!
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
//On click function
public void onClick(View view) {
//do something - for example open the dialog box you want
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_alert_dialogue);
dialog.show();
}
});
You should add onClickListener to your buttons. For example (this code is for your Activity class):
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
//On click function
public void onClick(View view) {
//do something - for example open the dialog box you want
}
});

null pointer exception in splash

I have a main activity which should load a small splash xml file for 2 or 3 seconds when the app is opened. I tried this snippet of code in the oncreate before adding it to my major project. Keep in mind, both apps worked seperately but for some reason, i get a null pointer exception when running the app..... help?
MainActivity:
package com.Depauw.dpuhelpdesk;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button knowledgeBase, submitRequest, helpme, faq, technician, call;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
//display the logo during 5 secondes,
new CountDownTimer(2000,1000){
public void onTick(long millisUntilFinished){}
public void onFinish(){
//set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
}
}.start();
Initialize();
}
private void Initialize(){
knowledgeBase = (Button) findViewById(R.id.knowledgebase1);
submitRequest = (Button) findViewById(R.id.submitrequest1);
helpme = (Button) findViewById(R.id.helpButton);
faq = (Button) findViewById(R.id.faqButton);
technician = (Button) findViewById(R.id.submitrequest2);
call = (Button) findViewById(R.id.callButton);
knowledgeBase.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent knowledgeIntent = new Intent(MainActivity.this, knowledgebase1_activity.class);
startActivity(knowledgeIntent);
}
});
submitRequest.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent requestIntent = new Intent(MainActivity.this, submitRequest_activity.class);
startActivity(requestIntent);
}
});
helpme.setOnClickListener(new OnClickListener(){ //dialog box
public void onClick(View arg0){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Help");
builder.setMessage("This app allows you to access the IT KnowledgeBase from DePauw's website." + " " +
"If you experience any issues using our app, please send us an email to helpdesk#depauw.edu or call 765-658-4294");
builder.setCancelable(true);
builder.setIcon(R.drawable.ic_launcher);
builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
builder.setOnCancelListener(null);
}
});
builder.create().show(); // create and show the alert dialog
}
});
faq.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent requestIntent = new Intent(MainActivity.this, activity_main_faq.class);
startActivity(requestIntent);
}
});
technician.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent requestIntent = new Intent(MainActivity.this, submit_technician_request.class);
startActivity(requestIntent);
}
});
call.setOnClickListener(new OnClickListener(){ //dialog box
public void onClick(View arg0){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Complete the call?");
builder.setMessage("Click yes to connect your call. Otherwise, click no.");
builder.setCancelable(true);
builder.setIcon(R.drawable.ic_launcher);
//saying yes completes the call. This way, we don't get accidently calls as often
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:17656584294"));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("Calling a Phone Number", "Call failed", activityException);
}
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = builder.create();
// show it
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;
}
}
splash.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#raw/splash2" />
main_activity_main.xml:
<FrameLayout 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=".basic_activity1"
tools:ignore="MergeRootFrame"
android:background="#000000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/header"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#EAC117"
android:textSize="35sp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#raw/firstintentlogo"
android:layout_weight="1" android:contentDescription="#string/headLogoName"/>
</LinearLayout>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/knowledgebase1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/knowledgebase1"
android:textColor="#EAC117" />
<Button
android:id="#+id/submitrequest1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/submitrequest1"
android:textColor="#EAC117" />
<Button
android:id="#+id/submitrequest2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/submitTechnician"
android:textColor="#EAC117" />
<Button
android:id="#+id/helpButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/helpButton"
android:textColor="#EAC117" />
<Button
android:id="#+id/faqButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/faqButton"
android:textColor="#EAC117" />
<Button
android:id="#+id/callButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/callButton"
android:textColor="#EAC117" />
</LinearLayout>
</ScrollView>
</FrameLayout>
Initialize() is getting called immediately.
This is happening before MainActivity.this.setContentView(R.layout.activity_main) is called.

android number picker default design changes in jelly bean and ice-cream sandwitch

i've created an android application which displays a number picker, it all works fine...but the problem is with the design....when i run the application in gingerbread the number picker looks fine good....but when i run the same stuff in ice-cream sandwich and jelly bean the number picker design is been altered just like as shown below.
can anyone please tell me how to retain the default number-picker design that is in gingerbread in jelly bean
when runs in ice-cream sandwich and jelly bean
when runs in ginger-bread
i'm using a custom dialog box within which the number picker is placed, the code is as given below
import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.NumberPicker;
public class QuantityChangeDialog extends Dialog implements android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button save, cancel;
NumberPicker np;
public QuantityChangeDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.selecteditem_dialog);
save = (Button) findViewById(R.id.btn_save);
cancel = (Button) findViewById(R.id.btn_cancel);
save.setOnClickListener(this);
cancel.setOnClickListener(this);
np = (NumberPicker) findViewById(R.id.qntypicker);
np.setMaxValue(120);
np.setMinValue(1);
np.setValue(3);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_save:
c.finish();
break;
case R.id.btn_cancel:
dismiss();
break;
default:
break;
}
dismiss();
}
}
Quoting from docs
If the current theme is derived from Theme the widget presents the current value as an editable input field with an increment button above and a decrement button below. Long pressing the buttons allows for a quick change of the current value. Tapping on the input field allows to type in a desired value.
You need to set your theme that is derieved from Theme like fro example Theme.NoTitleBar.Fullscreen
activity_main.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: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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Open" />
</RelativeLayout>
dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:theme = "#style/cust_dialog"
android:layout_height="fill_parent" >
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberPicker1"
android:layout_marginLeft="20dp"
android:layout_marginTop="98dp"
android:layout_toRightOf="#+id/numberPicker1"
android:text="Cancel" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="#+id/numberPicker1"
android:text="Set" />
</RelativeLayout>
Then to display custom dialog
public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener
{
private TextView tv;
static Dialog d ;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
tv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
tv.setTextColor(Color.RED);
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
// set to normal color
tv.setTextColor(0);
}
return true;
}
});
Button b = (Button) findViewById(R.id.button11);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
show();
}
});
}
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
Log.i("value is",""+newVal);
}
public void show()
{
final Dialog d=new Dialog(this,R.style.cust_dialog);
d.setTitle("NumberPicker");
d.setContentView(R.layout.dialog);
Button b1 = (Button) d.findViewById(R.id.button1);
Button b2 = (Button) d.findViewById(R.id.button2);
final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1);
np.setMaxValue(100);
np.setMinValue(0);
np.setWrapSelectorWheel(false);
np.setOnValueChangedListener(this);
b1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
tv.setText(String.valueOf(np.getValue()));
d.dismiss();
}
});
b2.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
d.dismiss();
}
});
d.show();
}
}
Styles.xml
</style>
<style name="cust_dialog" parent="#android:style/Theme.NoTitleBar.Fullscreen">
</style>
Snap Shot
You can just add this attribute into your NumberPicker
android:theme="#android:style/Theme.Dialog"
E.g.
<NumberPicker android:theme="#android:style/Theme.Dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This will limit the impact to just the number picker widget, not the entire activity page.

I am trying to get input using Alert Dialog box but getting a run time error in Android

Here is My Code
package com.dialog;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.dialog, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText name=(EditText)findViewById(R.id.name);
final EditText contact=(EditText)findViewById(R.id.contact);
final HashMap<String,String> map=new HashMap<String,String>();
Button button_add=(Button)findViewById(R.id.main_add);
final AlertDialog alert;
builder.setCancelable(true)
.setPositiveButton("Add",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
map.put(contact.getText().toString(), name.getText().toString());
//Toast.makeText(Main.this,map.get(contact.getText().toString()), Toast.LENGTH_LONG).show();
dialog.cancel();
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setView(textEntryView);
alert=builder.create();
button_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.show();
}
});
}
}
Her are my two xml files:
1)main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/main_add" android:text="Add"></Button>
</LinearLayout>
2)dialog.xml
<?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/name_label"
android:text="Name:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/name"
android:layout_below="#+id/name_label"
android:hint="Enter your name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/contact_label"
android:layout_below="#+id/name"
android:text="Contact:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/contact"
android:layout_below="#+id/contact_label"
android:hint="Enter the contact number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Help me!!!
Here's the problem:
final EditText name=(EditText)findViewById(R.id.name);
final EditText contact=(EditText)findViewById(R.id.contact);
You look them up from main layout, not from dialog. Should be:
final EditText name=(EditText)textEntryView.findViewById(R.id.name);
final EditText contact=(EditText)textEntryView.findViewById(R.id.contact);
AlertDialog dialog = new Builder(Main.this.getParent()).create();
dialog.setTitle("Delete");
dialog.setButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
dialog.setButton2("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Whatever you want to do
dialog.dismiss();
}
});
dialog.show();

Categories

Resources