How to create a Stack Panel menu in Android SDK? - java

I'd like to create a menu for my widget configuration similar like GWT Stack Panel
Is it posible to create similar type of Menu in android.? Any Help or suggestions are welcome.

use this layout :
<?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">
<Button
android:text="Bt1"
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/linearLayout1">
<ImageView
android:layout_width="wrap_content"
android:id="#+id/imageView1"
android:layout_height="wrap_content"
android:src="#drawable/icon"></ImageView>
</LinearLayout>
<Button
android:text="Bt2"
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/linearLayout2">
<ImageView
android:layout_width="wrap_content"
android:id="#+id/imageView2"
android:layout_height="wrap_content"
android:src="#drawable/icon"></ImageView>
</LinearLayout>
<Button
android:text="Bt3"
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/linearLayout3">
<ImageView
android:layout_width="wrap_content"
android:id="#+id/imageView3"
android:layout_height="wrap_content"
android:src="#drawable/icon"></ImageView>
</LinearLayout>
</LinearLayout>
and use this code :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.linearLayout1).setVisibility(View.GONE);
findViewById(R.id.linearLayout2).setVisibility(View.GONE);
findViewById(R.id.linearLayout3).setVisibility(View.GONE);
Button bt1=(Button) findViewById(R.id.button1);
Button bt2=(Button) findViewById(R.id.button2);
Button bt3=(Button) findViewById(R.id.button3);
bt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
findViewById(R.id.linearLayout1).setVisibility(View.VISIBLE);
findViewById(R.id.linearLayout2).setVisibility(View.GONE);
findViewById(R.id.linearLayout3).setVisibility(View.GONE);
}
});
bt2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
findViewById(R.id.linearLayout1).setVisibility(View.GONE);
findViewById(R.id.linearLayout2).setVisibility(View.VISIBLE);
findViewById(R.id.linearLayout3).setVisibility(View.GONE);
}
});
bt3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
findViewById(R.id.linearLayout1).setVisibility(View.GONE);
findViewById(R.id.linearLayout2).setVisibility(View.GONE);
findViewById(R.id.linearLayout3).setVisibility(View.VISIBLE);
}
});
}
you can use your layout in the linearlayout1,linearlayout2 and linearlayout3 for build stack panel for android.

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;

buttons inside the drawerLayout dont work

I have problem with drawerLayout. in drawerLayout, i have two Linearlayouts. one is the main menu that shows directly, the otherone is active with drawer slide. when i slide the drawer menü i cant click the buttons. but when i remove the second linearLayout then the buttons in the drawer work. how can i solve it?
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=""
android:clickable="true"
android:contextClickable="true">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="left|start"
android:background="#color/list_back"
android:layout_width="250dp"
android:layout_height="match_parent"
android:onClick="setContentView"
android:contextClickable="true"
android:clickable="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ana Sayfa"
android:id="#+id/buttonAnaSayfa"
android:onClick="setContentView"
android:clickable="false"
android:contextClickable="false"
android:nestedScrollingEnabled="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kalori Tablosu"
android:id="#+id/buttonKalori"
android:onClick="setContentView"
android:clickable="true"
android:contextClickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Glisemik İndeks Tablosu"
android:id="#+id/buttonGITablosu"
android:onClick="setContentView"
android:clickable="true"
android:contextClickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vücut Kütle İndeksi"
android:id="#+id/buttonVucutKutle"
android:onClick="setContentView"
android:clickable="true"
android:contextClickable="true" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="center">
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonKaloriListesi"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/kalori_listesi_button"
android:background="#null"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonGITablosu"
android:src="#drawable/gi_tablosu_button"
android:layout_below="#+id/imageButtonKaloriListesi"
android:layout_alignParentStart="true"
android:background="#null"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonBugunKaloriHesabi"
android:src="#drawable/bugun_kac_kalori_aldin_button"
android:background="#null"
android:layout_above="#+id/imageButtonVucutKutleIndeks"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonVucutKutleIndeks"
android:src="#drawable/vucut_kutle_indeksi_button"
android:background="#null"
android:layout_centerVertical="true"
android:layout_alignStart="#+id/imageButtonBugunKaloriHesabi"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:id="#+id/buttontest1"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_gravity="center_horizontal"
android:id="#+id/ad"
android:layout_marginTop="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"></LinearLayout>
</LinearLayout>
here is my .java ;
package com.azelirbrevo.glisemikindeks
public class "" extends AppCompatActivity {
//DrawerLayout drawerLayout;
//ActionBarDrawerToggle toggle;
//CharSequence actionBarTitle, appTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstaftersplash);
ImageButton ImageButtonKaloriListesi = (ImageButton) findViewById(R.id.imageButtonKaloriListesi);
ImageButton imageButtonGITablosu = (ImageButton) findViewById(R.id.imageButtonGITablosu);
ImageButton imageButtonVucutKutleIndeks = (ImageButton) findViewById(R.id.imageButtonVucutKutleIndeks);
ImageButton imageButtonBugunKaloriHesabi = (ImageButton) findViewById(R.id.imageButtonBugunKaloriHesabi);
ImageButtonKaloriListesi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(firstAfterSplash.this, KaloriListesi.class);
startActivity(intent);
}
});
imageButtonGITablosu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(firstAfterSplash.this, GITablosu.class);
startActivity(intent);
}
});
imageButtonVucutKutleIndeks.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(firstAfterSplash.this, VucutKutleIndexHesaplama.class);
startActivity(intent);
}
});
imageButtonBugunKaloriHesabi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(firstAfterSplash.this, bugunKacKaloriAldim.class);
startActivity(intent);
}
});
//drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
//actionBarTitle = appTitle = getSupportActionBar().getTitle();
Button buttonAnasayfa = (Button) findViewById(R.id.buttonAnaSayfa);
Button buttonKalori = (Button) findViewById(R.id.buttonKalori);
Button buttonGı = (Button) findViewById(R.id.buttonGITablosu);
Button buttonVucutKutle = (Button) findViewById(R.id.buttonVucutKutle);
Button test = (Button) findViewById(R.id.buttontest1);
test.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(firstAfterSplash.this, TEST.class);
startActivity(intent);
}
});
buttonAnasayfa.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "tiklandi", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(firstAfterSplash.this, GITablosu.class);
startActivity(intent);
}
});
buttonKalori.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "tiklandi", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(firstAfterSplash.this, GITablosu.class);
startActivity(intent);
}
});
buttonGı.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "tiklandi", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(firstAfterSplash.this, GITablosu.class);
startActivity(intent);
}
});
buttonVucutKutle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "tiklandi", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(firstAfterSplash.this, GITablosu.class);
startActivity(intent);
}
});
}}
this is the design
You can create a application with Navigation drawer Activity here you can see custom layout inside drawer layout and inside layout you can set any type of widgets as button, TextView and any type what ever you want.
In xml file, my drawer menu items (LinearLayout and Buttons) were over my main LinearLayout. I just cut-paste it under the main LinearLayout and it helped.
here is the updated xml file of my project, hope will help someone.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=""
android:clickable="true"
android:contextClickable="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="center">
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonKaloriListesi"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/kalori_listesi_button"
android:background="#null"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonGITablosu"
android:src="#drawable/gi_tablosu_button"
android:layout_below="#+id/imageButtonKaloriListesi"
android:layout_alignParentStart="true"
android:background="#null"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonBugunKaloriHesabi"
android:src="#drawable/bugun_kac_kalori_aldin_button"
android:background="#null"
android:layout_above="#+id/imageButtonVucutKutleIndeks"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonVucutKutleIndeks"
android:src="#drawable/vucut_kutle_indeksi_button"
android:background="#null"
android:layout_centerVertical="true"
android:layout_alignStart="#+id/imageButtonBugunKaloriHesabi"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_gravity="center_horizontal"
android:id="#+id/ad"
android:layout_marginTop="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"></LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_gravity="left|start"
android:layout_width="250dp"
android:layout_height="match_parent"
android:onClick="setContentView"
android:contextClickable="true"
android:clickable="true"
android:background="#85c7f1">
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/ButtonDrawerAnaSayfa"
android:background="#null"
android:layout_gravity="left"
android:gravity="left"/>
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/ButtonDrawerKalori"
android:background="#null"
android:layout_gravity="left"
android:gravity="left"/>
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/ButtonDrawerGI"
android:background="#null"
android:layout_gravity="left"
android:gravity="left"/>
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/ButtonDrawerVucutKutle"
android:background="#null"
android:layout_gravity="left"
android:gravity="left"/>
</LinearLayout>

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......

