So im currently doing a project that is due soon.
it consists of making a phone app with number buttons etc..
One of the questions that the teacher asked us is to add a Imageview and use it as a button to clear the TextView when there is 10 characters or over.
any help is appreciated, I'm still very new to Android coding so my knowledge is very bad haha
here is my Java code:
Button no1,no2,no3,no4,no5,no6,no7,no8,no9,no0;
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
no1=(Button) findViewById(R.id.no1);
no2=(Button) findViewById(R.id.no2);
no3=(Button) findViewById(R.id.no3);
no4=(Button) findViewById(R.id.no4);
no5=(Button) findViewById(R.id.no5);
no6=(Button) findViewById(R.id.no6);
no7=(Button) findViewById(R.id.no7);
no8=(Button) findViewById(R.id.no8);
no9=(Button) findViewById(R.id.no9);
no0=(Button) findViewById(R.id.no0);
tv1=(TextView)findViewById(R.id.tv1);
no1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"1");
}
});
no2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"2");
}
});
no3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"3");
}
});
no4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"4");
}
});
no5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"5");
}
});
no6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"6");
}
});
no7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"7");
}
});
no8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"8");
}
});
no9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"9");
}
});
no0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(tv1.getText()+"0");
}
});
}
}
And here Is my XML:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/tv1"
android:layout_width="262dp"
android:layout_height="85dp"
android:layout_marginTop="4dp"
android:gravity="center|right"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:maxLength="10"
android:inputType="phone" />
<ImageView
android:id="#+id/btnclear"
android:layout_width="79dp"
android:layout_height="73dp"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:clickable="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/phone_on" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="345dp"
android:layout_height="404dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.994">
<Button
android:id="#+id/no7"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="7"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no4"
app:layout_constraintEnd_toStartOf="#+id/no8"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/no8"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="8"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no5"
app:layout_constraintEnd_toStartOf="#+id/no9"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no7"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/no9"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="9"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no8"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/no4"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="4"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no1"
app:layout_constraintEnd_toStartOf="#+id/no5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/no7" />
<Button
android:id="#+id/no5"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="5"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no2"
app:layout_constraintEnd_toStartOf="#+id/no6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no4"
app:layout_constraintTop_toBottomOf="#+id/no8" />
<Button
android:id="#+id/no6"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="6"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no5"
app:layout_constraintTop_toBottomOf="#+id/no9" />
<Button
android:id="#+id/no1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="1"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/etoile"
app:layout_constraintEnd_toStartOf="#+id/no2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/no4" />
<Button
android:id="#+id/no2"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="2"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/no0"
app:layout_constraintEnd_toStartOf="#+id/no3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no1"
app:layout_constraintTop_toBottomOf="#+id/no5" />
<Button
android:id="#+id/no3"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="3"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/Hashtag"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no2"
app:layout_constraintTop_toBottomOf="#+id/no6" />
<Button
android:id="#+id/etoile"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="*"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/no0"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/no1" />
<Button
android:id="#+id/no0"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="0"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/Hashtag"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/etoile"
app:layout_constraintTop_toBottomOf="#+id/no2" />
<Button
android:id="#+id/Hashtag"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/no0"
app:layout_constraintTop_toBottomOf="#+id/no3" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
And heres a Screenshot of my design:
Design part
Thanks for the help
First, you will need to make your ImageView clickable in the XML:
android:clickable="true"
You can then set the OnClickListener on the ImageView like you did for the rest of your Buttons.
Related
I'd like to show the progress bar, generate the computation, then after I do that I'd like the progressbar to disappear and I want to set the textview to the result of my generate result. Yet when I do the below, the progress bar never displays, yet the textview updates appropriately. I originally wanted to do this using AsyncTask but since thats been deprecated I wanted to try a modern method but I'm afraid I can't get it to work. Can anyone see what is wrong?
genResult.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new Thread(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
// do onPreExecute stuff
pb.setVisibility(View.VISIBLE);
}
});
// do your stuff
boolean res = exp.genResult();
long startTime = System.nanoTime();
while(System.nanoTime()-startTime<50000){
}
result.outcomes.add(res);
runOnUiThread(new Runnable() {
public void run() {
// do onPostExecute stuff
pb.setVisibility(View.INVISIBLE);
lastRes.setText(Boolean.toString(res));
}
});
}
}).start();
}
});
Below is the XML file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/gen_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="generate result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.941" />
<TextView
android:id="#+id/title_bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
app:layout_constraintBottom_toTopOf="#+id/gen_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.153" />
<Button
android:id="#+id/detail_bin_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View Details"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.941"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.005" />
<Button
android:id="#+id/backbutton_bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="go back"
app:layout_constraintEnd_toStartOf="#+id/detail_bin_button"
app:layout_constraintHorizontal_bias="0.105"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/lastresultbin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginLeft="36dp"
android:text="lastresult"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/backbutton_bin"
app:layout_constraintVertical_bias="0.313" />
<TextView
android:id="#+id/probabilityViewer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="208dp"
android:text="Probability"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.65"
app:layout_constraintStart_toEndOf="#+id/lastresultbin"
app:layout_constraintTop_toBottomOf="#+id/detail_bin_button" />
<TextView
android:id="#+id/plaintext_lastres_bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="Last Result"
app:layout_constraintBottom_toTopOf="#+id/lastresultbin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.105"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/backbutton_bin"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/plaintext_prob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="164dp"
android:layout_marginEnd="44dp"
android:layout_marginRight="44dp"
android:text="Probability of Success"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/detail_bin_button" />
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_centerInParent="true"
android:layout_marginTop="148dp"
android:indeterminate="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
The issue is that System.nanoTime() used in the while loop was too quick to complete. When we switch to using System.currentTimeInMillis() we can notice a difference in the delay and thus the spinner shows and hides as expected.
I have two EditText in MyActivity. Here I have provided UI XML.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button_insta"
android:layout_width="135dp"
android:layout_height="0dp"
android:layout_marginBottom="33dp"
android:text="Download"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/editUrli"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="64dp"
android:layout_marginBottom="18dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textColor="#0C0C0C"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView2"
android:layout_width="177dp"
android:layout_height="39dp"
android:layout_marginEnd="81dp"
android:layout_marginRight="81dp"
android:text="Paste Instagram video link to download "
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/imageView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView3"
app:layout_constraintTop_toTopOf="#+id/imageView3" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="78dp"
android:layout_height="43dp"
android:layout_marginStart="41dp"
android:layout_marginLeft="41dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="18dp"
android:layout_marginRight="18dp"
android:src="#drawable/insta"
app:layout_constraintEnd_toStartOf="#+id/textView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/card"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_margin="8dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editUrl"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="64dp"
android:layout_marginBottom="18dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textColor="#0C0C0C"
app:layout_constraintBottom_toTopOf="#+id/button_download"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_download"
android:layout_width="135dp"
android:layout_height="0dp"
android:layout_marginBottom="33dp"
android:text="Download"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/fblogo"
android:layout_width="95dp"
android:layout_height="39dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="13dp"
android:layout_marginEnd="18dp"
android:layout_marginRight="18dp"
android:layout_marginBottom="13dp"
android:contentDescription="TODO"
android:src="#drawable/fb"
app:layout_constraintBottom_toTopOf="#+id/editUrl"
app:layout_constraintEnd_toStartOf="#+id/textView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="75dp"
android:layout_marginRight="75dp"
android:layout_marginBottom="84dp"
android:text="Paste FB video link to download"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/button_download"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/fblogo"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
In my Activity java code has this button click code
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
inputURl = findViewById(R.id.editUrl);
editUrlinsta = findViewById(R.id.editUrli);
BtnDownload = (Button) findViewById(R.id.button_download);
downloadinsta = (Button) findViewById(R.id.button_insta);
BtnDownload.setOnClickListener(this);
downloadinsta.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_download:
try{
final FacebookDownloader downloaders = new FacebookDownloader(Download.this,inputURl.getText().toString());
downloaders.DownloadVideo();
inputURl.getText().clear();
} catch(Exception e) {}
break;
case R.id.button_insta:
final InstaDownloader downloaderInsta = new InstaDownloader(Download.this,editUrlinsta.getText().toString());
downloaderInsta.DownloadVideo();
editUrlinsta.getText().clear();
break;
default:
break;
}
}
The problem is The first button which I have named button_download is working as expected. But the second button button_insta not working as expected. what I want to do, is When one of the buttons clicks, getting data from EditText. The button_download works fine. the button_insta is not working fine.
I couldn't get value from editUrli when the button click. But same code works for button_download and editUrl
Try to separate those button click function like so :
BtnDownload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
final FacebookDownloader downloaders = new FacebookDownloader(Download.this,inputURl.getText().toString());
downloaders.DownloadVideo();
inputURl.getText().clear();
} catch(Exception e) {}
}
});
downloadinsta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final InstaDownloader downloaderInsta = new InstaDownloader(Download.this,editUrlinsta.getText().toString());
downloaderInsta.DownloadVideo();
editUrlinsta.getText().clear();
}
});
I have to program a TicTacToe app:
This is the code of the activity_main.xml:
(It's a simple 3x3 field of TextViews on a black background)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:screenOrientation="portrait"
tools:context=".MainActivity">
<androidx.gridlayout.widget.GridLayout
android:layout_width="294dp"
android:layout_height="286dp"
android:background="#000000"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<TextView
android:id="#+id/tictactoe_center_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_left"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="1" />
<TextView
android:id="#+id/tictactoe_center_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_right"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_row="1" />
<TextView
android:id="#+id/tictactoe_center_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_center"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_row="1" />
<TextView
android:id="#+id/tictactoe_top_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:ems="10"
android:enabled="false"
android:focusable="true"
android:onClick="click_top_left"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="0"
tools:text="#string/game_playersymbol_x" />
<TextView
android:id="#+id/tictactoe_top_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_top_center"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tictactoe_top_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_top_right"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tictactoe_bottom_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_left"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="2" />
<TextView
android:id="#+id/tictactoe_bottom_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_right"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_row="2" />
<TextView
android:id="#+id/tictactoe_bottom_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_center"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_row="2" />
</androidx.gridlayout.widget.GridLayout>
<Button
android:id="#+id/button"
android:layout_width="213dp"
android:layout_height="61dp"
android:layout_marginBottom="56dp"
android:background="#drawable/roundbutton"
android:text="#string/menu_play_singleplayer"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#979797"
android:textSize="50sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.066" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the code of the MainActivity.java:
package com.example.tictactoe;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private int counter = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public void click_top_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_left)); }
public void click_top_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_center)); }
public void click_top_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_right)); }
public void click_center_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_left)); }
public void click_center_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_center)); }
public void click_center_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_right)); }
public void click_bottom_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_left)); }
public void click_bottom_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_center)); }
public void click_bottom_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_right)); }
public void click_action(TextView tv) {
if ((counter%2)==0)
tv.setText("O");
else
tv.setText("X");
counter++;
}
}
I've set the android:clickable to true.
I've set a onclick listener.
I simply cannot find my mistake...
I would appreciate any kind of help!
You have set to all the TextViews:
android:enabled="false"
Why?
Change it to:
android:enabled="true"
first give an id to your textview then Define a Sample like Textview textview find it in java and use setOnClickListener() in onCreate method just like below.It will definitely works.
textveiw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { }});
My app is always crashing because of this error. It says:
com.rengwuxian.materialedittext.MaterialEditText cannot be cast to android.view.ViewGroup.
I'm a beginner who's looking for some code on YouTube, but I can't figure it out.
<ImageView
android:id="#+id/imageView2"
android:layout_width="243dp"
android:layout_height="166dp"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo" />
<TextView
android:id="#+id/textView"
android:layout_width="102dp"
android:layout_height="24dp"
android:layout_marginStart="8dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="8dp"
android:text="USERNAME"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.259"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2" />
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/usernameTIL"
android:layout_width="251dp"
android:layout_height="46dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.527"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<com.rengwuxian.materialedittext.MaterialEditText
android:layout_width="268dp"
android:layout_height="36dp"
android:hint="username" />
</com.rengwuxian.materialedittext.MaterialEditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="PASSWORD"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.255"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/usernameTIL"
app:layout_constraintVertical_bias="0.018" />
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/passwordTIL2"
android:layout_width="253dp"
android:layout_height="47dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.535"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<CheckBox
android:id="#+id/rememberCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:text="Remember Me?"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/passwordTIL2" />
<Button
android:id="#+id/signInBTN"
android:layout_width="145dp"
android:layout_height="49dp"
android:layout_marginTop="28dp"
android:background="#color/signInBTN"
android:text="SIGN IN"
android:textColor="#android:color/white"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rememberCB" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Dont't have an Account?"
android:textColor="#android:color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.323"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/signInBTN" />
My Java:
public class sign_in extends AppCompatActivity {
EditText usernameTIL, passwordTIL;
Button signInBTN;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
usernameTIL = (MaterialEditText) findViewById(R.id.usernameTIL);
passwordTIL = (MaterialEditText) findViewById(R.id.passwordTIL2);
signInBTN = (Button) findViewById(R.id.signInBTN);
//Init FireBase
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference table_user = database.getReference("User");
signInBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final ProgressDialog mDialog = new Progress codeDialog(sign_in.this);
mDialog.setMessage("Please wait...");
mDialog.show();
table_user.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
//check if user doesn't have an account
if (dataSnapshot.child(usernameTIL.getText().toString()).exists()) {
//get user information
mDialog.dismiss();
User user = dataSnapshot.child(usernameTIL.getText().toString()).getValue(User.class);
if (user.getPassword().equals(passwordTIL.getText().toString())) {
Intent homeIntent = new Intent(sign_in.this, HomeScreen.class);
Common.curentUser = user;
startActivity(homeIntent);
finish();
} else {
Toast.makeText(sign_in.this, "Sign In Failed !!", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(sign_in.this, "User not found !!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
});
}
}
MaterialEditText is an EditText
public class MaterialEditText extends AppCompatEditText {
it can not contain any widgets. In your layout you have
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/usernameTIL"
android:layout_width="251dp"
android:layout_height="46dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.527"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<com.rengwuxian.materialedittext.MaterialEditText
android:layout_width="268dp"
android:layout_height="36dp"
android:hint="username" />
</com.rengwuxian.materialedittext.MaterialEditText>
a MaterialEditText containing another MaterialEditText, which is causing your app to crash for ClassCastException
According to this library's sample code, you must cast your MaterialEditText to EditTextlike this:
usernameTIL = (EditText) findViewById(R.id.usernameTIL);
passwordTIL = (EditText) findViewById(R.id.passwordTIL2);
I am new to android programming world, I have designed as a layout with username, password AS edittext control and "forgot password" as textview control, on click/tap of "forgot password", I want to open a new activity.
This is what I am trying, looking forward for your help.
My Application's xml and java code is mentioned below.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light">
<TextView
android:id="#+id/txtVwAccountLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Account Login"
android:textColor="#android:color/background_light"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.057" />
<EditText
android:id="#+id/eTxtUserName"
android:layout_width="288dp"
android:layout_height="48dp"
android:background="#android:color/background_light"
android:ems="10"
android:hint=" Username"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.593"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />
<EditText
android:id="#+id/eTxtPassword"
android:layout_width="290dp"
android:layout_height="45dp"
android:background="#android:color/background_light"
android:ems="10"
android:hint=" Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.606"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.535" />
<TextView
android:id="#+id/txtVwForgetPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password ?"
android:textColor="#android:color/background_light"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.854"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.632" />
<Button
android:id="#+id/btnLogin"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:text="Login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.779" />
<Button
android:id="#+id/btnRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:background="#android:color/holo_blue_light"
android:text="Register"
android:textColor="#android:color/background_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.662" />
</android.support.constraint.ConstraintLayout>
Login.java
public class Login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final TextView tvForgotPwd = (TextView) findViewById(R.id.txtVwForgetPassword);
tvForgotPwd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Login.this, "you clicked me", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(view.getContext(),PatientSearch.class);
startActivityForResult(myIntent, 0);
}
});
final Button btnRegister = (Button) findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),MainActivity.class);
startActivityForResult(myIntent,0);
}
});
Even though this question has been asked so many times, I decided to answer anyway. This will work:
tvForgotPwd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(Login.this, PatientSearch.class);
startActivity(myIntent);
}
});
In your onCreate() method, set an OnClickListener callback on your TextView. In the callback, use an Intent to start the desired activity.
Here is an exemple of code:
yourTextView= (TextView) findViewById(R.id.txtVwForgetPassword);
yourTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://youraddress.com"));
startActivity(intent);
}
});
Probably it is necessary to try a method
startActivity (new Intent (Activity.this, Open Activity.class));