I need to set this layout visible when i click a button, my java is like:
Layout propLayout = (Layout) findViewById(R.id.properLayout);
public void propsBtn(View view) {
propLayout.setVisiblity(View.Visible);
}
I know I'm totally wrong with the layout line! I'll be very grateful if someone could show me how to set it right :)
This is my XML:
<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:background="#color/mainBackGround"
tools:context="com.myapplication2.app.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
...contents...
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:id="#+id/properLayout">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="bottom"
android:padding="8dp">
...contents...
</RelativeLayout>
</LinearLayout>
DerGolem gave the right answer but he then deleted it, so I report it here now:
//First we set visibility value of interested layout either to gone, visible or invisible
android:visibility="gone"
Then under onCreate write like:
//specify the button that has to handle visibility
ImageButton properties = (ImageButton) findViewById(R.id.propBtn);
//And the layout we want to change is visibility
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.properLayout);
properties.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (propLayout.getVisibility() == View.VISIBLE){
propLayout.setVisibility(View.INVISIBLE);
} else {
propLayout.setVisibility(View.VISIBLE);
}
}
});
The another way of toggle, with less lines, if someone likes
// button to click
ImageButton properties = (ImageButton) findViewById(R.id.propBtn);
// and LinearLayout to toggle
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.properLayout);
properties.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
propLayout.setVisibility((propLayout.getVisibility() == View.VISIBLE)
? View.INVISIBLE
: View.VISIBLE);
}
});
You will need to set an OnClickListener() on the layout you want to toggle. Then inside the listener you check to see if the layout is visible or not. If it is, then you set its visibility to View.INVISIBLE. Otherwise, you set it to View.VISIBLE.
Try this:
int counter = 0;
TextVeiw tv = (TextView) findViewById(R.id.textView);
Button button = (Button ) findViewById(R.id.but);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
if(counter%2==0){
tv.setVisibility(View.Visible);//show
} else {
tv.setVisibility(View.Gone);//hide
}
}
});
Related
Hello my goal is to make my custom view (ViewCircleOfInterest.java) receiving onClick. Now every time I click onClickListener on ViewCircleOfInterest.java is not called.
My interesting part of layout looks like that:
my_layout.xml
<FrameLayout
android:id="#+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/rl"
android:layout_alignParentTop="true"
android:background="#bbb">
<abc.x.y.ViewCircleOfInterest
android:id="#+id/view_circle_of_interest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
I have FrameLayout on my layout and I added view (SurfaceView - CameraPreview.java) to it like that
MyActivity.java
rootView = findViewById(android.R.id.content).getRootView();
viewCircleOfInterest = rootView.findViewById(R.id.view_circle_of_interest);
viewCircleOfInterest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(NewWayMeasurementActivity.this, "circle a", Toast.LENGTH_SHORT).show();
}
});
(...)
((FrameLayout) rootView.findViewById(R.id.camera_view)).addView(cameraPreview);
(...)
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
}
On CameraPreview.java in my init() method I do like that:
(...)
viewCircleOfInterest = ((NewWayMeasurementActivity)activity).viewCircleOfInterest;
viewCircleOfInterest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "circle aa", Toast.LENGTH_SHORT).show();
}
});
I also set onClickListener on my ViewCircleOfInterest.java
public ViewCircleOfInterest(SurfaceView surfaceView) {
super(surfaceView.getContext());
this.surfaceView = surfaceView;
setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(surfaceView.getContext(), "circle aaa", Toast.LENGTH_SHORT).show();
}
But unfortunately non of this method works, I still cannot receive onclicks from my custom view. How to solve that? I hope my question and code is understandable.
Make these changes in your code and check if it works.
my_layout.xml
<FrameLayout
android:id="#+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/rl"
android:layout_alignParentTop="true"
android:background="#bbb">
<abc.x.y.ViewCircleOfInterest
android:id="#+id/view_circle_of_interest"
android:layout_width="wrap_content"
android:clickable="true"
android:layout_height="wrap_content"/>
</FrameLayout>
MyActivity.java
((FrameLayout)rootView.findViewById(R.id.camera_view)).addView(cameraPreview,0);
Here we are adding cameraPreview at 0 index so your custom view is on top now and that should receive clicks.
I am building an music player. Whenever the user will click on a ImageView, a dialog will come. The dialog will have a switch in it. I wanted to say that whenever the user switches on the switch to on, I wanted to change the ContentView of MainActivity.java to a custom layout file. How can I do that.
Any suggestions would be accepted..
Thanks
I would add a callback to your Dialog that you simply invoke to set your content, something like
class MyDialog(val switchChangedCallback: (Boolean) -> Unit) : DialogFragment() ...
When you change the switch, invoke the callback and handle the result in the Activity:
val dialog = MyDialog(switchChangedCallback = { isOn ->
if (isOn) {
setContentView(R.layout.abc)
} else {
setContentView(R.layout.def)
}
})
dialog.show(supportFragmentManager, MyDialog::class.java.name)
You may need to check that the callback survives app rotation!
You can try below code...
Dialogue :
Dialog dialogue = new Dialog(MainActivity.this);
dialogue.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogue.setCancelable(false);
dialogue.setContentView(R.layout.dialogue);
Switch mySwitch = (Switch) dialogue.findViewById(R.id.MYSWITCH);
Button btnClose = (Button) dialogue.findViewById(R.id.CLOSEBTN);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
setContentView(R.layout.layout1);
}
else{
setContentView(R.layout.layout2);
}
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialogue.dismiss();
}
});
dialogue.show();
dialogue.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="16dp"
android:orientation="vertical"
android:layout_height="match_parent">
<Switch
android:id="#+id/MYSWITCH"
android:text="Change Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/CLOSEBTN"
android:text="Close"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
as your question says you need a switch inside dialogue , so we need to create a custom dialogue layout and apply it to dialogue !
I'm trying add a listener to a ImageButton that is on a widget but i can't.
My widget xml is:
<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="#09C"
android:padding="#dimen/widget_margin"
>
<ImageButton
android:id="#+id/activityCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#09C"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Hecho"/>
</RelativeLayout>
The method that i need execute when the imageButton "activityCheck" is pressed is the next:
public void changeColor(View view) {
Log.d("changeColor", "PASE POR AQUĆ");
ImageButton button = view.findViewById(R.id.activityCheck);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
_changeColor(v);
}
});
}
How can I do it?
in layout add
android:clickable="true"
in ImageButton tag.
this will made your image clickable and then it will detect any click events.
I'm wanting a list of clickable textviews this is the xml I have
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/marque_scrolling_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:padding="16dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Create a Marquee (Scrolling Text) in Android Using TextView. Android Marquee (Scrolling Text) Tutorial with Example"
android:textSize="24sp" />
</LinearLayout>
Would like a list of items all clickable
This is the code I'm using:
TextView marque1 = (TextView) this.findViewById(R.id.marque_scrolling_text);
marque1.setSelected(true);
marque1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
System.out.println("dhgjgf jfgsfjhsgfsjfgdfjh");
}
});
Is this possible?
If you can't use ListView then you can dynamically create and add the TextViews yourself!
I assume you have an array of Strings you want to show as TextViews:
String[] strings = {"TextA", "TextB"};
Get the Layout that will contain all the TextViews:
LinearLayout layout = (LinearLayout) findViewById(R.id.listLayout);
// you will need to set an id for the layout in the xml
Then iterate through the list, create a new TextView for each String, add the onClickListener and do whatever you want with it (like changing text colour), and then add it to the Layout:
for(String s : strings)
{
TextView newTextView = new TextView(this);
newTextView.setText(s);
newTextView.setTextColor(#0066ff); // for example
newTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
System.out.println("dhgjgf jfgsfjhsgfsjfgdfjh");
}
});
layout.addView(newTextView);
}
I tried to make ImageView(bul1) disappear when ImageView(Seethrough) is pressed. I get a nullpointer error when i try to run this code. What is wrong with it?
JAVA code
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);
final ImageView view1 = (ImageView) findViewById(R.id.bul1);
seethrough1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(view1.getVisibility() == View.VISIBLE)
{
view1.setVisibility(View.INVISIBLE);
}
}
});
}
XML code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingBottom="6dp"
android:src="#drawable/gun"
android:clickable="true"
android:id="#+id/Seethrough"
android:onClick="next"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="#drawable/bullet"
android:id="#+id/bul1"
/>
</LinearLayout>
You need to reconcile seethrough's onClickListener with its onClick XML attribute. I'd suggest removing this line from the xml:
android:onClick="next"
and placing the code inside your next method (if you have one)
public void next (View v){
some code
}
behind or before your visibility checking if, whichever suits you more:
#Override
public void onClick(View v) {
//place some code here
if(view1.getVisibility() == View.VISIBLE){
view1.setVisibility(View.INVISIBLE);
}
//or here
}
I think their is problem with out xml code, please try writing xml as follows,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingBottom="6dp"
android:src="#drawable/gun"
android:clickable="true"
android:id="#+id/Seethrough"
android:onClick="next"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="#drawable/bullet"
android:id="#+id/bul1"
/>
</LinearLayout>
If it return NullPointerExeption, I think your ImageView is Null, because
setContentView(R.layout.activity_main);
and activity_main.xml is not like contents of your post, check name of layout and try again.
I found out that I was supposed to declare the imageviews inside the method instead of before.
like this
public void onClick(View v) {
ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);