hide layout checkbox is checked android - java

I am trying to simply hide a linear layout if a checkbox is clicked. I have defined function and onClick attribute calls it which is also tested with toast but when I try hiding the layout, it doesn't do anything.
The layout.xml:
<CheckBox
android:id="#+id/cbguest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.9"
android:onClick="itemClicked"
/>
In MainActivity.java
int[] i = {0};
public void itemClicked(View v) {
LinearLayout layoutguest1 = (LinearLayout) findViewById(R.id.guestlayout);
if (lns[0]%2 == 0) {
Toast.makeText(getApplicationContext(),"Visible", Toast.LENGTH_SHORT).show();
layoutguest1.setVisibility(View.VISIBLE);
lns[0]+=1;
}
if (lns[0]%2 != 0) {
layoutguest1.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),"Gone", Toast.LENGTH_SHORT).show();
lns[0]+=1;
}
}

**activity_main.xml
put this code in activity_main.xml file**
<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="match_parent"
tools:context="com.example.demo.MainActivity" >
<LinearLayout
android:id="#+id/layoutLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UserName" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<CheckBox
android:id="#+id/chkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hide" />
</RelativeLayout>
**MainActivity.java
put this code in MainActivity.java file**
public class MainActivity extends Activity {
private LinearLayout layoutLogin;
private CheckBox chkBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chkBox = (CheckBox) findViewById(R.id.chkBox);
layoutLogin = (LinearLayout) findViewById(R.id.layoutLogin);
chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
// TODO Auto-generated method stub
if(isChecked)
{
layoutLogin.setVisibility(View.GONE);
chkBox.setText("UnHide");
}
else
{
layoutLogin.setVisibility(View.VISIBLE);
chkBox.setText("Hide");
}
}
});
}
}

You should be using onCheckedChangeListener as I don't think it registers as an onClick- but an onCheck.
Android: checkbox listener

Related

How do I identify buttons right inside Java Activity Class?

I designed two buttons b1,b2 inside "KeyboardService.java" which should update the value of PACK_LIB inside "Stickers.java".
Three files attached:
main_board_layout.xml
Stickers.java
KeyboardService.java
I assigned both buttons with IDs of Button1 = b1 and Button2 = b2.
The built is successful but when I click on the buttons both do not work.
I think it is maybe a problem with this line ->
final Button button1 = (Button) mainBoard.findViewById(R.id.b1);
Because the Buttons are inside a FrameLayout. But when I use ID of that it won't work either.
Any ideas?
-------main_board_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="#+id/main_board"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/black"
android:orientation="vertical">
<android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black" />
<!--top bar-->
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ffffff"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:padding="2dp"
android:id="#+id/buttons"
>
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="#string/button2"
/>
<TextView
android:id="#+id/packNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="#string/app_name"
android:textColor="#000000"
android:textSize="1sp"
android:visibility="gone"
/>
<ImageView
android:id="#+id/btShareLinkGP"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="#mipmap/bt_share"
tools:srcCompat="#mipmap/bt_share"
/>
</FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000000"/>
<ScrollView
android:id="#+id/gif_view"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#color/black"
android:paddingLeft="0dp"
android:paddingRight="15dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:gravity="left|center"
android:orientation="horizontal"
android:padding="0dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/pack_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/black"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
</LinearLayout>
</LinearLayout>
-------Stickers.java
public static String PACK_LIB ="";
public void setDefaultStickerPack() {
checkVersion(true);
InputStream in = null;
String packList[]=new String[0];
final String PACK_APP="pack_app";
final String PACK_ICON="pack_on.png";
String curAssets="";
-------KeyboardService.java
#Override
public View onCreateInputView() {
mainBoard = (LinearLayout) getLayoutInflater().inflate(R.layout.main_board_layout, null);
packNameLabel = (TextView) mainBoard.findViewById(R.id.packNameLabel);
scrollView = (ScrollView) mainBoard.findViewById(R.id.gif_view);
stickerView = (RecyclerView) getLayoutInflater().inflate(R.layout.recycler_view, null);
stickerView.addItemDecoration(new MarginDecoration(this));
stickerView.setHasFixedSize(true);
stickerView.setLayoutManager(new GridLayoutManager(this, 4));
scrollView.addView(stickerView);
ImageView btShareLinkGP = (ImageView) mainBoard.findViewById(R.id.btShareLinkGP);
btShareLinkGP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
shareLinkToGP();
}
});
// packs bar
packView = (RecyclerView) mainBoard.findViewById(R.id.pack_recycler_view);
// BUTTONS ACTIONS
final Button button1 = (Button) mainBoard.findViewById(R.id.b1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Stickers.PACK_LIB = "allstickers";
}
});
final Button button2 = (Button) mainBoard.findViewById(R.id.b2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Stickers.PACK_LIB = "teststickers";
}
});
showStickers();
return mainBoard;
}

