0

I have set a Layout in which a user can insert a description regarding a video and a button Confirm. This is the code of the xml layout file:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:id="@+id/TitleAndDescription" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="250dp" 
android:minHeight="200dp"
>

<TextView
android:id="@+id/TextViewDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5px"
android:text="Video Description" >
</TextView>
<EditText android:layout_height="wrap_content" android:lines="3"       android:minWidth="250px" android:id="@+id/EditTextDescr"  android:layout_below="@+id/TextViewDescr" android:text="" android:layout_width="fill_parent"></EditText>


<LinearLayout android:id="@+id/buttonPanel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="54dip"
    android:orientation="vertical"
    android:layout_below="@+id/EditTextDescr"
     >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="4dip"
        android:paddingLeft="2dip"
        android:paddingRight="2dip" >
        <LinearLayout android:id="@+id/leftSpacer"
            android:layout_weight="0.25"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:visibility="gone" />

        <Button android:id="@+id/confirm"
            android:layout_width="0dip"
            android:layout_gravity="left"
            android:text="Confirm"
            android:layout_weight="1"
            android:maxLines="2"
            android:layout_height="wrap_content" />

        <LinearLayout android:id="@+id/rightSpacer"
            android:layout_width="0dip"
            android:layout_weight="0.25"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:visibility="gone" />
    </LinearLayout>
 </LinearLayout>
</RelativeLayout>

the piece of code in the .java file I have up to now is this:

public class Descrizione extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.descrizione);
    Button confirm = (Button) findViewById(R.id.confirm);
        confirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Context context = getApplicationContext();
                CharSequence text = "Descrption:" ........ "
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();


            }
        });
    }

What I have to do/insert in order to let the toast showing the text inserted for the description?

user2312945
  • 137
  • 1
  • 1
  • 7

3 Answers3

2

EDIT

public class Descrizione extends Activity {
EditText Desc;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.descrizione);
    Desc = (EditText) findViewById(R.id.EditTextDescr);
    Button confirm = (Button) findViewById(R.id.confirm);
        confirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Context context = Descrizione.this;
                String text =desc.getText().toString();
                int duration = Toast.LENGTH_SHORT;

                Toast.makeText(context, text, duration).show();


            }
        });
    }
Ayush
  • 3,989
  • 1
  • 26
  • 34
  • Yes, but What I have to insert in Charsequence text=.... in order to see what written in the description form? – user2312945 May 05 '13 at 18:16
  • 2
    @asyush using getApplicationContext() is not recommended.http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context/7298955#7298955. he has a character seqence text which he can use as an argument in toast. – Raghunandan May 05 '13 at 18:41
  • k @Raghunandan what can be the Best to Use in such Scenario. – Ayush May 05 '13 at 18:50
  • check the link in the above comment especially check the answer by commonsware – Raghunandan May 05 '13 at 18:54
0

Another way to do this would be the following:

@Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Description:\"....\"", Toast.LENGTH_LONG).show();


        }
TronicZomB
  • 8,667
  • 7
  • 35
  • 50
0
       Toast.makeText(Descrizione.this, text,LENGTH_SHORT).show();

public static Toast makeText (Context context, CharSequence text, int duration)

Added in API level 1 Make a standard toast that just contains a text view.

Parameters

context The context to use. Usually your Application or Activity object.

text The text to show. Can be formatted text.

duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

Also use a activity context in place of getApplicationContext();

When to call activity context OR application context?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Yes, but What I have to insert in Charsequence text=.... in order to see what written in the description form? I have to visualize the text the user insert in the form and so I have to recall for sure a variable in the toast but I don't know how – user2312945 May 05 '13 at 18:23