Domanda

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:

dialouge.

But i am want to display my dialogue as follows

need as this

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.... :)

È stato utile?

Soluzione 2

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>

Altri suggerimenti

<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" />

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top