Android Popup Menu fill parent

I try set my popup menu in way to fill hole item on grid. Currently it look like on attached first picture and the next one is effect which I would like to have.
My code:
private void showPopupMenu(View view) {
// inflate menu
ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.PopupMenu);
PopupMenu popup = new PopupMenu(ctw, view);
Menu menu = popup.getMenu();
menu.add(Menu.NONE, 1, Menu.NONE, "Remove");
menu.add(Menu.NONE, 2, Menu.NONE, "Block");
popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
popup.show();
}
Could you please point me to right direction to achieve effect from project?
Demo App for your requirment by using PopupWindow. Preview
You can add list in it or customize it according to your needs.
MainActivity
public class MainActivity extends Activity {
boolean isClicked = true;
PopupWindow popUpWindow;
RelativeLayout relative;
ImageView btnClickHere;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relative = (RelativeLayout) findViewById(R.id.relative);
popUpWindow = new PopupWindow(this);
popUpWindow.setContentView(getLayoutInflater().inflate(R.layout.popup_design, null));
popUpWindow.getContentView().findViewById(R.id.textViewa).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "PopItemClicked", Toast.LENGTH_LONG).show();
}
});
btnClickHere = (ImageView) findViewById(R.id.imageView);
btnClickHere.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isClicked) {
isClicked = false;
popUpWindow.setHeight(relative.getHeight());
popUpWindow.setWidth(relative.getWidth());
popUpWindow.showAsDropDown(relative, 0, -relative.getHeight());
} else {
isClicked = true;
popUpWindow.dismiss();
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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.example.sohailzahid.testapp.MainActivity">
<RelativeLayout
android:id="#+id/relative"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#color/colorAccent"
tools:layout_editor_absoluteX="150dp"
tools:layout_editor_absoluteY="150dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="#android:drawable/arrow_down_float" />
</RelativeLayout>
</RelativeLayout>
popup_design.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F93567">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textViewa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Block"
android:textColor="#ffffff"
android:textSize="20dp" />
<TextView
android:id="#+id/textVsiewa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Add to friends"
android:textColor="#ffffff"
android:textSize="20dp" />
<TextView
android:id="#+id/textViesw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Remove"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
<ImageView
android:id="#+id/imageView"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:src="#android:drawable/arrow_down_float" />
</RelativeLayout>
Try this
popup.getWindow().getAttributes().height = ViewGroup.LayoutParams.MATCH_PARENT;
popup.getWindow().getAttributes().width = ViewGroup.LayoutParams.MATCH_PARENT;

Checkbuttons crashes app when checked

I created a list of checkboxes, and I was trying to increase an int value proportionally to the number of the selected ones. Instead, every time I try to check them (through .isChecked()) the app crashes. I've tried to search everywhere for some solutions, but nothing worked. I really can't understand where is the problem.
Here's the code
XML file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.example.deadlybanana.myfirstapp.DisplayPicture">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="0dp">
<LinearLayout
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/unicorn" />
<TextView
android:id="#+id/frasenome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<CheckBox
android:id="#+id/checkbox1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Beatiful?" />
<CheckBox
android:id="#+id/checkbox2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Fabolous?" />
<CheckBox
android:id="#+id/checkbox3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Unique?" />
<CheckBox
android:id="#+id/checkbox4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you a horse?" />
<CheckBox
android:id="#+id/checkbox5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Do you eat grass?" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="checkbutton"
android:text="Check if you are a unicorn" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Java file
package com.example.deadlybanana.myfirstapp;
public class DisplayPicture extends AppCompatActivity {
List<Boolean> checkboxes = new ArrayList<Boolean>();
CheckBox press1;
CheckBox press2;
CheckBox press3;
CheckBox press4;
CheckBox press5;
int points = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_picture);
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
CheckBox press1 = (CheckBox)findViewById(R.id.checkbox1);
CheckBox press2 = (CheckBox)findViewById(R.id.checkbox2);
CheckBox press3 = (CheckBox)findViewById(R.id.checkbox3);
CheckBox press4 = (CheckBox)findViewById(R.id.checkbox4);
CheckBox press5 = (CheckBox)findViewById(R.id.checkbox5);
TextView NameString = (TextView) findViewById(R.id.frasenome);
String phrase = name + ", are you some of these things?";
NameString.setText(phrase);
}
public void checkbutton(View view){
if (press1.isChecked()){
points += 4;
}
if (points > 3){
Toast.makeText(this, "You are a unicorn", Toast.LENGTH_LONG).show();
} else{
Toast.makeText(this, "You are not a unicorn", Toast.LENGTH_LONG).show();
}
}
}
(right now it is being examined just the first checkbox to simplify the code)
Try this: in onCreate()
press1 = (CheckBox)findViewById(R.id.checkbox1);
press2 = (CheckBox)findViewById(R.id.checkbox2);
press3 = (CheckBox)findViewById(R.id.checkbox3);
press4 = (CheckBox)findViewById(R.id.checkbox4);
press5 = (CheckBox)findViewById(R.id.checkbox5);
error occurs as press1.isChecked() is refers to the global CheckBox press1;
which is not yet initialized..
AS you are initializing the checkbox like:
CheckBox press1 = (CheckBox)findViewById(R.id.checkbox1); inside onCreate() which only gives it a local instance which is not accessable in your checkbutton(View view) function
Solution
remove the local initialization just use: press1 = (CheckBox)findViewById(R.id.checkbox1);
Set id for your button lets assume it be button and add the following code
Button btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
checkbutton();
}
});
Note:remove the view parameter
Implementing Listeners is the best way to handle the events
I never use the method isChecked() because it never works for me. Do you want to increase the points when you select a checkbutton and maybe show the points when you press a button?
Here is the class code:
public class ButtonActivity extends AppCompatActivity
{
int points=0;
boolean check1=false;
boolean check2=false;
boolean check3=false;
boolean check4=false;
boolean check5=false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),points+"",Toast.LENGTH_SHORT).show();
}
});
}
public void onClick(View view)
{
switch(view.getId())
{
case R.id.checkBox:
if(check1)
{
check1=false;
points--;
}
else
{
check1=true;
points++;
}
break;
case R.id.checkBox2:
if(check2)
{
check2=false;
points--;
}
else
{
check2=true;
points++;
}
break;
case R.id.checkBox3:
if(check3)
{
check3=false;
points--;
}
else
{
check3=true;
points++;
}
break;
case R.id.checkBox4:
if(check4)
{
check4=false;
points--;
}
else
{
check4=true;
points++;
}
break;
case R.id.checkBox5:
if(check5)
{
check5=false;
points--;
}
else
{
check5=true;
points++;
}
break;
}
}
}
Here is the layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.utente.stackoverflow.ButtonActivity">
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox2"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox3"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox4"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox5"
android:onClick="onClick"/>
<Button
android:text="Points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button" />
I tested it and it works. I hope this helps you.

