In my XML layout, I use SliderLayout in RelativeLayout where height and width are match_parent. I have a skip button to finish activity. I think SliderLayout overrides the skip button so I can not use the click event of button. It does not work. How can I avoid of this problem?
activity_tutorial.xml
<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"
android:background="#color/White" >
<Button
android:id="#+id/skipButton"
style="#style/button_type"
android:layout_width="#dimen/dp_150"
android:layout_height="#dimen/dp_40"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_20"
android:text="#string/skip" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
TutorialActivity.java
public class TutorialActivity extends Activity implements
OnSliderClickListener, OnClickListener {
private static final String FONTH_PATH_BUTTON = "fonts/Brandon_bld.otf";
private Typeface buttonFont;
private Button skipButton;
private SliderLayout mDemoSlider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial);
buttonFont = Typeface.createFromAsset(getAssets(), FONTH_PATH_BUTTON);
skipButton = (Button) findViewById(R.id.skipButton);
skipButton.setTypeface(buttonFont);
skipButton.setOnClickListener(this);
mDemoSlider = (SliderLayout) findViewById(R.id.imageSlider);
HashMap<String, Integer> file_maps = new HashMap<String, Integer>();
file_maps.put("Challenge", R.drawable.tutorial_challenge);
file_maps.put("Select", R.drawable.tutorial_select);
file_maps.put("Image", R.drawable.tutorial_image);
file_maps.put("Friend", R.drawable.tutorial_friend);
for (String name : file_maps.keySet()) {
TextSliderView textSliderView = new TextSliderView(this);
// initialize a SliderLayout
textSliderView.description(name).image(file_maps.get(name))
.setScaleType(BaseSliderView.ScaleType.Fit)
.setOnSliderClickListener(this);
// add your extra information
textSliderView.getBundle().putString("extra", name);
mDemoSlider.addSlider(textSliderView);
}
mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
mDemoSlider
.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
mDemoSlider.setCustomAnimation(new DescriptionAnimation());
mDemoSlider.setDuration(4000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tutorial, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onSliderClick(BaseSliderView slider) {
// TODO Auto-generated method stub
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.skipButton:
finish();
break;
default:
break;
}
}
}
Simply open XML and add the Button in your SliderLayout body. It can be set by dragging the button on top of the SliderLayout.
Your code:
<Button
android:id="#+id/skipButton"
style="#style/button_type"
android:layout_width="#dimen/dp_150"
android:layout_height="#dimen/dp_40"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_20"
android:text="#string/skip" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
Change it to:
<com.daimajia.slider.library.SliderLayout
android:id="#+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<Button
android:id="#+id/skipButton"
style="#style/button_type"
android:layout_width="#dimen/dp_150"
android:layout_height="#dimen/dp_40"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_10"
android:text="#string/skip" />
</com.daimajia.slider.library.SliderLayout>
That should do it. Hope it helps. :)
<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"
android:background="#color/White" >
<com.daimajia.slider.library.SliderLayout
android:id="#+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<Button
android:id="#+id/skipButton"
style="#style/button_type"
android:layout_width="#dimen/dp_150"
android:layout_height="#dimen/dp_40"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_10"
android:text="#string/skip" />
</com.daimajia.slider.library.SliderLayout>
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
Just move button inside of SliderLayout solves my problem.
Related
My layout's XML is like this
<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.hirewand.hudki.UploadResumeActivity"
android:background="#303030"
android:id="#+id/rootLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/actionBar"
android:background="#color/black">
<ImageButton
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="#drawable/ic_menu"
android:id="#+id/menu_button"
android:layout_marginLeft="13dp"
android:onClick="moveMenu"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Your profile"
android:layout_toRightOf="#id/menu_button"
android:gravity="center_vertical"
android:textSize="25dp"
android:layout_marginLeft="10dp"
android:textColor="#color/white"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="match_parent"
android:id="#+id/fragmentPlaceholder"
android:background="#color/black"
android:layout_below="#id/actionBar">
</RelativeLayout>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="170dp"
android:text="UPLOAD RESUME"
android:id="#+id/uploadButton"
android:background="#drawable/button_bg_rounded_corners"
android:layout_centerHorizontal="true"
android:onClick="startSignupActivity"
android:drawableLeft="#drawable/ic_action_upload"
android:drawablePadding="0dp"
/>
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="This helps us find the perfect job for you"
android:gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:textColor="#color/white"
android:textSize="25dp"
android:layout_below="#id/uploadButton"
android:layout_marginTop="70dp"/>
</RelativeLayout>
I'm dynamically adding a fragment onto the place holder and animating it onClick
Here's the Java of my activity:
public class UploadResumeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_resume);
//Hide the place holder, View.GONE implies it occupies no space
RelativeLayout r1 = (RelativeLayout) findViewById(R.id.fragmentPlaceholder);
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragmentPlaceholder, NavigationFragment.newInstance());
ft.commit();
r1.bringToFront();
r1.setVisibility(View.GONE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
public void moveMenu(View view) {
RelativeLayout r1 = (RelativeLayout) findViewById(R.id.fragmentPlaceholder);
Animation slideIn,slideOut;
slideIn = AnimationUtils.loadAnimation(this,R.anim.enter_from_left);
slideOut = AnimationUtils.loadAnimation(this,R.anim.exit_to_left);
if(r1.getVisibility() == View.GONE)
{
r1.startAnimation(slideIn);
r1.setVisibility(View.VISIBLE);
}
else
{
r1.startAnimation(slideOut);
r1.setVisibility(View.GONE);
}
}
}
This is how it looks:
When i try open the fragment by clicking:
I need the upload button to go behind the fragment. It works when I use View.setZ(). But my min API level is 14. Why doesn't View.bringToFront() work on the button?
Steve Cs solution to encase my button and Textview inside a RelativeLayout worked.
i want to hide my keypad while keypad collapse image clicks and animate my dialPad enter number relative layout to upside of bottom call layout
But i want to animate my enter number edittext layout to place up on bottom layout, please anyone help me.
This is my code
public class MainActivity extends ActionBarActivity {
private GridView gridView;
private TextView grid_num_tv;
private EditText phone_num_edt;
private RelativeLayout gridView_collapse_rl, dialPad_overflow_rl, dialPad_num_edt_rl;
private ImageView dialPad_collapse_iv;
static final String[] GRID_NUM = new String[] {
"1", "2", "3",
"4", "5", "6",
"7" ,"8", "9" ,
"*", "0", "#"};
static final String[] GRID_LETTERS = new String[] {
" ", "abc", "def",
"ghi", "jkl", "mno",
"pqrs" ,"tuv", "wxyz" ,
" ", "+", " "};
Context context;
ArrayList<GridListDataModel> myGridList = new ArrayList<GridListDataModel>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView1);
phone_num_edt = (EditText) findViewById(R.id.dial_pad_num_editText);
gridView_collapse_rl = (RelativeLayout) findViewById(R.id.dial_close_rl);
dialPad_overflow_rl = (RelativeLayout) findViewById(R.id.dial_overflow_rl);
dialPad_collapse_iv = (ImageView) findViewById(R.id.dialpad_collapse_imageView);
dialPad_num_edt_rl = (RelativeLayout) findViewById(R.id.dial_num_edt_rl);
gridView.setVisibility(View.VISIBLE);
gridView.setAdapter(new CustomGridAdapter(this, GRID_NUM));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
grid_num_tv = (TextView) view.findViewById(R.id.grid_num_textView);
Toast.makeText(getApplicationContext(), grid_num_tv.getText(), Toast.LENGTH_SHORT).show();
}
});
gridView_collapse_rl.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gridView.isShown()){
dialPad_collapse_iv.setImageResource(R.drawable.ic_action_collapse);
gridView.setVisibility(View.INVISIBLE);
}else{
dialPad_collapse_iv.setImageResource(R.drawable.ic_action_expand);
gridView.setVisibility(View.VISIBLE);
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml is my main layout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.gridviewdemo.MainActivity" >
<RelativeLayout
android:id="#+id/dialpad_bottom_rl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<RelativeLayout
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_toLeftOf="#+id/dial_overflow_rl"
android:layout_toRightOf="#+id/dial_close_rl"
android:background="#android:color/holo_green_light" >
<ImageView
android:id="#+id/dialpad_call_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#android:drawable/ic_menu_call" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/dial_close_rl"
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="#+id/dialpad_collapse_imageView"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_expand" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/dial_overflow_rl"
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="#+id/dialpad_overflow_imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_overflow" />
</RelativeLayout>
</RelativeLayout>
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/dialpad_bottom_rl"
android:gravity="center"
android:numColumns="3" >
</GridView>
<RelativeLayout
android:id="#+id/dial_num_edt_rl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/gridView1"
android:layout_above="#+id/gridView1"
android:layout_marginBottom="20dp"
android:layout_marginTop="189dp" >
<EditText
android:id="#+id/dial_pad_num_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/backspace_imageView"
android:ems="10"
android:hint="Enter number" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/backspace_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:src="#drawable/ic_action_backspace" />
</RelativeLayout>
</RelativeLayout>
grid_item.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" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/grid_num_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="22dp"
android:text="2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30dp" />
<TextView
android:id="#+id/grid_letter_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/grid_num_textView"
android:paddingLeft="5dp"
android:text="abc"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</RelativeLayout>
I don't know what exactly your layout was, so i write an example here, hope it can help you.
The key here is android:animateLayoutChanges="true" in your container layout.
I assume that your "enter number" is my tv_b, and the keypad is tv_a.
Here is the layout:
activity_test.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:animateLayoutChanges="true"
android:orientation="vertical">
<Button
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="#+id/btn_animate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animate" />
<TextView
android:id="#+id/tv_a"
android:layout_centerHorizontal="true"
android:layout_above="#+id/btn_animate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABCCC" />
<TextView
android:id="#+id/tv_b"
android:layout_above="#+id/tv_a"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABCCD" />
</RelativeLayout>
And activity code:
MainActivity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Button btnAnimate = (Button) findViewById(R.id.btn_animate);
final TextView tva = (TextView) findViewById(R.id.tv_a);
final TextView tvb = (TextView) findViewById(R.id.tv_b);
btnAnimate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tva.getVisibility() == View.VISIBLE) {
tva.setVisibility(View.GONE);
} else {
tva.setVisibility(View.VISIBLE);
}
}
});
}
}
You will see animation when click button Animate.
res/anim/scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="100%"
android:pivotY="100%"
android:toXScale="1.0"
android:toYScale="0.0" />
</set>
use this scale animation for that layout on button click as below:
Animation animation=AnimationUtils.loadAnimation(activity, R.anim.scale);
animation.setFillAfter(true);
gridView.startAnimation(animation);
I'm new to Android. I'm trying to test 2 Activities in my project where when I click on a button in my first Activity, it should take me to the second Activity. Here I have given a click event to my button but when I run the project, I'm unable to see anything, its just a blank screen. What am I missing ?
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onAddCLick(View view) {
Intent TaskIntent = new Intent(this,SecondScreen.class);
startActivity(TaskIntent);
}
}
This is the second activity :-
public class SecondScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Intent task = getIntent();
}
public void cancl_btn(View view) {
Intent goBack = getIntent();
finish();
}
}
This is activity_main.xml :-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ListView
android:layout_width="wrap_content"
android:layout_height="516dp"
android:id="#+id/theListView">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/add_task"
android:onClick="onAddCLick"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/setloc"
android:onClick="onMapbtnClck"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/settings"
android:onClick="onSetngClck"/>
</LinearLayout>
This is second_layout.xml :-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left">
<EditText
android:layout_width="364dp"
android:layout_height="wrap_content"
android:id="#+id/task_name_edit_txt"
android:text="Enter your Details here" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Set Location"
android:id="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Set_loc_btn"
android:onClick="set_usr_loc"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Add_task_btn"
android:onClick="add_usr_tsk_btn"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel_btn"
android:onClick="cancl_btn"/>
</LinearLayout>
</LinearLayout>
Use this code:
In first activity:
public void onAddCLick(View view) {
Intent TaskIntent = new
Intent(MainActivity.this,SecondScreen.class);
startActivity(TaskIntent);
}
In second Activity:
public void cancl_btn(View view) {
Intent goBack =new
Intent(SecondScreen.this,MainActivity.class);
startActivity(goBack);
}
by assuming that you have posted the complete XML file of the second layout,, I tried running it on my android studio and as you can see in the xml file itself you are missing lot of information in it,,,
you need to define the schema as you have done in the first xml and
the < EditText > tag cannot be outside a container, you need to place it inside the container, that is, inside any kind of layout (linear, relative etc)..
if the issue is with the xml file then modifying it as per above two points will solve your problem,, hope this helps
I need to place two ImageButtons on top of each other something like the following image.
While placing them has been no issue, I am unable to click the blue button. The red button when clicked works perfectly well.
The XML layout code is as follows:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageButton
android:id="#+id/bluebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onButtonClicked"
android:scaleType="fitCenter"
android:src="#drawable/bluebutton" />
<ImageButton
android:id="#+id/redbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onButtonClicked"
android:scaleType="fitCenter"
android:src="#drawable/redbutton" />
</FrameLayout>
How do i ensure that both buttons can be clicked ?
You can simply use a RelativeLayout to do that. See this I made just now:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton btn = (ImageButton)findViewById(R.id.bluebutton);
ImageButton btn2 = (ImageButton)findViewById(R.id.redbutton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView t = (TextView)findViewById(R.id.txt);
t.setText("Hello!");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView t = (TextView)findViewById(R.id.txt);
t.setText("Hi, how are you?");
}
});
}
it's just an example
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageButton
android:background="#ff040ab2"
android:id="#+id/bluebutton"
android:layout_width="150dp"
android:layout_height="250dp"
android:onClick="onButtonClicked"
android:scaleType="fitCenter"/>
<ImageButton
android:rotation="90"
android:background="#be2222"
android:id="#+id/redbutton"
android:layout_width="150dp"
android:layout_height="250dp"
android:onClick="onButtonClicked"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt"
android:textColor="#ffffff"/>
</RelativeLayout>
Changed the code.
public class MainActivity extends Activity {
ImageButton red,blue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
red = (ImageButton)findViewById(R.id.redbutton);
blue = (ImageButton)findViewById(R.id.bluebutton);
}
public void onRedButtonClicked(final View view) {
Toast.makeText(getApplicationContext(), "Button red is clicked!", Toast.LENGTH_SHORT).show();
}
public void onBlueButtonClicked(final View view) {
Toast.makeText(getApplicationContext(), "Button blue is clicked!", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And the layout:
<FrameLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/redbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onRedButtonClicked"
android:scaleType="fitCenter"
android:src="#drawable/redbutton"
android:visibility="visible"/>
<ImageButton
android:id="#+id/bluebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:onClick="onBlueButtonClicked"
android:scaleType="fitCenter"
android:src="#drawable/bluebutton"
android:visibility="visible"/>
</FrameLayout>
Hope this helps..:)
Here is the code that supposed to open a dialog box when i press the About button in my min activity page. But nothing really happens. : /
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newgameButton = findViewById(R.id.new_button);
continueButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
continueButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
continueButton.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()) {
case R.id.continue_button:
break;
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.new_button:
openNewGameDialog();
break;
case R.id.exit_button:
finish();
break;
}
}
Main Activity XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="30dip"
android:orientation="horizontal" >
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center" >
<TextView
android:text="#string/main_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/new_game_label" />
<Button
android:id="#+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/about_label" />
<Button
android:id="#+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exit_label" />
</LinearLayout>
</LinearLayout>
Example of a button: About
About Activity
public class About extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.about, menu);
return true;
}
}
About XML
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" >
<TextView
android:id="#+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_text" />
</ScrollView>
Here's your problem:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newgameButton = findViewById(R.id.new_button);
**newgameButton**.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
**aboutButton**.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
**exitButton**.setOnClickListener(this);
}
Only your 'continueButton' has an onClickListener registered...
Also:
It is good practice to also have a default case in a switch statement which gets executed if none of the cases hold. Add a default case with a log message or something and see if it gets executed.
First, make sure your activity implements View.OnClickListener interface.
Secondly, try to add #Override before the public void onClick(View view) { } method declaration. If this does not help, try to add a log statement at top of the onClick method, for example logging the ID of the view, and compare it to the ones you're trying to match it against.
As far as i can say, you need to call button.setOnClickListener(this); in your activity onCreate method