Buttons are not displaying with header in Android - android

I want to create a header which contains an ImageView and three Buttons. I have done some coding but the problem is that when we run our app, the header only displays the image and the Buttons are not showing.
My Activity:
public class Header extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.header);
}
}
My XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:scaleType="fitXY"
android:src="#drawable/header" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/mid_belt"
android:orientation="horizontal"
android:weightSum="90" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="10"
android:background="#drawable/selector"
android:onClick="click"
android:text="Latest"
android:textColor="#ffffff"
android:textStyle="bold" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="10"
android:background="#drawable/selector"
android:text="News Room"
android:textColor="#ffffff"
android:textStyle="bold" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="10"
android:background="#drawable/selector"
android:text="Featured"
android:textColor="#ffffff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>

you have solved your problem tried like code in your class :
public class Header extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.header);
setContentView(R.layout.main);
}
}

Related

included bottom layout for all the android activites with onclick events not working

I am new to android development. I want to include the same horizontal Scroll view in all android activities.I have defined the layout and Onclick events in one separate activity and extended that class with other activites.But the onclick events are not working
here is my Base Activity
public class Footer extends AppCompatActivity implements View.OnClickListener {
private ImageView img_school, img_group, img_news, img_schemes, img_jobs, img_gallery, img_goddess, img_services, img_census, img_address, img_abt_us;
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.imgs_school:
Intent i = new Intent(Footer.this, SchoolActivity.class);
startActivity(i);
break;
case R.id.imgs_galary:
Intent gi = new Intent(Footer.this, GalleryActivity.class);
startActivity(gi);
break;
case R.id.imgs_events:
Intent ni = new Intent(Footer.this, EventsActivity.class);
startActivity(ni);
break;
case R.id.imgs_abtus:
break;
case R.id.imgs_jobs:
Intent ji = new Intent(Footer.this, JobsActivity.class);
startActivity(ji);
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_footer);
initAll();
img_gallery.setOnClickListener(this);
img_school.setOnClickListener(this);
img_news.setOnClickListener(this);
img_jobs.setOnClickListener(this);
img_abt_us.setOnClickListener(this);
}
public void initAll() {
img_school = findViewById(R.id.imgs_school);
img_news = findViewById(R.id.imgs_events);
img_jobs = findViewById(R.id.imgs_jobs);
img_gallery = findViewById(R.id.imgs_galary);
img_abt_us = findViewById(R.id.imgs_abtus);
}
}
and the XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:paddingLeft="#dimen/_20sdp"
android:paddingRight="#dimen/_50sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_school"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/school"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="School"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:paddingRight="#dimen/_50sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_events"
android:clickable="true"
android:src="#drawable/news"
android:layout_gravity="center"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="Events"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:paddingRight="#dimen/_50sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_jobs"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/jobs"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="Jobs"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:paddingRight="#dimen/_50sdp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_galary"
android:clickable="true"
android:src="#drawable/gallery"
android:layout_gravity="center"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="Gallery"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:paddingRight="#dimen/_50sdp"
android:layout_height="wrap_content">
<ImageView
android:layout_gravity="center"
android:id="#+id/imgs_abtus"
android:clickable="true"
android:src="#drawable/abt_us"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="About Us"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
And i am extending this activity with other classes
such as
public class SchoolActivity extends Footer {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school);
}
}
And its activity_school xml
<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.evoqis.manovaru.SchoolActivity">
<include layout="#layout/activity_footer"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
But the onclick events are not happening.
Thanks in adavance
Here is the problem:
In subclass you call:
super.onCreate(savedInstanceState);
Which calls:-
setContentView(R.layout.activity_footer); //which creates a new views
initAll(); //which finds views by id
img_gallery.setOnClickListener(this); //setting listeners.
and then in subclass you call:
setContentView(R.layout.activity_school); which creates new view, and overrides all the listeners you have set in superclass.
How to fix it
Move
img_gallery.setOnClickListener(this);
img_school.setOnClickListener(this);
img_news.setOnClickListener(this);
img_jobs.setOnClickListener(this);
img_abt_us.setOnClickListener(this);
Inside public void initAll(), and in subclass do this:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school);
initAll();
Also you can remove onCreate() method from superclass.

Click TextView inside linear layout inside ScrollLayout

