Convert Edittext to readonly and Make it editable on Button Press - java

i got this nw. Before clicking on the button, my EditText is just viewable:
After button press, it should convert to an editable field:
I want the EditText field line to disappear and become read only and on button click make it editable. But this just fades the line and also the text inside it loses it visual appearance.
Here's my code:
et1.setEnabled(false);
et1.setFocusable(true);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
et1.setEnabled(true);
et1.requestFocus();
}
});

Your XML should look like this,
<?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:gravity="center"
android:orientation="vertical">
<EditText
android:id="#+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Now, add the following code to your activity class.
//init the views.
et1.setVisiblity(GONE);
tv1.setVisiblity(VISIBLE);
//if the textView is shown, the btn click will hide it and show editText
//instead, and same happens for editText
btn1.setOnClickListener(new View.OnClickListener(){
public void onClick(){
if(et1.getVisiblity() != VISIBLE) {
et1.setVisbility(VISIBLE);
tv1.setVisibility(GONE);
} else {
et1.setVisbility(GONE);
tv1.setVisibility(VISIBLE);
}
} });
P.S: This isn't executable code, although, hopefully it will get you the idea.

Related

How to change a layout in same activity by clicking a Button?

Change a layout in same Activity when click on play button then layout show pause Button at the same place in Android Studio music player app.
How it is possible?
I'm a beginner so I don't know how to use method about it?
I cannot get an answer.
I understand from your question that when your user taps pause button, you should hide pause button and show play button and vice-versa. This is relatively simple to achieve. Just create a layout with a linear layout with two buttons like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:padding="5dp">
<Button
android:id="#+id/btnPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:text="Play"
android:padding="5dp"/>
<Button
android:id="#+id/btnPause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="Pause"/>
</LinearLayout>
Now set on click listner to both of the buttons and handle button visibility within that like this :
Button btnPlay = barcodeDialog.findViewById(R.id.btnPlay);
Button btnPause = barcodeDialog.findViewById(R.id.btnPause);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnPlay.setVisibility(View.GONE);
btnPause.setVisibility(View.VISIBLE);
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnPlay.setVisibility(View.VISIBLE);
btnPause.setVisibility(View.GONE);
}
});
This way you can handle the visibility of your Play and Pause buttons. Reply if you need more information.

New Value EditText not being displayed , no errors whatsoever

EDIT : By the way , if i hard code edt4.setText("any text") IN THE SCOPE , it shows the value
EDIT2: tried rebuilding/cleaning project , could this be a bug in settext method, it looks like im doing it right . When i look at other code with settext
EDIT3: startactivityforresult might be my answer ? , i openend another question related to what i wanna try out , i still havent found another solution , stuck with this for a week now : (
this is the link to my question Can i use startActivityForResult , with one activity?
EDIT4 : now trying it with making views visible and invisible
I have read every single article about anything related on stackoverflow , dreamincode etc, and i cant find anybody that knows the answer for my problem.
I have a qr scanner and after a successful scan the result needs to be put in a EDIT TEXT field with name editText4 , my code throws no errors but it is NOT displaying any value .
I've posted on different forums but to no avail (https://www.dreamincode.net/forums/topic/412000-settext-is-not-showing-set-value-in-edittext-in-gui/page__st__15__gopid__2372214&#entry2372214)
, as you can see the commented code . That's pretty much also what I've tried , I think I somehow got to get my handle result method inside of the scope.
NOTE : Log.v outputs the result very well , but when i try anything else with the result it just don't works or being displayed
ps: I m beginner at java
Thanks for your help
here's my mainactivity
` public class MainActivity extends Activity implements
ZXingScannerView.ResultHandler {
Button sendButton;
EditText edt4;
EditText edt2;
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt4 = findViewById(R.id.editText4);
mScannerView = findViewById(xmlScannerView);
mScannerView.setFormats(ZXingScannerView.ALL_FORMATS);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
EditText delete2;
Button button3;
button3 = findViewById(R.id.button3);
delete2 = findViewById(R.id.editText2);
final EditText finalEdittext = delete2;
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Clear EditText
finalEdittext.getText().clear();
}
});
EditText delete4;
Button button4;
delete4 = findViewById(editText4);
button4 = findViewById(R.id.button4);
final EditText finalEdittext1 = delete4;
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Clear EditText
finalEdittext1.getText().clear();
}
});
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
public void onClick(View v){
RelativeLayout someid = findViewById(R.id.someId);
mScannerView.setVisibility(View.VISIBLE);
someid.setVisibility(View.INVISIBLE);
}
// EditText edt4;
#Override
public void handleResult(final Result result) {
//handle result
Log.v("handleResult", result.getText());
edt4 = findViewById(editText4);
edt4.setText(result.getText());
//edt4.setText(String.valueOf(result.getText()));
// edt4.setText(new
StringBuilder().append("Resultaat:").append(result.getText()).toString());
}
`
this is xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="#mipmap/ic_launcher_foreground">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:id="#+id/xmlScannerView"
android:visibility="gone"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<RelativeLayout
android:id="#+id/someId"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_marginTop="67dp"
android:ems="10"
android:hint="#string/scan_locatie"
android:inputType="text"
android:text=""
tools:backgroundTint="#android:color/holo_red_light" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText4"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_red_light"
android:onClick="onClick"
android:text="#string/scan_qr"
tools:text="scan qr code" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="61dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="197dp"
android:ems="10"
android:hint="#string/scan_order"
android:inputType=""
android:visibility="visible"
tools:backgroundTint="#android:color/holo_red_light" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_red_light"
android:onClick="onClick"
android:text="#string/scan_qr"
tools:text="scan qr code" />
<Button
android:id="#+id/sendButton"
android:layout_width="157dp"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="72dp"
android:background="#android:color/holo_red_light"
android:text="#string/button"
tools:text="Versturen.." />
<Button
android:id="#+id/button3"
android:layout_width="40dp"
android:layout_height="38dp"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignParentEnd="true"
android:background="#android:drawable/ic_delete" />
<Button
android:id="#+id/button4"
android:layout_width="39dp"
android:layout_height="37dp"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignParentEnd="true"
android:background="#android:drawable/ic_delete" />
</RelativeLayout>
EDIT:5 also changed XML , and main , still not working :(
try removing recreate();
you are recreating the activity after setting the value, so the previous value is lost
I think result.getText() is a String anyway, so you could change the following line:
edt4.setText(String.valueOf(result.getText()));
To:
edt4.setText(result.getText());
After that you call updateScannerData, which is also writing to the EditText once again, but from the UI-Thread. I would recommend to remove the UI-Thread code as well since I assume the code runs on that Thread anyway.
You can either use
edt4.clear();
or
edt4.setText("");

Trouble identifying the type of widget to expand/collapse an area

Consider the widget hello:
When clicked, it opens the area at the bottom with additional widgets (text, buttons, etcetera):
What is this kind of expand/collapse widget called?
One way to do this is to build your whole layout with the text and the buttons, but hide them initially by setting their visibility to gone or invisible. Then in code in the onClick listener of the "hello_text" text view, you can change their visibility to visible.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/hello_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_text" />
<TextView
android:id="#+id/the_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/my_text"
android:visibility="invisible" />
<LinearLayout android:id="#+id/btn_holder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="invisible">
<Button android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text1" />
<Button android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text2" />
</LinearLayout>
</LinearLayout>
Now in your activity or fragment you can do:
TextView the_text_view = (TextView) v.findViewById(R.id.the_text);
LinearLayout ll_btn_holder = (LinearLayout) v.findViewById(R.id.btn_holder);
TextView hello_text_view = (TextView) v.findViewById(R.id.hello_text);
hello_text_view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
the_text_view.setVisibility(View.VISIBLE);
ll_btn_holder.setVisibility(View.VISIBLE);
}
});