View Flipper that uses Only One ImageView

I'm new to Android, and am currently using a ViewFlipper. I'd like to know if it's possible to use only one ImageView? That way I can just create an Arraylist of my images. I think using multiple ImageViews is not a good practice since I have 50+ images.
public void initContent() {
imageArrayList = new ArrayList<Integer>();
imageArrayList.add(R.drawable.pig2);
imageArrayList.add(R.drawable.pig3);
imageArrayList.add(R.drawable.pig4);
imageArrayList.add(R.drawable.pig5);
imageArrayList.add(R.drawable.pig6);
imageArrayList.add(R.drawable.pig7);
imageArrayList.add(R.drawable.pig8);
imageArrayList.add(R.drawable.pig9);
imageArrayList.add(R.drawable.pig10);
imageArrayList.add(R.drawable.pig11);
imageArrayList.add(R.drawable.pig12);
imageArrayList.add(R.drawable.pig13);
imageArrayList.add(R.drawable.pig14);
imageArrayList.add(R.drawable.pig24);
imageArrayList.add(R.drawable.pig25);
imageArrayList.add(R.drawable.theend);
MainActivity.java
public class MainActivity extends Activity {
ViewFlipper viewFlipper;
Button Next;
private Integer images[] = {R.drawable.ic_launcher,
R.drawable.ic_no_image, R.drawable.calendar52};
ImageView imageView1;
private int currImage = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
Next = (Button) findViewById(R.id.Next);
imageView1 = (ImageView) findViewById(R.id.imageView1);
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
currImage++;
if (currImage == 3) {
currImage = 0;
}
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(images[currImage]);
viewFlipper.showNext();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ViewFlipper
android:id="#+id/ViewFlipper01"
android:layout_width="fill_parent"
android:layout_height="400dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4B0082" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout03"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center" >
<Button
android:id="#+id/Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:text="Next" />
</RelativeLayout>
</LinearLayout>
Yes. Use the frame animation, delete your arraylist. Here is some sample code: Link

Unable to click ImageButton

I have a following layout in main_view.xml which contains a ImageButton in it, button when i click/touch the button the ButtonListener function never gets called.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayoutFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/mapArea"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/locate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:background="#80000000"
android:gravity="center"
android:padding="10dp"
android:text="#string/findingLocation"
android:textColor="#FFFFFF"
android:textSize="12sp" />
<ImageButton
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="#dimen/rightLeftMargin"
android:layout_marginLeft="#dimen/rightLeftMargin"
android:background="#null"
android:clickable="true"
android:contentDescription="#string/button1text"
android:src="#drawable/ic_feed_active" />
<ImageButton
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/rightLeftMargin"
android:background="#null"
android:contentDescription="#string/button2"
android:src="#drawable/ic_button2" />
<ImageButton
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="#dimen/rightLeftMargin"
android:layout_marginRight="#dimen/rightLeftMargin"
android:background="#null"
android:contentDescription="#string/button3View"
android:src="#drawable/ic_settings_active" />
</RelativeLayout>
And following code in MainActivity.Java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
User usrObj = new User();
if (usrObj.authenticateUser())
setContentView(R.layout.main_view);
else
setContentView(R.layout.login);
}
public void ButtonListener() {
ImageButton oggleButton = (ImageButton) findViewById(R.id.Button1);
ToggleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "ImageButton is clicked!",
Toast.LENGTH_SHORT).show();
}
});
}
}
please help.
you forget to call ButtonListener method when usrObj.authenticateUser() is true. do it as:
if (usrObj.authenticateUser())
{
setContentView(R.layout.main_view);
ButtonListener (); << call here
}
else
setContentView(R.layout.login);
and also attach setOnClickListener to oggleButton instead of ToggleButton
apart from above suggestions from ρяσѕρєя K, here is one more thing you have to take care is.
instead of this below code.
oggleButton.setOnClickListener(new OnClickListener() {
use this one
oggleButton.setOnClickListener(new View.OnClickListener() { // use View.oncli......

Categories

Resources