onClickListener cause app crash

I have a problem with onClickListener method. When it's called application crashes. I have been searching for solution but seems like my code have everything what has been told.
public class Sudoku extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sudoku);
View continue_button = findViewById(R.id.continue_button);
continue_button.setOnClickListener(this);
View new_button = findViewById(R.id.new_button);
new_button.setOnClickListener(this);
View about_button = findViewById(R.id.about_button);
about_button.setOnClickListener(this);
View exit_button = findViewById(R.id.exit_button);
exit_button.setOnClickListener(this);
}
// ...
public void onClick(View v)
{
switch (v.getId())
{
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
}
--
public class About extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
}
}
And main activity:
<?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:background="#color/background"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip"
tools:context=".Sudoku" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/main_tittle"
android:gravity="center"
android:textColor="#color/main_tittle"
android:textSize="25sp"
android:layout_marginBottom="25dip"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/continue_label" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/new_game_label" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/about_label" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exit_label" />
</LinearLayout>
</LinearLayout>
I'm very beginner in android/java programming, please be forgiving.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dip"
android:gravity="center"
android:id="#+id/main_tittle"
android:textSize="25sp" />
<Button
android:id="#+id/continue_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/new_game_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/about_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/exit_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sudoku);
View continue_button = findViewById(R.id.continue_button); /// findviewbyid not by text String resource
continue_button.setOnClickListener(this);
View new_button = findViewById(R.id.new_button);
new_button.setOnClickListener(this);
View about_button = findViewById(R.id.about_button);
about_button.setOnClickListener(this);
View exit_button = findViewById(R.id.exit_button);
exit_button.setOnClickListener(this);
}
I don't see any id's being declared for your Buttons in the xml.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/about_button"
android:text="#string/about_label" />
If you are using Button then also you are using View
you are using this
View continue_button = findViewById(R.id.continue_button);
continue_button.setOnClickListener(this);
View new_button = findViewById(R.id.new_button);
new_button.setOnClickListener(this);
View about_button = findViewById(R.id.about_button);
about_button.setOnClickListener(this);
View exit_button = findViewById(R.id.exit_button);
exit_button.setOnClickListener(this);
It should be like this
Button continue_button = (Button)findViewById(R.id.continue_button);
continue_button.setOnClickListener(this);
Button new_button = (Button)findViewById(R.id.new_button);
new_button.setOnClickListener(this);
Button about_button =(Button) findViewById(R.id.about_button);
about_button.setOnClickListener(this);
Button exit_button =(Button) findViewById(R.id.exit_button);
exit_button.setOnClickListener(this);
you have post xml file in every button add id attribute like this
(in every Button tag)
<Button
android:id="#+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/continue_label" />
<Button
android:id="#+id/new_button"
android:id="#+id/new_game_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/about_button"
android:id="#+id/about_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/exit_button"
android:id="#+id/exit_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

Categories

Resources