• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with MyView extends View and main activity file

 
Jaimesh Vyas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q 1. What should I use to access from main_activity to custom_view activity?

Q 2. Is there any relation between a layout and the click event?

Q 3. Do I need to write setContentView in MyView constructor?

I need to use View class in this program

MainActivity

package com.easyway2win;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;


public class MainActivity extends Activity {

//Button bpink;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent= new Intent(MainActivity.this,MyView.class);
startActivity(intent);

}

}


MyView

package com.easyway2win;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class MyView extends View implements OnClickListener{

public MyView(Context context) {
super(context);

bpink = (Button)findViewById(R.id.pinkColor);
bpink.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Log.d("Hi","Hello");
}
});

myview_activity.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="wrap_content">

<TextView
android:id="@+id/mytxtId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mytext" />

<Button
android:id="@+id/pinkColor"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_below="@id/mytxtId"
android:background="@drawable/pinkcolor"
android:text="Submit" />

<Button
android:id="@+id/blueColor"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_below="@id/mytxtId"
android:layout_toRightOf="@id/pinkColor"
android:background="@drawable/bluecolor" />

</RelativeLayout>
 
ab singh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aimesh Vyas]Q 1. What should I use to access from main_activity to custom_view activity?

Q 2. Is there any relation between a layout and the click event?

Q 3. Do I need to write setContentView in MyView constructor?

I need to use View class in this program

MainActivity



MyView


myview_activity.xml
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic