Thursday, July 9, 2015

android: How to insert Phone icon in every line in second Activity in Android code

1.You have to replace main.xml file's code with this one
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/llContainer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/travel1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Travels1 - " />

        <TextView
            android:id="@+id/txtNumber1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="20dip"
            android:text="325235" />

        <ImageButton
            android:id="@+id/btnPhone1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/stat_sys_phone_call" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/llContainer2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/travel2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Travels2 - " />

        <TextView
            android:id="@+id/txtNumber2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="20dip"
            android:text="455345" />

        <ImageButton
            android:id="@+id/btnPhone2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/stat_sys_phone_call" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/llContainer3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/travel3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Travels3 - " />

        <TextView
            android:id="@+id/txtNumber3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="20dip"
            android:text="453532" />

        <ImageButton
            android:id="@+id/btnPhone3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/stat_sys_phone_call" />
    </LinearLayout>

</LinearLayout>
  1. Change App2Activity.java code as follows:
    ImageButton btnPhoneOne, btnPhoneTwo, btnPhoneThree; TextView txtNum1, txtNum2, txtNum3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
    
    btnPhoneOne = (ImageButton) findViewById(R.id.btnPhone1);
    btnPhoneTwo = (ImageButton) findViewById(R.id.btnPhone2);
    btnPhoneThree = (ImageButton) findViewById(R.id.btnPhone3);
    
    txtNum1 = (TextView) findViewById(R.id.txtNumber1);
    txtNum2 = (TextView) findViewById(R.id.txtNumber2);
    txtNum3 = (TextView) findViewById(R.id.txtNumber3);
    
    String number1 = getIntent().getStringExtra("number1");
    String number2 = getIntent().getStringExtra("number2");
    String number3 = getIntent().getStringExtra("number3");
    
    txtNum1.setText(number1);
    txtNum2.setText(number2);
    txtNum3.setText(number3);
    
    btnPhoneOne.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            Intent callIntent = new Intent(Intent.ACTION_DIAL);
            callIntent.setData(Uri.parse("tel:" + txtNum1.getText()));
            startActivity(callIntent);
        }
    });
    
    btnPhoneTwo.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            Intent callIntent = new Intent(Intent.ACTION_DIAL);
            callIntent.setData(Uri.parse("tel:" + txtNum2.getText()));
            startActivity(callIntent);
        }
    });
    
    btnPhoneThree.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            Intent callIntent = new Intent(Intent.ACTION_DIAL);
            callIntent.setData(Uri.parse("tel:" + txtNum3.getText()));
            startActivity(callIntent);
        }
    });
    }
  2. You can see image main.xml screenshot



No comments:

Post a Comment