So I have the following code for a popup window in an activity:
private void initiatePopupWindow(String popupText, boolean hasEditText){
LayoutInflater inflater = (LayoutInflater) PlayActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout= inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_layout));
final PopupWindow popupWindow= new PopupWindow(layout, 300, 370, true);
findViewById(R.id.play_activity_layout).post(new Runnable() {
#Override
public void run() {
popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
});
TextView textPopup= (TextView) layout.findViewById(R.id.tvPopupText);
textPopup.setText(popupText);
if(hasEditText) {
EditText et = (EditText) findViewById(R.id.etNumber);
MAX_NUMBER_OF_MOVES = Integer.parseInt(et.getText().toString());
}
btnClosePopup= (Button) layout.findViewById(R.id.btnClosePopup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
//finish();
}
});
}
I get the NullPointerException for this row:
MAX_NUMBER_OF_MOVES = Integer.parseInt(et.getText().toString());
I tried to fix this by searching on the site for ideas but still dont know why it isnt proper.
EDIT:
My popup_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="#+id/tvPopupText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="#string/under_construction"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
<Button
android:id="#+id/btnClosePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/ok"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/etNumber"
android:inputType="number"
android:layout_alignRight="#+id/tvPopupText"/>
</RelativeLayout>
I think you have to use
EditText et = (EditText) layout.findViewById(R.id.etNumber);
see layout. missing.
Note that getText() never returns null. So only findViewById should raise the NullPointerException.
Try to change this:
final View layout= inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_layout));
for this other one:
// container = get it from param of onCreateView
final View layout= inflater.inflate(R.layout.popup_layout, container);
...
EditText et = (EditText) layout.findViewById(R.id.etNumber);
After you've fix the layout.findViewById....
Initialize the EditText to some default value...see the android:text="0" below
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/etNumber"
android:inputType="number"
android:layout_alignRight="#+id/tvPopupText"
android:text="0"
/>
And you should also ALWAYS wrap Integer.parseInt("string_value") in a try/catch block in case string_value is not a valid number:
try {
MAX_NUMBER_OF_MOVES = Integer.parseInt(et.getText().toString());
}
catch (NumberFormatException nfe_) {
MAX_NUMBER_OF_MOVES = 5; // Or whatever your default number should be
}
Related
How to get value from dynamically created EditText in Android?
This is my dynamic view (XML file)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/et_waypoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/background"
android:hint="Way Points"
android:padding="10dp"
android:textSize="16sp" />
<Button
android:id="#+id/btn_waypoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>
And I am using LayoutInflater because I want perfect Designed layout which is not accomplish by me via setLayoutParams so this is the reason I am using LayoutInflater.
And here is my code:
LayoutInflater l = getLayoutInflater();
final LinearLayout mainActivity = (LinearLayout) findViewById(R.id.dynamic);
final View view = l.inflate(R.layout.dynamic_layout_waypoints, null);
buttonWayPoints = (Button) view.findViewById(R.id.btn_waypoint);
editTextWayPoints = (EditText) view.findViewById(R.id.et_waypoint);
mainActivity.addView(editTextWayPoints);
I am new to Android. Most of people suggest this solution but it's not worked for me, the issue is when I implement this code my ADD NEW EDIT TEXT BUTTON not respond me.
This is how I am trying to get inserted value but it returns value of only last created edit text:
public Cursor getPerson(String Query) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(Query, null);
return c;
}
sb is StringBuilder sb;, w is String w;
String queryWayPoints = "select * from route_table where trip_id = " + t;
Cursor s = myDb.getPerson(queryWayPoints);
int count3 = s.getCount();
for (int j = 0; j < count3; j++) {
s.moveToNext();
w = s.getString(s.getColumnIndex("waypoints"));
// se.append(w + "." + "00");
}
trip_name.setText(n);
trip_date.setText(d);
sb.append(w);
}
trip_route.setText(sb.toString());
Although, I didn't get what exactly your question is, still I'll try to help.
What exactly this code does is, on clicking the button on top an new layout(one that you wanted) will add to the activity screen at runtime, i.e an edittext with a button, and when you click on that button, A toast with the value of respective edittext will be shown. Hence solving the problem of getting the value of dynamically added edittext
MainActivity
public class MainActivity extends AppCompatActivity {
Button generateET;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout myLinearLay = (LinearLayout) findViewById(R.id.dynamic);
generateET = (Button) findViewById(R.id.generateBtn);
generateET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater l = getLayoutInflater();
final View viewToAdd = l.inflate(R.layout.to_add, null);
Button buttonWayPoints = (Button) viewToAdd.findViewById(R.id.btn_waypoint);
final EditText editTextWayPoints = (EditText) viewToAdd.findViewById(R.id.et_waypoint);
buttonWayPoints.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (editTextWayPoints.getText().toString() != null) {
Toast.makeText(MainActivity.this, editTextWayPoints.getText().toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "No text found", Toast.LENGTH_SHORT).show();
}
}
});
myLinearLay.addView(viewToAdd);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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.techmahindra.stackques.MainActivity">
<Button
android:id="#+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="add editText To layout" />
<ScrollView
android:layout_below="#+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:id="#+id/dynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
></LinearLayout>
</ScrollView>
</RelativeLayout>
to_add.xml(layout which will be inflated at runtime)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/et_waypoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Way Points"
android:padding="10dp"
android:textSize="16sp" />
<Button
android:id="#+id/btn_waypoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>
There are some problems with your code. You're trying to add editTextWayPoints to mainActivity, but editTextWayPoints already has a parent, which is view. I think you should be adding view to mainActivity (mainActivity.addView(view)). Finally, to access the value of editTextWayPoints, you simply do editTextWayPoints.getText().toString();
I created a popup window to show a disclaimer in my app but I can't figure out why i cannot set text to TextView. It is an HTML string so I did this:
private void showPopup(final Activity context) {
final PopupWindow pwindo;
Button btnClosePopup;
try {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
TextView txt = (TextView)findViewById(R.id.txtView);
txt.setText(Html.fromHtml(getString(R.string.tos_text)));
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
popup_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4d4d4d"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:textColor="#ffffff" />
<Button
android:id="#+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>
strings.xml
<string name="tos_text"><![CDATA[
<p>This is a html-formatted string with <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph of the same string.</p>
]]>
</string>
It works only if I add android:text="#string/tos_text" in the XML file, but i see the raw html codes. What could be the problem?
Try next:
private void showPopup() {
//if you call this method correctly then you do not need to wrap
// this method by try-catch block which affects performance
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element), false);
final PopupWindow pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//get txt view from "layout" which will be added into popup window
//before it you tried to find view in activity container
TextView txt = (TextView) layout.findViewById(R.id.txtView);
txt.setText(Html.fromHtml(getString(R.string.tos_text)));
//init your button
Button btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
//show popup window after you have done initialization of views
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
EDIT:
To add scrollable content you need to change approach of initialization of popup window:
final PopupWindow pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
where you need to change width of window to ViewGroup.LayoutParams.MATCH_PARENT.
Then you need to wrap your text view by ScrollView which has root child - LinearLayout:
popup_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4d4d4d"
android:orientation="vertical"
android:padding="10sp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:textColor="#ffffff"/>
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close"/>
</LinearLayout>
ScrollView has field with weight:
android:layout_weight="1"
I have added this to avoid hiding of button when text will fill all of visible content.
EDIT 2:
Also you can fix height of scroll view, if you do not want to fill all the screen:
-----
<ScrollView
android:layout_width="match_parent"
android:layout_height="150dp">
-----
I am having an issue with grabbing my Buttons and other elements from within my PopupWindow, using the debugger it just report back as null?
private void initiatePopupWindow()
{
try
{
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) LoginActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_register,(ViewGroup)
findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 600, 900, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnReg = (Button) findViewById(R.id.btnReg);
inputName = (EditText) findViewById(R.id.name);
inputDOB = (DatePicker) findViewById(R.id.dob);
String name = inputName.getText().toString();
btnReg.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if ( ( !inputName.getText().toString().equals("")) &&
( getAge(inputDOB.getDayOfMonth(), inputDOB.getMonth(), inputDOB.getYear()) > 15) )
{
//register user
}
else if ( ( inputName.getText().toString().equals("")) )
{
Toast.makeText(getApplicationContext(),
"Please enter your name", Toast.LENGTH_SHORT).show();
}
else if (( getAge(inputDOB.getDayOfMonth(), inputDOB.getMonth(), inputDOB.getYear()) < 16) )
{
Toast.makeText(getApplicationContext(),
"You must be at least 16 to use this app", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), email, Toast.LENGTH_SHORT).show();
}
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
Could anyone point me in the correct direction or explain why I can't find them using R.id?
Cheers
Update: Activity_Register.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/rounded"
android:orientation="vertical"
android:padding="10sp" >
<EditText
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="#string/name"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<requestFocus />
</EditText>
<DatePicker
android:id="#+id/dobPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:layout_below="#+id/dob"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dob"
android:layout_below="#+id/name"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp"/>
<Button
android:id="#+id/btnReg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:text="#string/register"
android:layout_below="#+id/dobPicker"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
You can get like
View layout = inflater.inflate(R.layout.activity_register,
(ViewGroup) youractivity.this.findViewById(R.id.popup_element));
btnReg = (Button) layout.findViewById(R.id.btnReg);
inputName = (EditText) layout.findViewById(R.id.name);
inputDOB = (DatePicker) layout.findViewById(R.id.dob);
It's becoz your all the elements coming from inflated layout view and you need to pass context to get your popup_element layout as ViewGroup.
use this layout.findViewById(R.id.popup_element)); instead of this findViewById(R.id.popup_element));
I am new to android world and am working on dialog and in that I am trying to create a text view, button and imageview, but it is neither showing me error nor textview or imageview. It is showing a button. Here is the image:
.
But i am want to display my dialogue as follows
My xml file is as follows ....
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".AddFriendsCustomActivity">
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/submit_dialog"
android:layout_alignParentRight="true"
android:layout_marginRight="37dp"
android:contentDescription="#string/add_Friend"
android:src="#drawable/add" />
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="64dp"
android:layout_toLeftOf="#+id/add"
android:text="#string/submit" />
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/add"
android:layout_marginRight="17dp"
android:layout_toLeftOf="#+id/add"
android:ems="10" />
</RelativeLayout>
I am calling it in java file as follows
dialog = new Dialog(context);
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.activity_add_friends_dialog, null, false);
dialog.setContentView(view);
dialog.setCancelable(true);
//dialog.setContentView(R.layout.activity_add_friends_dialog);
//dialog.setTitle("Add Friends");
add= (ImageView) dialog.findViewById(R.id.add);
friends1 = (TextView) dialog.findViewById(R.id.writename);
submit_dialog = (Button) dialog.findViewById(R.id.submit_dialog);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
try {
friendsCount++;
Log.d("addFriendsActivity", "ashish comes here ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
submit_dialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(AddFriendsActivity.this, DetailsFriendsActivity.class);
startActivity(i);
}
});
dialog.show();
After some google I understand that the issues might be in r.java or you have imported android .r but i have not done any thing in this ..
Thanks in advance.... :)
<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:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
There is no error in your java code. Its perfect you have just change your xml. Use this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".AddFriendsCustomActivity" >
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:ems="10" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/writename"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/writename"
android:contentDescription="Add Friend"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/writename"
android:layout_below="#+id/add"
android:layout_marginRight="46dp"
android:text="Submit" />
</RelativeLayout>
For your xml I think it should look like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/my_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/add_btn" />
<ImageButton
android:id="#+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/add_btn"
android:background="#null" />
<Button
android:id="#+id/submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/my_edit_text"
android:text="Submit" />
</RelativeLayout>
Be aware that there can be some typo mistakes, because I didn't compile that code, and you should change your real variables. And create your AlertDialog like this:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MyActivity.this);
View mView = mInflater.inflater(R.layout.my_layout, null, false);
EditText mEdit = (EditText) mView.findViewById(R.id.my_edit_text);
ImageButton mImageBtn = (ImageButton) mView.findViewById(R.id.add_btn);
Button mSubmit = (Button) mView.findViewById(R.id.submit_btn);
mBuilder.setView(mView);
AlertDialog mAlert = mBuilder.create();
mAlert.show();
Using AlertDialog.Builder instead of Dialog is lot more easier for your situation (at least in my opinion). And you can add your Submit Button to your AlertDialog as default positive button.
Hope this helps you.
Look at your code i think
add= (ImageView) dialog.findViewById(R.id.add);
friends1 = (TextView) dialog.findViewById(R.id.writename);
as
add= (ImageView) view.findViewById(R.id.add);
friends1 = (EditText) view.findViewById(R.id.writename);
That should work.
Try this..
final Dialog dialog = new Dialog(AddFriendsActivity.this);
dialog.setContentView(R.layout.activity_add_friends_dialog);
dialog.setCanceledOnTouchOutside(true);
add= (ImageView) dialog.findViewById(R.id.add);
EditText friends1 = (EditText) dialog.findViewById(R.id.writename);
submit_dialog = (Button) dialog.findViewById(R.id.submit_dialog);
add.setOnClickListener(new OnClickListener() {
//#Override
#SuppressWarnings({ "unchecked", "rawtypes" })
#SuppressLint("NewApi")
public void onClick(View v) {
friendsCount++;
//TextView textview = new TextView(context);
//textview.setId(friendsCount);
//final RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//params.addRule(RelativeLayout.BELOW, friendsCount--);
//textview.setLayoutParams(params);
//textview.setVisibility(View.VISIBLE);
//relativelayout.addView(textview ,params);
Log.d("addFriendsActivity", "ashish comes here ");
}
});
submit_dialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(AddFriendsActivity.this, DetailsFriendsActivity.class);
startActivity(i);
}
});
dialog.show();
Your view is covered.
May be you can try to set your parent layout to Linearlayout, not RelativeLayout.
I have two ImageViews, one text field and one WebView in a LinearLayout. My ImageView listener is not working. Here is my 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:background="#drawable/bg_main"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="back"
android:adjustViewBounds="true"
android:clickable="true"
android:src="#drawable/back_icon" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:clickable="true"
android:contentDescription="forward"
android:src="#drawable/back_icon"/>
<EditText
android:id="#+id/adressBar"
android:imeOptions="actionDone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ems="10" >
</EditText>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Here is my Java code. I should point out that the Log.d statement is not firing, so it is definitely not getting called:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState == null) {
L.d(">>onCreateView", "savedInstanceState null");
}
View v = inflater.inflate(R.layout.browser,null);
mWebView = (WebView) v.findViewById(R.id.webView);
back =(ImageView) v.findViewById(R.id.imageView1);
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("backkkkkkkkkkkkkkkk", "msg");
}
});
return v;
}
try this
v = inflater.inflate(R.layout.browser, container, false);
ImageView back = (ImageView)findViewById(R.id.imageVIew1);
back.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Log.d("backkkkkkkkkkkkkkkk", "msg");
}
});
try this-
back.setOnClickListener(this);
and implement onclicklistener..
You need to inflate the view like
v = inflater.inflate(R.layout.browser, container, false);
back =(ImageView) v.findViewById(R.id.imageView1);
back.setOnClickListener(new View.OnClickListener())
{
..
}
set to focusable false for all the views in your xml file. or setOnClickListener for imageview in onActivityCreated() method in your fragment.
I had a similar issue and fixed it by adding android:layout_width="44dp" to make sure user would have enough area to click on. Then, you can align image content the way you wannt. In my case:
<ImageView
android:id="#+id/locationImageView"
android:layout_width="44dp"
android:layout_height="16dp"
android:layout_centerVertical="true"
android:layout_marginStart="4dp"
android:layout_toEndOf="#id/locationText"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:src="#drawable/icn_edit_small_white"/>