Creating a new instance of an Imagebutton with a button click

The app I am programming has a menu in which the user can pick a new building to build. The user will press "Buy" I would like this to create a new instance of the imagebutton that corresponds to that particular building. My current problem is that I only understand how to do this by setting up these in the xml file and setting them invisible until they buy it. I would like to dynamically create these buildings when they are bought. Is there a way to dynamically create an instance of an image button is this way? I don't really think including my source code is necessary here since I just need an example that works (most examples I have seen just don't work out). But if you would like to see some source code let me know.
Any help is appreciated. Thanks.
Code inside buy building button:
newColonyHut = new ImageButton(runGraphics.this);
newColonyHut.setImageResource(R.drawable.mainhut);
newColonyHut.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layout.addView(newColonyHut);
Here is the xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="0dp"
android:layout_marginRight="20dp" >
<Button
android:id="#+id/buyColonyHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_below="#+id/colonyHutPrice" />
</LinearLayout>
You can create ImageView and dynamically add to layout you specified.
sample:
in oncreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
final LinearLayout test = (LinearLayout)findViewById(R.id.test);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(your_image_here);
image.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
test.addView(image);
}
});
the activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >
<LinearLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/layer"
android:text="Button" />
</LinearLayout>
As you can see ImageView is created when the button is clicked in the layout and then add it to the LinearLayout dynamically.
The LayoutParams is where you set the height and width of your ImageView respected the its parent which is the LinearLayout

Android Beginner(Add Button)

I'm a beginner in android and making my first app. Here, I had taken 3 textViews and a button named Add. When I click on this button, I need to show the content of 2 textViews on third textView. I used eventListener and buttonClick event but it doesn't work. Please guide me with the code of Add button.
First create the xml as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:layout_weight="1"
android:id="#+id/singleEmployee"
android:background="#ffffff">
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextView"
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextView"
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
And then use this Java code to done your desire action:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView tv1,tv2,tv3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testing);
tv1 = findViewById(R.id.textView1);
tv2 = findViewById(R.id.textView2);
tv3 = findViewById(R.id.textView3);
tv1.setText("Hello");
tv2.setText("World");
Button add = findViewById(R.id.button1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv3.setText("");
String tvValue1 = tv1.getText().toString();
String tvValue2 = tv2.getText().toString();
tv3.setText("Value of First Text is: "+tvValue1+".And the value of second TextView is: "+tvValue2);
}
});
}

Categories

Resources