this below code for my login_activity xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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"
android:background="#color/colorBackgroundLogin"
android:clipToPadding="false"
android:fillViewport="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear_layout"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingTop="20dp"
android:paddingBottom="20dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="#drawable/login"
tools:ignore="ContentDescription"/>
</RelativeLayout>
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email_edit_text"
android:hint="#string/email_address_string" />
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:id="#+id/password_edit_text"
android:hint="#string/password_string" />
<Button
android:layout_width="match_parent"
android:textAllCaps="true"
android:text="#string/login_string"
android:background="#drawable/button_style"
android:layout_marginTop="20dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:id="#+id/login_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
</LinearLayout>
</ScrollView>
I want to click on TextView id=need_an_account that like below:
public class LoginActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
}
Note that i don't want to use findViewById() method
But onClick not Working at all how can I solve this problem?
If you don't want to use OnClickListener, Here is an alternative -
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:onClick="myTextViewMethod"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
Now write myTextViewMethodin you Activity
public void myTextViewMethod(View v) {
// Do your stuff here...
}
Plus point, No need to implement OnClickListener, Android will do it in background.
And if you wanna use an Interface View.OnClickListener
Simple, set need_an_account.setOnClickListener(this);
In oncreate add this,
TextView need_an_account = (TextView) findViewById(R.id.need_an_account);
need_an_account.setOnClickListener(this);
You can add OnClick to your xml to avoid using findViewById:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:clickable="true"
android:textStyle="italic"
android:onClick="textViewClick"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
and then write your function in the activity as:
public void textViewClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
no need to implement an OnClickListener if you dont want to define the TextView programatically. Remember to set TextView to clickable and for authenticity i also set background to ?selectableItemBackground so the user knows that the TextView can be clicked

ImageView onClick is not working FragmentActivity

May be it is so easy but do not know i have tried so much but it's not working :(
I have layout like this way
And i have following java code
public class MainActivity extends FragmentActivity{
private ImageView ivMainMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivMainMenu = (ImageView) findViewById(R.id.ivMainMenu);
ivMainMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("showSlider clicked");
}
});
}
i have tried
android:clickable="true"
but still not working
below is my xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/rlMainHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/header_bg"
android:orientation="vertical" >
<TextView
android:id="#+id/tvHeaderText"
style="#style/header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<TextView
android:id="#+id/txtScore"
style="#style/black_large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="#dimen/extra_small"
android:background="#drawable/keyboard_btn"
android:padding="#dimen/extra_small"
android:text="#string/finish"
android:textColor="#color/dark_text_color"
android:textStyle="bold"
android:visibility="gone" />
<ImageView
android:id="#+id/ivMainMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:clickable="true"
android:src="#drawable/menu" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/rlMainHeader"
android:background="#color/main_bg"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/flMain"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<LinearLayout
android:id="#+id/llMainSlider"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:visibility="visible" >
<include
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="#layout/slider" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/dark_text_color"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
in my logcat i am getting following message whenever i click on imageview
04-27 11:09:42.996: V/Provider/Settings(1018): from settings cache , name = sound_effects_enabled , value = 0
also i want to let you know that if i load any fragment in flMain, in that fragment all event is working fine but not on main layout :( also in llMainSlider noting was happening :(
Why don't you try declaring the event on the XML?
It is as simple as this
android:id="#+id/ivMainMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="myNewMethod" <!--add this extra line!--->
android:src="#drawable/menu" />
and simply on your Main activity add the myNewMethod as am extra function like this
public class MainActivity extends FragmentActivity{
private ImageView ivMainMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void myNewMethod(View v) {
System.out.println("showSlider clicked");
}
Good Luck ;)

Android Horizontal orientation while rotation some parts missing

I am currently working on a application .when i installed my_app in android mobile while rotating in horizontal view my buttons are missing,please help!! Thanks in advance
My java coding is:
public class Draw extends Activity {
ImageView iv1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final DrawView drawView = new DrawView(this);
setContentView(R.layout.draw);
FrameLayout frm_layout=(FrameLayout) findViewById(R.id.main_frame);
iv1=(ImageView) findViewById(R.id.imageView1);
frm_layout.addView(drawView);
Intent myIntent = getIntent(); // this is just for example purpose
String test=myIntent.getExtras().getString("guided");
if(test.equals("1"))
{
iv1.setImageResource(R.drawable.untitled);
iv1.setAlpha(100);
}
else
{
//iv1.setImageResource(null);
}
//Toast.makeText(getApplicationContext(), test.toString(), 2000).show();
Button btn_undo=(Button) findViewById(R.id.button1);
btn_undo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
drawView.onClickUndo();
}
});
Button btn_redo=(Button) findViewById(R.id.button2);
btn_redo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
drawView.onClickRedo();
}
});
}
}
My XML coding is below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/white" >
<FrameLayout
android:id="#+id/main_frame"
android:layout_width="fill_parent"
android:layout_height="472dp"
android:background="#android:color/white" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Redo" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Undo" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Save" />
</RelativeLayout>
</LinearLayout>
Just Copy and Past in you xml file, it wil run completely. I have tested.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/white" >
<FrameLayout
android:id="#+id/main_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" android:layout_weight="0.1">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.9" android:layout_gravity="bottom" android:layout_marginTop="-10dp">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Redo" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Undo" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Save" />
</RelativeLayout>
</LinearLayout>
Hope this is what you want.
do one thing make a folder named Layout-land in res folder , in which put your xml file. now you can design your xml in horizontal mode . do whatever changes you want in that file and check :
otw put a scroll bar in whole layout and then check :) good luck

