Get value from custom dialog edittext in android - android-emulator

I my project i have a text view and button on my screen. On button click event i show custom dialog box (Dialog box content Edit text and a button). I put some data in dialog box edit text and click dialog box save button.
Now i want to show value of dialog edit text on screen text view on button click. How can i do this....
holder.list1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final TextView tv=(TextView)v.findViewById(R.id.textView3);
final Dialog dialog = new Dialog(activity);
dialog.setContentView(R.layout.customdailogfroaddmilk);
dialog.setTitle("Account");
// set the custom dialog components - text, image and button
final EditText text = (EditText) dialog.findViewById(R.id.editText1);
text.setText(tv.getText().toString());
Button save = (Button) dialog.findViewById(R.id.button1);
Button cancle = (Button) dialog.findViewById(R.id.button2);
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
tv.setText(text.getText().toString());
}
});
cancle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
on button(save) click event edittext(save) value not set in textview(tv)
?????.....

Create a layout in your layout folder and provide it as the View to a Dialog Object.
And for the example shown below, you need to have a TextView, a Button and EditText in that layout.
final Dialog dialog = new Dialog(context,
android.R.style.Theme_Translucent_NoTitleBar);
Window window = dialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.xmlfile);
Button ok = (Button) dialog.findViewById(R.id.alert_ok_button);
TextView alert_title = (TextView ) dialog.findViewById(R.id.alert_title);
final EditText shelf_name_edit=(EditText)dialog.findViewById(R.id.shelf_name_edit_area);
alert_title.setText(title);
alert_title.setTextSize(20);
ok.setText("OK");
ok.setTextSize(20);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.cancel();
Log.i("EditText Value",shelf_name_edit.getEditableText().toString());
}
});
dialog.show();

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/ClkBtn"
android:text="Click Me"></Button>
</RelativeLayout>
mydialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff" android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView android:id="#+id/Tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px"
android:textColor="#6aa4cc"
android:text="Friendcaster for Facebook"
android:padding="10dip"
/>
<TextView android:id="#+id/Tv2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="14px"
android:textColor="#android:color/black"
android:layout_below="#+id/Tv1"
android:textStyle="bold"
android:text="A , B , C , D and or 5 friend like your status "
android:padding="10dip"
/>
<View android:layout_height="15dip" android:layout_width="fill_parent"/>
<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent"
android:orientation="horizontal" android:background="#d6d6d6"
android:layout_weight="3"
>
<LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="left"
android:id="#+id/ll"
android:layout_weight="1" android:padding="5dip"
>
<ImageView android:src="#drawable/video"
android:layout_height="30dip" android:layout_width="30dip" android:layout_gravity="center_vertical|center_horizontal"
/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="view" android:layout_gravity="center_vertical|center_horizontal"
android:textColor="#0388e5"
/>
</LinearLayout>
<View android:layout_height="match_parent" android:layout_width="1dip" android:background="#android:color/darker_gray"/>
<LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_weight="1" android:padding="5dip"
>
<ImageView android:src="#drawable/chat"
android:layout_height="30dip" android:layout_width="30dip" android:layout_gravity="center_vertical|center_horizontal"
/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Reply" android:layout_gravity="center_vertical|center_horizontal"
android:textColor="#0388e5"
/>
</LinearLayout>
<View android:layout_height="match_parent" android:layout_width="1dip" android:background="#android:color/darker_gray"/>
<LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_weight="1" android:padding="5dip"
android:gravity="right"
>
<ImageView android:src="#drawable/cancel"
android:layout_height="30dip" android:layout_width="30dip" android:layout_gravity="center_vertical|center_horizontal"
/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Zip" android:layout_gravity="center_vertical|center_horizontal"
android:textColor="#0388e5"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
main.java
package com.example.cusmizepopup;
import android.os.Bundle;
import android.os.Message;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
Dialog myDialog;
Button myButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.ClkBtn);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
myDialog = new Dialog(MainActivity.this);
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
myDialog.setContentView(R.layout.mydialog);
//myDialog.setTitle("My Dialog");
myDialog.setCancelable(true);
//myDialog.setCancelable(true);
//myDialog.setCancelMessage("Are you Sure ? ")
LinearLayout button = (LinearLayout) myDialog.findViewById(R.id.ll);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
//myDialog.dismiss();
Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_LONG).show();
}
});
myDialog.unregisterForContextMenu(myButton);
myDialog.show();
}
});
}
}

Related

error while spec.setContent(Intent) on tabSpace

