I am looking for a way to create a Toolbar likes a lot of apps with name and a photo.
I need to allow touch, if user touchs the area near the name or the image it will change activity.
Example:
You just need to put the ImageView and TextView inside your Toolbar, he is just a ViewGroup. For example:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="#dimen/abc_action_bar_default_height_material">
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/your_image_description"
android:src="#drawable/your_image"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/your_string" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
After that, you can set the click event on your activity:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "teste", Toast.LENGTH_LONG).show();
}
});
Run on the emulator to see the result:
Hope this helps.
Related
EDITED
The examples in google searches and the android documentation are basic, and my question is more complex.
What I do not know, is how to put multiple components together and turn it into a single component. CardView + LinearLayout + TextView + ImageView = Custom View.
If I have a basic example, of cardview with at least one button and a space for external content. It would be enough for me to figure out how to do the rest. This within the cardview extension class, because I also do not know how the extension will understand where to create the components
EDITED 2
Example
QUESTION
I was trying to create a method of using Cardview with an expandable button, and I was able to do this in XML. But I wanted to be able to create a library so I could reuse it in other cases.
I'll show you the source code of what I did.
Layout XML
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/cwlltitulobtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#### TITLE OF BUTTON ####"
android:textSize="18sp"
android:textStyle="bold"/>
<ImageView
android:id="#+id/imgarrowdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:scaleType="fitEnd"
android:visibility="visible"
app:srcCompat="#drawable/ic_keyboard_arrow_down_black_24dp" />
<ImageView
android:id="#+id/imgarrowup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:scaleType="fitEnd"
android:visibility="gone"
app:srcCompat="#drawable/ic_keyboard_arrow_up_black_24dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/cwllconteudo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:visibility="gone">
<!-- ########################################
######## CARDVIEW CONTENT HERE #########
########################################-->
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Java code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.cwlltitulobtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CVOpenCloseBTN();
}
});
}
public void CVOpenCloseBTN(){
ImageView arrowdown = (ImageView) findViewById(R.id.cvimgarrowdown);
ImageView arrowup = (ImageView) findViewById(R.id.cvimgarrowup);
LinearLayout pagecontent = (LinearLayout) findViewById(R.id.cwllconteudo);
if (conteudopage .getVisibility() == View.GONE) {
// it's collapsed - expand it
pagecontent.setVisibility(View.VISIBLE);
arrowdown.setVisibility(View.GONE);
arrowup.setVisibility(View.VISIBLE);
} else {
// it's expanded - collapse it
pagecontent.setVisibility(View.GONE);
arrowdown.setVisibility(View.VISIBLE);
arrowup.setVisibility(View.GONE);
}
}
}
My idea is that I can transform all this part of the xml and java code to be used in this way and faster, than to have to always repeat again:
<br.com.samantabiblioteca.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:titulo="#### TITLE OF BUTTON ####">
<!-- ########################################
######### CARDVIEW CONTENT HERE ########
########################################-->
</br.com.samantabiblioteca.CardView>
Is it possible to do this?
I want when I click add icon (+) as this image, it will add more a EditText to add another address. Number address can expand as user want. How I can do that ?
Add a EditText
First of all you need to take container in which you can addviews at runtime :
For example:
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"/>
<Button
android:id="#+id/addAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Now you need to add edit text at runtime.
final LinearLayout container = (LinearLayout) findViewById(R.id.container);
Button button = (Button) findViewById(R.id.addAddress);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = new EditText(HomeActivity.this);
container.addView(editText);
}
});
Please read more on how to add views at runtime.
I have an activity with the following layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button ....
<EditText ....
//..........
I want to place a group of controls into the 2nd LinearLayout so that I'll be able to hide them all at once when I click the button. I suppose I should wrap them in one more LinearLayout and hide/show it when I click the button.
I've tried that and got an error saying basically it wasn't allowed. Did I do anything wrong with that? If not, what should I use instead?
As you need to hide/show your layout containing some views, on button click:
LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(ll.getVisibility() == View.VISIBLE)
{
ll.setVisibility(View.INVISIBLE); //use View.GONE, if required
}
else
{
ll.setVisibility(View.VISIBLE);
}
}
}
I want to replace the content of the linear layout which has button and textview with an editText type="phone" after an onclick event of button. They're all in the located in the same page.
Is there a way to do that?
Use following code to remove all Views.
lauout.removeAllViews();
You'll have to make two separate fragments and use a fragmenttransaction to replace one with the other.
See fragmenttransactions:
http://developer.android.com/reference/android/app/FragmentTransaction.html
What I can understand from your question
1) There are 3 views Button, TextView and EditText under one LinearLayout
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:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My TextView " />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" // with input type phone
android:visibility="gone" />
</LinearLayout>
And You want to remove the textview and replace it with Edit Text on Button Click
like this
tv=(TextView)findViewById(R.id.textView1);
et=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setVisibility(View.GONE);
et.setVisibility(View.VISIBLE);
}
});
place all of your three elements in the layout in your xml_layout, and set the EditText visibility to "gone", then when clicking the Button you mentioned, just set the Button and the TextView visibility to "gone" and the EditText visibility to "visible":
<LinearLayout android:id="#+id/layout"
.
.
.>
<Button android:id="#+id/button"
.
.
.
android:visibility="visible"/>
<TextView android:id="#+id/text"
.
.
.
android:visibility="visible"/>
<EditText android:id="#+id/edit_text"
.
.
.
android:inputType="phone"
android:visibility="gone"/>
</LinearLayout>
when clicking the Button:
public OnClickListener onButClick = new OnClickListener() {
#Override
public void onClick(View v) {
button.setVisibility(View.GONE);
text.setVisibility(View.GONE);
text_view.setVisibility(View.VISIBLE);
}
};
I am new to android and i am unable to solve my simple problem.I have a parent Tablelayout and inside it i have two tablelayouts with ids tbl1 and tbl2 respectively in my xml file.In tbl1 layout i have three textviews and three edittext controls similarly i have some views in tbl2 layout.Now i want that my tbl1 layout is visible when my activity starts but on click of my button2 which is in tbl1 layout my tablelayout tbl1 gets invisible and my tablelayout tbl2 becomes visible.Actually i know i can achieve this in asp.net with the help of panels but in android i am not able to achieve the same thing.Please help
You are going to want to look at the setVisibility() method. In the on click listeners for button 2, put the following;
Button.setVisibility(View.INVISIBLE)
TextView.setVisibility(View.INVISIBLE)
etc...
This will make the views invisible, but they will still take up space. If you don't want them to take up space you should use
setVisibility(Veiw.GONE);
Finally, to make you buttons and textview and edittexts in the second table, appear, you need to do the following;
setVisibility(View.VISIBLE);
Java Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.visibility_1);
// Find the view whose visibility will change
mVictim = findViewById(R.id.victim);
// Find our buttons
Button visibleButton = (Button) findViewById(R.id.vis);
Button invisibleButton = (Button) findViewById(R.id.invis);
Button goneButton = (Button) findViewById(R.id.gone);
// Wire each button to a click listener
visibleButton.setOnClickListener(mVisibleListener);
invisibleButton.setOnClickListener(mInvisibleListener);
goneButton.setOnClickListener(mGoneListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.VISIBLE);
}
};
OnClickListener mInvisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.INVISIBLE);
}
};
OnClickListener mGoneListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.GONE);
}
};
}
XML Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:background="#drawable/box"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="#drawable/red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/visibility_1_view_1"/>
<TextView android:id="#+id/victim"
android:background="#drawable/green"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/visibility_1_view_2"/>
<TextView
android:background="#drawable/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/visibility_1_view_3"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="#+id/vis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/visibility_1_vis"/>
<Button android:id="#+id/invis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/visibility_1_invis"/>
<Button android:id="#+id/gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/visibility_1_gone"/>
</LinearLayout>
</LinearLayout>