Android Application force closes. (NullPointerException / ToggleButton)

My Code:
public class My_Application extends Activity {
public ToggleButton privBtn = (ToggleButton) findViewById(R.id.privacyShow);
public LinearLayout privLay = (LinearLayout) findViewById(R.id.privacyLayout);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
privBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(privBtn.isChecked()) {
privLay.setVisibility(LinearLayout.VISIBLE);
}
else {
privLay.setVisibility(LinearLayout.INVISIBLE);
}
}
});
}
}
When I launch the application it force closes. I debugged the application with the built-in Dev Tools application in the emulator, and see the following:
[2011-06-23 20:27:06 - ddms]null
java.lang.NullPointerException
at com.android.ddmlib.Client.sendAndConsume(Client.java:572)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:671)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
As well as a "Source not found." error at:
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2417
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="My Application" android:gravity="center_horizontal"
android:textSize="25dp" android:background="#222" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text=" Server IP: " android:textSize="20dp" android:textColor="#0F0"
android:layout_weight="0.7" />
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:hint="eg: 0.0.0.0" android:layout_weight="0.5" android:textSize="20dp"
android:id="#+id/loginIP" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Server Port: " android:textSize="20dp" android:textColor="#0F0"
android:layout_weight="0.7" />
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:hint="eg: 1234" android:layout_weight="0.5" android:textSize="20dp"
android:id="#+id/loginPort" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:id="#+id/privacyLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Privacy Options" android:textSize="20dp" android:textColor="#0F0"
android:layout_weight="0.4" />
<ToggleButton android:layout_width="fill_parent" android:layout_height="wrap_content"
android:checked="false" android:layout_weight="0.6" android:textOff="Show Options"
android:textOn="Hide Options" android:id="#+id/privacyShow" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Username: " android:textColor="#0F0" android:textSize="15dp"
android:layout_weight="0.5" />
<EditText android:layout_width="fill_parent" android:layout_height="30dp"
android:id="#+id/privacyUsername" android:textSize="15dp" android:layout_weight="0.2" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Password: " android:textColor="#0F0" android:textSize="15dp"
android:layout_weight="0.5" />
<EditText android:layout_width="fill_parent" android:layout_height="30dp"
android:id="#+id/privacyPassword" android:textSize="15dp" android:layout_weight="0.2" />
</LinearLayout>
</LinearLayout>
You can't set the privBtn and privLay before the setContentView() is called. Move those two statements into your onCreate().
public class My_Application extends Activity {
public ToggleButton privBtn;
public LinearLayout privLay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
privBtn = (ToggleButton) findViewById(R.id.privacyShow);
privLay = (LinearLayout) findViewById(R.id.privacyLayout);
privBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(privBtn.isChecked()) {
privLay.setVisibility(LinearLayout.VISIBLE);
}
else {
privLay.setVisibility(LinearLayout.INVISIBLE);
}
}
});
}
}

Categories

Resources