There is an error while i am putting Intent as spec.setContent(Intent); on tabSpace,and it works fine if i put a layout on spec.setContent(tab1);
And following is the error:-
did you forget to call 'public void setup(localactivitymanager
activitygroup)
Here is the class:-
public class test extends AppCompatActivity {
Point size;
RelativeLayout r1;
CallbackManager callbackManager;
Context context;
TabHost.TabSpec spec_signin,spec_signup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
context = getApplicationContext();
Display display = getWindowManager().getDefaultDisplay();
size = new Point();
display.getSize(size);
Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
getResources(),R.drawable.accont_bannre),size.x,size.y,true);
Drawable dr = new BitmapDrawable(bmp);
r1 = (RelativeLayout)findViewById(R.id.r1);
r1.setBackgroundDrawable(dr);
TabHost host = (TabHost)this.findViewById(R.id.tabhost);
host.setup();
//Tab 1
spec_signin = host.newTabSpec("SIGNIN");
Intent songsIntent = new Intent(this, NotificationActivity.class);
spec_signin.setContent(songsIntent);
spec_signin.setIndicator("SIGN IN", getResources().getDrawable(R.drawable.active_arrow));
// spec_signin.setIndicator("SIGNIN");
host.addTab(spec_signin);
//Tab 2
spec_signup = host.newTabSpec("SIGNUP");
Intent Intent = new Intent(this, NotificationActivity.class);
spec_signup.setContent(Intent);
spec_signup.setIndicator("SIGN UP", getResources().getDrawable(R.drawable.active_arrow));
// spec_signup.setIndicator("SIGNUP");
host.addTab(spec_signup);
}//oncreate ends
}
and the XML:-<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabHost
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/r2">
<RelativeLayout
android:id="#+id/r1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="270dp">
<LinearLayout
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_above="#+id/tabs"
android:id="#+id/v1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ccc7c7"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
<FrameLayout
android:background="#dede"
android:layout_below="#+id/r1"
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
</TabHost>
</RelativeLayout>
`
i have added following lines and it running.
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
host.setup(mLocalActivityManager);

list view on relative layout didn't show

activity_sms.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/SMSList"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_margin="10dp"
/>
</RelativeLayout>
code on SMSActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
smsListView = (ListView) findViewById(R.id.SMSList);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, smsMessagesList);
smsListView.setAdapter(arrayAdapter);
smsListView.setOnItemClickListener(this);
code on fragment_1.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_sms, container, false);
return rootView;
I tried to make a program to read SMS from inbox and show it on a ListView. It works when SMSActivity is the only activity there
Now, I use MainActivity.java instead of SMSActivity.java as Main Activity.
My goal is to invoke SMSActivity.java using fragment.
If anyone can help me it would be nice :)
What exactly you want to show in your layout?For example you have some items like this
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/list_item_title"
android:text="#string/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/list_item_details"
android:text="#string/description"
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
In ths case you can set the value for this items in your adapter
#Override
public View getView (int position,View convertView,ViewGroup parent){
View view = LayoutInflater.from(context).inflate(R.layout.linear_big_list_item, parent, false);
TextView nameOfItem = (TextView)convertView.findViewById(R.id.list_item_title);
TextView descriptionOfItem = (TextView)convertView.findViewById(R.id.list_item_details);
return view;
}

TransitionManager scene animation behavior with root layout

I'm testing out the Android Kit Kat transition feature. When the transition happens, the margins of the root layout are getting padded. I find this behavior strange.
The source
public class MainActivity extends ActionBarActivity {
Button btn;
TextView txt;
#TargetApi(Build.VERSION_CODES.KITKAT)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.text);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewGroup root = (ViewGroup)findViewById(R.id.root);
Scene scene = Scene.getSceneForLayout(root, R.layout.activity_main2, MainActivity.this);
TransitionManager.go(scene);
}
});
}
...
}
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:id="#+id/root"
tools:context="com.sebouh00.test.scenes.MainActivity">
<Button android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:layout_marginBottom="12dp"/>
<TextView
android:id="#+id/text"
android:layout_below="#id/btn"
android:text="#string/hello_world"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="18dp"/>
</RelativeLayout>
activity_main2.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:id="#+id/root"
tools:context="com.sebouh00.test.scenes.MainActivity">
<Button android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:layout_marginBottom="12dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/btn"
android:id="#+id/img"
android:src="#drawable/flag"
android:layout_marginBottom="12dp"/>
<TextView
android:id="#+id/text"
android:layout_below="#id/img"
android:text="#string/hello_world"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="18dp"/>
</RelativeLayout>
Results in this unless I remove the paddings from activity_main2.xml
Any ideas why?
It's because the View produced by inflating activity_main2 gets inserted into the Scene's root, i.e. your R.id.root RelativeLayout, so the padding from R.id.root is still applied.

Custom Buttons - Error in Java - widget.Button;

Followed example to the letter. When I try to use a custom button via a custom.xml, emulator crashes with a Java error - inflating class android.widget.Button.
import android.os.Bundle;
import android.content.Intent;
import android.app.Activity;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MasterActivity extends Activity {
Button uccBtn, tipBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master);
uccBtn = (Button) findViewById(R.id.button1);
tipBtn = (Button) findViewById(R.id.button2);
uccBtn.setOnClickListener (uccApp);
tipBtn.setOnClickListener (tipApp);
custom.xml
<?xml version="1.0" encoding="UTF-8"?>
<Selector xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:state_pressed="true" android:drawable="#drawable/btn_ecolab_selected"></item>
<item android:state_focused="true"
android:drawable="#drawable/btn_ecolab_highlight"></item>
<item android:drawable="#drawable/btn_ecolab"></item>
</Selector>
activity_main.xml
<?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"
android:background ="#ffffcc"
tools:context=".MasterActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:text="#string/button1" android:typeface="serif"
android:background="#drawable/custom"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/version"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="56dp"
android:text="#string/button10" android:typeface="serif"
android:background="#drawable/custom" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button10"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:text="#string/button2" android:typeface="serif"
android:background="#drawable/custom"
/>
</RelativeLayout>
If I reference just the button by name (btn_ecolab), it works fine, just no animation of the buttons when pressed or selected. I've tried it with a LinearLayout to make sure that the Relitavelayout was not the problem. The custom buttons are in my Res/drawable-hpdi folder. Also tried copies of buttons and custom.xml in the -mdpi, -xhdpi and -xxhdpi. Tried without #string assignments.
try this one.
uccBtn = (Button) findViewById(R.id.button1);
tipBtn = (Button) findViewById(R.id.button2);
uccBtn.setOnClickListener (new OnClickListener() {
#Override
public void onClick(View v) {
//your code is here
}
});
make one folder drawable in res folder put your custom.xml
I am using this way and work great.

Delayed ContextMenu on ListView

I have an Activity with a ListView. You long-press each row to get a ContextMenu. At the bottom of the ListView is an EditText box. It looks like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/TVReviewItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="90"
android:text="Item Name"
android:textSize="20sp" />
<Spinner
android:id="#+id/spinnerSort"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:gravity="left" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#bbb" >
</LinearLayout>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:longClickable="true"
android:scrollbars="none" >
</ListView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#bbb" >
</LinearLayout>
<EditText
android:id="#+id/etTweetReview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:ems="10"
android:hint="Write a Review"
android:imeOptions="actionDone"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="140"
android:maxLines="4"
android:minLines="1"
android:paddingBottom="9dp"
android:paddingTop="9dp"
android:singleLine="false" >
<requestFocus />
</EditText>
The problem: The ContextMenu will not show up until you physically press the cursor inside the EditText box (even though I am already requesting focus) and then LongPress the List row. If you long-press the list row FIRST, nothing happens. Here is the OnCreateContextMenu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
android.view.MenuInflater inflater = getActivity()
.getMenuInflater();
inflater.inflate(R.menu.reviews_context, menu);
menu.setHeaderTitle("Available Actions");
android.view.MenuItem Edit = menu.findItem(R.id.edit);
android.view.MenuItem Delete = menu.findItem(R.id.delete);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
int position = info.position;
ListView lv = (ListView) v;
int firstVisible = lv.getFirstVisiblePosition();
View rowView = lv.getChildAt(position - firstVisible);
ReviewUser = ((TextView) rowView.findViewById(R.id.labelUser))
.getText().toString();
ReviewWords = ((TextView) rowView.findViewById(R.id.labelComment))
.getText().toString();
if (Rateit.isUserLoggedIn == true) {
if (Rateit.username.equals(ReviewUser) {
Edit.setVisible(true);
Delete.setVisible(true);
} else {
Edit.setVisible(false);
Delete.setVisible(false);
}
} else {
Edit.setVisible(false);
Delete.setVisible(false);
}
}
I've tried this with the Super above and below the other content. Can anyone see what's wrong?
UPDATE: New info. I found if the ListView scrolls, you have to scroll to the bottom THEN it will allow you to access ContextMenu if you try. In this case, it has nothing to do with EditText box.
I figured it out. In my row layout file (not shown), I have three buttons. I had to set each to:
android:focusable="false"
Simple fix apparently.