i was wondering how can i make a textview viewable where the user can click on it and when he click on it the textview creates another page where the text is by her own and the user can select the textview now, because i have more than 30 textviews and i want to make each of them when they get clicked they have their own layout where user can select them now and add more stuff like sharing the textview and this is all in one layout for each textview alone
i hope you can help me and thanks in advance
Make a new activity and use put/get extra to pass on data to new activity.
try following
http://developer.android.com/training/basics/firstapp/index.html
you should create list view
and in on item click of the list you can simply start another activity which will have layout according to the item you have clicked.
click here
and there onitemclick you can start new activity
Intent intent = new Intent(this, NewActivity.class);
intent.purStringExtra("key",rowItems.get(position));
startActivity(intent);
try it and ask if you have any doubt or it is not clear
You can use this code to make your TextView visible on click event
myTextView.setVisibility(View.VISIBLE);
try using Buttons instead of TextViews for getting TextViews...
example
you have Buttons "A" and "B" and want to show Button "B" only when when A Clicked and then want to show TextView "C" only when when B Clicked
in this way (A->B->C )
then try using these codes:
only important parts are written...
in activity_main.xml :
<Button
android:id="#+id/button_A"
android:onClick="do_A"/>
<Button
android:id="#+id/button_B"
android:onClick="do_B"/>
<TextView
android:id="#+id/Textview_C" />
in MainActivity.java :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button A = findViewById(R.id.button_A);
Button B = findViewById(R.id.button_B);
TextView C = findViewById(R.id.Textview_C);
B.setVisibility(View.GONE);
C.setVisibility(View.GONE);
}
public void do_A (View v){
B.setVisibility(View.VISIBLE);
}
public void do_B (View v){
C.setVisibility(View.VISIBLE);
}
}
Related
I'm a beginner with Android development and want to create a simple onClick event to bring me to a blank activity page. I have named my new activity page InsurancePage and added a button called insurance to my xml file. When I click on this button I want to to bring me to the InsurancePage.
<Button
android:id="#+id/insurance"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="46.24"
android:background="#drawable/curvebutton"
android:lines="1"
android:text="#string/insurance"
android:textColor="#ffffff"
android:onClick="onClickbtnInsurance" />
To add on click event to button you can look at the next link:
how to add button click event in android studio or android eclipse button OnClick event
To start new activity on button click you can look at the next link:
How to start new activity on button click
Next, you can just search in Google before :)
Let's say that you have a button called myButton and this is in you MainActivity class:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(Bundle savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton = findViewById(R.id.new_button);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, InsurancePage.class));
}
}
You can read more about activities here : http://developer.android.com/training/basics/firstapp/starting-activity.html
Please solve this problem in android studio, I am new to app development.
I want to create 3 buttons in a single page and navigate each button to each different page.
I need code for java i.e "Mainactivity.java"
I have declared 3 button id's
I have set everything in app manifest.
I am able to navigate only single button at once but how can I arrange all three buttons for navigation?
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonWRGL = (Button)findViewById(R.id.buttonWRGL);
Button buttonHNK = (Button)findViewById(R.id.buttonHNK);
Button buttonKZP = (Button)findViewById(R.id.buttonKZP);
buttonWRGL.setOnClickListener(this);
buttonHNK.setOnClickListener(this);
buttonKZP.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonWRGL:
break;
case R.id.buttonHNK:
break;
case R.id.buttonKZP:
break;
}
}
}
Your question seems unclear, but I suppose that you are asking how to load another layout/activity/fragment by clicking on a button.
Well, it depends on which of the three actions you want to do:
1) in order to load another layout, you need to inflate the new layout in your view; in order to do that you need to clear the actual layout and inflate the new one.
Here's some sample code:
//you may change it to whichever layout you used
LinearLayout ll = (LinearLayout) findViewById(R.id.mainLayout);
//remove previous view
ll.removeAllViews();
//set the new view
setContentView(R.layout.new_layout);
2) in case you want to start a new activity, you need to use a Intent and load it.
Sample code:
//create the new intent; it will refer to the new activity
Intent intent = new Intent(this, NewActivity.class);
//pass any data to the new activity; cancel this line if you don't need it
intent.putExtra(extra_title, extra)
//start the new activity
startActivity(intent);
3) in case you want to change fragment, you need to perform a transaction.
Sample code:
//create the new fragment
Fragment newFragment = new MyFragment();
//start a fragment transaction
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//replace the old fragment with the new
transaction.replace(R.id.frame, newFragment).commit();
Hope this helps; if not, try to edit your question in order to clarify what you mean.
EDIT:
You should add a new OnClickListener to each button, but I would do it differently than you are doing right now.
I would do something like this sample code:
buttonWRGL.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(this, NewActivity1.class);
startActivity(intent);
}
});
For each button. In this code, I directly attach a specific OnClickListener to the button; it will contain the intent that you need.
You can copy this in every button, even 10k buttons, you just need to change the activity name inside the intent declaration with the activity that you want to launch.
I am trying to display a button on my android app but everytime i run the app it crashes. i realise this is because i use setContentView multiple times? I dont understand how it works, and dont understand how i can fix this problem so my button will display. my code is below.
public class MainActivity extends Activity {
Draw draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new Draw(this);
draw.setBackgroundColor(Color.BLUE);
setContentView(draw);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
setContentView(l);
l.addView(new Draw(this));
//setContentView(R.layout.activity_main);
setUpBlockBtn();
}
private void setUpBlockBtn(){
setContentView(R.layout.activity_main);
Button addBlockButton = (Button)findViewById(R.id.btnBlock);
addBlockButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("DemoButtonApp", "you clicked the button");
//finish();
}
});
}
You try to access Button from android xml layout but you do not set this layout in Activity.
Put you button activity_main.xml and use this button in your activity.
Thanks
You can create one more layout and add Draw and Linear layout to that layout.
Something like this.
LinearLayout l1=new LinearLayout(this);
l1.setOrientation(LinearLayout.VERTICAL);
l1.addView(draw);
l1.addView(l2) // your linearLayout.
setContentView(l1)
Remember you can't use setContentView more than one time.
There should be top layout which includes subview and other layouts and then you can add that layout to your activity.
I need to link the button with the page (not the main page) like when I click on the button to go to the page for example (location page)?
private void setupLocationPageButton() {
Button LocationPageButton = (Button) findViewById(R.id.btnLocationPage);
LocationPageButton.setOnClickListener(new View.OnClickListener()
If your goal is to open another activity this is what you are going to want to do.
In your xml file you are going to want something like this
...
<Button
android:id="#+id/activity_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClicked"
android:text="#string/text_of_button" />
...
Then in your activity you want
public void onButtonClicked(View view) {
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);
}
what i think you trying to do: is when you click on button it's change the MainActivity
Button LocationPageButton = (Button) findViewById(R.id.btnLocationPage);
LocationPageButton.setOnClickListener(new View.OnClickListener({
public void onClick(View _view) {
Intent i = new Intent(MainActivity.this,TheActivityTheclassNameYouWannGoTo.class);
startActivity(i);
}
}));
but first you have to creat activity and class inherit Activity like MainActivity
initialize the class in AndroidMainfest.xml
i hope this help you
You can use fragment, each fragment contains a specific page, see this for Android fragment.
I'm a beginner to Java and I want to validate an EditText. What I have in mind: my editText has to match "helloworld". When you press a button this has to be validated. If this is true--> go to a new class in which I have a setContentView to display a new layout.
If the text which I have just typed does not match "helloworld", it should do nothing. It seems very easy but since I'm a beginner you would help me BIGTIME!
Here's most of the logic handled. You will need to fill in your actual layout id's and make your launch intent. Put this code in your onCreate method in the activity with the layout that contains the edit text box
EditText editText = (EditText)findViewById(R.id.editTextBox);
Button btn = (Button)findViewById(R.id.checkBtn);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
if(editText.getText().toString().equalsIgnoreCase("helloworld")){
//Launch activity with new view
}
}
});
In an activity (or android class) you have to get the instance of your EditText. Your edit text has an id, and you can get it using R. R is the resources for your app.
EditText t = (EditText)findViewById(R.id.<Name of your textfield>);
Then you can get the value of that textfield and compare it
t.getText().toString().equals("helloworld");
will return true or false. If you dont care about the case of the letters use
t.getText().toString().toLowerCase().equals("helloworld");
you will need an onClickListener for your button, check out the android api
http://developer.android.com/reference/android/view/View.OnClickListener.html
in your onCreate, when declaring your submit button, add a listener
Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(submitListener);
make a new onClick listener and fire an Intent to start a new activity
View.OnClickListener submitListener = new View.OnClickListener() {
public void onClick(View v) {
//if string matches helloworld fire new activity
Intent newActivity = new Intent();
startActivity(newActivity);
}
};
// create a reference to the EditText in your layout
EditText editText = (EditText)findViewById(R.id.editTextIdInLayout);
// create a reference to the check Button in your layout
Button btn = (Button)findViewById(R.id.buttonIdInLayout);
// set up an onClick listener for the button reference
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String userInput = editText.getText().toString(); // get the user input
if (userInput.equals("helloworld") // see if the input is "helloworld"
{
setContentView(R.layout.newLayout); // change the content view
}
}
});